Lighteval 文档

添加自定义任务

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

添加自定义任务

要添加新任务,首先请提交一个 issue,以确定它将被集成到 lighteval 的核心评估、扩展任务还是社区任务中,并在 Hub 上添加其数据集。

  • 核心评估是只需要在其指标和处理中使用标准逻辑的评估,我们将添加到我们的测试套件中,以确保随时间推移不会发生回归。它们已经在社区中得到广泛使用。
  • 扩展评估是在其指标中需要自定义逻辑(复杂的标准化、LLM 作为评判者等)的评估,我们添加这些评估是为了方便用户。它们已经在社区中得到广泛使用。
  • 社区评估是社区提交的新任务。

受欢迎的社区评估可能会随着时间的推移转变为扩展评估或核心评估。

您可以在 community_task 目录中找到自定义任务的示例。

自定义任务的逐步创建

要将您的自定义指标贡献给 lighteval 仓库,您首先需要运行 pip install -e .[dev] 安装所需的开发依赖项,然后运行 pre-commit install 安装 pre-commit 钩子。

首先,在 community_tasks 目录下创建一个 python 文件。

您需要定义一个 prompt 函数,该函数将数据集中的一行转换为用于评估的文档。

# 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 aggreagte 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),您在上一步中定义的 prompt 函数
  • 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,或以下之一
    • balancedfew_shots_split 中选择具有平衡标签的示例,以避免少量示例(因此模型生成)偏向于某个特定标签
    • randomfew_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),充当您生成的句子结束 token 的字符串列表
  • 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 \
    "pretrained=HuggingFaceH4/zephyr-7b-beta" \
    "community|{custom_task}|{fewshots}|{truncate_few_shot}" \
    --custom-tasks {path_to_your_custom_task_file}
< > 在 GitHub 上更新