Lighteval 文档
添加自定义任务
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
添加自定义任务
要添加新任务,首先请提交一个 issue,以确定该任务是集成到 lighteval 的核心评估、扩展任务还是社区任务中,并将其数据集添加到 Hub 上。
- 核心评估是指那些在指标和处理中仅需标准逻辑的评估,我们会将其添加到我们的测试套件中,以确保长期不会出现回归问题。这些评估在社区中已经得到广泛使用。
- 扩展评估是指那些在其指标中需要自定义逻辑的评估(例如复杂的归一化、使用 LLM 作为评判者等),我们添加这些评估是为了方便用户。这些评估在社区中也已经得到广泛使用。
- 社区评估是由社区提交的新任务。
一个受欢迎的社区评估随着时间的推移可能会转变为扩展评估或核心评估。
你可以在 community_task 目录中找到自定义任务的示例。
创建自定义任务的步骤
要将你的自定义指标贡献到 lighteval 仓库,首先需要运行 pip install -e .[dev]
安装所需的开发依赖项,然后运行 pre-commit install
来安装 pre-commit 钩子。
首先,在 community_tasks
目录下创建一个 Python 文件。
你需要定义一个提示函数 (prompt function),该函数将数据集中的一行转换为用于评估的文档。
# Define as many as you need for your different tasks
def prompt_fn(line, task_name: str = None):
"""Defines how to go from a dataset line to a doc object.
Follow examples in src/lighteval/tasks/default_prompts.py, or get more info
about what this function should do in the README.
"""
return Doc(
task_name=task_name,
query=line["question"],
choices=[f" {c}" for c in line["choices"]],
gold_index=line["gold"],
instruction="",
)
然后,你需要选择一个指标:你可以使用现有的指标(定义在 lighteval.metrics.metrics.Metrics
中),也可以创建一个自定义指标。
custom_metric = SampleLevelMetric(
metric_name="my_custom_metric_name",
higher_is_better=True,
category=MetricCategory.IGNORED,
use_case=MetricUseCase.NONE,
sample_level_fn=lambda x: x, # how to compute score for one sample
corpus_level_fn=np.mean, # How to aggregate the samples metrics
)
接着,你需要使用 LightevalTaskConfig 来定义你的任务。你可以定义一个带或不带子集的任务。要定义一个没有子集的任务:
# This is how you create a simple task (like hellaswag) which has one single subset
# attached to it, and one evaluation possible.
task = LightevalTaskConfig(
name="myothertask",
prompt_function=prompt_fn, # must be defined in the file or imported from src/lighteval/tasks/tasks_prompt_formatting.py
suite=["community"],
hf_repo="",
hf_subset="default",
hf_avail_splits=[],
evaluation_splits=[],
few_shots_split=None,
few_shots_select=None,
metric=[], # select your metric in Metrics
)
如果你想创建一个包含多个子集的任务,请将它们添加到 SAMPLE_SUBSETS
列表中,并为每个子集创建一个任务。
SAMPLE_SUBSETS = [] # list of all the subsets to use for this eval
class CustomSubsetTask(LightevalTaskConfig):
def __init__(
self,
name,
hf_subset,
):
super().__init__(
name=name,
hf_subset=hf_subset,
prompt_function=prompt_fn, # must be defined in the file or imported from src/lighteval/tasks/tasks_prompt_formatting.py
hf_repo="",
metric=[custom_metric], # select your metric in Metrics or use your custom_metric
hf_avail_splits=[],
evaluation_splits=[],
few_shots_split=None,
few_shots_select=None,
suite=["community"],
generation_size=-1,
stop_sequence=None,
)
SUBSET_TASKS = [CustomSubsetTask(name=f"mytask:{subset}", hf_subset=subset) for subset in SAMPLE_SUBSETS]
以下是参数及其含义的列表:
name
(str),你的评估名称。suite
(list),你的评估应该属于的一个或多个套件。此字段允许我们比较不同的任务实现,并用作任务选择以区分要启动的版本。目前,你会发现的关键字有 [“helm”, “bigbench”, “original”, “lighteval”, “community”, “custom”];对于核心评估,请选择lighteval
。prompt_function
(Callable),你在上一步中定义的提示函数。hf_repo
(str),你在 Hub 上的评估数据集的路径。hf_subset
(str),你希望用于评估的特定子集(注意:当数据集没有子集时,请将此字段填写为"default"
,而不是None
或""
)。hf_avail_splits
(list),你的数据集可用的所有数据拆分(train、valid 或 validation、test、other…)。evaluation_splits
(list),你希望用于评估的数据拆分。few_shots_split
(str, 可以为null
),你希望从中选择少样本示例的特定数据拆分。它应该不同于evaluation_splits
中包含的数据集。few_shots_select
(str, 可以为null
),你将用于选择少样本示例的方法。可以为null
,或以下之一:balanced
从few_shots_split
中选择具有平衡标签的样本,以避免少样本示例(以及模型的生成结果)偏向某个特定标签。random
从few_shots_split
中随机选择样本。random_sampling
对于每个新项,都从few_shots_split
中随机选择新样本,但如果抽样到的样本与当前项相同,则会从可用样本中移除。random_sampling_from_train
对于每个新项,都从few_shots_split
中随机选择新样本,但如果抽样到的样本与当前项相同,它将被保留!只有在你清楚自己在做什么的情况下才使用此方法。sequential
选择few_shots_split
的前n
个样本。
generation_size
(int),生成式评估允许的最大 token 数。如果你的评估是对数似然评估(多项选择),此值应为 -1。stop_sequence
(list),一个字符串列表,用作生成内容的句末标记。metric
(list),你希望用于评估的指标(详细解释请参见下一节)。trust_dataset
(bool),如果你信任该数据集,则设置为 True。
然后你需要将你的任务添加到 TASKS_TABLE
列表中。
# STORE YOUR EVALS
# tasks with subset:
TASKS_TABLE = SUBSET_TASKS
# tasks without subset:
# TASKS_TABLE = [task]
文件创建完成后,你可以使用以下命令运行评估:
lighteval accelerate \
"model_name=HuggingFaceH4/zephyr-7b-beta" \
"community|{custom_task}|{fewshots}|{truncate_few_shot}" \
--custom-tasks {path_to_your_custom_task_file}