README.md 7.94 KB
Newer Older
Rayyyyy's avatar
Rayyyyy committed
1
2
3
4
5
# llama3
## 论文
[llama3](https://llama.meta.com/llama3/)

## 模型结构
Rayyyyy's avatar
Rayyyyy committed
6
7
8
9
10
Llama-3中选择了一个相对标准的decoder-only的transformer架构。与Llama-2相比,做了几个关键的改进:
- 基于超过15T token训练数据,大小相当于Llama 2数据集的7倍还多,增强了推理、代码生成和指令跟随等方面的能力;
- 支持8K长文本(之前是4k),改进的tokenizer具有128K tokens的词汇量,可以更有效地对语言进行编码,从而大大提高了模型的性能;
- 采用分组查询注意力(grouped query attention,GQA)、掩码等技术,帮助开发者以最低的能耗获取绝佳的性能。
- 在8,192个tokens的序列上训练模型,使用掩码来确保self-attention不会跨越文档边界。
Rayyyyy's avatar
Rayyyyy committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

## 算法原理

<div align=center>
    <img src="./doc/method.png"/>
</div>

## 环境配置
-v 路径、docker_name和imageID根据实际情况修改

### Docker(方法一)

```bash
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk23.10.1-py38
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=32G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash

cd /your_code_path/llama3_pytorch
pip install -e .
Rayyyyy's avatar
Rayyyyy committed
29
30
pip install -U xtuner
pip install bitsandbytes-0.43.0-py3-none-any.whl
Rayyyyy's avatar
Rayyyyy committed
31
32
33
34
35
```

### Dockerfile(方法二)

```bash
Rayyyyy's avatar
Rayyyyy committed
36
cd docker
Rayyyyy's avatar
Rayyyyy committed
37
38
39
40
41
docker build --no-cache -t llama3:latest .
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=32G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash

cd /your_code_path/llama3_pytorch
pip install -e .
Rayyyyy's avatar
Rayyyyy committed
42
43
pip install -U xtuner
pip install bitsandbytes-0.43.0-py3-none-any.whl
Rayyyyy's avatar
Rayyyyy committed
44
45
46
47
48
```

### Anaconda(方法三)
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。
```bash
Rayyyyy's avatar
Rayyyyy committed
49
50
51
DTK驱动: dtk23.10.1
python: python3.8
torch: 2.1.0
Rayyyyy's avatar
Rayyyyy committed
52
```
Rayyyyy's avatar
Rayyyyy committed
53
`Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应`
Rayyyyy's avatar
Rayyyyy committed
54
55
56
57

其它非深度学习库安装方式如下:
```bash
pip install -e .
Rayyyyy's avatar
Rayyyyy committed
58
59
pip install -U xtuner
pip install bitsandbytes-0.43.0-py3-none-any.whl
Rayyyyy's avatar
Rayyyyy committed
60
61
62
63
64
65
```

## 数据集
官方暂无

## 训练
Rayyyyy's avatar
Rayyyyy committed
66
67
68
69
70
71
72
73
### xtuner微调方法
1. 修改[llama3_8b_instruct_qlora_alpaca_e3_M.py](./llama3_8b_instruct_qlora_alpaca_e3_M.py)代码中的`pretrained_model_name_or_path``data_path`为本地对应数据地址;
2. 根据硬件环境和自身训练需求来调整 `max_length``batch_size``accumulative_counts``max_epochs``lr``save_steps``evaluation_freq`、model.lora中的`r``lora_alpha`参数;
3. ${DCU_NUM}参数修改为要使用的DCU卡数量;
4. 执行
```bash
NPROC_PER_NODE=${DCU_NUM} xtuner train ./llama3_8b_instruct_qlora_alpaca_e3_M.py --deepspeed deepspeed_zero2
```
Rayyyyy's avatar
Rayyyyy committed
74
75

## 推理
Rayyyyy's avatar
Rayyyyy committed
76
预训练模型下载方法请参考下面的[预训练权重](#预训练权重)章节,不同的模型需要不同的模型并行(MP)值,如下表所示:
Rayyyyy's avatar
Rayyyyy committed
77

Rayyyyy's avatar
Rayyyyy committed
78
79
80
|  Model | MP |
|--------|----|
| 8B     | 1  |
Rayyyyy's avatar
Rayyyyy committed
81
| 70B    | 8  |
Rayyyyy's avatar
Rayyyyy committed
82
83
84
85

所有模型都支持序列长度高达8192个tokens,但我们根据max_seq_len和max_batch_size值预先分配缓存。根据你的硬件设置。

**Tips:**
Rayyyyy's avatar
Rayyyyy committed
86
- `–nproc_per_node`需要根据模型的MP值进行设置(参考上表)。
Rayyyyy's avatar
Rayyyyy committed
87
88
89
- `max_seq_len``max_batch_size`参数按需设置。

### Pretrained模型
Rayyyyy's avatar
Rayyyyy committed
90
这些模型都没有针对聊天或者Q&A进行微调。可以参考`example_text_completion.py`里的用例。
Rayyyyy's avatar
Rayyyyy committed
91

Rayyyyy's avatar
Rayyyyy committed
92
- Meta-Llama-3-8B 模型示例,Meta-Llama-3-70B模型仅需替换--ckpt_dir、--tokenizer_path对应模型地址即可。
Rayyyyy's avatar
Rayyyyy committed
93
94
95
96
97
98
99
```bash
torchrun --nproc_per_node 1 example_text_completion.py \
    --ckpt_dir Meta-Llama-3-8B/original/ \
    --tokenizer_path Meta-Llama-3-8B/original/tokenizer.model \
    --max_seq_len 128 --max_batch_size 4
```

Rayyyyy's avatar
Rayyyyy committed
100
### Instruction-tuned模型
Rayyyyy's avatar
Rayyyyy committed
101
102
103
104
105
经过微调的模型被训练用于对话应用程序。为了获得模型的预期特性和性能,需要遵循 [`ChatFormat`](llama/tokenizer.py#L202)中定义的特定格式:
- 提示以特殊令牌 <|begin_of_text|> 开始,之后跟随一个或多个消息。
- 每条消息以标签`<|start_header_id|>`开始,角色为`system``user`或者`assistant`、并以标签 `<|end_header_id|>`  结束。
- 在双换行符`\n\n`之后,消息的内容随之而来。
- 每条消息的结尾由`<|eot_id|>`令牌标记。
Rayyyyy's avatar
Rayyyyy committed
106
107
108

您还可以部署额外的分类器来过滤被认为不安全的输入和输出。有关如何向推理代码的输入和输出添加安全检查器,请参阅[llama-recipes repo](https://github.com/meta-llama/llama-recipes/blob/main/recipes/inference/local_inference/inference.py)

Rayyyyy's avatar
Rayyyyy committed
109
- Meta-Llama-3-8B-Instruct 模型示例,Meta-Llama-3-70B-Instruct模型仅需替换--ckpt_dir、--tokenizer_path对应模型地址即可。
Rayyyyy's avatar
Rayyyyy committed
110
111
112
113
114
115
116
```bash
torchrun --nproc_per_node 1 example_chat_completion.py \
    --ckpt_dir Meta-Llama-3-8B-Instruct/original/ \
    --tokenizer_path Meta-Llama-3-8B-Instruct/original/tokenizer.model \
    --max_seq_len 512 --max_batch_size 6
```

Rayyyyy's avatar
Rayyyyy committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
## result
- Meta-Llama-3-8B-Instruct
<div align=center>
    <img src="./doc/Meta-Llama-3-8B-Instruct.png"/>
</div>

- Meta-Llama-3-8B
<div align=center>
    <img src="./doc/Meta-Llama-3-8B.png"/>
</div>

### 精度
暂无


## 应用场景
### 算法类别
对话问答

### 热点应用行业
制造,广媒,家居,教育

## 预训练权重
1. 环境安装
```bash
pip install -U huggingface_hub hf_transfer
export HF_ENDPOINT=https://hf-mirror.com
```

2. 预训练模型下载,**token**参数通过huggingface账号获取
Rayyyyy's avatar
Rayyyyy committed
147
148
149
150
151
152
153

- Meta-Llama-3-8B 模型
```bash
mkdir Meta-Llama-3-8B
huggingface-cli download meta-llama/Meta-Llama-3-8B --include "original/*" --local-dir Meta-Llama-3-8B --token hf_*
```

Rayyyyy's avatar
Rayyyyy committed
154
155
156
- Meta-Llama-3-8B-Instruct 模型
```bash
mkdir Meta-Llama-3-8B-Instruct
Rayyyyy's avatar
Rayyyyy committed
157
huggingface-cli download meta-llama/Meta-Llama-3-8B-Instruct --include "original/*" --local-dir Meta-Llama-3-8B-Instruct --token hf_*
Rayyyyy's avatar
Rayyyyy committed
158
```
Rayyyyy's avatar
Rayyyyy committed
159

Rayyyyy's avatar
Rayyyyy committed
160
161
162
163
164
165
166
167
168
169
170
171
- Meta-Llama-3-70B 模型
```bash
mkdir Meta-Llama-3-70B
huggingface-cli download meta-llama/Meta-Llama-3-70B --include "original/*" --local-dir Meta-Llama-3-70B --token hf_*
```

- Meta-Llama-3-70B-Instruct 模型
```bash
mkdir Meta-Llama-3-70B-Instruct
huggingface-cli download meta-llama/Meta-Llama-3-70B-Instruct --include "original/*" --local-dir Meta-Llama-3-70B-Instruct --token hf_*
```

Rayyyyy's avatar
Rayyyyy committed
172
173
174
模型目录结构如下:
```bash
├── llama3_pytorch
Rayyyyy's avatar
Rayyyyy committed
175
│   ├── Meta-Llama-3-8B
Rayyyyy's avatar
Rayyyyy committed
176
│       └── original
Rayyyyy's avatar
Rayyyyy committed
177
178
179
│           ├── consolidated.00.pth
│           ├── params.json
│           └── tokenizer.model
Rayyyyy's avatar
Rayyyyy committed
180
│   ├── Meta-Llama-3-8B-Instruct
Rayyyyy's avatar
Rayyyyy committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
│       └── original
│           ├── consolidated.00.pth
│           ├── params.json
│           └── tokenizer.model
│   ├── Meta-Llama-3-70B
│       └── original
│           ├── consolidated.00.pth
│           ├── consolidated.01.pth
│           ├── consolidated.02.pth
│           ├── consolidated.03.pth
│           ├── consolidated.04.pth
│           ├── consolidated.05.pth
│           ├── consolidated.06.pth
│           ├── consolidated.07.pth
│           ├── params.json
│           └── tokenizer.model
│   └── Meta-Llama-3-70B-Instruct
│       └── original
Rayyyyy's avatar
Rayyyyy committed
199
│           ├── consolidated.00.pth
Rayyyyy's avatar
Rayyyyy committed
200
201
202
203
204
205
206
│           ├── consolidated.01.pth
│           ├── consolidated.02.pth
│           ├── consolidated.03.pth
│           ├── consolidated.04.pth
│           ├── consolidated.05.pth
│           ├── consolidated.06.pth
│           ├── consolidated.07.pth
Rayyyyy's avatar
Rayyyyy committed
207
208
209
210
│           ├── params.json
│           └── tokenizer.model
```

Rayyyyy's avatar
Rayyyyy committed
211
212
213
214
215
## 源码仓库及问题反馈
- https://developer.hpccube.com/codes/modelzoo/llama3_pytorch

## 参考资料
- https://github.com/meta-llama/llama3
Rayyyyy's avatar
Rayyyyy committed
216
217
- https://github.com/InternLM/xtuner
- https://github.com/SmartFlowAI/EmoLLM