OpenCompass介绍
上海人工智能实验室科学家团队正式发布了大模型开源开放评测体系 “司南” (OpenCompass2.0),用于为大语言模型、多模态模型等提供一站式评测服务。其主要特点如下:
- 开源可复现:提供公平、公开、可复现的大模型评测方案
- 全面的能力维度:五大维度设计,提供 70+ 个数据集约 40 万题的的模型评测方案,全面评估模型能力
- 丰富的模型支持:已支持 20+ HuggingFace 及 API 模型
- 分布式高效评测:一行命令实现任务分割和分布式评测,数小时即可完成千亿模型全量评测
- 多样化评测范式:支持零样本、小样本及思维链评测,结合标准型或对话型提示词模板,轻松激发各种模型最大性能
- 灵活化拓展:想增加新模型或数据集?想要自定义更高级的任务分割策略,甚至接入新的集群管理系统?OpenCompass 的一切均可轻松扩展!
评测对象
本算法库的主要评测对象为语言大模型与多模态大模型。我们以语言大模型为例介绍评测的具体模型类型。
- 基座模型:一般是经过海量的文本数据以自监督学习的方式进行训练获得的模型(如OpenAI的GPT-3,Meta的LLaMA),往往具有强大的文字续写能力。
- 对话模型:一般是在的基座模型的基础上,经过指令微调或人类偏好对齐获得的模型(如OpenAI的ChatGPT、上海人工智能实验室的书生·浦语),能理解人类指令,具有较强的对话能力。
评测方法
OpenCompass 采取客观评测与主观评测相结合的方法。针对具有确定性答案的能力维度和场景,通过构造丰富完善的评测集,对模型能力进行综合评价。针对体现模型能力的开放式或半开放式的问题、模型安全问题等,采用主客观相结合的评测方式。
客观评测
针对具有标准答案的客观问题,我们可以通过使用定量指标比较模型的输出与标准答案的差异,并根据结果衡量模型的性能。同时,由于大语言模型输出自由度较高,在评测阶段,我们需要对其输入和输出作一定的规范和设计,尽可能减少噪声输出在评测阶段的影响,才能对模型的能力有更加完整和客观的评价。 为了更好地激发出模型在题目测试领域的能力,并引导模型按照一定的模板输出答案,OpenCompass 采用提示词工程 (prompt engineering)和语境学习(in-context learning)进行客观评测。 在客观评测的具体实践中,我们通常采用下列两种方式进行模型输出结果的评测:
- 判别式评测:该评测方式基于将问题与候选答案组合在一起,计算模型在所有组合上的困惑度(perplexity),并选择困惑度最小的答案作为模型的最终输出。例如,若模型在 问题? 答案1 上的困惑度为 0.1,在 问题? 答案2 上的困惑度为 0.2,最终我们会选择 答案1 作为模型的输出。
- 生成式评测:该评测方式主要用于生成类任务,如语言翻译、程序生成、逻辑分析题等。具体实践时,使用问题作为模型的原始输入,并留白答案区域待模型进行后续补全。我们通常还需要对其输出进行后处理,以保证输出满足数据集的要求。
主观评测
语言表达生动精彩,变化丰富,大量的场景和能力无法凭借客观指标进行评测。针对如模型安全和模型语言能力的评测,以人的主观感受为主的评测更能体现模型的真实能力,并更符合大模型的实际使用场景。 OpenCompass 采取的主观评测方案是指借助受试者的主观判断对具有对话能力的大语言模型进行能力评测。在具体实践中,我们提前基于模型的能力维度构建主观测试问题集合,并将不同模型对于同一问题的不同回复展现给受试者,收集受试者基于主观感受的评分。由于主观测试成本高昂,本方案同时也采用使用性能优异的大语言模拟人类进行主观打分。在实际评测中,本文将采用真实人类专家的主观评测与基于模型打分的主观评测相结合的方式开展模型能力评估。 在具体开展主观评测时,OpenComapss 采用单模型回复满意度统计和多模型满意度比较两种方式开展具体的评测工作。
概览
在 OpenCompass 中评估一个模型通常包括以下几个阶段:配置 -> 推理 -> 评估 -> 可视化。
- 配置:这是整个工作流的起点。您需要配置整个评估过程,选择要评估的模型和数据集。此外,还可以选择评估策略、计算后端等,并定义显示结果的方式。
- 推理与评估:在这个阶段,OpenCompass 将会开始对模型和数据集进行并行推理和评估。推理阶段主要是让模型从数据集产生输出,而评估阶段则是衡量这些输出与标准答案的匹配程度。这两个过程会被拆分为多个同时运行的“任务”以提高效率,但请注意,如果计算资源有限,这种策略可能会使评测变得更慢。如果需要了解该问题及解决方案,可以参考 FAQ: 效率。
- 可视化:评估完成后,OpenCompass 将结果整理成易读的表格,并将其保存为 CSV 和 TXT 文件。你也可以激活飞书状态上报功能,此后可以在飞书客户端中及时获得评测状态报告。 接下来,我们将展示 OpenCompass 的基础用法,展示书生浦语在
C-Eval
基准任务上的评估。它们的配置文件可以在configs/eval_demo.py
中找到。
环境配置
创建开发机和 conda 环境
在创建开发机界面选择镜像为 Cuda11.7-conda,并选择 GPU 为10% A100。
安装
面向GPU的环境安装
studio-conda -o internlm-base -t opencompass
source activate opencompass
安装成功如下所示:
开始clone opencompass,执行以下命令:
git clone -b 0.2.4 https://github.com/open-compass/opencompass
开始安装环境依赖的包,执行以下命令。
cd opencompass
pip install -e .
如果pip install -e .安装未成功,请运行:
pip install -r requirements.txt
有部分第三方功能,如代码能力基准测试 HumanEval 以及 Llama 格式的模型评测,可能需要额外步骤才能正常运行,如需评测,详细步骤请参考安装指南。
数据准备
解压评测数据集到 data/ 处
cp /share/temp/datasets/OpenCompassData-core-20231110.zip /root/opencompass/
unzip OpenCompassData-core-20231110.zip
将会在 OpenCompass 下看到data文件夹
查看支持的数据集和模型
列出所有跟 InternLM 及 C-Eval 相关的配置
python tools/list_configs.py internlm ceval
将会看到
+----------------------------------------+----------------------------------------------------------------------+
| Model | Config Path |
|----------------------------------------+----------------------------------------------------------------------|
| hf_internlm2_1_8b | configs/models/hf_internlm/hf_internlm2_1_8b.py |
| hf_internlm2_20b | configs/models/hf_internlm/hf_internlm2_20b.py |
| hf_internlm2_7b | configs/models/hf_internlm/hf_internlm2_7b.py |
| hf_internlm2_base_20b | configs/models/hf_internlm/hf_internlm2_base_20b.py |
| hf_internlm2_base_7b | configs/models/hf_internlm/hf_internlm2_base_7b.py |
| hf_internlm2_chat_1_8b | configs/models/hf_internlm/hf_internlm2_chat_1_8b.py |
| hf_internlm2_chat_1_8b_sft | configs/models/hf_internlm/hf_internlm2_chat_1_8b_sft.py |
| hf_internlm2_chat_20b | configs/models/hf_internlm/hf_internlm2_chat_20b.py |
| hf_internlm2_chat_20b_sft | configs/models/hf_internlm/hf_internlm2_chat_20b_sft.py |
| hf_internlm2_chat_20b_with_system | configs/models/hf_internlm/hf_internlm2_chat_20b_with_system.py |
| hf_internlm2_chat_7b | configs/models/hf_internlm/hf_internlm2_chat_7b.py |
| hf_internlm2_chat_7b_sft | configs/models/hf_internlm/hf_internlm2_chat_7b_sft.py |
| hf_internlm2_chat_7b_with_system | configs/models/hf_internlm/hf_internlm2_chat_7b_with_system.py |
| hf_internlm2_chat_math_20b | configs/models/hf_internlm/hf_internlm2_chat_math_20b.py |
| hf_internlm2_chat_math_20b_with_system | configs/models/hf_internlm/hf_internlm2_chat_math_20b_with_system.py |
| hf_internlm2_chat_math_7b | configs/models/hf_internlm/hf_internlm2_chat_math_7b.py |
| hf_internlm2_chat_math_7b_with_system | configs/models/hf_internlm/hf_internlm2_chat_math_7b_with_system.py |
| hf_internlm_20b | configs/models/hf_internlm/hf_internlm_20b.py |
| hf_internlm_7b | configs/models/hf_internlm/hf_internlm_7b.py |
| hf_internlm_chat_20b | configs/models/hf_internlm/hf_internlm_chat_20b.py |
| hf_internlm_chat_7b | configs/models/hf_internlm/hf_internlm_chat_7b.py |
| hf_internlm_chat_7b_8k | configs/models/hf_internlm/hf_internlm_chat_7b_8k.py |
| hf_internlm_chat_7b_v1_1 | configs/models/hf_internlm/hf_internlm_chat_7b_v1_1.py |
| internlm_7b | configs/models/internlm/internlm_7b.py |
| lmdeploy_internlm2_chat_20b | configs/models/hf_internlm/lmdeploy_internlm2_chat_20b.py |
| lmdeploy_internlm2_chat_7b | configs/models/hf_internlm/lmdeploy_internlm2_chat_7b.py |
| ms_internlm_chat_7b_8k | configs/models/ms_internlm/ms_internlm_chat_7b_8k.py |
+----------------------------------------+----------------------------------------------------------------------+
+--------------------------------+------------------------------------------------------------------+
| Dataset | Config Path |
|--------------------------------+------------------------------------------------------------------|
| ceval_clean_ppl | configs/datasets/ceval/ceval_clean_ppl.py |
| ceval_contamination_ppl_810ec6 | configs/datasets/contamination/ceval_contamination_ppl_810ec6.py |
| ceval_gen | configs/datasets/ceval/ceval_gen.py |
| ceval_gen_2daf24 | configs/datasets/ceval/ceval_gen_2daf24.py |
| ceval_gen_5f30c7 | configs/datasets/ceval/ceval_gen_5f30c7.py |
| ceval_internal_ppl_1cd8bf | configs/datasets/ceval/ceval_internal_ppl_1cd8bf.py |
| ceval_ppl | configs/datasets/ceval/ceval_ppl.py |
| ceval_ppl_1cd8bf | configs/datasets/ceval/ceval_ppl_1cd8bf.py |
| ceval_ppl_578f8d | configs/datasets/ceval/ceval_ppl_578f8d.py |
| ceval_ppl_93e5ce | configs/datasets/ceval/ceval_ppl_93e5ce.py |
| ceval_zero_shot_gen_bd40ef | configs/datasets/ceval/ceval_zero_shot_gen_bd40ef.py |
+--------------------------------+------------------------------------------------------------------+
启动评测 (10% A100 8GB 资源)
确保按照上述步骤正确安装 OpenCompass 并准备好数据集后,可以通过以下命令评测 InternLM2-Chat-1.8B 模型在 C-Eval 数据集上的性能。由于 OpenCompass 默认并行启动评估过程,我们可以在第一次运行时以 --debug 模式启动评估,并检查是否存在问题。在 --debug 模式下,任务将按顺序执行,并实时打印输出。
python run.py --datasets ceval_gen --hf-path /share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b --tokenizer-path /share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b --tokenizer-kwargs padding_side='left' truncation='left' trust_remote_code=True --model-kwargs trust_remote_code=True device_map='auto' --max-seq-len 1024 --max-out-len 16 --batch-size 2 --num-gpus 1 --debug
评测完成后,将会看到:
dataset version metric mode opencompass.models.huggingface.HuggingFace_Shanghai_AI_Laboratory_internlm2-chat-1_8b
------------------------------------------------- --------- ---------------- ------ ---------------------------------------------------------------------------------------
lukaemon_mmlu_college_biology caec7d accuracy gen 51.39
lukaemon_mmlu_college_chemistry 520aa6 accuracy gen 34
lukaemon_mmlu_college_computer_science 99c216 accuracy gen 41
lukaemon_mmlu_college_mathematics 678751 accuracy gen 32
lukaemon_mmlu_college_physics 4f382c accuracy gen 29.41
lukaemon_mmlu_electrical_engineering 770ce3 accuracy gen 44.14
lukaemon_mmlu_astronomy d3ee01 accuracy gen 48.03
lukaemon_mmlu_anatomy 72183b accuracy gen 45.93
lukaemon_mmlu_abstract_algebra 2db373 accuracy gen 31
lukaemon_mmlu_machine_learning 0283bb accuracy gen 32.14
lukaemon_mmlu_clinical_knowledge cb3218 accuracy gen 51.32
lukaemon_mmlu_global_facts ab07b6 accuracy gen 24
lukaemon_mmlu_management 80876d accuracy gen 62.14
lukaemon_mmlu_nutrition 4543bd accuracy gen 48.37
lukaemon_mmlu_marketing 7394e3 accuracy gen 65.81
lukaemon_mmlu_professional_accounting 444b7f accuracy gen 35.11
lukaemon_mmlu_high_school_geography 0780e6 accuracy gen 56.06
lukaemon_mmlu_international_law cf3179 accuracy gen 49.59
lukaemon_mmlu_moral_scenarios f6dbe2 accuracy gen 24.47
lukaemon_mmlu_computer_security ce7550 accuracy gen 63
lukaemon_mmlu_high_school_microeconomics 04d21a accuracy gen 48.32
lukaemon_mmlu_professional_law 5f7e6c accuracy gen 31.1
lukaemon_mmlu_medical_genetics 881ef5 accuracy gen 54
lukaemon_mmlu_professional_psychology 221a16 accuracy gen 42.48
lukaemon_mmlu_jurisprudence 001f24 accuracy gen 50
lukaemon_mmlu_world_religions 232c09 accuracy gen 60.82
lukaemon_mmlu_philosophy 08042b accuracy gen 49.2
lukaemon_mmlu_virology 12e270 accuracy gen 37.35
lukaemon_mmlu_high_school_chemistry ae8820 accuracy gen 35.96
lukaemon_mmlu_public_relations e7d39b accuracy gen 53.64
lukaemon_mmlu_high_school_macroeconomics a01685 accuracy gen 45.64
lukaemon_mmlu_human_sexuality 42407c accuracy gen 54.2
lukaemon_mmlu_elementary_mathematics 269926 accuracy gen 29.89
lukaemon_mmlu_high_school_physics 93278f accuracy gen 34.44
lukaemon_mmlu_high_school_computer_science 9965a5 accuracy gen 38
lukaemon_mmlu_high_school_european_history eefc90 accuracy gen 58.18
lukaemon_mmlu_business_ethics 1dec08 accuracy gen 42
lukaemon_mmlu_moral_disputes a2173e accuracy gen 43.64
lukaemon_mmlu_high_school_statistics 8f3f3a accuracy gen 40.28
lukaemon_mmlu_miscellaneous 935647 accuracy gen 55.17
lukaemon_mmlu_formal_logic cfcb0c accuracy gen 26.98
lukaemon_mmlu_high_school_government_and_politics 3c52f9 accuracy gen 60.62
lukaemon_mmlu_prehistory bbb197 accuracy gen 46.3
lukaemon_mmlu_security_studies 9b1743 accuracy gen 55.1
lukaemon_mmlu_high_school_biology 37b125 accuracy gen 56.13
lukaemon_mmlu_logical_fallacies 9cebb0 accuracy gen 55.83
lukaemon_mmlu_high_school_world_history 048e7e accuracy gen 65.4
lukaemon_mmlu_professional_medicine 857144 accuracy gen 45.59
lukaemon_mmlu_high_school_mathematics ed4dc0 accuracy gen 21.48
lukaemon_mmlu_college_medicine 38709e accuracy gen 43.35
lukaemon_mmlu_high_school_us_history 8932df accuracy gen 51.96
lukaemon_mmlu_sociology c266a2 accuracy gen 64.68
lukaemon_mmlu_econometrics d1134d accuracy gen 31.58
lukaemon_mmlu_high_school_psychology 7db114 accuracy gen 65.5
lukaemon_mmlu_human_aging 82a410 accuracy gen 48.43
lukaemon_mmlu_us_foreign_policy 528cfe accuracy gen 69
lukaemon_mmlu_conceptual_physics 63588e accuracy gen 32.34
mmlu-humanities - naive_average gen 47.19
mmlu-stem - naive_average gen 38.98
mmlu-social-science - naive_average gen 53.9
mmlu-other - naive_average gen 47.13
mmlu - naive_average gen 45.85
mmlu-weighted - weighted_average gen 44.3