"vscode:/vscode.git/clone" did not exist on "be53add4d11011b4316e9a0fbe3aa42a5ce33119"
README.md 4.76 KB
Newer Older
1
# Baichuan-13B
2

zhouxiang's avatar
zhouxiang committed
3
4
5
6
7
8
## 论文

`Baichuan 2: Open Large-scale Language Models`

https://arxiv.org/abs/2309.10305

9
10
## 模型结构

zhouxiang's avatar
zhouxiang committed
11
12
13
Baichuan-13B是由百川智能继Baichuan-7B之后开发的包含130亿参数模型,它在高质量的语料上训练了1.4万亿tokens,超过LLaMA-13B 40%。

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

zhouxiang's avatar
zhouxiang committed
15
16
![baichuan](doc/transformer.jpg)

17
18
模型具体参数:

zhouxiang's avatar
zhouxiang committed
19
20
21
22
| 模型名称 | 隐含层维度 | 层数 | 头数 | 词表大小 | 位置编码 | 最大序列长度 |
| -------- | -------- | -------- | -------- | -------- | -------- | -------- |
| Baichuan-13B | 5,120 | 40 | 	40 | 64000 | ALiBi | 4096 |
| Baichuan2-13B | 5,120 | 40 | 40 | 125696 | ALiBi | 4096 |
23
24

## 算法原理
zhouxiang's avatar
zhouxiang committed
25
26
27
Baichuan整体模型基于标准的Transformer结构,采用了和LLaMA一样的模型设计。其中,Baichuan-7B在结构上采用Rotary Embedding位置编码方案、SwiGLU激活函数、基于RMSNorm的Pre-Normalization。Baichuan-13B使用了ALiBi线性偏置技术,相对于Rotary Embedding计算量更小,对推理性能有显著提升.

![baichuan](doc/transformer.png)
28
29
30
31

## 环境配置

### 环境准备
zhouxiang's avatar
zhouxiang committed
32

33
在光源可拉取推理的docker镜像,拉取方式如下:
zhouxiang's avatar
zhouxiang committed
34

35
```
36
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk23.10.1-py38
37
```
zhouxiang's avatar
zhouxiang committed
38

39
40
41
42
43
44
45
### 容器启动

模型推理容器启动命令参考如下,用户根据需要修改:

```
# <container_name> 自定义容器名
# <project_path> 当前工程所在路径
zhouxiang's avatar
zhouxiang committed
46
docker run -it --name=<container_name> -v <project_path>:/work -w /work --privileged -v /opt/hyhal:/opt/hyhal --device=/dev/kfd --device=/dev/dri --security-opt seccomp=unconfined --cap-add=SYS_PTRACE --ipc=host --network host --shm-size=16G --group-add video image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk23.10.1-py38 /bin/bash
47
48
49
50
51
52
53
```

### 加载环境

进入容器后执行如下命令,加载运行环境变量

```
zhouxiang's avatar
zhouxiang committed
54
source /opt/dtk/cuda/env.sh
55
56
57
58
59
60
61
```

### 安装方法

```
#进入本工程目录
cd package
zhouxiang's avatar
zhouxiang committed
62
python setup.py install
63
64
```

zhouxiang's avatar
zhouxiang committed
65
66
67
68
69
70
71
72
73
74
75
76
## 数据集



## 推理

### 原版模型下载

[baichuan-inc/Baichuan-13B-Chat · Hugging Face](https://huggingface.co/baichuan-inc/Baichuan-13B-Chat)

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

chenzk's avatar
chenzk committed
77
78


79
80
81
82
83
84
### 模型转换

```
# 为了精简镜像,光源镜像中未包含模型推理时不需要的原版Baichuan-13B-chat模型运行所需要的依赖,
# 如果有现成的原版Baichuan-13B-chat的运行环境中,可以将模型转换脚本baichuan2flm.py移动到原版模型的运行环境中,
# 也可以通过执行pip install -r requirements.txt安装模型转换所需依赖;
zhouxiang's avatar
zhouxiang committed
85
# 对于已经下载完成的模型或者自己finetune的模型需要修改baichuan2flm.py文件中创建tokenizer, model时的模型存放路径
zhouxiang's avatar
zhouxiang committed
86
# 在本工程目录下执行:
87
python3 baichuan2flm.py baichuan-13b-fp16.bin float16 # 导出fp16模型,参数为导出的模型路径
zhouxiang's avatar
zhouxiang committed
88
89
90

# 如果使用的dcu显存为16G,则需要用int8精度模型:
python3 baichuan2flm.py baichuan-13b-int8.bin int8 # 导出fp16模型,参数为导出的模型路径
91
92
93
```


zhouxiang's avatar
zhouxiang committed
94
### 模型推理
95
96
97
98
99

```
# 命令行聊天程序,使用了模型创建以及流式对话效果
python cli_demo.py -p baichuan-13b-fp16.bin

zhouxiang's avatar
zhouxiang committed
100
# 简易webui,需要先安装streamlit-chat,并且需要在容器启动时映射streamlit的端口到外部网络
101
streamlit run web_demo.py baichuan-13b-fp16.bin 
zhouxiang's avatar
zhouxiang committed
102
103
104
105
106
107
108
109
110

# 按照openai接口实现的api_server的实例:
# 需要先进入api_server_demo,安装所需依赖:
cd api_server_demo
pip install -r requirements.txt
# 运行api_server服务,使用-p指定转换后的模型文件,客户端代码可以参考openai-client.py实现:
python fastllm-openai.py -p ../baichuan-13b-fp16.bin 
# 如果需要测试服务的并发性能,可以使用openai-client.py,修改其中的prompt和concurrencys变量值后执行:
python openai-client.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
```

### 推理性能测试

可以使用benchmark程序进行测速,根据./benchmark -h描述进行配置和测试,不同配置、不同输入,推理速度也会有一些差别

```
# 进入benchmark所在目录
cd benchmark

# 添加benchmark可执行权限
chmod +x benchmark

# 测试示例
zhouxiang's avatar
zhouxiang committed
125
126
./benchmark -p ../baichuan-13b-fp16.bin -f prompts/beijing.txt -b 1
./benchmark -p ../baichuan-13b-fp16.bin -f prompts/beijing.txt -b 16
127
128
```

zhouxiang's avatar
zhouxiang committed
129
## result
130

zhouxiang's avatar
zhouxiang committed
131
![baochuan推理](doc/baichuan-13b.gif)
132

zhouxiang's avatar
zhouxiang committed
133
134
135
136
### 精度



137
138
139
140
## 应用场景

### 算法类别

zhouxiang's avatar
zhouxiang committed
141
`对话问答`
142
143
144

### 热点应用行业

zhouxiang's avatar
zhouxiang committed
145
`医疗,科研,金融,教育`
146

chenzk's avatar
chenzk committed
147
## 预训练权重
chenzk's avatar
chenzk committed
148

chenzk's avatar
chenzk committed
149

150
151
## 源码仓库及问题反馈

chenzk's avatar
chenzk committed
152
- https://developer.sourcefind.cn/codes/modelzoo/baichuan-13b_fastllm
153

zhouxiang's avatar
zhouxiang committed
154
## 参考资料
155

zhouxiang's avatar
zhouxiang committed
156
157
158
- https://github.com/baichuan-inc/Baichuan-13B
- https://github.com/baichuan-inc/Baichuan2