README.md 10.7 KB
Newer Older
zhuwenwen's avatar
zhuwenwen committed
1
2
3
4
<!--
 * @Author: zhuww
 * @email: zhuww@sugon.com
 * @Date: 2024-06-13 14:38:07
zhuwenwen's avatar
zhuwenwen committed
5
 * @LastEditTime: 2024-09-30 09:16:01
zhuwenwen's avatar
zhuwenwen committed
6
-->
laibao's avatar
laibao committed
7

zhuwenwen's avatar
zhuwenwen committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## 论文

`GLM: General Language Model Pretraining with Autoregressive Blank Infilling`

- [https://arxiv.org/abs/2103.10360](https://arxiv.org/abs/2103.10360)

## 模型结构

ChatGLM-6B 是清华大学开源的开源的、支持中英双语的对话语言模型,基于 [General Language Model (GLM)](https://github.com/THUDM/GLM) 架构,具有 62 亿参数。ChatGLM-6B 使用了和 ChatGPT 相似的技术,针对中文问答和对话进行了优化。经过约 1T 标识符的中英双语训练,辅以监督微调、反馈自助、人类反馈强化学习等技术的加持,62 亿参数的 ChatGLM-6B 已经能生成相当符合人类偏好的回答。ChatGLM2-6B 是开源中英双语对话模型 ChatGLM-6B 的第二代版本,ChatGLM3 是智谱AI和清华大学 KEG 实验室联合发布的新一代对话预训练模型。ChatGLM3-6B 是 ChatGLM3 系列中的开源模型,在保留了前两代模型对话流畅、部署门槛低等众多优秀特性的基础上,ChatGLM3-6B 具有更强大的基础模型、更完整的功能支持、更全面的开源序列。

<div align="center">
<img src="docs/transformers.jpg" width="300" height="400">
</div>

以下是ChatGLM系列模型的主要网络参数配置:

| 模型名称    | 隐含层维度 | 层数 | 头数 | 词表大小 | 位置编码 | 最大序列长度 |
| ----------- | ---------- | ---- | ---- | -------- | -------- | ------------ |
| ChatGLM2-6B | 4096       | 28   | 32   | 65024    | RoPE     | 8192         |
| ChatGLM3-6B | 4096       | 28   | 32   | 65024    | RoPE     | 8192         |
laibao's avatar
laibao committed
28
| glm-4-9b    | 4096       | 40   | 32   | 151552   | RoPE     | 131072       |
zhuwenwen's avatar
zhuwenwen committed
29
30
31
32
33
34
35
36
37
38
39
40

## 算法原理

ChatGLM系列模型基于GLM架构开发。GLM是一种基于Transformer的语言模型,以自回归空白填充为训练目标, 同时具备自回归和自编码能力。

<div align="center">
<img src="docs/GLM.png" width="550" height="200">
</div>

## 环境配置

### Docker(方法一)
laibao's avatar
laibao committed
41

zhuwenwen's avatar
zhuwenwen committed
42
43
44
提供[光源](https://www.sourcefind.cn/#/image/dcu/custom)拉取推理的docker镜像:

```
45
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.3.0-ubuntu22.04-dtk24.04.3-py3.10
zhuwenwen's avatar
zhuwenwen committed
46
47
48
# <Image ID>用上面拉取docker镜像的ID替换
# <Host Path>主机端路径
# <Container Path>容器映射路径
zhuwenwen's avatar
zhuwenwen committed
49
# 若要在主机端和容器端映射端口需要删除--network host参数
zhuwenwen's avatar
zhuwenwen committed
50
51
docker run -it --name chatglm_vllm --privileged --shm-size=64G  --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v /opt/hyhal:/opt/hyhal -v <Host Path>:<Container Path> <Image ID> /bin/bash
```
laibao's avatar
laibao committed
52

zhuwenwen's avatar
zhuwenwen committed
53
`Tips:若在K100/Z100L上使用,使用定制镜像docker pull image.sourcefind.cn:5000/dcu/admin/base/custom:vllm0.5.0-dtk24.04.1-ubuntu20.04-py310-zk-v1,K100/Z100L不支持awq量化`
zhuwenwen's avatar
zhuwenwen committed
54
55

### Dockerfile(方法二)
laibao's avatar
laibao committed
56

zhuwenwen's avatar
zhuwenwen committed
57
58
59
60
```
# <Host Path>主机端路径
# <Container Path>容器映射路径
docker build -t chatglm:latest .
zhuwenwen's avatar
zhuwenwen committed
61
docker run -it --name chatglm_vllm --privileged --shm-size=64G  --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v /opt/hyhal:/opt/hyhal:ro -v <Host Path>:<Container Path> llama:latest /bin/bash
zhuwenwen's avatar
zhuwenwen committed
62
63
64
```

### Anaconda(方法三)
laibao's avatar
laibao committed
65

zhuwenwen's avatar
zhuwenwen committed
66
67
68
```
conda create -n chatglm_vllm python=3.10
```
laibao's avatar
laibao committed
69

zhuwenwen's avatar
zhuwenwen committed
70
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。
laibao's avatar
laibao committed
71
72
73
74
75
76
77

* DTK驱动:dtk24.04.3
* Pytorch: 2.3.0
* triton: 2.1.0
* lmslim: 0.1.2
* flash_attn: 2.6.1
* vllm: 0.6.2
zhuwenwen's avatar
zhuwenwen committed
78
79
* python: python3.10

80
81
82
83
84
85
86
87
88
89
90
91
92
`Tips:需先安装相关依赖,最后安装vllm包`  

环境变量:  
export ALLREDUCE_STREAM_WITH_COMPUTE=1  
export VLLM_NUMA_BIND=1  
export VLLM_RANK0_NUMA=0  
export VLLM_RANK1_NUMA=1  
export VLLM_RANK2_NUMA=2  
export VLLM_RANK3_NUMA=3  
export VLLM_RANK4_NUMA=4  
export VLLM_RANK5_NUMA=5  
export VLLM_RANK6_NUMA=6  
export VLLM_RANK7_NUMA=7  
zhuwenwen's avatar
zhuwenwen committed
93
94

## 数据集
laibao's avatar
laibao committed
95

zhuwenwen's avatar
zhuwenwen committed
96
97
98
99
100
101


## 推理

### 模型下载

laibao's avatar
laibao committed
102
103
104
105
| 基座模型                                                             | 长文本模型                                                                     |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| [chatglm2-6b](http://113.200.138.88:18080/aimodels/chatglm2-6b)         | [chatglm2-6b-32k](http://113.200.138.88:18080/aimodels/thudm/chatglm2-6b-32k.git) |
| [chatglm3-6b](http://113.200.138.88:18080/aimodels/chatglm3-6b)         | [chatglm3-6b-32k](http://113.200.138.88:18080/aimodels/chatglm3-6b-32k)           |
panhb's avatar
panhb committed
106
| [glm-4-9b-chat](http://113.200.138.88:18080/aimodels/glm-4-9b-chat.git) | [glm-4-9b-chat-1m](https://modelscope.cn/models/ZhipuAI/glm-4-9b-chat-1m)         |
zhuwenwen's avatar
zhuwenwen committed
107
108

### 离线批量推理
laibao's avatar
laibao committed
109

zhuwenwen's avatar
zhuwenwen committed
110
```bash
zhuwenwen's avatar
zhuwenwen committed
111
python examples/offline_inference.py
zhuwenwen's avatar
zhuwenwen committed
112
```
laibao's avatar
laibao committed
113

zhuwenwen's avatar
zhuwenwen committed
114
115
116
117
其中,`prompts`为提示词;`temperature`为控制采样随机性的值,值越小模型生成越确定,值变高模型生成更随机,0表示贪婪采样,默认为1;`max_tokens=16`为生成长度,默认为1;
`model`为模型路径;`tensor_parallel_size=1`为使用卡数,默认为1;`dtype="float16"`为推理数据类型,如果模型权重是bfloat16,需要修改为float16推理,`quantization="gptq"`为使用gptq量化进行推理,需下载以上GPTQ模型。

### 离线批量推理性能测试
laibao's avatar
laibao committed
118

zhuwenwen's avatar
zhuwenwen committed
119
1、指定输入输出
laibao's avatar
laibao committed
120

zhuwenwen's avatar
zhuwenwen committed
121
```bash
zhuwenwen's avatar
zhuwenwen committed
122
python benchmarks/benchmark_throughput.py --num-prompts 1 --input-len 32 --output-len 128 --model THUDM/glm-4-9b-chat -tp 1 --trust-remote-code --enforce-eager --dtype float16
zhuwenwen's avatar
zhuwenwen committed
123
```
laibao's avatar
laibao committed
124
125

其中 `--num-prompts`是batch数,`--input-len`是输入seqlen,`--output-len`是输出token长度,`--model`为模型路径,`-tp`为使用卡数,`dtype="float16"`为推理数据类型,如果模型权重是bfloat16,需要修改为float16推理。若指定 `--output-len  1`即为首字延迟。`-q gptq`为使用gptq量化模型进行推理。
panhb's avatar
panhb committed
126
glm-4-9b-chat-1m模型默认的model_max_length为1024000,官方vllm也尚不支持该长度,模型启动时必须添加--max_model_len(包括后面的启动命令), 经测试,500000左右也可以正常进行推理。
zhuwenwen's avatar
zhuwenwen committed
127
128
129

2、使用数据集
下载数据集:
laibao's avatar
laibao committed
130

zhuwenwen's avatar
zhuwenwen committed
131
```bash
dcuai's avatar
dcuai committed
132
wget http://113.200.138.88:18080/aidatasets/vllm_data/-/raw/main/ShareGPT_V3_unfiltered_cleaned_split.json
zhuwenwen's avatar
zhuwenwen committed
133
134
135
```

```bash
zhuwenwen's avatar
zhuwenwen committed
136
python benchmarks/benchmark_throughput.py --num-prompts 1 --model THUDM/glm-4-9b-chat --dataset ShareGPT_V3_unfiltered_cleaned_split.json -tp 1 --trust-remote-code --enforce-eager --dtype float16
zhuwenwen's avatar
zhuwenwen committed
137
138
```

laibao's avatar
laibao committed
139
140
141
其中 `--num-prompts`是batch数,`--model`为模型路径,`--dataset`为使用的数据集,`-tp`为使用卡数,`dtype="float16"`为推理数据类型,如果模型权重是bfloat16,需要修改为float16推理。`-q gptq`为使用gptq量化模型进行推理。

### OpenAI api服务推理性能测试
zhuwenwen's avatar
zhuwenwen committed
142
143

1、启动服务端:
laibao's avatar
laibao committed
144

zhuwenwen's avatar
zhuwenwen committed
145
```bash
zhuwenwen's avatar
zhuwenwen committed
146
python -m vllm.entrypoints.openai.api_server  --model THUDM/glm-4-9b-chat  --dtype float16 --enforce-eager -tp 1 
zhuwenwen's avatar
zhuwenwen committed
147
148
149
```

2、启动客户端:
laibao's avatar
laibao committed
150

zhuwenwen's avatar
zhuwenwen committed
151
```bash
zhuwenwen's avatar
zhuwenwen committed
152
python benchmarks/benchmark_serving.py --model THUDM/glm-4-9b-chat --dataset ShareGPT_V3_unfiltered_cleaned_split.json  --num-prompts 1 --trust-remote-code
zhuwenwen's avatar
zhuwenwen committed
153
154
```

laibao's avatar
laibao committed
155
参数同使用数据集,离线批量推理性能测试,具体参考[benchmarks/benchmark_serving.py](benchmarks/benchmark_serving.py)
zhuwenwen's avatar
zhuwenwen committed
156
157

### OpenAI兼容服务
laibao's avatar
laibao committed
158

zhuwenwen's avatar
zhuwenwen committed
159
启动服务:
laibao's avatar
laibao committed
160

zhuwenwen's avatar
zhuwenwen committed
161
```bash
laibao's avatar
laibao committed
162
vllm serve THUDM/glm-4-9b-chat --enforce-eager --dtype float16 --trust-remote-code --chat-template template_chatglm2.jinja --port 8000
zhuwenwen's avatar
zhuwenwen committed
163
```
laibao's avatar
laibao committed
164
165

这里serve之后 为加载模型路径,`--dtype`为数据类型:float16,默认情况使用tokenizer中的预定义聊天模板,`--chat-template`可以添加新模板覆盖默认模板,`-q gptq`为使用gptq量化模型进行推理。
zhuwenwen's avatar
zhuwenwen committed
166
167

列出模型型号:
laibao's avatar
laibao committed
168

zhuwenwen's avatar
zhuwenwen committed
169
170
171
172
173
```bash
curl http://localhost:8000/v1/models
```

### OpenAI Completions API和vllm结合使用
laibao's avatar
laibao committed
174

zhuwenwen's avatar
zhuwenwen committed
175
176
177
178
179
180
181
182
183
184
185
```bash
curl http://localhost:8000/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "THUDM/glm-4-9b-chat",
        "prompt": "晚上睡不着怎么办",
        "max_tokens": 7,
        "temperature": 0
    }'
```

laibao's avatar
laibao committed
186
或者使用[examples/openai_completion_client.py](examples/openai_completion_client.py)
zhuwenwen's avatar
zhuwenwen committed
187
188

### OpenAI Chat API和vllm结合使用
laibao's avatar
laibao committed
189

zhuwenwen's avatar
zhuwenwen committed
190
191
192
193
194
195
196
197
198
199
200
```bash
curl http://localhost:8000/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "THUDM/glm-4-9b-chat",
        "messages": [
            {"role": "system", "content": "晚上睡不着怎么办"},
            {"role": "user", "content": "晚上睡不着怎么办"}
        ]
    }'
```
laibao's avatar
laibao committed
201

zhuwenwen's avatar
zhuwenwen committed
202
或者使用[examples/openai_chatcompletion_client.py](examples/openai_chatcompletion_client.py)
laibao's avatar
laibao committed
203

laibao's avatar
laibao committed
204
### **gradio和vllm结合使用**
zhuwenwen's avatar
zhuwenwen committed
205

laibao's avatar
laibao committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
1.安装gradio

```
pip install gradio
```

2.安装必要文件

    2.1 启动gradio服务,根据提示操作

```
python  gradio_openai_chatbot_webserver.py --model "THUDM/glm-4-9b-chat" --model-url http://localhost:8000/v1 --temp 0.8 --stop-token-ids ""
```

    2.2 更改文件权限

打开提示下载文件目录,输入以下命令给予权限

```
chmod +x frpc_linux_amd64_v0.*
```
laibao's avatar
laibao committed
227

laibao's avatar
laibao committed
228
229
230
231
    2.3端口映射

```
ssh -L 8000:计算节点IP:8000 -L 8001:计算节点IP:8001 用户名@登录节点 -p 登录节点端口
laibao's avatar
laibao committed
232
```
laibao's avatar
laibao committed
233
234
235
236

3.启动OpenAI兼容服务

```
laibao's avatar
laibao committed
237
vllm serve THUDM/glm-4-9b-chat  --enforce-eager --dtype float16 --trust-remote-code --chat-template template_chatglm2.jinja --port 8000
laibao's avatar
laibao committed
238
239
240
241
242
```

4.启动gradio服务

```
laibao's avatar
laibao committed
243
python  gradio_openai_chatbot_webserver.py --model "THUDM/glm-4-9b-chat" --model-url http://localhost:8000/v1 --temp 0.8 --stop-token-ids --host "0.0.0.0" --port 8001"
laibao's avatar
laibao committed
244
245
246
247
248
```

5.使用对话服务

在浏览器中输入本地 URL,可以使用 Gradio 提供的对话服务。
zhuwenwen's avatar
zhuwenwen committed
249
250

## result
laibao's avatar
laibao committed
251

zhuwenwen's avatar
zhuwenwen committed
252
使用的加速卡:1张 DCU-K100_AI-64G
laibao's avatar
laibao committed
253

zhuwenwen's avatar
zhuwenwen committed
254
255
256
257
258
```
Prompt: '晚上睡不着怎么办', Generated text: '?\n晚上睡不着可以尝试以下方法来改善睡眠质量:\n\n1. **调整作息时间**:尽量每天同一时间上床睡觉和起床,建立规律的生物钟。\n\n2. **放松身心**:睡前进行深呼吸、冥想或瑜伽等放松活动,有助于减轻压力和焦虑。\n\n3. **避免咖啡因和酒精**:晚上避免摄入咖啡因和酒精,因为它们可能会干扰睡眠。\n\n'
```

### 精度
laibao's avatar
laibao committed
259

zhuwenwen's avatar
zhuwenwen committed
260
261
262
263
264


## 应用场景

### 算法类别
laibao's avatar
laibao committed
265

zhuwenwen's avatar
zhuwenwen committed
266
267
268
对话问答

### 热点应用行业
laibao's avatar
laibao committed
269

zhuwenwen's avatar
zhuwenwen committed
270
271
272
医疗,金融,科研,教育

## 源码仓库及问题反馈
laibao's avatar
laibao committed
273

zhuwenwen's avatar
zhuwenwen committed
274
275
276
* [https://developer.hpccube.com/codes/modelzoo/llama_vllm](https://developer.hpccube.com/codes/modelzoo/chatglm_vllm)

## 参考资料
laibao's avatar
laibao committed
277

zhuwenwen's avatar
zhuwenwen committed
278
* [https://github.com/vllm-project/vllm](https://github.com/vllm-project/vllm)
laibao's avatar
laibao committed
279
* [https://github.com/THUDM/ChatGLM3](https://github.com/THUDM/ChatGLM3)