README.md 5.77 KB
Newer Older
lvzhen's avatar
lvzhen committed
1
# TeleChat
lvzhen's avatar
first  
lvzhen committed
2
3
4

## 论文

dcuai's avatar
dcuai committed
5
https://arxiv.org/abs/2401.03804
lvzhen's avatar
first  
lvzhen committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

## 模型结构

采用标准的 `Decoder-only` 结构设计了 **TeleChat** 模型,并在模型维度做了如下的一些改进:

- **位置编码**:使用 [Rotary Embedding](https://arxiv.org/pdf/2104.09864.pdf) 的位置编码方法,该方法将相对位置信息依赖集成到 self-attention 中,并且具有较好的位置外推性。Rotary Embedding还可以较好地与Flash-Attention v2 配合使用,将模型的训练速度提升约20%。
- **激活函数**:使用 [SwiGLU](https://arxiv.org/pdf/2002.05202.pdf) 激活函数来替代GELU激活函数 , 为了减少计算量,将`ffn_hidden_size`设置为小于原始SwiGLU中的4倍隐藏层大小。
- **层标准化**: 基于 [RMSNorm](https://arxiv.org/abs/1910.07467) 的 Pre-Normalization。
- **词嵌入层与输出层解耦**:将**TeleChat-12B**的词嵌入层和输出lm head层参数分开,有助于增强训练稳定性和收敛性。

|      | layer_num | hidden_size | ffn_hidden_size | head_num | tie_word_embeddings |
| ---- | --------- | ----------- | --------------- | -------- | ------------------- |
| 1B   | 16        | 2048        | 5460            | 32       | 否                  |
| 7B   | 30        | 4096        | 12288           | 32       | 是                  |
| 12B  | 38        | 5120        | 12288           | 32       | 否                  |

dcuai's avatar
dcuai committed
22
23
## 算法原理
TeleChat 是一个基于 Transformer 架构的语义大模型,拥有千亿级别的参数量。它采用了先进的预训练技术,通过对大量高质量文本数据的学习,使得模型具备了强大的语义理解和生成能力。在模型结构上,TeleChat 借鉴了GPT等生成式预训练模型的成功经验,同时结合了中国电信的实际需求进行了优化,从而实现了更高的性能和更广泛的应用场景。
lvzhen's avatar
first  
lvzhen committed
24
25
## 环境配置

dcuai's avatar
dcuai committed
26
### Docker(方法一)
lvzhen's avatar
first  
lvzhen committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

```
拉取镜像:
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10

创建并启动容器:
docker run --shm-size 80g --network=host --name=telechat --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined  -v /opt/hyhal:/opt/hyhal:ro -v <Host Path>:<Container Path> -it <Your Image ID> bash

安装依赖:
cd TeleChat
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install 'ms-swift[llm]' -U -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install optimum -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install auto-gptq -i https://pypi.mirrors.ustc.edu.cn/simple/
```
dcuai's avatar
dcuai committed
42
43
44
### Dockerfile(方法二)
此处提供dockerfile的使用方法
```
dcuai's avatar
dcuai committed
45
cd TeleChat/docker
dcuai's avatar
dcuai committed
46
docker build --no-cache -t telechat_pytorch:latest .
dcuai's avatar
dcuai committed
47
docker run --shm-size 80g --network=host --name=telechat --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined  -v /opt/hyhal:/opt/hyhal:ro -v <Host Path>:<Container Path> -it telechat_pytorch:latest bash
lvzhen's avatar
first  
lvzhen committed
48

dcuai's avatar
dcuai committed
49
50
51
52
53
54
55
56
安装依赖:
cd TeleChat
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install 'ms-swift[llm]' -U -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install optimum -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install auto-gptq -i https://pypi.mirrors.ustc.edu.cn/simple/
```
### Anaconda(方法三)
dcuai's avatar
dcuai committed
57
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装:
chenzk's avatar
chenzk committed
58
- https://developer.sourcefind.cn/tool/
dcuai's avatar
dcuai committed
59
```
dcuai's avatar
dcuai committed
60
61
62
63
DTK驱动:dtk24.04.1
python:python3.10
torch:2.1.0
torchvision:0.16.0
dcuai's avatar
dcuai committed
64
65
```

dcuai's avatar
dcuai committed
66
67
68
`Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应。`

2、其它非深度学习库参照requirements.txt安装:
dcuai's avatar
dcuai committed
69
70
71
72
73
74
75
76
```
安装依赖:
cd TeleChat
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install 'ms-swift[llm]' -U -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install optimum -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install auto-gptq -i https://pypi.mirrors.ustc.edu.cn/simple/
```
dcuai's avatar
dcuai committed
77
78
79

## 数据集

lvzhen's avatar
first  
lvzhen committed
80
81
82
83
## 模型下载链接

| **模型版本** | **下载链接**                                                 |
| ------------ | ------------------------------------------------------------ |
chenzk's avatar
chenzk committed
84
85
| 7B-FP16      | [TeleChat-7B](https://huggingface.co/Tele-AI/telechat-7B) |
| 12B-V2-FP16  | [TeleChat-12B-V2-FP16](https://huggingface.co/Tele-AI/TeleChat-12B-v2) |
lvzhen's avatar
first  
lvzhen committed
86
87


dcuai's avatar
dcuai committed
88
## 推理
lvzhen's avatar
first  
lvzhen committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

### TeleChat-7B
进入Telechat/inference_telechat

```
bash infer.sh
```

### TeleChat-12B/TeleChat-12B-V2

修改模型文件路径里面的config.json,把flash_attn改为false

进入Telechat/inference_telechat

```
bash infer.sh
```

dcuai's avatar
dcuai committed
107
## 训练
lvzhen's avatar
first  
lvzhen committed
108
109
110
```
cd Telechat/ms-swift/examples/pytorch/llm
```
dcuai's avatar
dcuai committed
111
### 单机单卡LORA微调训练
lvzhen's avatar
first  
lvzhen committed
112
113
114
115
116

```
bash sft_single_lora.sh
```

dcuai's avatar
dcuai committed
117
### 单机多卡LORA微调训练
lvzhen's avatar
first  
lvzhen committed
118
119
120
121
122

```
bash sft_multi_lora.sh
```

dcuai's avatar
dcuai committed
123
### 单机多卡全参微调训练
lvzhen's avatar
first  
lvzhen committed
124
125
126
127
128
129
130
131
132
133
134
135

```
bash sft_multi_full.sh
```

### 训练后的推理

```
与微调训练同文件夹下
bash infer.sh
```

dcuai's avatar
dcuai committed
136
## result
dcuai's avatar
dcuai committed
137
![推理1](推理1.png)
dcuai's avatar
dcuai committed
138
### 精度
dcuai's avatar
dcuai committed
139

lvzhen's avatar
first  
lvzhen committed
140
141


dcuai's avatar
dcuai committed
142
## 量化
lvzhen's avatar
first  
lvzhen committed
143
### GPTQ量化
lvzhen's avatar
lvzhen committed
144
/usr/local/lib/python3.10/site-packages/optimum/gptq/quantizer.py 找到这个文件,676行,把safe_serialization=False
lvzhen's avatar
first  
lvzhen committed
145
146
147
148
149
```
HIP_VISIBLE_DEVICES=0 swift export --model_type telechat-7b \
    --quant_bits 8 --quant_method gptq --model_id_or_path /path/to/telechat-7b \
    --quant_output_dir ./quant_out
```
dcuai's avatar
dcuai committed
150

lvzhen's avatar
first  
lvzhen committed
151
152
153
154
155
156
157

### 量化后推理

```
HSA_FORCE_FINE_GRAIN_PCIE=1 swift infer --model_type telechat-7b --model_id_or_path quant_out/
```

lvzhen's avatar
lvzhen committed
158
![推理2](推理2.png)
lvzhen's avatar
lvzhen committed
159
160
161
162
163
164
165
166

## 应用场景
### 算法类别
LLM
### 热点应用行业
运营商、科研、教育

## 源码仓库及问题反馈
dcuai's avatar
dcuai committed
167
https://developer.sourcefind.cn/codes/modelzoo/Telechat_pytorch/-/tree/master
lvzhen's avatar
lvzhen committed
168
169

## 参考资料
lvzhen's avatar
lvzhen committed
170
171
172
173

https://github.com/Tele-AI/Telechat/tree/master 


lvzhen's avatar
lvzhen committed
174
https://github.com/modelscope/ms-swift
lvzhen's avatar
lvzhen committed
175