get_started.md 15.2 KB
Newer Older
gaotongxiao's avatar
gaotongxiao committed
1
2
# 安装

Ezra-Yu's avatar
Ezra-Yu committed
3
1. 准备 OpenCompass 运行环境:
gaotongxiao's avatar
gaotongxiao committed
4

Leymore's avatar
Leymore committed
5
6
7
8
   ```bash
   conda create --name opencompass python=3.10 pytorch torchvision pytorch-cuda -c nvidia -c pytorch -y
   conda activate opencompass
   ```
gaotongxiao's avatar
gaotongxiao committed
9

Leymore's avatar
Leymore committed
10
   如果你希望自定义 PyTorch 版本或相关的 CUDA 版本,请参考 [官方文档](https://pytorch.org/get-started/locally/) 准备 PyTorch 环境。需要注意的是,OpenCompass 要求 `pytorch>=1.13`
Ma Zerun's avatar
Ma Zerun committed
11

gaotongxiao's avatar
gaotongxiao committed
12
13
2. 安装 OpenCompass:

Leymore's avatar
Leymore committed
14
   ```bash
Songyang Zhang's avatar
Songyang Zhang committed
15
   git clone https://github.com/open-compass/opencompass.git
Leymore's avatar
Leymore committed
16
17
18
   cd opencompass
   pip install -e .
   ```
gaotongxiao's avatar
gaotongxiao committed
19

Ezra-Yu's avatar
Ezra-Yu committed
20
3. 安装 humaneval(可选):
gaotongxiao's avatar
gaotongxiao committed
21

Leymore's avatar
Leymore committed
22
   如果你需要**在 humaneval 数据集上评估模型代码能力**,请执行此步骤,否则忽略这一步。
Ezra-Yu's avatar
Ezra-Yu committed
23

Leymore's avatar
Leymore committed
24
25
   <details>
   <summary><b>点击查看详细</b></summary>
gaotongxiao's avatar
gaotongxiao committed
26

Leymore's avatar
Leymore committed
27
28
29
30
31
32
33
   ```bash
   git clone https://github.com/openai/human-eval.git
   cd human-eval
   pip install -r requirements.txt
   pip install -e .
   cd ..
   ```
gaotongxiao's avatar
gaotongxiao committed
34

Leymore's avatar
Leymore committed
35
   请仔细阅读 `human_eval/execution.py` **第48-57行**的注释,了解执行模型生成的代码可能存在的风险,如果接受这些风险,请取消**第58行**的注释,启用代码执行评测。
gaotongxiao's avatar
gaotongxiao committed
36

Leymore's avatar
Leymore committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
   </details>

4. 安装 Llama(可选):

   如果你需要**使用官方实现评测 Llama / Llama-2 / Llama-2-chat 模型**,请执行此步骤,否则忽略这一步。

   <details>
   <summary><b>点击查看详细</b></summary>

   ```bash
   git clone https://github.com/facebookresearch/llama.git
   cd llama
   pip install -r requirements.txt
   pip install -e .
   cd ..
   ```

Songyang Zhang's avatar
Songyang Zhang committed
54
   你可以在 `configs/models` 下找到所有 Llama / Llama-2 / Llama-2-chat 模型的配置文件示例。([示例](https://github.com/open-compass/opencompass/blob/eb4822a94d624a4e16db03adeb7a59bbd10c2012/configs/models/llama2_7b_chat.py))
Leymore's avatar
Leymore committed
55
56

   </details>
Ezra-Yu's avatar
Ezra-Yu committed
57
58
59
60
61
62
63
64
65

# 数据集准备

OpenCompass 支持的数据集主要包括两个部分:

1. Huggingface 数据集: [Huggingface Dataset](https://huggingface.co/datasets) 提供了大量的数据集,这部分数据集运行时会**自动下载**

2. 自建以及第三方数据集:OpenCompass 还提供了一些第三方数据集及自建**中文**数据集。运行以下命令**手动下载解压**

Tong Gao's avatar
Tong Gao committed
66
在 OpenCompass 项目根目录下运行下面命令,将数据集准备至 `${OpenCompass}/data` 目录下:
Ezra-Yu's avatar
Ezra-Yu committed
67
68

```bash
Songyang Zhang's avatar
Songyang Zhang committed
69
wget https://github.com/open-compass/opencompass/releases/download/0.1.1/OpenCompassData.zip
Ezra-Yu's avatar
Ezra-Yu committed
70
71
72
73
74
unzip OpenCompassData.zip
```

OpenCompass 已经支持了大多数常用于性能比较的数据集,具体支持的数据集列表请直接在 `configs/datasets` 下进行查找。

gaotongxiao's avatar
gaotongxiao committed
75
76
# 快速上手

Ezra-Yu's avatar
Ezra-Yu committed
77
我们会以测试 [OPT-125M](https://huggingface.co/facebook/opt-125m) 以及 [OPT-350M](https://huggingface.co/facebook/opt-350m) 预训练基座模型在 [SIQA](https://huggingface.co/datasets/social_i_qa)[Winograd](https://huggingface.co/datasets/winogrande) 上的性能为例,带领你熟悉 OpenCompass 的一些基本功能。
Ezra-Yu's avatar
Ezra-Yu committed
78

Ezra-Yu's avatar
Ezra-Yu committed
79
运行前确保已经安装了 OpenCompass,本实验可以在单张 _GTX-1660-6G_ 显卡上成功运行。
Songyang Zhang's avatar
Songyang Zhang committed
80
更大参数的模型,如 Llama-7B, 可参考 [configs](https://github.com/open-compass/opencompass/tree/main/configs) 中其他例子。
Ma Zerun's avatar
Ma Zerun committed
81

82
83
84
85
86
87
88
89
## 配置任务

OpenCompass 中,每个评测任务都由待评测的模型和数据集组成,而评测的入口为 `run.py`。用户可以通过命令行或配置文件的方式去选择待测的模型和数据集。

`````{tabs}

````{tab} 命令行形式
用户可以通过 `--models` 和 `--datasets` 组合待测试的模型和数据集。
Ma Zerun's avatar
Ma Zerun committed
90

Ezra-Yu's avatar
Ezra-Yu committed
91
```bash
92
python run.py --models opt_125m opt_350m --datasets siqa_gen winograd_ppl
Ezra-Yu's avatar
Ezra-Yu committed
93
```
Ma Zerun's avatar
Ma Zerun committed
94

95
模型和数据集以配置文件的形式预先存放在 `configs/models` 和 `configs/datasets` 下。用户可以通过 `tools/list_configs.py` 查看或筛选当前可用的模型和数据集配置。
Tong Gao's avatar
Tong Gao committed
96
97

```bash
98
99
100
101
# 列出所有配置
python tools/list_configs.py
# 列出所有跟 llama 及 mmlu 相关的配置
python tools/list_configs.py llama mmlu
Tong Gao's avatar
Tong Gao committed
102
103
```

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
部分样例输出如下:

```text
+-----------------+-----------------------------------+
| Model           | Config Path                       |
|-----------------+-----------------------------------|
| hf_llama2_13b   | configs/models/hf_llama2_13b.py   |
| hf_llama2_70b   | configs/models/hf_llama2_70b.py   |
| ...             | ...                               |
+-----------------+-----------------------------------+
+-------------------+---------------------------------------------------+
| Dataset           | Config Path                                       |
|-------------------+---------------------------------------------------|
| cmmlu_gen         | configs/datasets/cmmlu/cmmlu_gen.py               |
| cmmlu_gen_ffe7c0  | configs/datasets/cmmlu/cmmlu_gen_ffe7c0.py        |
| ...               | ...                                               |
+-------------------+---------------------------------------------------+
```

用户可以按照第一列中的名称去作为 `python run.py` 中 `--models` 和 `--datasets` 的传入参数。在数据集部分,相同名称但不同后缀的数据集一般意味着其提示词或评测方式是不一样的。

对于 HuggingFace 模型,用户可以直接通过命令行设定模型参数,而无需额外配置文件。例如,对于 `facebook/opt-125m` 模型,可以通过以下命令进行评测:
Tong Gao's avatar
Tong Gao committed
126
127

```bash
128
129
130
131
132
133
134
135
python run.py --datasets siqa_gen winograd_ppl \
--hf-model facebook/opt-125m \
--model-kwargs device_map='auto' \
--tokenizer-kwargs padding_side='left' truncation='left' trust_remote_code=True \
--max-seq-len 2048 \
--max-out-len 100 \
--batch-size 128  \
--num-gpus 1
Tong Gao's avatar
Tong Gao committed
136
137
```

138
139
140
```{tip}
关于 `run.py` 支持的所有 HuggingFace 相关参数,请阅读 [评测任务发起](./user_guides/experimentation.md#评测任务发起)。
```
Ma Zerun's avatar
Ma Zerun committed
141

Ezra-Yu's avatar
Ezra-Yu committed
142

143
````
Tong Gao's avatar
Tong Gao committed
144

145
146
147
148
149
````{tab} 配置形式

除了通过在命令行中对实验进行配置,OpenCompass 也支持用户把实验全量配置写入一份配置文件中,并直接通过 `run.py` 运行。这样的配置方式允许用户方便地修改实验参数,对实验进行更灵活的配置,也让运行命令更为简洁。配置文件以 Python 格式组织,且必须包含 `datasets` 和 `models` 字段。

本次的测试的配置文件为 [configs/eval_demo.py](/configs/eval_demo.py)。该配置通过[继承机制](./user_guides/config.md#继承机制)引入了所需的数据集和模型配置,并按照格式组合了 `datasets` 和 `models` 字段。
Ma Zerun's avatar
Ma Zerun committed
150
151

```python
152
from mmengine.config import read_base
Ma Zerun's avatar
Ma Zerun committed
153
154

with read_base():
155
156
    from .datasets.siqa.siqa_gen import siqa_datasets
    from .datasets.winograd.winograd_ppl import winograd_datasets
157
158
    from .models.opt.hf_opt_125m import opt125m
    from .models.opt.hf_opt_350m import opt350m
159
160
161

datasets = [*siqa_datasets, *winograd_datasets]
models = [opt125m, opt350m]
Tong Gao's avatar
Tong Gao committed
162

Ezra-Yu's avatar
Ezra-Yu committed
163
164
```

165
在运行任务时,我们只需要往 `run.py` 传入配置文件的路径即可:
Ezra-Yu's avatar
Ezra-Yu committed
166

167
168
169
```bash
python run.py configs/eval_demo.py
```
Ma Zerun's avatar
Ma Zerun committed
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
````

`````

配置文件评测方式较为简洁,下文将以该方式为例讲解其余功能。

## 运行评测

由于 OpenCompass 默认使用并行的方式进行评测,为了便于及时发现问题,我们可以在首次启动时使用 debug 模式运行,该模式会将任务串行执行,并会实时输出任务的执行进度。

```bash
python run.py configs/eval_demo.py -w outputs/demo --debug
```

如果一切正常,屏幕上会出现 "Starting inference process":

```bash
Loading cached processed dataset at .cache/huggingface/datasets/social_i_qa/default/0.1.0/674d85e42ac7430d3dcd4de7007feaffcb1527c535121e09bab2803fbcc925f8/cache-742512eab30e8c9c.arrow
[2023-07-12 18:23:55,076] [opencompass.openicl.icl_inferencer.icl_gen_inferencer] [INFO] Starting inference process...
```

此时可以使用 `ctrl+c` 中断 debug 模式的执行,并运行以下命令进行并行评测:

```bash
python run.py configs/eval_demo.py -w outputs/demo
```

运行 demo 期间,我们来介绍一下本案例中的配置内容以及启动选项。

## 配置详解
Ma Zerun's avatar
Ma Zerun committed
201

Tong Gao's avatar
Tong Gao committed
202
### 模型列表 `models`
Ma Zerun's avatar
Ma Zerun committed
203

204
OpenCompass 在 `configs/models` 下提供了一系列预定义好的模型配置。下面为 [opt-350m](/configs/models/hf_opt_350m.py) (`configs/models/hf_opt_350m.py`) 相关的配置片段:
Ezra-Yu's avatar
Ezra-Yu committed
205
206

```python
Tong Gao's avatar
Tong Gao committed
207
208
# 提供直接使用 HuggingFaceCausalLM 模型的接口
from opencompass.models import HuggingFaceCausalLM
Ezra-Yu's avatar
Ezra-Yu committed
209

Ezra-Yu's avatar
Ezra-Yu committed
210
211
212
# OPT-350M
opt350m = dict(
       type=HuggingFaceCausalLM,
Tong Gao's avatar
Tong Gao committed
213
       # 以下参数为 HuggingFaceCausalLM 相关的初始化参数
214
       path='facebook/opt-350m',  # HuggingFace 模型地址
Ezra-Yu's avatar
Ezra-Yu committed
215
216
217
218
219
       tokenizer_path='facebook/opt-350m',
       tokenizer_kwargs=dict(
           padding_side='left',
           truncation_side='left',
           trust_remote_code=True),
220
       model_kwargs=dict(device_map='auto'),  # 构造 model 的参数
Tong Gao's avatar
Tong Gao committed
221
       # 下列参数为所有模型均需设定的初始化参数,非 HuggingFaceCausalLM 独有
Ezra-Yu's avatar
Ezra-Yu committed
222
       abbr='opt350m',                    # 模型简称,用于结果展示
Tong Gao's avatar
Tong Gao committed
223
       max_seq_len=2048,              # 模型能接受的最大序列长度
Ezra-Yu's avatar
Ezra-Yu committed
224
225
       max_out_len=100,                   # 最长生成 token 数
       batch_size=64,                     # 批次大小
226
       run_cfg=dict(num_gpus=1),          # 运行模型所需的gpu数
Ma Zerun's avatar
Ma Zerun committed
227
    )
228
```
Tong Gao's avatar
Tong Gao committed
229

230
在使用配置时,我们可以通过在命令行参数中使用 `--models` 指定相关文件,也可以通过继承机制在实验配置文件中导入模型配置,并加入到 `models` 列表中。
Ezra-Yu's avatar
Ezra-Yu committed
231

232
233
234
235
236
237
238
239
240
241
242
243
如果你想要测试的 HuggingFace 模型不在其中,也可以在命令行中直接指定相关参数。

```bash
python run.py \
--hf-model facebook/opt-350m \  # HuggingFace 模型地址
--tokenizer-path facebook/opt-350m \  # HuggingFace tokenizer 地址(如与模型地址相同,可省略)
--tokenizer-kwargs padding_side='left' truncation='left' trust_remote_code=True \  # 构造 tokenizer 的参数
--model-kwargs device_map='auto' \  # 构造 model 的参数
--max-seq-len 2048 \  # 模型能接受的最大序列长度
--max-out-len 100 \  # 最长生成 token 数
--batch-size 64  \  # 批次大小
--num-gpus 1  # 运行模型所需的gpu数
Ma Zerun's avatar
Ma Zerun committed
244
245
```

Tong Gao's avatar
Tong Gao committed
246
HuggingFace 中的 'facebook/opt-350m' 以及 'facebook/opt-125m' 权重会在运行时自动下载。
Ezra-Yu's avatar
Ezra-Yu committed
247

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
```{note}
如果需要了解更多参数的说明,或 API 模型及自定义模型的测试,可阅读 [准备模型](./user_guides/models.md)。
```

### 数据集列表 `datasets`

与模型类似,数据集的配置文件都提供在 `configs/datasets` 下,用户可以在命令行中通过 `--datasets` ,或在配置文件中通过继承导入相关配置。

以下为 `configs/eval_demo.py` 中与数据集相关的配置片段:

```python
from mmengine.config import read_base  # 使用 mmengine.read_base() 读取基础配置

with read_base():
    # 直接从预设数据集配置中读取需要的数据集配置
    from .datasets.winograd.winograd_ppl import winograd_datasets  # 读取 Winograd 的配置,基于 PPL (perplexity) 进行评测
    from .datasets.siqa.siqa_gen import siqa_datasets  # 读取 SIQA 的配置,基于生成式进行评测

datasets = [*siqa_datasets, *winograd_datasets]       # 最后 config 需要包含所需的评测数据集列表 datasets
```

数据集的配置通常为 'ppl' 和 'gen' 两类配置文件,表示使用的评估方式。其中 `ppl` 表示使用判别式评测, `gen` 表示使用生成式评测。

Songyang Zhang's avatar
Songyang Zhang committed
271
此外,[configs/datasets/collections](https://github.com/open-compass/opencompass/blob/main/configs/datasets/collections) 存放了各类数据集集合,方便做综合评测。OpenCompass 常用 [`base_medium.py`](/configs/datasets/collections/base_medium.py) 对模型进行全量测试。若需要复现结果,直接导入该文件即可。如:
272
273
274
275
276
277
278
279

```bash
python run.py --models hf_llama_7b --datasets base_medium
```

```{note}
更多介绍可查看 [数据集配置](./user_guides/dataset_prepare.md)。
```
Ma Zerun's avatar
Ma Zerun committed
280

Tong Gao's avatar
Tong Gao committed
281
282
283
### 启动评测

配置文件准备完毕后,我们可以使用 debug 模式启动任务,以检查模型加载、数据集读取是否出现异常,如未正确读取缓存等。
Ma Zerun's avatar
Ma Zerun committed
284
285

```shell
Ezra-Yu's avatar
Ezra-Yu committed
286
python run.py configs/eval_demo.py -w outputs/demo --debug
Ma Zerun's avatar
Ma Zerun committed
287
288
289
290
291
```

`--debug` 模式下只能逐一序列执行任务,因此检查无误后,可关闭 `--debug` 模式,使程序充分利用多卡资源

```shell
Ezra-Yu's avatar
Ezra-Yu committed
292
python run.py configs/eval_demo.py -w outputs/demo
Ma Zerun's avatar
Ma Zerun committed
293
294
295
296
```

以下是一些与评测相关的参数,可以帮助你根据自己的环境情况配置更高效的推理任务。

Tong Gao's avatar
Tong Gao committed
297
- `-w outputs/demo`: 评测日志及结果保存目录。若不指定,则默认为 `outputs/default`
Ma Zerun's avatar
Ma Zerun committed
298
299
300
301
302
303
304
305
306
307
308
309
- `-r`: 重启上一次(中断的)评测
- `--mode all`: 指定进行某一阶段的任务
  - all: 进行全阶段评测,包括推理和评估
  - infer: 仅进行各个数据集上的推理
  - eval: 仅基于推理结果进行评估
  - viz: 仅展示评估结果
- `--max-partition-size 2000`: 数据集拆分尺寸,部分数据集可能比较大,利用此参数将其拆分成多个子任务,能有效利用资源。但如果拆分过细,则可能因为模型本身加载时间过长,反而速度更慢
- `--max-num-workers 32`: 最大并行启动任务数,在 Slurm 等分布式环境中,该参数用于指定最大提交任务数;在本地环境中,该参数用于指定最大并行执行的任务数,注意实际并行执行任务数受制于 GPU 等资源数,并不一定为该数字。

如果你不是在本机进行评测,而是使用 slurm 集群,可以指定如下参数:

- `--slurm`: 使用 slurm 在集群提交任务
Ezra-Yu's avatar
Ezra-Yu committed
310
- `--partition(-p) my_part`: slurm 集群分区
Ma Zerun's avatar
Ma Zerun committed
311
312
- `--retry 2`: 任务出错重试次数

Ezra-Yu's avatar
Ezra-Yu committed
313
314
315
316
```{tip}
这个脚本同样支持将任务提交到阿里云深度学习中心(DLC)上运行,以及更多定制化的评测策略。请参考 [评测任务发起](./user_guides/experimentation.md#评测任务发起) 了解更多细节。
```

Ezra-Yu's avatar
Ezra-Yu committed
317
## 评测结果
Ma Zerun's avatar
Ma Zerun committed
318
319
320
321

评测完成后,会打印评测结果表格如下:

```text
Ezra-Yu's avatar
Ezra-Yu committed
322
323
324
325
dataset    version    metric    mode      opt350m    opt125m
---------  ---------  --------  ------  ---------  ---------
siqa       e78df3     accuracy  gen         21.55      12.44
winograd   b6c7ed     accuracy  ppl         51.23      49.82
Ma Zerun's avatar
Ma Zerun committed
326
327
```

Tong Gao's avatar
Tong Gao committed
328
所有过程的日志,预测,以及最终结果会放在 `outputs/demo/` 目录下。目录结构如下所示:
Ezra-Yu's avatar
Ezra-Yu committed
329
330
331
332

```text
outputs/default/
├── 20200220_120000
Ezra-Yu's avatar
Ezra-Yu committed
333
├── 20230220_183030   # 一次实验
Tong Gao's avatar
Tong Gao committed
334
335
│   ├── configs       # 每次实验都会在此处存下用于追溯的 config
│   ├── logs          # 运行日志
Ezra-Yu's avatar
Ezra-Yu committed
336
337
│   │   ├── eval
│   │   └── infer
Tong Gao's avatar
Tong Gao committed
338
339
340
│   ├── predictions   # 储存了每个任务的推理结果
│   ├── results       # 储存了每个任务的评测结果
│   └── summary       # 汇总每次实验的所有评测结果
Ezra-Yu's avatar
Ezra-Yu committed
341
├── ...
Ezra-Yu's avatar
Ezra-Yu committed
342
343
```

Leymore's avatar
Leymore committed
344
345
346
347
打印评测结果的过程可被进一步定制化,用于输出一些数据集的平均分 (例如 MMLU, C-Eval 等)。

关于评测结果输出的更多介绍可阅读 [结果展示](./user_guides/summarizer.md)

Ezra-Yu's avatar
Ezra-Yu committed
348
349
350
## 更多教程

想要更多了解 OpenCompass, 可以点击下列链接学习。
Ezra-Yu's avatar
Ezra-Yu committed
351

Tong Gao's avatar
Tong Gao committed
352
353
354
- [数据集配置](./user_guides/dataset_prepare.md)
- [准备模型](./user_guides/models.md)
- [任务运行和监控](./user_guides/experimentation.md)
Ezra-Yu's avatar
Ezra-Yu committed
355
- [如何调Prompt](./prompt/overview.md)
Leymore's avatar
Leymore committed
356
- [结果展示](./user_guides/summarizer.md)
Tong Gao's avatar
Tong Gao committed
357
- [学习配置文件](./user_guides/config.md)