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 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,或以下之一:
    • 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),一個字串列表,用作生成內容的句末標記。
  • 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}
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.