Skip to content

Commit e127955

Browse files
authored
Merge branch 'main' into nathan-add-tests-for-metrics
2 parents c574035 + 7ed2636 commit e127955

91 files changed

Lines changed: 2157 additions & 1284 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

community_tasks/arabic_evals.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ def compute(self, responses: list[str], formatted_docs: list[Doc], **kwargs) ->
843843
Args:
844844
responses (list[str]): The predicted answers
845845
formatted_docs (list[Doc]): Documents containing questions and gold answers
846+
kwargs: Additional keyword arguments (not used)
846847
847848
Returns:
848849
dict[str, float]: Dictionary containing evaluation scores

community_tasks/serbian_eval.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ def prompt_fn_oz_eval_task(line, task_name: str = None):
133133
- choices (list of str): List of option identifiers ["A", "B", "C", "D", "E"].
134134
- gold_index (int): Index of the correct answer within the 'choices' list.
135135
136-
Raises:
137-
ValueError: If the 'choices' list does not contain exactly five items,
138-
or if 'answer_str' is not one of ["A", "B", "C", "D", "E"].
139-
140136
Note:
141137
The OZ Eval dataset is available at https://huggingface.co/datasets/DjMel/oz-eval.
142138
@@ -268,6 +264,7 @@ def create_task_config(
268264
suite: The suite of tasks.
269265
hf_avail_splits: Available splits (default is "test", "validation").
270266
few_shots_split: Split used for few-shot examples.
267+
generation_size: Number of generations to produce (default is 5).
271268
272269
Returns:
273270
A `LightevalTaskConfig` object for the task configuration.

docs/source/_toctree.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@
4646
title: Model Configs
4747
- local: package_reference/pipeline
4848
title: Pipeline
49-
- local: package_reference/models_outputs
50-
title: Model's Output
5149
title: Main classes
5250
- local: package_reference/metrics
5351
title: Metrics
5452
- local: package_reference/tasks
5553
title: Tasks
5654
- local: package_reference/logging
5755
title: Logging
56+
- local: package_reference/models_outputs
57+
title: ModelResponse
58+
- local: package_reference/doc
59+
title: Doc
5860
title: Reference
Lines changed: 107 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
11
# Adding a Custom Task
22

3-
To add a new task, first either open an issue, to determine whether it will be
4-
integrated in the core evaluations of lighteval, in the extended tasks, or the
5-
community tasks, and add its dataset on the hub.
6-
7-
- Core evaluations are evaluations that only require standard logic in their
8-
metrics and processing, and that we will add to our test suite to ensure non
9-
regression through time. They already see high usage in the community.
10-
- Extended evaluations are evaluations that require custom logic in their
11-
metrics (complex normalisation, an LLM as a judge, ...), that we added to
12-
facilitate the life of users. They already see high usage in the community.
13-
- Community evaluations are submissions by the community of new tasks.
3+
Lighteval provides a flexible framework for creating custom evaluation tasks. This guide explains how to create and integrate new tasks into the evaluation system.
4+
5+
## Task Categories
6+
7+
Before creating a custom task, consider which category it belongs to:
8+
9+
### Core Evaluations
10+
Core evaluations are evaluations that only require standard logic in their
11+
metrics and processing, and that we will add to our test suite to ensure non-regression through time. They already see high usage in the community.
12+
13+
### Extended Evaluations
14+
Extended evaluations are evaluations that require custom logic in their
15+
metrics (complex normalization, an LLM as a judge, etc.), that we added to
16+
facilitate the life of users. They already see high usage in the community.
17+
18+
### Community Evaluations
19+
Community evaluations are submissions by the community of new tasks.
1420

1521
A popular community evaluation can move to become an extended or core evaluation over time.
1622

1723
> [!TIP]
18-
> You can find examples of custom tasks in the <a href="https://github.com/huggingface/lighteval/tree/main/community_tasks">community_task</a> directory.
24+
> You can find examples of custom tasks in the [community_tasks](https://github.com/huggingface/lighteval/tree/main/community_tasks) directory.
1925
20-
## Step by step creation of a custom task
26+
## Step-by-Step Creation of a Custom Task
2127

2228
> [!WARNING]
23-
> To contribute your custom metric to the lighteval repo, you would first need
29+
> To contribute your custom task to the Lighteval repository, you would first need
2430
> to install the required dev dependencies by running `pip install -e .[dev]`
2531
> and then run `pre-commit install` to install the pre-commit hooks.
2632
27-
First, create a python file under the `community_tasks` directory.
33+
### Step 1: Create the Task File
34+
35+
First, create a Python file under the `community_tasks` directory.
36+
37+
### Step 2: Define the Prompt Function
2838

2939
You need to define a prompt function that will convert a line from your
3040
dataset to a document to be used for evaluation.
3141

3242
```python
43+
from lighteval.tasks.requests import Doc
44+
3345
# Define as many as you need for your different tasks
34-
def prompt_fn(line, task_name: str = None):
46+
def prompt_fn(line: dict, task_name: str):
3547
"""Defines how to go from a dataset line to a doc object.
3648
Follow examples in src/lighteval/tasks/default_prompts.py, or get more info
3749
about what this function should do in the README.
@@ -44,47 +56,68 @@ def prompt_fn(line, task_name: str = None):
4456
)
4557
```
4658

47-
Then, you need to choose a metric: you can either use an existing one (defined
48-
in [`lighteval.metrics.metrics.Metrics`]) or [create a custom one](adding-a-new-metric)).
49-
[//]: # (TODO: Replace lighteval.metrics.metrics.Metrics with ~metrics.metrics.Metrics once its autodoc is added)
59+
### Step 3: Choose or Create Metrics
60+
61+
You can either use an existing metric (defined in [`lighteval.metrics.metrics.Metrics`]) or [create a custom one](adding-a-new-metric).
62+
63+
#### Using Existing Metrics
64+
65+
```python
66+
from lighteval.metrics import Metrics
67+
68+
# Use an existing metric
69+
metric = Metrics.ACCURACY
70+
```
71+
72+
#### Creating Custom Metrics
5073

5174
```python
75+
from lighteval.metrics.utils.metric_utils import SampleLevelMetric
76+
import numpy as np
77+
5278
custom_metric = SampleLevelMetric(
5379
metric_name="my_custom_metric_name",
5480
higher_is_better=True,
55-
category=SamplingMethod.{GENERATIVE,LOGPROBS},
56-
sample_level_fn=lambda x: x, # how to compute score for one sample
57-
corpus_level_fn=np.mean, # How to aggregate the samples metrics
81+
category="accuracy",
82+
sample_level_fn=lambda x: x, # How to compute score for one sample
83+
corpus_level_fn=np.mean, # How to aggregate the sample metrics
5884
)
5985
```
6086

61-
Then, you need to define your task using [`~tasks.lighteval_task.LightevalTaskConfig`].
62-
You can define a task with or without subsets.
63-
To define a task with no subsets:
87+
### Step 4: Define Your Task
88+
89+
You can define a task with or without subsets using [`~tasks.lighteval_task.LightevalTaskConfig`].
90+
91+
#### Simple Task (No Subsets)
6492

6593
```python
66-
# This is how you create a simple task (like hellaswag) which has one single subset
94+
from lighteval.tasks.lighteval_task import LightevalTaskConfig
95+
96+
# This is how you create a simple task (like HellaSwag) which has one single subset
6797
# attached to it, and one evaluation possible.
6898
task = LightevalTaskConfig(
6999
name="myothertask",
70-
prompt_function=prompt_fn, # must be defined in the file or imported from src/lighteval/tasks/tasks_prompt_formatting.py
100+
prompt_function=prompt_fn, # Must be defined in the file or imported
71101
suite=["community"],
72-
hf_repo="",
102+
hf_repo="your_dataset_repo_on_hf",
73103
hf_subset="default",
74-
hf_avail_splits=[],
75-
evaluation_splits=[],
76-
few_shots_split=None,
77-
few_shots_select=None,
78-
metrics=[], # select your metric in Metrics
104+
hf_avail_splits=["train", "test"],
105+
evaluation_splits=["test"],
106+
few_shots_split="train",
107+
few_shots_select="random_sampling_from_train",
108+
metrics=[metric], # Select your metric in Metrics
109+
generation_size=256,
110+
stop_sequence=["\n", "Question:"],
79111
)
80112
```
81113

82-
If you want to create a task with multiple subset, add them to the
114+
#### Task with Multiple Subsets
115+
116+
If you want to create a task with multiple subsets, add them to the
83117
`SAMPLE_SUBSETS` list and create a task for each subset.
84118

85119
```python
86-
SAMPLE_SUBSETS = [] # list of all the subsets to use for this eval
87-
120+
SAMPLE_SUBSETS = ["subset1", "subset2", "subset3"] # List of all the subsets to use for this eval
88121

89122
class CustomSubsetTask(LightevalTaskConfig):
90123
def __init__(
@@ -95,37 +128,63 @@ class CustomSubsetTask(LightevalTaskConfig):
95128
super().__init__(
96129
name=name,
97130
hf_subset=hf_subset,
98-
prompt_function=prompt_fn, # must be defined in the file or imported from src/lighteval/tasks/tasks_prompt_formatting.py
99-
hf_repo="",
100-
metric=[custom_metric], # select your metric in Metrics or use your custom_metric
101-
hf_avail_splits=[],
102-
evaluation_splits=[],
103-
few_shots_split=None,
104-
few_shots_select=None,
131+
prompt_function=prompt_fn, # Must be defined in the file or imported
132+
hf_repo="your_dataset_name",
133+
metrics=[custom_metric], # Select your metric in Metrics or use your custom_metric
134+
hf_avail_splits=["train", "test"],
135+
evaluation_splits=["test"],
136+
few_shots_split="train",
137+
few_shots_select="random_sampling_from_train",
105138
suite=["community"],
106-
generation_size=-1,
107-
stop_sequence=None,
139+
generation_size=256,
140+
stop_sequence=["\n", "Question:"],
108141
)
142+
109143
SUBSET_TASKS = [CustomSubsetTask(name=f"mytask:{subset}", hf_subset=subset) for subset in SAMPLE_SUBSETS]
110144
```
111145

146+
### Step 5: Add Tasks to the Table
147+
112148
Then you need to add your task to the `TASKS_TABLE` list.
113149

114150
```python
115151
# STORE YOUR EVALS
116152

117-
# tasks with subset:
153+
# Tasks with subsets:
118154
TASKS_TABLE = SUBSET_TASKS
119155

120-
# tasks without subset:
156+
# Tasks without subsets:
121157
# TASKS_TABLE = [task]
122158
```
123159

124-
Once your file is created you can then run the evaluation with the following command:
160+
### Step 6: Creating a requirement file
161+
162+
If your task has requirements, you need to create a `requirement.txt` file with
163+
only the required dependencies so that anyone can run your task.
164+
165+
## Running Your Custom Task
166+
167+
Once your file is created, you can run the evaluation with the following command:
125168

126169
```bash
127170
lighteval accelerate \
128171
"model_name=HuggingFaceH4/zephyr-7b-beta" \
129172
"community|{custom_task}|{fewshots}" \
130173
--custom-tasks {path_to_your_custom_task_file}
131174
```
175+
176+
### Example Usage
177+
178+
```bash
179+
# Run a custom task with zero-shot evaluation
180+
lighteval accelerate \
181+
"model_name=openai-community/gpt2" \
182+
"community|myothertask|0" \
183+
--custom-tasks community_tasks/my_custom_task.py
184+
185+
# Run a custom task with few-shot evaluation
186+
lighteval accelerate \
187+
"model_name=openai-community/gpt2" \
188+
"community|myothertask|3" \
189+
--custom-tasks community_tasks/my_custom_task.py
190+
```

0 commit comments

Comments
 (0)