README.md 7.39 KB
Newer Older
zhaoying1's avatar
zhaoying1 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
# Baichuan 2

## 论文

`Baichuan 2: Open Large-scale Language Models`

https://arxiv.org/abs/2309.10305

## 模型结构
Baichuan 2 是百川智能推出的新一代开源大语言模型,采用 2.6 万亿Tokens 的高质量语料训练。

模型具体参数:

| 模型名称 | 隐含层维度 | 层数 | 头数 | 词表大小 |  位置编码 | 最大长 |
| -------- | -------- | -------- | -------- |   -------- | -------- | -------- |
| Baichuan 2-7B | 4,096 | 32 | 32 | 125,696 |  RoPE | 4096 |
| Baichuan 2-13B | 5,120 | 40 | 	40 | 125,696 |   ALiBi | 4096 |
<div align="center">
<img src="./media/transformer.jpg" width="400" height="300">
</div>

## 算法原理
Baichuan整体模型基于标准的Transformer结构,采用了和LLaMA一样的模型设计。其中,Baichuan-7B在结构上采用Rotary Embedding位置编码方案、SwiGLU激活函数、基于RMSNorm的Pre-Normalization。Baichuan-13B使用了ALiBi线性偏置技术,相对于Rotary Embedding计算量更小,对推理性能有显著提升。
<div align="center">
<img src="./media/transformer.png" width="450" height="300">
</div>

## 环境配置

### Docker(方式一)
推荐使用docker方式运行,提供拉取的docker镜像:
```
yinger_z's avatar
yinger_z committed
33
34
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
docker run -dit --shm-size 80g --network=host --name=baichuan2 --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /opt/hyhal/:/opt/hyhal/:ro image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10 /bin/bash
zhaoying1's avatar
zhaoying1 committed
35
docker exec -it baichuan2 /bin/bash
zhaoying1's avatar
zhaoying1 committed
36
37
38
```
安装docker中没有的依赖:
```
zhaoying1's avatar
update  
zhaoying1 committed
39
pip install -r requirements.txt  -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
zhaoying1's avatar
zhaoying1 committed
40
41
42
43
44
45

```

### Dockerfile(方式二)
```
docker build -t baichuan2:latest .
yinger_z's avatar
yinger_z committed
46
docker run -dit --shm-size 80g --network=host --name=baichuan2 --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /opt/hyhal/:/opt/hyhal/:ro baichuan2:latest /bin/bash
zhaoying1's avatar
zhaoying1 committed
47
48
49
50
51
52
docker exec -it baichuan2 /bin/bash
```

### Conda(方式三)
1. 创建conda虚拟环境:
```
yinger_z's avatar
yinger_z committed
53
conda create -n baichuan2 python=3.10
zhaoying1's avatar
zhaoying1 committed
54
55
```

chenzk's avatar
chenzk committed
56
2. 关于本项目DCU显卡所需的工具包、深度学习库等均可从[光合](https://developer.sourcefind.cn/tool/)开发者社区下载安装。
yinger_z's avatar
yinger_z committed
57
58
59
- [DTK 24.04](https://cancon.hpccube.com:65024/1/main/DTK-24.04.1)
- [Pytorch 2.1.0](https://cancon.hpccube.com:65024/4/main/pytorch/DAS1.1.1)
- [Deepspeed 0.12.3](https://cancon.hpccube.com:65024/4/main/deepspeed/DAS1.1)
zhaoying1's avatar
zhaoying1 committed
60
61
62
63
64

    Tips:以上dtk驱动、python、deepspeed等工具版本需要严格一一对应。

3. 其它依赖库参照requirements.txt安装:
```
zhaoying1's avatar
update  
zhaoying1 committed
65
66
67
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
```

zhaoying1's avatar
zhaoying1 committed
68

zhaoying1's avatar
zhaoying1 committed
69

yinger_z's avatar
yinger_z committed
70
71
### 注意3
若不支持xformers,在训练中可能会出现xformers相关报错:"ImportError: This modeling file reguires the following packages that were not found in your environment: xformers." ,您可通过直接将[modeling_baichuan.py](./modeling_baichuan.py)中xpos设置为None来解决,即注释import xformers相关代码,并设置`xops=None`
zhaoying1's avatar
zhaoying1 committed
72

zhaoying1's avatar
zhaoying1 committed
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
## 数据集

输入数据为放置在项目[fine-tune/data](./fine-tune/data)目录下的 json 文件,`fine-tune/data/belle_chat_ramdon_10k.json`,该样例数据是从 [multiturn_chat_0.8M](https://huggingface.co/datasets/BelleGroup/multiturn_chat_0.8M) 采样出 1 万条,并且做了格式转换。主要是展示多轮数据怎么训练,不保证效果。json 文件示例格式如下:
```
[
 {
  "id": "77771",
  "conversations": [
   {
    "from": "human",
    "value": "写一篇关于气候变化对海洋生态的影响的文章。\n"
   },
   {
    "from": "gpt",
    "value": "好的,以下是你要求的文章:\n气候变化对海洋生态的影响\n气候变化对海洋生态产生了广泛的影响。全球变暖导致海洋温度上升,这可能会对许多水生生物的生存、繁殖和迁移造成巨大的影响。另外,海洋酸化也是一个问题,因为大气中二氧化碳的增加导致海水中的pH值下降,使得许多生物的外骨骼变得脆弱,从而影响它们的生存和繁殖。\n此外,气候变化还会引起海平面上升,这不仅对海岸线和沿海居民产生影响,还会对许多海洋生物的栖息地造成影响。一些岛屿上的陆地生物面临几乎完全消失的风险,而另一些生物范围则会扩大到更广泛的领域,这可能会导致新的竞争和生态平衡的改变。\n总之,气候变化并不只是一个地球气温的问题,它对海洋生态系统产生了广泛和深远的影响。\n"
   },
   {
    "from": "human",
    "value": "从刚刚的文章中提取出气候变化对海洋生物繁殖的影响。\n"
   },
   {
    "from": "gpt",
    "value": "文章已经提到过气候变化会对海洋生物的生存、繁殖和迁移造成影响。例如,海洋酸化会使得许多生物的外骨骼变得脆弱,进而影响它们的生存和繁殖。此外,海洋温度上升也可能会对很多生物的生存和繁殖产生负面影响。"
   }
  ]
 }
    ....
]
```


zhaoying1's avatar
zhaoying1 committed
104
### 模型下载
zhaoying1's avatar
zhaoying1 committed
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
136
Hugging Face模型下载地址:

[Baichuan2-7B-Base](https://huggingface.co/baichuan-inc/Baichuan2-7B-Base)

[Baichuan2-7B-Chat](https://huggingface.co/baichuan-inc/Baichuan2-7B-Chat)

[Baichuan2-13B-Chat](https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat)

[Baichuan2-13B-Base](https://huggingface.co/baichuan-inc/Baichuan2-13B-Base)

## 训练
### 全参数微调训练

1. 单机训练
```
cd fine-tune
bash ft_train.sh
```

2. 多机训练
```
cd fine-tune/multi_node
``` 
进入节点1,根据环境修改hostfile,保证两节点文件路径一致,配置相同,按需修改run-13b-sft.sh中--mca btl_tcp_if_include enp97s0f1,enp97s0f1改为ip a命令后对应节点ip的网卡名,numa可以根据当前节点拓扑更改绑定,微调命令:
``` 
bash run_ft.sh
``` 
### LoRA微调训练

1. 单机训练
```
cd fine-tune
zhaoying1's avatar
zhaoying1 committed
137
bash lora_train.sh
zhaoying1's avatar
zhaoying1 committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
```


2. 多机训练
```
cd fine-tune/multi_node
``` 
进入节点1,根据环境修改hostfile,保证两节点文件路径一致,配置相同,按需修改run-13b-sft.sh中--mca btl_tcp_if_include enp97s0f1,enp97s0f1改为ip a命令后对应节点ip的网卡名,numa可以根据当前节点拓扑更改绑定,微调命令:
``` 
bash run_lora.sh
``` 



## 推理

### 命令行测试

```bash
python cli_demo.py
```
请根据情况修改其中的模型加载路径。

zhaoying1's avatar
zhaoying1 committed
161
162
注:以上脚本用于全参数模型推理,不适用于直接加载lora微调后的模型。

zhaoying1's avatar
zhaoying1 committed
163
164
165
166
167
168
169
170


## Result
- 以下为我们基于baichuan2-7b-base模型进行全参数指令微调实验后的推理效果:
<div align="center">
<img src="./media/baichuan2-test.png" width="500" height="230">
</div>

dcuai's avatar
dcuai committed
171
### 精度
zhaoying1's avatar
zhaoying1 committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
- 以下为我们基于baichuan2-7b-base模型进行全参数指令微调实验的loss收敛情况:
<div align="center">
<img src="./media/baichuan2_7bbase_ft_96c_bs1_acum1_fp16_lr2e-5.jpg" width="300" height="250">
</div>



## 应用场景

### 算法类别

`对话问答`

### 热点应用行业

`医疗,教育,科研,金融`

chenzk's avatar
chenzk committed
189
190
191
## 预训练权重


zhaoying1's avatar
zhaoying1 committed
192
193
## 源码仓库及问题反馈

chenzk's avatar
chenzk committed
194
- https://developer.sourcefind.cn/codes/modelzoo/baichuan2_pytorch
zhaoying1's avatar
zhaoying1 committed
195

dcuai's avatar
dcuai committed
196
## 参考资料
zhaoying1's avatar
zhaoying1 committed
197

zhaoying1's avatar
zhaoying1 committed
198
- [https://github.com/baichuan-inc/Baichuan2/tree/main](https://github.com/baichuan-inc/Baichuan2/tree/main)