README.md 5.57 KB
Newer Older
lvzhen's avatar
lvzhen committed
1
# TeleChat
lvzhen's avatar
first  
lvzhen committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

## 论文

[TeleChat Technical Report](https://arxiv.org/abs/2401.03804)

## 模型结构

采用标准的 `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
45
46
### Dockerfile(方法二)
此处提供dockerfile的使用方法
```
docker build --no-cache -t telechat_pytorch:latest .
docker run xxx
lvzhen's avatar
first  
lvzhen committed
47

dcuai's avatar
dcuai committed
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
安装依赖:
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(方法三)
此处提供本地配置、编译的详细步骤,例如:

关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。
```
DTK驱动:dtk24.04
python:python3.10
```
`Tips:以上dtk驱动、python、paddle等DCU相关工具版本需要严格一一对应`

其它非深度学习库参照requirements.txt安装:
```
安装依赖:
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/
```
lvzhen's avatar
first  
lvzhen committed
74
75
76
77
## 模型下载链接

| **模型版本** | **下载链接**                                                 |
| ------------ | ------------------------------------------------------------ |
lvzhen's avatar
lvzhen committed
78
| 7B-FP16      | [TeleChat-7B](http://113.200.138.88:18080/aimodels/findsource-dependency/telechat7b) |
lvzhen's avatar
lvzhen committed
79
| 12B-V2-FP16  | [TeleChat-12B-V2-FP16](http://113.200.138.88:18080/aimodels/findsource-dependency/telechat) |
lvzhen's avatar
first  
lvzhen committed
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135



## 推理测试

### TeleChat-7B

进入Telechat/inference_telechat

```
bash infer.sh
```

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

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

进入Telechat/inference_telechat

```
bash infer.sh
```

## 微调训练

```
cd Telechat/ms-swift/examples/pytorch/llm
```

### 单机单卡LORA微调

```
bash sft_single_lora.sh
```

### 单机多卡LORA微调

```
bash sft_multi_lora.sh
```

### 单机多卡全参微调

```
bash sft_multi_full.sh
```

### 训练后的推理

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

结果如下:

lvzhen's avatar
lvzhen committed
136
![推理1](推理1.png)
lvzhen's avatar
first  
lvzhen committed
137
138
139
140

## 量化

### GPTQ量化
lvzhen's avatar
lvzhen committed
141
/usr/local/lib/python3.10/site-packages/optimum/gptq/quantizer.py 找到这个文件,676行,把safe_serialization=False
lvzhen's avatar
first  
lvzhen committed
142
143
144
145
146
147
148
149
150
151
152
153
```
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
```

### 量化后推理

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

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

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

## 源码仓库及问题反馈
https://developer.hpccube.com/codes/-/ide/project/modelzoo/telechat/tree/master/

## 参考资料
lvzhen's avatar
lvzhen committed
166
167
168
169

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


lvzhen's avatar
lvzhen committed
170
https://github.com/modelscope/ms-swift
lvzhen's avatar
lvzhen committed
171