README.md 4.21 KB
Newer Older
zhaoying1's avatar
zhaoying1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# ChatGLM-6B

## 模型介绍

ChatGLM-6B 是清华大学开源的开源的、支持中英双语的对话语言模型,基于 [General Language Model (GLM)](https://github.com/THUDM/GLM) 架构,具有 62 亿参数。ChatGLM-6B 使用了和 ChatGPT 相似的技术,针对中文问答和对话进行了优化。经过约 1T 标识符的中英双语训练,辅以监督微调、反馈自助、人类反馈强化学习等技术的加持,62 亿参数的 ChatGLM-6B 已经能生成相当符合人类偏好的回答。


## 数据集
本仓库以 [ADGEN](https://aclanthology.org/D19-1321.pdf) (广告生成) 数据集为例介绍代码的使用方法,该数据集任务为根据输入(content)生成一段广告词(summary)。数据集可从 [Google Drive](https://drive.google.com/file/d/13_vf0xRTQsyneRKdD1bZIr93vBGOczrk/view?usp=sharing) 或者 [Tsinghua Cloud](https://cloud.tsinghua.edu.cn/f/b3f119a008264b1cabd1/?dl=1) 下载处理好的 ADGEN 数据集,将解压后的AdvertiseGen目录放到 [ptuning](./ptuning)本目录下。


## 环境配置
推荐使用docker方式运行,提供[光源](https://www.sourcefind.cn/#/service-details)拉取的docker镜像:image.sourcefind.cn:5000/dcu/admin/base/pytorch:1.10.0-centos7.6-dtk-22.10.1-py37-latest

进入docker:

    cd /opt/dtk/.hip
    source replace_origin.sh
    pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
    pip3 install rouge_chinese nltk jieba datasets -i https://mirrors.aliyun.com/pypi/simple
此外,需要安装 Deepspeed,可从开发者社区下载对应版本安装包[Deepspeed](https://cancon.hpccube.com:65024/directlink/4/deepspeed/dtk22.10/deepspeed-0.6.3+1b2721a.dtk2210-cp37-cp37m-manylinux2014_x86_64.whl)进行安装。

## P-tuning v2微调
本仓库实现了对于ChatGLM-6B模型基于[P-Tuning v2](https://github.com/THUDM/P-tuning-v2)的微调。P-Tuning v2是由清华大学提出的一种高效参数微调方法,采用该方法可以将需要微调的参数量减少到原来的 0.1%。

### 实验设置

    max_source_length=64
    max_target_length=64
    max_steps=3000
    pre_seq_len=128
    learning_rate=5e-3
    per_device_train_batch_size=16
    gradient_accumulation_steps=1
    fp16

### 训练
该微调脚本运行环境为1节点,4张DCU-Z100-32G

微调训练命令

    cd ptuning
    bash pt_train.sh

### 训练Loss收敛情况
![](./ptuning/logs/6B_ds_pt_bs16_accum1_4cards_zero2_5e-3.jpg)

### 推理测评
在 P-tuning v2 训练时模型只保存 PrefixEncoder 部分的参数,所以在推理时需要同时加载原 ChatGLM-6B 模型以及 PrefixEncoder 的权重,可直接运行一下命令:

    cd ptuning
    bash evaluate_pt.sh

测试结果:
| Checkpoint | Training Loss |BLEU-4 | Rouge-1 |  Rouge-2 | Rouge-l |
| :------: | :------: |:------: | :------: |:------: | :------: |
| 2000 steps |  3.57 | 7.9777 | 31.0344 |  6.981 | 24.7393 |

## Finetune全参数微调
### 实验设置

    max_source_length=64
    max_target_length=64
    max_steps=5000
    pre_seq_len=128
    learning_rate=5e-5
    per_device_train_batch_size=32
    gradient_accumulation_steps=1
    fp16

### 训练
该微调脚本运行环境为1节点,4张DCU-Z100-32G

微调训练命令

    cd ptuning
    bash ft_train.sh

### 训练Loss收敛情况
![](./ptuning/logs/6B_ds_ft_bs32_accum1_4cards_zero3_5e-5.jpg)

### 推理测评

    cd ptuning
    bash evaluate_ft.sh

测试结果:
| Checkpoint | Training Loss |BLEU-4 | Rouge-1 |  Rouge-2 | Rouge-l |
| :------: | :------: |:------: | :------: |:------: | :------: |
| 3000 steps |  2.3398 | 7.6501 | 29.2229 | 6.466 | 23.8506 |


<!-- ## 评估结果
|               | Finetune | P-tuning v2 | LoRA |
| ------------- | ----------- | ----- | ------------- |
| BLEU-4        | 8.01    | 8.10 | 7.62 |
| Rouge-1       | 31.23  | 31.12 | 30.60 |
| Rouge-2       | 7.36    | 7.11 | 6.96 |
| Rouge-l       | 25.08  | 24.97 | 24.80 |
| Training Loss | 3.00 | 3.57 | 3.32 | -->
## 模型使用
运行如下命令:

    python cli_demo.py
    
程序会在命令行中进行交互式的对话,在命令行中输入指示并回车即可生成回复,输入 clear 可以清空对话历史,输入 stop 终止程序。

## 参考
[THUDM/ChatGLM-6B](https://github.com/THUDM/ChatGLM-6B/tree/main)