README.md 9.07 KB
Newer Older
zhouxiang's avatar
zhouxiang committed
1
# CodeLlama
zhouxiang's avatar
zhouxiang committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

## 论文
- [https://arxiv.org/pdf/2302.13971.pdf](https://arxiv.org/pdf/2302.13971.pdf)

## 模型结构
Code Llama 是基于LLAMA预训练和微调的生成文本模型,支持很多种编程语言,包括 Python, C++, Java, PHP, Typescript (Javascript), C#, Bash 等等。具备代码续写、代码填空、对话、python专项等 4 种能力。

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

![img](./docs/llama.png)

## 算法原理
Code Llama 是一组预训练和微调的生成文本模型,其规模从 7 亿到 34 亿个参数不等。

![img](./docs/llama_1.png)

## 环境配置

提供光源拉取推理的docker镜像:
xuxzh1's avatar
xuxzh1 committed
24
25
```bash
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10(推荐)
zhouxiang's avatar
zhouxiang committed
26
27
28
docker pull image.sourcefind.cn:5000/dcu/admin/base/custom:lmdeploy0.0.13_dtk23.04_torch1.13_py38
# <Host Path>主机端路径
# <Container Path>容器映射路径
xuxzh1's avatar
update  
xuxzh1 committed
29
docker run -it --name codellama --shm-size=1024G  --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v <Host Path>:<Container Path> <Image ID> /bin/bash
30
31

# 如果需要使用dtk23.10,请使用如下基础镜像
xuxzh1's avatar
update  
xuxzh1 committed
32
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
33
34
# <Host Path>主机端路径
# <Container Path>容器映射路径
xuxzh1's avatar
update  
xuxzh1 committed
35
docker run -it --name codellama --shm-size=1024G -v /opt/hyhal:/opt/hyhal --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v <Host Path>:<Container Path> <Image ID> /bin/bash
36

zhouxiang's avatar
zhouxiang committed
37
```
xuxzh1's avatar
xuxzh1 committed
38
39
40
41
42
43
44
45
46
> [!NOTE]
>
> > 使用lmdeploy0.0.13_dtk23.04_torch1.13_py38如果遇到 importError:libgemm multiB int4.so: cannot open shared obiect file: No such file or directory
> >
> > 解决方法:
> >
> > ```bash
> > rm /usr/local/lib/python3.8/site-packages/_turbomind.cpython-38-x86_64-linux-gnu.so
> > ```
zhouxiang's avatar
zhouxiang committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

## 推理

### 源码编译安装
```
# 若使用光源的镜像,可以跳过源码编译安装,镜像里面安装好了lmdeploy。
git clone http://developer.hpccube.com/codes/modelzoo/codellama_lmdeploy.git
cd codellama_lmdeploy
git submodule init && git submodule update
cd lmdeploy
mkdir build && cd build
sh ../generate.sh
make -j 32
make install
cd .. && python3 setup.py install
```
### 模型下载

它在 [HuggingFace](https://huggingface.co/codellama) 上发布了基座模型,Python模型和指令微调模型:

| 基座模型                                                                        | Python微调模型                                                                                | 指令模型                                                                                          |
| ------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [codellama/CodeLlama-7b-hf](https://huggingface.co/codellama/CodeLlama-7b-hf)   | [codellama/CodeLlama-7b-Python-hf](https://huggingface.co/codellama/CodeLlama-7b-Python-hf)   | [codellama/CodeLlama-7b-Instruct-hf](https://huggingface.co/codellama/CodeLlama-7b-Instruct-hf)   |
| [codellama/CodeLlama-13b-hf](https://huggingface.co/codellama/CodeLlama-13b-hf) | [codellama/CodeLlama-13b-Python-hf](https://huggingface.co/codellama/CodeLlama-13b-Python-hf) | [codellama/CodeLlama-13b-Instruct-hf](https://huggingface.co/codellama/CodeLlama-13b-Instruct-hf) |
| [codellama/CodeLlama-34b-hf](https://huggingface.co/codellama/CodeLlama-34b-hf) | [codellama/CodeLlama-34b-Python-hf](https://huggingface.co/codellama/CodeLlama-34b-Python-hf) | [codellama/CodeLlama-34b-Instruct-hf](https://huggingface.co/codellama/CodeLlama-34b-Instruct-hf) |

luopl's avatar
luopl committed
73
模型在SCNet上的快速下载链接:
luopl's avatar
luopl committed
74
75
76
77
78
79
80
81
82

| 基座模型                                                                        | Python微调模型                                                                                | 指令模型                                                                                          |
| ------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [codellama/CodeLlama-7b-hf](http://113.200.138.88:18080/aimodels/CodeLlama-7b-hf)   | [codellama/CodeLlama-7b-Python-hf](http://113.200.138.88:18080/aimodels/CodeLlama-7b-Python-hf)   | [codellama/CodeLlama-7b-Instruct-hf](http://113.200.138.88:18080/aimodels/CodeLlama-7b-Instruct-hf)   |
| [codellama/CodeLlama-13b-hf](http://113.200.138.88:18080/aimodels/CodeLlama-13b-hf) | [codellama/CodeLlama-13b-Python-hf](http://113.200.138.88:18080/aimodels/CodeLlama-13b-Python-hf) | [codellama/CodeLlama-13b-Instruct-hf](http://113.200.138.88:18080/aimodels/CodeLlama-13b-Instruct-hf) |
| [codellama/CodeLlama-34b-hf](http://113.200.138.88:18080/aimodels/CodeLlama-34b-hf) | [codellama/CodeLlama-34b-Python-hf](http://113.200.138.88:18080/aimodels/CodeLlama-34b-Python-hf) | [codellama/CodeLlama-34b-Instruct-hf](http://113.200.138.88:18080/aimodels/CodeLlama-34b-Instruct-hf) |



zhouxiang's avatar
zhouxiang committed
83
84
85
86
87
88
89
90
模型和能力的对应关系为:

| 模型           | 代码续写 | 代码填空          | 对话 | Python专项 |
| -------------- | -------- | ----------------- | ---- | ---------- |
| 基座模型       | Y        | Y(7B,13B), N(34B) | N    | N          |
| Python微调模型 | Y        | N                 | N    | Y          |
| 指令微调模型   | Y        | Y(7B,13B), N(34B) | Y    | N          |

xuxzh1's avatar
update  
xuxzh1 committed
91
92
93
94
95
96
97
98
99
100
### 运行前

```bash
#step 1
cd lmdeploy
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
#step 2
source /opt/dtk/cuda/env.sh
```

zhouxiang's avatar
zhouxiang committed
101
### 运行 
xuxzh1's avatar
update  
xuxzh1 committed
102

zhouxiang's avatar
zhouxiang committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
接下来,可参考如下章节,在控制台与 codellama 进行交互式对话。

**注意**:

- **transformers最低要求 v4.33.0**
- `lmdeploy.turbomind.chat` 支持把代码块拷贝到控制台,**结束输出的方式为回车,再输入"!!",再回车**。其他非 codellama 模型,仍然是两次回车结束输入。

### 代码续写

```shell
lmdeploy chat turbomind ./workspace --cap completion
```

### 代码填空

```shell
lmdeploy chat turbomind ./workspace --cap infilling
```

输入的代码块中要包含 `<FILL>`,比如:

xuxzh1's avatar
update  
xuxzh1 committed
124
```python
zhouxiang's avatar
zhouxiang committed
125
126
127
128
129
130
131
132
133
def remove_non_ascii(s: str) -> str:
    """ <FILL>
    return result
```

`turbomind.chat` 输出的代码即是要填到 `<FILL>` 中的内容

### 对话

xuxzh1's avatar
update  
xuxzh1 committed
134
```bash
zhouxiang's avatar
zhouxiang committed
135
136
137
138
139
140
141
lmdeploy chat turbomind ./workspace --cap chat --sys-instruct "Provide answers in Python"
```

可以把 `--sys-instruct` 的指令换成 codellama 支持的其他变成语言。

### Python 专项

xuxzh1's avatar
update  
xuxzh1 committed
142
```bash
zhouxiang's avatar
zhouxiang committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
lmdeploy chat turbomind ./workspace --cap python
```

建议这里部署 Python 微调模型


### 服务

**目前,server 支持的是对话功能**,其余功能后续再加上。

启动 sever 的方式是:

```shell
# --instance_num: turbomind推理实例的个数。可理解为支持的最大并发数
# --tp: 在 tensor parallel时,使用的GPU数量
xuxzh1's avatar
update  
xuxzh1 committed
158
lmdeploy serve api_server ./workspace --server-name 0.0.0.0 --server-port ${server_port} --instance_num 32 --tp 1
zhouxiang's avatar
zhouxiang committed
159
160
161
162
```

打开 `http://{server_ip}:{server_port}`,即可访问 swagger,查阅 RESTful API 的详细信息。

zhouxiang's avatar
zhouxiang committed
163
你可以用命令行,在控制台与 server 通信(在新启的命令行页面下执行):
zhouxiang's avatar
zhouxiang committed
164
165
166
167
168
169
170
171
172
173
174
175

```shell
# restful_api_url 就是 api_server 产生的,比如 http://localhost:23333
lmdeploy serve api_client restful_api_url
```

或者,启动 gradio,在 webui 的聊天对话框中,与 codellama 交流:

```shell
# restful_api_url 就是 api_server 产生的,比如 http://localhost:23333
# server_ip 和 server_port 是用来提供 gradio ui 访问服务的
# 例子: lmdeploy serve gradio http://localhost:23333 --server_name localhost --server_port 6006 --restful_api True
xuxzh1's avatar
update  
xuxzh1 committed
176
lmdeploy serve gradio restful_api_url --server-name ${server_ip} --server-port ${server_port} --restful_api True
zhouxiang's avatar
zhouxiang committed
177
178
```

zhouxiang's avatar
zhouxiang committed
179
关于 RESTful API的详细介绍,请参考[这份](https://developer.hpccube.com/codes/aicomponent/lmdeploy/-/blob/dtk23.04-v0.0.13/docs/zh_cn/restful_api.md)文档。
zhouxiang's avatar
zhouxiang committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

## result
![llama](docs/codellama.gif)

### 精度



## 应用场景

### 算法类别

`对话问答`


### 热点应用行业

`金融,科研,教育`


## 源码仓库及问题反馈
https://developer.hpccube.com/codes/modelzoo/codellama_lmdeploy

## 参考资料
https://github.com/InternLM/LMDeploy