longbench_zh (original) (raw)
LongBench推理脚本
LongBench是一个多任务、中英双语、针对大语言模型长文本理解能力的评测基准,覆盖了多个长文本应用场景。接下来介绍LongBench评测方法。
环境配置
按照官方的requirements.txt配置环境,相关依赖复制如下:
datasets
tqdm
rouge
jieba
fuzzywuzzy
einops
torch>=2.0.1
transformers>=4.40.0
数据准备
无需单独下载数据。预测脚本会自动从 🤗 Datasets下载所需数据。
运行预测脚本
运行以下脚本进行推理:
model_path=path/to/llama-3-chinese output_path=path/to/output_dir data_class=zh with_inst="auto" max_length=7680
cd scripts/longbench
python pred.py
--model_path ${model_path}
--predict_on ${data_class}
--output_dir ${output_dir}
--max_length ${max_length}
--use_flash_attention_2
--with_inst ${with_inst}
参数说明
--model_path ${model_path}:待评测模型所在目录(完整的Llama-3-Chinese或Llama-3-Chinese-Instruct模型,非LoRA)--predict_on {data_class}: 指定待预测的任务,可以为en,zh,code,或它们的组合,以逗号分隔,如en,zh,code--output_dir ${output_dir}:评测结果的输出目录--max_length ${max_length}:指令的最大长度。注意此长度不包括任务相关prompt在内--gpus ${gpus}:如需指定特定的GPU,请使用此参数,如0,1--e:在LongBench-E数据集上进行预测。参考LongBench官方文档以了解LongBench-E的详细说明。--with_inst ${with_inst}:在构建模型的输入指令时是否使用Llama-3-Chinese-8B-Instruct的prompt及模版:true:所有任务上都使用prompt及模版false:所有任务上都不使用prompt及模版auto:只在部分任务上使用prompt及模版(LongBench官方的策略)
我们建议测试Llama-3-Chinese-8B-Instruct时设置with_inst="auto",测试Llama-3-Chinese-8B时设置with_inst="false"
--use_flash_attention_2:使用Flash-Attention 2加速推理,否则使用SDPA加速
待模型运行结束后,在${output_dir}/pred/或${output_dir}/pred_e/下(取决于是否在LongBench-E上测试,即是否使用了-e)将生成各对应任务的预测文件(jsonl格式)。执行以下命令计算效果指标:
python eval.py --output_dir ${output_dir}
如果在上一步预测时使用了-e,在eval时也需提供-e参数:
python eval.py --output_dir ${output_dir} -e
计算结果存放在${output_dir}/pred/result.json或${output_dir}/pred_e/result.json下。形如:
{ "lsht": 42.0, "multifieldqa_zh": 50.28, "passage_retrieval_zh": 89.5, "vcsum": 16.41, "dureader": 34.15 }