"src/vscode:/vscode.git/clone" did not exist on "a643c6300eebab07e904e7834703d32949940218"
README.md 7.5 KB
Newer Older
1
# ChatGLM3-6B
2

3
4
5
## 论文
`GLM: General Language Model Pretraining with Autoregressive Blank Infilling`
- [https://arxiv.org/abs/2103.10360](https://arxiv.org/abs/2103.10360)
6

7
8
## 模型结构
ChatGLM3 是智谱AI和清华大学 KEG 实验室联合发布的新一代对话预训练模型。ChatGLM3-6B 是 ChatGLM3 系列中的开源模型,在保留了前两代模型对话流畅、部署门槛低等众多优秀特性的基础上,ChatGLM3-6B的基础模型ChatGLM3-6B-Base 采用了更多样的训练数据、更充分的训练步数和更合理的训练策略。
9

10
ChatGLM3-6B同样采用Transformer模型结构:
11

12
13
14
<div align="center">
<img src="./media/transformers.jpg" width="300" height="400">
</div>
15

16
以下是ChatGLM2-6B的主要网络参数配置:
17
18


19
20
21
| 模型名称 | 隐含层维度 | 层数 | 头数 | 词表大小 | 位置编码 | 最大长 |
| -------- | -------- | -------- | -------- | -------- | -------- | -------- | 
|ChatGLM3-6B | 4,096 | 28 | 32 | 65024 |  RoPE | 8192 |
22
23


24
25
## 算法原理
模型基于 [General Language Model (GLM)](https://github.com/THUDM/GLM) 架构,GLM是一种基于Transformer的语言模型,以自回归空白填充为训练目标,同时具备自回归和自编码能力。
26

27
28
29
<div align="center">
<img src="./media/GLM.png" width="550" height="200">
</div>
30
31


32
## 环境配置
33

34
35
36
### Docker(方式一)
推荐使用docker方式运行,提供拉取的docker镜像:
```bash
37
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
38
```
39

40
41
进入docker,安装docker中没有的依赖:
```bash
42
docker run -dit --network=host --name=chatglm3 --privileged --device=/dev/kfd --device=/dev/dri --ipc=host --shm-size=16G   -v /opt/hyhal/:/opt/hyhal/:ro --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root --ulimit stack=-1:-1 --ulimit memlock=-1:-1 image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
43
44
45
46
47
48
49
docker exec -it chatglm3 /bin/bash
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
cd finetune_demo
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
```
### Conda(方法二)
1. 创建conda虚拟环境:
50

51
52
53
```bash
conda create -n chatglm python=3.10
```
54

chenzk's avatar
chenzk committed
55
2. 关于本项目DCU显卡所需的工具包、深度学习库等均可从[光合](https://developer.sourcefind.cn/tool/)开发者社区下载安装。
56

57
58
59
- [DTK 24.04.1](https://cancon.hpccube.com:65024/1/main/DTK-24.04.1)
- [Pytorch 2.1.0](https://cancon.hpccube.com:65024/4/main/pytorch)
- [Deepspeed 0.12.3](https://cancon.hpccube.com:65024/directlink/4/deepspeed/DAS1.1/deepspeed-0.12.3+gita724046.abi1.dtk2404.torch2.1.0-cp310-cp310-manylinux_2_31_x86_64.whl)
60

61
    Tips:以上dtk驱动、python、deepspeed等工具版本需要严格一一对应。
62

63
64
65
66
67
68
3. 其它依赖库参照requirements.txt安装:
```bash
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
cd finetune_demo
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
```
69

70
### 注意
71

72
73
74
```python
#到虚拟环境下对应的python/site-packages注释掉一些版本判断
site-packages/accelerate/accelerator.py 文件
75

76
77
78
79
80
81
82
83
84
85
 287             #if not is_deepspeed_available():
 288             #    raise ImportError("DeepSpeed is not installed => run `pip install deepspeed` or build it from source.")
 289             #if compare_versions("deepspeed", "<", "0.9.3"):
 290             #    raise ImportError("DeepSpeed version must be >= 0.9.3. Please update DeepSpeed.")
 
site-packages/transformers/utils/versions.py 文件
 43     #if not ops[op](version.parse(got_ver), version.parse(want_ver)):
 44     #    raise ImportError(
 45     #        f"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}"
 46     #    )
86
87
```

88
89
90
## 数据集
单轮对话数据以[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)
luopl's avatar
luopl committed
91

92
下载处理好的 ADGEN 数据集,将解压后的AdvertiseGen目录放到 [finetune_demo/data](./finetune_demo/data)目录下。数据集目录结构如下:
93
```
94
95
96
 ── AdvertiseGen
    │   ├── dev.json
    │   └── train.json
97
```
98
99
100
101
通过以下方式将数据集处理成模型需要的格式:
```bash
cd finetune_demo
python process.py
102
103
```

104
### 模型下载
luopl's avatar
luopl committed
105

106
107
| Model | Seq Length |                                                              Download                                                               
| :---: |:---------------------------:|:-----------------------------------------------------------------------------------------------------------------------------------:
chenzk's avatar
chenzk committed
108
109
110
| ChatGLM3-6B | 8k |      [HuggingFace](https://huggingface.co/THUDM/chatglm3-6b) \| [ModelScope](https://modelscope.cn/models/ZhipuAI/chatglm3-6b)   \| [SCNet]    
| ChatGLM3-6B-Base | 8k | [HuggingFace](https://huggingface.co/THUDM/chatglm3-6b-base) \| [ModelScope](https://modelscope.cn/models/ZhipuAI/chatglm3-6b-base) \| [SCNet]
| ChatGLM3-6B-32K | 32k |                                   [HuggingFace](https://huggingface.co/THUDM/chatglm3-6b-32k) \| [ModelScope](https://modelscope.cn/models/ZhipuAI/chatglm3-6b-32k) \| [SCNet]
111

112
## 训练
113

114
### SFT微调
115

116
117
#### 单轮对话微调
```bash
lvzhen's avatar
lvzhen committed
118
119
cd ./finetune_demo
bash sft.sh
120
```
121
注意:请根据自己的需求配置其中的模型路径、数据集路径;batchsize、学习率等参数在./finetune_demo/configs/sft.yaml;
122

123
124
#### 推理验证
对于输入输出格式的微调,可使用 `sft_inf.sh` 进行基本的推理验证。
125

126
在完成微调任务之后,我们可以查看到 `output` 文件夹下多了很多个`checkpoint-*`的文件夹,这些文件夹代表了训练的轮数。 我们选择最后一轮的微调权重,并使用inference进行导入。
127

128
注意:此时要将hf上下载的原生`tokenizer_config.json``tokenization_chatglm.py` 两个文件放入要待测的 checkpoint 文件夹下,比如./finetune_demo/output/checkpoint-3000/
129

130
```bash
lvzhen's avatar
lvzhen committed
131
132
cd ./finetune_demo
bash sft_inf.sh
133
134
135
136
```



137
### LORA微调
138

139
#### 单轮对话微调
140

141
```bash
lvzhen's avatar
lvzhen committed
142
143
cd ./fintune_demo
bash lora.sh
144
145
```

146
注意:请根据自己的需求配置其中的模型路径、数据集路径;batchsize、学习率等参数在 ./finetune_demo/configs/lora.yaml;
147

148
#### 推理验证
149

150
在完成微调任务之后,我们可以查看到 `output` 文件夹下多了很多个`checkpoint-*`的文件夹,这些文件夹代表了训练的轮数。 我们选择最后一轮的微调权重,并使用inference进行导入。
151

152
注意:经过LORA微调训练后的checkpoint无需复制原生GLM3的tokenizer文件到其目录下。
153

154
```bash
lvzhen's avatar
lvzhen committed
155
156
cd ./finetune_demo
bash lora_inf.sh
157
158
159
160
```



161
## Result
162

163
### SFT微调
164

165
#### 单轮对话微调推理结果
166

167
<div align="center">
168
<img src="./media/result1.jpg">
169
</div>
170

171
### LORA微调
172

173
#### 单轮对话微调推理结果
174

175
<div align="center">
176
<img src="./media/result2.jpg">
177
</div>
178
179
180
181




182
183
### 精度

184
185


186
## 应用场景
187

188
### 算法类别
189

190
`对话问答`
191

192
### 热点应用行业
193

194
`医疗,教育,科研,金融`
195

196
## 源码仓库及问题反馈
197

chenzk's avatar
chenzk committed
198
- https://developer.sourcefind.cn/codes/modelzoo/chatglm3-6b_pytorch
199

200
## 参考
201

202
- [THUDM/ChatGLM3-6B](https://github.com/THUDM/ChatGLM3/tree/main)
203