1.背景介绍
EleutherAI 的 LM Evaluation Harness 最初于 2020 年推出,目的是在海量不同基准(如 GSM8K、MMLU、HellaSwag、TruthfulQA 等)上,为各种模型提供一致且可重现的评测环境GitHub。
内置支持 60+ 个学术界常用任务,涵盖开放式问答(open-QA)、多选题(multiple-choice)、生成式翻译、摘要等多种类型,也允许用户通过 YAML 或 Python 接口自定义新任务,轻松接入本地或 Hugging Face Hub 上的任意数据集。
2.安装
访问官方git-hub进行下载:
GitHub - EleutherAI/lm-evaluation-harness: A framework for few-shot evaluation of language models.
可以参考这篇文档的介绍:LLM 評估教學 | EleutherAI LM Evaluation Harness | by ChiChieh Huang | Medium
git clone https://github.com/EleutherAI/lm-evaluation-harness
cd lm-evaluation-harness
#安装依赖项
pip install -e .
3.使用
参考:LLM基础学习03:Qwen2.5-1.5B-Instruct指令微调全流程实践——LLaMA Factory框架与GSM8K评估_qwen2.5 1.5b微调-CSDN博客
3.1 官方数据集与托管模型
可以使用官方的数据集进行测评,例如以下的benchmark(基准程序测试集):
比较常用的工具:
- — model:選擇要評估的模型類型,如
hf
,gguf
,vllm
,openai
等。 - — model_args:模型的位置與參數,可用 huggingface 的倉庫名稱或本地的
./xxxxx
等。這個參數可以接受1個以上,用,
分隔即可。 - — tasks:決定哪些任務要評估,一樣可以接受一個以上。列表可以使用
lm-eval — tasks list
查看。 - — device:使用的 GPU,要一個以上請使用
accelerate launcher
或參數tensor_parallel_size
- — batch_size:
auto
代表自動選擇batch大小 - — output_path:結果儲存位置。
- — use_cache:緩存之前運行的結果,可以避免相同情境重複執行。
- — log_samples: 把評估的過程記錄下來,包括全部的 Prompt 和 ans。
根据需求运行命令:
lm_eval --model hf \
--model_args pretrained=./model_name \
--tasks arc_challenge, hellaswag, mmlu, triviaqa, winogrande, gsm8k \
--device cuda:0 \ #默认0号GPU
--batch_size auto \
--output_path ./eval_out/local_hf \
--use_cache ./eval_cache/local_hf
3.2本地数据集和本地模型
目前不知道怎么直接编写task文件,参考教程,可以直接复制一份官方文件并进行修改。
根据以下路径,找到tasks文件夹:lm-evaluation-harness/lm_eval/tasks/gsm8k
复制一份gsm8k.yaml文件,重命名并进行修改。
tag:
- math_word_problems
task: gsm8k-zh-alpaca #任务名
dataset_path: json # use the “json” loader
dataset_kwargs:#自定义数据集
data_files:
train: "C:/Users/Rue/LLaMa/llama-factory/data/gsm8k_zh_alpaca.jsonl"
validation: "C:/Users/Rue/LLaMa/llama-factory/data/gsm8k_zh_alpaca.jsonl"# 验证集
test: "C:/Users/Rue/LLaMa/llama-factory/data/gsm8k_zh_alpaca.jsonl"# 测试集
dataset_name: main
output_type: generate_until
training_split: train
fewshot_split: train
test_split: test
doc_to_text: "{{instruction}}\nQuestion: {{input}}\nAnswer:"# 数据集格式,自行调整
doc_to_target: "{{output}}"
metric_list:
- metric: exact_match
aggregation: mean
higher_is_better: true
ignore_case: true
ignore_punctuation: false
regexes_to_ignore:
- ","
- "\\$"
- "(?s).*#### "
- "\\.$"
generation_kwargs:
until:
- "Question:"
- "</s>"
- "<|im_end|>"
do_sample: false
temperature: 0.0
repeats: 1
num_fewshot: 5
filter_list:
- name: "strict-match"
filter:
- function: "regex"
regex_pattern: "#### (\\-?[0-9\\.\\,]+)"
- function: "take_first"
- name: "flexible-extract"
filter:
- function: "regex"
group_select: -1
regex_pattern: "(-?[$0-9.,]{2,})|(-?[0-9]+)"
- function: "take_first"
metadata:
version: 3.0
运行测试命令:
lm_eval --model hf --model_args pretrained="C:\Users\Rue\LLaMa\Qwen-Instruct-merged" --tasks gsm8k-zh-alpaca --include_path "C:/Users/Rue/LLaMa/lm-evaluation-harness/lm_eval/tasks/gsm8k-zh-alpaca" --device cuda:1 --batch_size 8 --num_fewshot 0 --output_path C:\Users\Rue\LLaMa\lm-evaluation-harness\save\qwen2.5-1.5b_merged
num_fewshot 0表示例题数量为零。
如果报错找不到对应的task,可以先检查以下task list
lm-eval --tasks list
有很多内置的任务,但可以找到如图(我自定义的task为gsm8k-zh-alpaca)
4.评估结果
解读:
Filter:匹配策略
flexible-extract
:采用更宽松的正则或模板方法提取模型回答中的数字或关键短语,然后与参考答案比对,允许格式或小数点差异
strict-match
:要求模型输出与参考答案的字符串逐字一致,连标点和空格都必须匹配
Metric:标准
exact_match :衡量模型回答完全等同于参考答案的比例。
Value:即exact_match的评分
Stderr:标准误差
通过多次随机种子重跑计算得到的标准误差,用于评估结果的置信度,±0.0025
表明在不同随机种子下,flexible-extract
得分的波动范围约为 0.25%
结语:本文将持续更新,探索这一工具的使用方法