"vscode:/vscode.git/clone" did not exist on "b6f41a17060b662c015dac1d9fb9334ea3895e0e"
README.md 4.4 KB
Newer Older
lvzhen's avatar
lvzhen committed
1
# TeleChat
lvzhen's avatar
first  
lvzhen committed
2
3
4
5
6

## 论文

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

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

lvzhen's avatar
first  
lvzhen committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
## 模型结构

采用标准的 `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       | 否                  |

## 环境配置

### Docker方法

```
拉取镜像:
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/
```

## 模型下载链接

| **模型版本** | **下载链接**                                                 |
| ------------ | ------------------------------------------------------------ |
lvzhen's avatar
lvzhen committed
48
| 7B-FP16      | [TeleChat-7B](http://113.200.138.88:18080/aimodels/findsource-dependency/telechat7b) |
lvzhen's avatar
lvzhen committed
49
| 12B-V2-FP16  | [TeleChat-12B-V2-FP16](http://113.200.138.88:18080/aimodels/findsource-dependency/telechat) |
lvzhen's avatar
first  
lvzhen committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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



## 推理测试

### 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
106
![推理1](推理1.png)
lvzhen's avatar
first  
lvzhen committed
107
108
109
110

## 量化

### GPTQ量化
lvzhen's avatar
lvzhen committed
111
/usr/local/lib/python3.10/site-packages/optimum/gptq/quantizer.py 找到这个文件,676行,把safe_serialization=False
lvzhen's avatar
first  
lvzhen committed
112
113
114
115
116
117
118
119
120
121
122
123
```
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
124
![推理2](推理2.png)
lvzhen's avatar
lvzhen committed
125
126
127
128
129
130
131
132
133
134
135

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

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

## 参考资料
lvzhen's avatar
lvzhen committed
136
137
138
139

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


lvzhen's avatar
lvzhen committed
140
https://github.com/modelscope/ms-swift
lvzhen's avatar
lvzhen committed
141