README.md 5.5 KB
Newer Older
dcuai's avatar
dcuai 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
## 环境配置

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

```
dcuai's avatar
dcuai committed
53
54
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
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 -v /opt/hyhal:/opt/hyhal:ro  image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
hepj987's avatar
hepj987 committed
55
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
70
## 数据集



## 推理
hepj987's avatar
hepj987 committed
71
72

### 单卡推理
zhaoying1's avatar
zhaoying1 committed
73
74

```
hepj987's avatar
hepj987 committed
75
76
77
78
79
./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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
--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
97
### 多张卡并行推理
zhaoying1's avatar
zhaoying1 committed
98
99
100
101
102
103
104

```
./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
105
#其他参数与单卡推理相同
zhaoying1's avatar
zhaoying1 committed
106
107
```

hepj987's avatar
hepj987 committed
108
### 多轮对话
zhaoying1's avatar
zhaoying1 committed
109
110
111
112
113
114
115
116

```
./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
117
#其他参数与单卡推理相同
zhaoying1's avatar
zhaoying1 committed
118
119
```

hepj987's avatar
hepj987 committed
120
121
122
123
124
125
## result

`多轮对话`

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

dcuai's avatar
dcuai committed
126
### 精度
hepj987's avatar
hepj987 committed
127
128
129



hepj987's avatar
hepj987 committed
130
131
132
133
## 应用场景

### 算法类别

hepj987's avatar
hepj987 committed
134
`对话问答`
hepj987's avatar
hepj987 committed
135
136
137

### 热点应用行业

dcuai's avatar
dcuai committed
138
`医疗,教育,科研,金融`
hepj987's avatar
hepj987 committed
139

wanglch's avatar
wanglch committed
140
141
142
143
144
145
146
## 预训练权重

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

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


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

chenzk's avatar
chenzk committed
149
https://developer.sourcefind.cn/codes/modelzoo/llama_inference_pytorch
zhaoying1's avatar
zhaoying1 committed
150

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

dcuai's avatar
dcuai committed
153
https://github.com/ProjectD-AI/llama_inference