lighteval

repository·main·Indexed 25 days ago

https://github.com/huggingface/lighteval

A lightweight and configurable evaluation toolkit for Large Language Models (LLMs). It supports various backends including local memory, vLLM, and remote inference endpoints (Hugging Face Inference Endpoints, TGI, LiteLLM). The library provides a CLI for evaluation and a Python API for in-memory models, allowing users to define custom tasks via LightevalTaskConfig and implement custom sample-level and corpus-level metrics.

Tokens
40.7K
Snippets
100
Records
187
Agent score
82%

What's inside lighteval

  1. Understand the Metric hierarchy in LightEval

    main

    LightEval organizes metrics into different levels of granularity depending on whether the score is calculated per individual sample or across the entire dataset (corpus).

    • SampleLevelMetric: Metrics that compute a score for each individual sample in the evaluation set (e.g., ExactMatches, ROUGE, F1_score).
    • CorpusLevelMetric: Metrics that aggregate results across the entire corpus to produce a single global score (e.g., CorpusLevelF1Score, CorpusLevelPerplexityMetric).
    • MetricGrouping: A way to group multiple metrics together. There are specific groupings for corpus-level (CorpusLevelMetricGrouping) and sample-level (SampleLevelMetricGrouping) metrics.
  2. Understand the result file structure

    main

    The main JSON results file is organized into several sections:

    General Configuration & Summary

    • config_general: Metadata including lighteval_sha, model_name, model_sha, model_size, and timing information (start_time, end_time, total_evaluation_time_secondes).
    • summary_general: General statistics for the entire run (e.g., total padded/non-padded samples).

    Task-Specific Information

    • config_tasks: Detailed configuration for each task, including prompt_function, metric definitions (name, higher_is_better, etc.), and few_shots_select settings.
    • summary_tasks: Task-specific metadata and hashes (e.g., hash_examples, hash_full_prompts).
    • versions: Version information for tasks and datasets.

    Results

    • results: The actual evaluation metrics and scores for each task (e.g., em, maj@8).
  3. Configure SGLang parallelism (Tensor and Data Parallelism)

    main

    SGLang supports distributing models across multiple GPUs using two methods:

    1. Tensor Parallelism (tp_size): Splits the model itself across multiple GPUs. Use this when a single model is too large for one GPU.
    2. Data Parallelism (dp_size): Runs multiple copies of the model on different GPUs to process different data batches in parallel. Use this when the model fits on a single GPU and you want to speed up evaluation.

    Set these by adding tp_size=N or dp_size=N to your model_args.

  4. Understand the detail file structure

    main

    The detailed evaluation files (Parquet) contain three primary columns for each sample:

    • __doc__: The document used for evaluation, including the gold reference, few-shot examples, and other task hyperparameters.
    • __model_response__: Contains the model's generations, log probabilities, and the original input sent to the model.
    • __metric__: The calculated metric value for that specific sample.
  5. Understand multilingual task formulations

    main

    When creating multilingual tasks, you must choose a formulation that defines how the prompt is presented and how the answer is expected. There are three primary types:

    1. Multiple Choice Formulation (MCF): Used for standard multiple choice questions where the model selects from lettered options (e.g., A, B, C, D).
    2. Classification Formulation (CF): Used for tasks where the model generates the answer text directly without choosing from a list.
    3. Hybrid Formulation: Used for tasks that present choices but expect the model to provide the full text of the correct answer.
    # Multiple Choice Formulation (MCF)
    MCFFormulation()
    
    # Classification Formulation (CF)
    CFFormulation()
    
    # Hybrid Formulation
    HybridFormulation()
  6. Use specialized info loggers for evaluation metadata

    main

    LightEval provides several specialized loggers to capture different aspects of the evaluation process. Depending on your needs, you can use:

    • GeneralConfigLogger: Logs general configuration settings.
    • DetailsLogger: Logs detailed information about the evaluation.
    • MetricsLogger: Logs evaluation metrics.
    • VersionsLogger: Logs version information.
    • TaskConfigLogger: Logs task-specific configurations.
  7. How the Lighteval caching system works

    main

    Lighteval uses a caching system to speed up evaluations by storing and reusing model predictions (generations, logits, and probabilities) on disk. This prevents redundant computations when running the same evaluation multiple times or comparing different metrics on the same model outputs.

    Cache Invalidation (Recreation)

    A new cache is automatically generated whenever any of the following change:

    • Model configuration: parameters, quantization settings, etc.
    • Model weights: different revisions or checkpoints.
    • Generation parameters: temperature, max_tokens, etc.

    This mechanism ensures that cached results remain consistent with the specific model setup used during generation.

  8. Core components of Lighteval

    main

    The Lighteval Python API is built around several core abstractions:

    • EvaluationTracker: Manages logging and saving of results. It supports local saving and pushing results to the Hugging Face Hub.
    • PipelineParameters: Configures the execution environment, including parallelism settings (via launcher_type) and task-specific configurations like custom_tasks_directory or max_samples.
    • Model Configuration: Defines the model identity and runtime parameters (e.g., model_name, dtype). Configuration classes vary by backend (e.g., VLLMModelConfig for VLLM, or Transformers-based configs).
    • Pipeline: The central orchestrator that accepts tasks, model configuration, pipeline parameters, and an evaluation tracker to execute the evaluation lifecycle.
  9. Implement a custom model by inheriting from LightevalModel

    main

    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:

    1. greedy_until(docs: List[Doc]) -> List[ModelResponse]: For generative tasks (text generation until a stop sequence or max tokens).
    2. loglikelihood(docs: List[Doc]) -> List[ModelResponse]: For multiple-choice tasks (computing log probabilities of specific continuations).
    3. 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
  10. Understand the Lighteval cache directory structure

    main

    Cached data is stored using HuggingFace datasets in a specific hierarchical structure on disk. This structure uses the model name, a hash of the model configuration, and the task name to organize files:

    .cache/
    └── huggingface/
        └── lighteval/
            └── predictions/
                └── {model_name}/
                    └── {model_hash}/
                        └── {task_name}.parquet
    • {model_name}: The model name (Hub path or local path).
    • {model_hash}: A hash of the model configuration used to trigger cache invalidation if parameters change.
    • {task_name}: The name of the specific evaluation task.
  11. Configure models using model-args or YAML files

    main

    In LightEval, model configurations define the model and its specific parameters. You can specify these parameters in two ways:

    1. model-args: Pass parameters directly via command-line arguments or configuration strings.
    2. YAML files: Define parameters in a dedicated model YAML file.

    For a concrete implementation example, refer to the vllm_model_config.yaml structure in the repository examples.