InternLM第四期实战-L1-OpenCompass测评
OpenCompass 评测
教程链接:https://github.com/InternLM/Tutorial/tree/camp4/docs/L1/Evaluation
OpenCompass 提供了 API 模式评测和本地直接评测两种方式。其中 API 模式评测针对那些以 API 服务形式部署的模型,而本地直接评测则面向那些可以获取到模型权重文件的情况。
更多使用说明,请参考 OpenCompass 官方文档。
评测 API 模型
如果你想要评测通过 API 访问的大语言模型,首先你需要获取模型的 API 密钥(API Key)和接口地址。以 OpenAI 的 GPT 模型为例,你只需要在 OpenAI 官网申请一个 API Key,然后在评测配置文件中设置好这个密钥和相应的模型参数就可以开始评测了。
评测过程中,评测框架会自动向模型服务发送测试用例,获取模型的回复并进行打分分析。整个过程你不需要准备任何模型文件,也不用担心本地计算资源是否足够,只要确保网络连接正常即可。
创建环境
conda create -n opencompass python=3.10
conda activate opencompass
cd /root
git clone -b 0.3.3 https://github.com/open-compass/opencompass
cd opencompass
pip install -e .
pip install -r requirements.txt
pip install huggingface_hub==0.25.2
获取API_KEY
以评测 internlm 模型为例:打开网站浦语官方地址 https://internlm.intern-ai.org.cn/api/document 获得 api key 和 api 服务地址 (也可以从第三方平台 硅基流动 获取)。
在终端中运行:export INTERNLM_API_KEY=xxxxxxxxxxxxxxxxxxxxxxx # 填入你申请的 API Key
配置模型
在终端中运行 cd /root/opencompass/ 和 touch opencompass/configs/models/openai/puyu_api.py, 然后打开文件, 贴入以下代码:
点击查看代码
import os
from opencompass.models import OpenAISDK
internlm_url = 'https://internlm-chat.intern-ai.org.cn/puyu/api/v1/' # 你前面获得的 api 服务地址
internlm_api_key = os.getenv('INTERNLM_API_KEY')
models = [
dict(
# abbr='internlm2.5-latest',
type=OpenAISDK,
path='internlm2.5-latest', # 请求服务时的 model name
# 换成自己申请的APIkey
key=internlm_api_key, # API key
openai_api_base=internlm_url, # 服务地址
rpm_verbose=True, # 是否打印请求速率
query_per_second=0.16, # 服务请求速率
max_out_len=1024, # 最大输出长度
max_seq_len=4096, # 最大输入长度
temperature=0.01, # 生成温度
batch_size=1, # 批处理大小
retry=3, # 重试次数
)
]
配置数据集
在终端中运行 cd /root/opencompass/ 和 touch opencompass/configs/datasets/demo/demo_cmmlu_chat_gen.py, 然后打开文件, 贴入以下代码:
点击查看代码
from mmengine import read_base
with read_base():
from ..cmmlu.cmmlu_gen_c13365 import cmmlu_datasets
# 每个数据集只取前2个样本进行评测
for d in cmmlu_datasets:
d['abbr'] = 'demo_' + d['abbr']
d['reader_cfg']['test_range'] = '[0:1]' # 这里每个数据集只取1个样本, 方便快速评测.
运行测评
完成配置后, 在终端中运行: python run.py --models puyu_api.py --datasets demo_cmmlu_chat_gen.py --debug. 预计运行10分钟后, 得到结果:

评测本地模型
如果你想要评测本地部署的大语言模型,首先需要获取到完整的模型权重文件。以开源模型为例,你可以从 Hugging Face 等平台下载模型文件。接下来,你需要准备足够的计算资源,比如至少一张显存够大的 GPU,因为模型文件通常都比较大。有了模型和硬件后,你需要在评测配置文件中指定模型路径和相关参数,然后评测框架就会自动加载模型并开始评测。这种评测方式虽然前期准备工作相对繁琐,需要考虑硬件资源,但好处是评测过程完全在本地完成,不依赖网络状态,而且你可以更灵活地调整模型参数,深入了解模型的性能表现。这种方式特别适合需要深入研究模型性能或进行模型改进的研发人员。
以下示例为评测 InternLM2-Chat-1.8B 在 C-Eval 数据集上的性能。
环境配置
cd /root/opencompass
conda activate opencompass
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y
apt-get update
apt-get install cmake
pip install protobuf==4.25.3
pip install huggingface-hub==0.23.2
将数据集下载到本地:
cp /share/temp/datasets/OpenCompassData-core-20231110.zip /root/opencompass/
unzip OpenCompassData-core-20231110.zip
加载本地模型
在 OpenCompass 中,模型和数据集的配置文件都存放在configs文件夹下。我们可以通过运行 list_configs 命令列出所有跟 InternLM 及 C-Eval 相关的配置。
python tools/list_configs.py internlm ceval
打开 opencompass 文件夹下 configs/models/hf_internlm/的 hf_internlm2_5_1_8b_chat.py 文件, 修改如下:
from opencompass.models import HuggingFacewithChatTemplate
models = [
dict(
type=HuggingFacewithChatTemplate,
abbr='internlm2_5-1_8b-chat-hf',
path='/share/new_models/Shanghai_AI_Laboratory/internlm2_5-1_8b-chat/',
max_out_len=2048,
batch_size=8,
run_cfg=dict(num_gpus=1),
)
]
# python run.py --datasets ceval_gen --models hf_internlm2_5_1_8b_chat --debug
可以通过以下命令评测 InternLM2-Chat-1.8B 模型在 C-Eval 数据集上的性能。由于 OpenCompass 默认并行启动评估过程,我们可以在第一次运行时以 --debug 模式启动评估,并检查是否存在问题。在 --debug 模式下,任务将按顺序执行,并实时打印输出。
python run.py --datasets ceval_gen --models hf_internlm2_5_1_8b_chat --debug
# 如果出现 rouge 导入报错, 请 pip uninstall rouge 之后再次安装 pip install rouge==1.0.1 可解决问题.
# 个人方法:报错的文件修改如下
# -from rouge import Rouge
# +from rouge_chinese import Rouge
最后可以看到结果:


浙公网安备 33010602011771号