README.md 7.44 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
change's avatar
change committed
37
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.4.1-ubuntu22.04-dtk25.04-py3.10-fixpy
38
```
39

40
41
进入docker,安装docker中没有的依赖:
```bash
change's avatar
change committed
42
43
44
45
# -v 映射目录根据本机自行修改
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 -v /物理机目录:/容器内目录 --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.4.1-ubuntu22.04-dtk25.04-py3.10-fixpy

# 启动容器
46
docker exec -it chatglm3 /bin/bash
change's avatar
change committed
47
48
49
50
51

# 环境包替换安装
## 执行该命令时,选择Y即可
apt remove python3-blinker
## sentence-transformers和transformers、numpy等库有依赖冲突,需要两步安装
52
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
change's avatar
change committed
53
pip install transformers==4.40.0 numpy==1.24.3 nltk==3.9.1 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
54
55
56
57
58
cd finetune_demo
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
```
### Conda(方法二)
1. 创建conda虚拟环境:
59

60
61
62
```bash
conda create -n chatglm python=3.10
```
63

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

change's avatar
change committed
66
67
68
- [DTK 25.04](https://download.sourcefind.cn:65024/1/main/latest)
- [Pytorch 2.4.1](https://download.sourcefind.cn:65024/4/main/pytorch/DAS1.5)
- [Deepspeed 0.14.2](https://download.sourcefind.cn:65024/4/main/deepspeed/DAS1.5)
69

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

72
73
74
75
76
77
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
```
78

79
### 注意
80

81
82
83
```python
#到虚拟环境下对应的python/site-packages注释掉一些版本判断
site-packages/accelerate/accelerator.py 文件
84

85
86
87
88
89
90
91
92
93
94
 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     #    )
95
96
```

97
## 数据集
luopl's avatar
luopl committed
98

change's avatar
change committed
99

100

101
### 模型下载
luopl's avatar
luopl committed
102

103
104
| Model | Seq Length |                                                              Download                                                               
| :---: |:---------------------------:|:-----------------------------------------------------------------------------------------------------------------------------------:
chenzk's avatar
chenzk committed
105
106
107
| 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]
108

109
## 训练
110

change's avatar
change committed
111

112
113


change's avatar
change committed
114
115
116
## 推理
使用以下的脚本推理时。都需要指定已经下载到本地的模型路径,如果没有指定,会从huggingface自动下载,指定模型路径方式如下:
### cli_demo
117
```
change's avatar
change committed
118
119
120
121
122
123
124
125
# 需修改路径
export MODEL_PATH=/models/chatglm3/chatglm3-6b
export TOKENIZER_PATH=/models/chatglm3/chatglm3-6b
# 推理
cd basic_demo
export HIP_VISIBLE_DEVICES=0
export PYTHONWARNINGS="ignore"
python cli_demo.py
126
```
change's avatar
change committed
127
128
129
<div align="center">
<img src="./media/cli_demo.png">
</div>
130

change's avatar
change committed
131
132
133
134
135
136
137
138
139
140
### web_demo_gradio
```
# 需修改路径
export MODEL_PATH=/models/chatglm3/chatglm3-6b
export TOKENIZER_PATH=/models/chatglm3/chatglm3-6b
# 推理
cd basic_demo
export HIP_VISIBLE_DEVICES=0
export PYTHONWARNINGS="ignore"
python web_demo_gradio.py
141
```
142
<div align="center">
change's avatar
change committed
143
<img src="./media/gradio.png">
144
</div>
145

change's avatar
change committed
146
147
148
149
150
151
152
153
154
155
156
### web_demo_streamlit
```
# 需修改路径
export MODEL_PATH=/models/chatglm3/chatglm3-6b
export TOKENIZER_PATH=/models/chatglm3/chatglm3-6b
# 推理
cd basic_demo
export HIP_VISIBLE_DEVICES=0
export PYTHONWARNINGS="ignore"
streamlit run web_demo_streamlit.py
```
157
<div align="center">
change's avatar
change committed
158
<img src="./media/streamlit.png">
159
</div>
160

change's avatar
change committed
161
162
163
164
165
166
167
### api_server
#### 服务端启动命令
```
# 需修改路径
export MODEL_PATH=/models/chatglm3/chatglm3-6b
export TOKENIZER_PATH=/models/chatglm3/chatglm3-6b
export EMBEDDING_PATH=/home/model/BAAI/bge-m3/
change's avatar
change committed
168
# 推理(案例中修改了api_server.py 536行的端口号为9000,根据端口占用情况自行修改)
change's avatar
change committed
169
170
171
172
173
python api_server.py
```
<div align="center">
<img src="./media/server.png">
</div>
174

change's avatar
change committed
175
176
#### api调用案例
```
change's avatar
change committed
177
curl -X POST "http://127.0.0.1:9000/v1/chat/completions" \
change's avatar
change committed
178
179
180
181
182
183
-H "Content-Type: application/json" \
-d "{\"model\": \"chatglm3-6b\", \"messages\": [{\"role\": \"system\", \"content\": \"You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user's instructions carefully. Respond using markdown.\"}, {\"role\": \"user\", \"content\": \"你好,给我讲一个故事,大概100字\"}], \"stream\": false, \"max_tokens\": 100, \"temperature\": 0.8, \"top_p\": 0.8}"
```
<div align="center">
<img src="./media/client.png">
</div>
184

185
186
### 精度

187
188


189
## 应用场景
190

191
### 算法类别
192

193
`对话问答`
194

195
### 热点应用行业
196

197
`医疗,教育,科研,金融`
198

199
## 源码仓库及问题反馈
200

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

203
## 参考
204

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