README.md 4.31 KB
Newer Older
lvzhen's avatar
first  
lvzhen committed
1
2
3
4
5
6
7
8
9
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
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
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
106
107
108
# 星辰语义大模型-TeleChat

## 论文

[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       | 否                  |

## 环境配置

### 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/
```

## 模型下载链接

| **模型版本** | **下载链接**                                                 |
| ------------ | ------------------------------------------------------------ |
| 1B-FP16      | [TeleChat-1B-FP16](https://huggingface.co/Tele-AI/Telechat-1B) |
| 7B-FP16      | [TeleChat-7B-FP16](https://huggingface.co/Tele-AI/Telechat-7B) |
| 7B-int8      | [TeleChat-7B-int8](https://huggingface.co/Tele-AI/Telechat-7B-int8) |
| 7B-int4      | [TeleChat-7B-int4](https://huggingface.co/Tele-AI/Telechat-7B-int4) |
| 12B-FP16     | [TeleChat-12B-FP16](https://huggingface.co/Tele-AI/TeleChat-12B) |
| 12B-int8     | [TeleChat-12B-int8](https://huggingface.co/Tele-AI/TeleChat-12B-int8) |
| 12B-int4     | [TeleChat-12B-int4](https://huggingface.co/Tele-AI/TeleChat-12B-int4) |
| 12B-V2-FP16  | [TeleChat-12B-V2-FP16](https://modelscope.cn/models/TeleAI/TeleChat-12B-v2/files) |



## 推理测试

### 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
109
![推理1](推理1.png)
lvzhen's avatar
first  
lvzhen committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

## 量化

### GPTQ量化

```
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
127
![推理2](推理2.png)
lvzhen's avatar
lvzhen committed
128
129
130
131
132
133
134
135
136
137
138
139
140

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

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

## 参考资料
https://github.com/Tele-AI/Telechat/tree/master
https://github.com/modelscope/ms-swift