README.md 6.94 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
```bash
xuxzh1's avatar
update  
xuxzh1 committed
25
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
26
27
# <Host Path>主机端路径
# <Container Path>容器映射路径
shantf's avatar
shantf committed
28
29
30
31
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 /opt/hyhal:/opt/hyhal:ro -v <Host Path>:<Container Path> <Image ID> /bin/bash

cd lmdeploy
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
shantf's avatar
shantf committed
32

shantf's avatar
shantf committed
33
source /opt/dtk/cuda/env.sh
34

zhouxiang's avatar
zhouxiang committed
35
36
37
38
39
40
41
```

## 推理

### 源码编译安装
```
# 若使用光源的镜像,可以跳过源码编译安装,镜像里面安装好了lmdeploy。
chenzk's avatar
chenzk committed
42
git clone http://developer.sourcefind.cn/codes/modelzoo/codellama_lmdeploy.git
zhouxiang's avatar
zhouxiang committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
62

zhouxiang's avatar
zhouxiang committed
63
64
65
66
67
68
69
70
模型和能力的对应关系为:

| 模型           | 代码续写 | 代码填空          | 对话 | 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
71
72


zhouxiang's avatar
zhouxiang committed
73
### 运行 
xuxzh1's avatar
update  
xuxzh1 committed
74

zhouxiang's avatar
zhouxiang committed
75
76
77
78
79
80
81
82
83
84
85
接下来,可参考如下章节,在控制台与 codellama 进行交互式对话。

**注意**:

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

### 代码续写

```shell
lmdeploy chat turbomind ./workspace --cap completion
shantf's avatar
shantf committed
86
./workspace:模型路径
zhouxiang's avatar
zhouxiang committed
87
88
89
90
91
92
```

### 代码填空

```shell
lmdeploy chat turbomind ./workspace --cap infilling
shantf's avatar
shantf committed
93
./workspace:模型路径
zhouxiang's avatar
zhouxiang committed
94
95
96
97
```

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

xuxzh1's avatar
update  
xuxzh1 committed
98
```python
zhouxiang's avatar
zhouxiang committed
99
100
101
102
103
104
105
106
107
def remove_non_ascii(s: str) -> str:
    """ <FILL>
    return result
```

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

### 对话

xuxzh1's avatar
update  
xuxzh1 committed
108
```bash
shantf's avatar
shantf committed
109
110
lmdeploy chat turbomind ./workspace --cap chat
./workspace:模型路径
zhouxiang's avatar
zhouxiang committed
111
112
113
114
```

### Python 专项

xuxzh1's avatar
update  
xuxzh1 committed
115
```bash
zhouxiang's avatar
zhouxiang committed
116
lmdeploy chat turbomind ./workspace --cap python
shantf's avatar
shantf committed
117
./workspace:模型路径
zhouxiang's avatar
zhouxiang committed
118
119
120
121
122
123
124
125
126
127
128
129
130
```

建议这里部署 Python 微调模型


### 服务

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

启动 sever 的方式是:

```shell
# --tp: 在 tensor parallel时,使用的GPU数量
shantf's avatar
shantf committed
131
lmdeploy serve api_server ./workspace --server-name 0.0.0.0 --server-port ${server_port} --tp 1
zhouxiang's avatar
zhouxiang committed
132
133
134
135
```

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

zhouxiang's avatar
zhouxiang committed
136
你可以用命令行,在控制台与 server 通信(在新启的命令行页面下执行):
zhouxiang's avatar
zhouxiang committed
137
138
139
140
141
142
143
144
145
146
147

```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 访问服务的
shantf's avatar
shantf committed
148
149
150
151
152
153
154
155
156
157
158
159
# 例子: lmdeploy serve gradio http://localhost:23333 --server-name localhost --server-port 6006
# 	    --server_port要和restful_api_url的端口不一样。
lmdeploy serve gradio restful_api_url --server-name ${server_ip} --server-port ${server_port}

注意:如果打不开网页,则按照一下办法调整
从https://github.com/bumblebeeMMa/DownLoad_frpc_linux_amd64 下载frpc_linux_amd64文件;
本地改名为frpc_linux_amd64_v0.2
上传到gradio安装路径下面,gradio安装包路径可以使用如下方法找到,在终端输入下面语句:
python
import gradio
gradio
```
zhouxiang's avatar
zhouxiang committed
160

chenzk's avatar
chenzk committed
161
关于 RESTful API的详细介绍,请参考[这份](https://developer.sourcefind.cn/codes/aicomponent/lmdeploy/-/blob/dtk23.04-v0.0.13/docs/zh_cn/restful_api.md)文档。
zhouxiang's avatar
zhouxiang committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

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

### 精度



## 应用场景

### 算法类别

`对话问答`


### 热点应用行业

`金融,科研,教育`


## 源码仓库及问题反馈
chenzk's avatar
chenzk committed
183
https://developer.sourcefind.cn/codes/modelzoo/codellama_lmdeploy
zhouxiang's avatar
zhouxiang committed
184
185
186

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