To evaluate models not supported by standard backends (like Transformers or VLLM), or to add custom pre/post-processing, you must create a class that inherits from LightevalModel.
Your implementation must include three core methods:
greedy_until(docs: List[Doc]) -> List[ModelResponse]: For generative tasks (text generation until a stop sequence or max tokens).loglikelihood(docs: List[Doc]) -> List[ModelResponse]: For multiple-choice tasks (computing log probabilities of specific continuations).loglikelihood_rolling(docs: List[Doc]) -> List[ModelResponse]: For perplexity metrics (computing rolling log probabilities of sequences).
Requirements:
- The Python file containing your custom model should contain exactly one class that inherits from
LightevalModel. This allows Lighteval to automatically detect and instantiate it. - It is highly recommended to use the
SampleCache and the @cached decorator to speed up evaluations.
from lighteval.models.abstract_model import LightevalModel
from lighteval.models.model_output import ModelResponse
from lighteval.tasks.requests import Doc, SamplingMethod
from lighteval.utils.cache_management import SampleCache, cached
from typing import List
class MyCustomModel(LightevalModel):
def __init__(self, config):
super().__init__(config)
# Initialize your model here...
# Enable caching (recommended)
self._cache = SampleCache(config)
@cached(SamplingMethod.GENERATIVE)
def greedy_until(self, docs: List[Doc]) -> List[ModelResponse]:
# Implement generation logic
pass
@cached(SamplingMethod.LOGPROBS)
def loglikelihood(self, docs: List[Doc]) -> List[ModelResponse]:
# Implement loglikelihood computation
pass
@cached(SamplingMethod.PERPLEXITY)
def loglikelihood_rolling(self, docs: List[Doc]) -> List[ModelResponse]:
# Implement rolling loglikelihood computation
pass