README.md 5.45 KB
Newer Older
hepj987's avatar
hepj987 committed
1
# LLaMa
zhaoying1's avatar
zhaoying1 committed
2

hepj987's avatar
hepj987 committed
3
## 论文
zhaoying1's avatar
zhaoying1 committed
4

hepj987's avatar
hepj987 committed
5
6
7
`LLaMA: Open and Efficient Foundation Language Models`

https://arxiv.org/pdf/2302.13971.pdf
zhaoying1's avatar
zhaoying1 committed
8
9
10

## 模型结构

hepj987's avatar
hepj987 committed
11
12
![llama_model](llama_model.png)

zhaoying1's avatar
zhaoying1 committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
```
LLAMA网络基于 Transformer 架构。提出了各种改进,并用于不同的模型,例如 PaLM。以下是与原始架构的主要区别:
预归一化。为了提高训练稳定性,对每个transformer 子层的输入进行归一化,而不是对输出进行归一化。使用 RMSNorm 归一化函数。
SwiGLU 激活函数 [PaLM]。使用 SwiGLU 激活函数替换 ReLU 非线性以提高性能。使用 2 /3 4d 的维度而不是 PaLM 中的 4d。
旋转嵌入。移除了绝对位置嵌入,而是添加了旋转位置嵌入 (RoPE),在网络的每一层。
```

以下是llama-7B的主要网络参数配置:

```
  "hidden_act": "silu",
  "hidden_size": 4096,
  "initializer_range": 0.02,
  "intermediate_size": 11008,
  "max_position_embeddings": 2048,
  "model_type": "llama",
  "num_attention_heads": 32,
  "num_hidden_layers": 32,
  "rms_norm_eps": 1e-06,
  "vocab_size": 32000
```

hepj987's avatar
hepj987 committed
35
36
37
38
39
40
41
42
43
44
45
46
47
## 算法原理

```
以下是与原始 Transformer 架构的主要区别:
预归一化。为了提高训练稳定性,对每个transformer 子层的输入进行归一化,而不是对输出进行归一化。使用 RMSNorm 归一化函数。
SwiGLU 激活函数 [PaLM]。使用 SwiGLU 激活函数替换 ReLU 非线性以提高性能。使用 2 /3 4d 的维度而不是 PaLM 中的 4d。
旋转嵌入。移除了绝对位置嵌入,而是添加了旋转位置嵌入 (RoPE),在网络的每一层。
```



![llama](llama.png)

zhaoying1's avatar
zhaoying1 committed
48
49
50
51
52
53
## 环境配置

推荐使用docker方式运行,提供[光源](https://www.sourcefind.cn/)拉取的docker镜像:

```
docker pull image.sourcefind.cn:5000/dcu/admin/base/vscode-pytorch:1.10.0-centos7.6-dtk-22.10-py37-latest
hepj987's avatar
hepj987 committed
54
55
docker run -dit --network=host --name=llama_inference_pytorch --privileged --device=/dev/kfd --device=/dev/dri --ipc=host --shm-size=16G  --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root --ulimit stack=-1:-1 --ulimit memlock=-1:-1 image.sourcefind.cn:5000/dcu/admin/base/pytorch:1.10.0-centos7.6-dtk-23.04-py37-latest
docker exec -it llama_inference_pytorch /bin/bash
zhaoying1's avatar
zhaoying1 committed
56
57
58
59
60
```

安装docker中没有的依赖

```
hepj987's avatar
hepj987 committed
61
pip install -r requirements.txt  -i https://mirrors.aliyun.com/pypi/simple/  --trusted-host mirrors.aliyun.com
zhaoying1's avatar
zhaoying1 committed
62
63
64
65
pip install tensor_parallel==1.2.5 --no-dependencies
pip install transformers==4.28.1 sentencepiece==0.1.99
```

hepj987's avatar
hepj987 committed
66
67
68
69
## 数据集



hepj987's avatar
hepj987 committed
70
## 模型权重下载
zhaoying1's avatar
zhaoying1 committed
71
72
73
74
75

[llama chat 7B](https://huggingface.co/Linly-AI/ChatFlow-7B)

[llama caht 13b](https://huggingface.co/Linly-AI/ChatFlow-13B)

hepj987's avatar
hepj987 committed
76
## 推理
hepj987's avatar
hepj987 committed
77
78

### 单卡推理
zhaoying1's avatar
zhaoying1 committed
79
80

```
hepj987's avatar
hepj987 committed
81
82
83
84
85
./run.sh
export HIP_VISIBLE_DEVICES=0   指定使用第0号卡
LOAD_MODEL  为下载的llama 模型bin路径
SPM_PATH	为下载的llama 模型tokenizer路径
--config_path 需要与使用的模型对齐,若使用13b的模型,这里需要改成config/llama_13b_config.json
zhaoying1's avatar
zhaoying1 committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
--load_model_path (必填项),预训练好的模型,默认是fp16的(如果需要fp32,修改llama_infer.py的L41为对应的精度)
--test_path (必填项),输入的prompts,每一行是一个prompts。
--prediction_path (必填项),输出结果保存的路径。
--config_path (必填项),模型参数配置文件,可以保存在config文件夹中。
--spm_model_path (必填项),模型tokenizer存放的路径。
--batch_size (可选),默认为1。批处理大小,注意按需使用,因为attention cache会根据这个大小来构造tensor并且保存在显存中。
--seq_length (可选),默认为128。生成句子的总长度,等于prompts + 模型生成的长度。
--world_size (可选),默认为1。使用多少张卡进行张量并行推理。
--use_int8 (可选),默认为False。是否使用int8推理。
--top_k (可选),默认为40。句子的生成会针对top_k做采样,影响生成多样性。
--top_p (可选),默认为0.95。句子的生成会针对累积概率top_p做采样,影响生成多样性。
--temperature (可选),默认为0.8。对最后的probabilities做一次放缩,影响token采样结果。
--repetition_penalty_range (可选),默认为1024。重复出现token的惩罚范围。
--repetition_penalty_slope (可选),默认为0。重复出现token的惩罚slope。
--repetition_penalty (可选),默认为1.15。重复出现token的惩罚系数。
```

hepj987's avatar
hepj987 committed
103
### 多张卡并行推理
zhaoying1's avatar
zhaoying1 committed
104
105
106
107
108
109
110

```
./run-tp.sh
export HIP_VISIBLE_DEVICES=0,1,2,3   指定使用第0,1,2,3号卡
LOAD_MODEL  为下载的llama 模型bin路径
SPM_PATH	为下载的llama 模型tokenizer路径
--config_path 需要与使用的模型对齐,若使用13b的模型,这里需要改成config/llama_13b_config.json
hepj987's avatar
hepj987 committed
111
#其他参数与单卡推理相同
zhaoying1's avatar
zhaoying1 committed
112
113
```

hepj987's avatar
hepj987 committed
114
### 多轮对话
zhaoying1's avatar
zhaoying1 committed
115
116
117
118
119
120
121
122

```
./run-dialogue.sh
#对话时输入  clear  清空聊天历史  输入 exit  退出程序
export HIP_VISIBLE_DEVICES=0,1,2,3   指定使用第0,1,2,3号卡
LOAD_MODEL  为下载的llama 模型bin路径
SPM_PATH	为下载的llama 模型tokenizer路径
--config_path 需要与使用的模型对齐,若使用13b的模型,这里需要改成config/llama_13b_config.json
hepj987's avatar
hepj987 committed
123
#其他参数与单卡推理相同
zhaoying1's avatar
zhaoying1 committed
124
125
```

hepj987's avatar
hepj987 committed
126
127
128
129
130
131
132
133
134
135
## result

`多轮对话`

![image-llama](llama-inf.jpg)

## 精度



hepj987's avatar
hepj987 committed
136
137
138
139
## 应用场景

### 算法类别

hepj987's avatar
hepj987 committed
140
`对话问答`
hepj987's avatar
hepj987 committed
141
142
143

### 热点应用行业

hepj987's avatar
hepj987 committed
144
`科研`
hepj987's avatar
hepj987 committed
145

zhaoying1's avatar
zhaoying1 committed
146
147
148
149
## 源码仓库及问题反馈

https://developer.hpccube.com/codes/hepj/llama_pytorch

hepj987's avatar
hepj987 committed
150
## 参考资料
zhaoying1's avatar
zhaoying1 committed
151
152

https://github.com/ProjectD-AI/llama_inference