README_zh-CN.md 7.17 KB
Newer Older
lvhan028's avatar
lvhan028 committed
1
<div align="center">
lvhan028's avatar
lvhan028 committed
2
  <img src="resources/lmdeploy-logo.png" width="450"/>
lvhan028's avatar
lvhan028 committed
3
4
5
6
7

[English](README.md) | 简体中文

</div>

8
<p align="center">
vansin's avatar
vansin committed
9
    👋 join us on <a href="https://twitter.com/intern_lm" target="_blank">Twitter</a>, <a href="https://discord.gg/xa29JuW87d" target="_blank">Discord</a> and <a href="https://r.vansin.top/?r=internwx" target="_blank">WeChat</a>
10
</p>
lvhan028's avatar
lvhan028 committed
11

12
13
______________________________________________________________________

q.yao's avatar
q.yao committed
14
## 更新 🎉
15

pppppM's avatar
pppppM committed
16
- \[2023/08\] TurboMind 支持权重 4-bit 量化和推理
17
18
- \[2023/07\] TurboMind 支持使用 GQA 的 Llama-2 70B 模型
- \[2023/07\] TurboMind 支持 Llama-2 7B/13B 模型
q.yao's avatar
q.yao committed
19
- \[2023/07\] TurboMind 支持 InternLM 的 Tensor Parallel 推理
20
21
22

______________________________________________________________________

lvhan028's avatar
lvhan028 committed
23
24
## 简介

25
26
LMDeploy 由 [MMDeploy](https://github.com/open-mmlab/mmdeploy)[MMRazor](https://github.com/open-mmlab/mmrazor) 团队联合开发,是涵盖了 LLM 任务的全套轻量化、部署和服务解决方案。
这个强大的工具箱提供以下核心功能:
lvhan028's avatar
lvhan028 committed
27

lvhan028's avatar
lvhan028 committed
28
- **高效推理引擎 TurboMind**:基于 [FasterTransformer](https://github.com/NVIDIA/FasterTransformer),我们实现了高效推理引擎 TurboMind,支持 InternLM、LLaMA、vicuna等模型在 NVIDIA GPU 上的推理。
lvhan028's avatar
lvhan028 committed
29

30
- **交互推理方式**:通过缓存多轮对话过程中 attention 的 k/v,记住对话历史,从而避免重复处理历史会话。
lvhan028's avatar
lvhan028 committed
31

tpoisonooo's avatar
tpoisonooo committed
32
- **多 GPU 部署和量化**:我们提供了全面的模型部署和量化支持,已在不同规模上完成验证。
lvhan028's avatar
lvhan028 committed
33

34
35
- **persistent batch 推理**:进一步优化模型执行效率。

pppppM's avatar
pppppM committed
36
  ![PersistentBatchInference](https://github.com/InternLM/lmdeploy/assets/67539920/e3876167-0671-44fc-ac52-5a0f9382493e)
lvhan028's avatar
lvhan028 committed
37

lvhan028's avatar
lvhan028 committed
38
## 性能
lvhan028's avatar
lvhan028 committed
39

40
**场景一**: 固定的输入、输出token数(1,2048),测试 output token throughput
lvhan028's avatar
lvhan028 committed
41

42
**场景二**: 使用真实数据,测试 request throughput
lvhan028's avatar
lvhan028 committed
43

44
测试配置:LLaMA-7B, NVIDIA A100(80G)
lvhan028's avatar
lvhan028 committed
45

46
47
TurboMind 的 output token throughput 超过 2000 token/s, 整体比 DeepSpeed 提升约 5% - 15%,比 huggingface transformers 提升 2.3 倍
在 request throughput 指标上,TurboMind 的效率比 vLLM 高 30%
lvhan028's avatar
lvhan028 committed
48

49
![benchmark](https://github.com/InternLM/lmdeploy/assets/4560679/7775c518-608e-4e5b-be73-7645a444e774)
lvhan028's avatar
lvhan028 committed
50

lvhan028's avatar
lvhan028 committed
51
## 快速上手
lvhan028's avatar
lvhan028 committed
52

lvhan028's avatar
lvhan028 committed
53
### 安装
lvhan028's avatar
lvhan028 committed
54

55
56
使用 pip ( python 3.8+) 安装 LMDeploy,或者[源码安装](./docs/zh_cn/build.md)

lvhan028's avatar
lvhan028 committed
57
```shell
lvhan028's avatar
lvhan028 committed
58
pip install lmdeploy
lvhan028's avatar
lvhan028 committed
59
60
```

lvhan028's avatar
lvhan028 committed
61
### 部署 InternLM
lvhan028's avatar
lvhan028 committed
62

lvhan028's avatar
lvhan028 committed
63
#### 获取 InternLM 模型
lvhan028's avatar
lvhan028 committed
64
65

```shell
lvhan028's avatar
lvhan028 committed
66
# 1. 下载 InternLM 模型
lvhan028's avatar
lvhan028 committed
67

pppppM's avatar
pppppM committed
68
69
# Make sure you have git-lfs installed (https://git-lfs.com)
git lfs install
del-zhenwu's avatar
del-zhenwu committed
70
git clone https://huggingface.co/internlm/internlm-chat-7b /path/to/internlm-chat-7b
pppppM's avatar
pppppM committed
71
72
73
74
75

# if you want to clone without large files – just their pointers
# prepend your git clone with the following env var:
GIT_LFS_SKIP_SMUDGE=1

lvhan028's avatar
lvhan028 committed
76
# 2. 转换为 trubomind 要求的格式。默认存放路径为 ./workspace
77
python3 -m lmdeploy.serve.turbomind.deploy internlm-chat-7b /path/to/internlm-chat-7b
lvhan028's avatar
lvhan028 committed
78

lvhan028's avatar
lvhan028 committed
79
```
lvhan028's avatar
lvhan028 committed
80

lvhan028's avatar
lvhan028 committed
81
#### 使用 turbomind 推理
lvhan028's avatar
lvhan028 committed
82
83

```shell
lvhan028's avatar
lvhan028 committed
84
python3 -m lmdeploy.turbomind.chat ./workspace
lvhan028's avatar
lvhan028 committed
85
86
```

87
88
89
90
91
92
> **Note**<br />
> turbomind 在使用 FP16 精度推理 InternLM-7B 模型时,显存开销至少需要 15.7G。建议使用 3090, V100,A100等型号的显卡。<br />
> 关闭显卡的 ECC 可以腾出 10% 显存,执行 `sudo nvidia-smi --ecc-config=0` 重启系统生效。

> **Note**<br />
> 使用 Tensor 并发可以利用多张 GPU 进行推理。在 `chat` 时添加参数 `--tp=<num_gpu>` 可以启动运行时 TP。
lvhan028's avatar
lvhan028 committed
93

94
95
96
97
98
99
100
101
102
#### 启动 gradio server

```shell
python3 -m lmdeploy.serve.gradio.app ./workspace
```

![](https://github.com/InternLM/lmdeploy/assets/67539920/08d1e6f2-3767-44d5-8654-c85767cec2ab)

#### 通过容器部署推理服务
lvhan028's avatar
lvhan028 committed
103
104

使用下面的命令启动推理服务:
lvhan028's avatar
lvhan028 committed
105
106

```shell
lvhan028's avatar
lvhan028 committed
107
bash workspace/service_docker_up.sh
lvhan028's avatar
lvhan028 committed
108
109
```

lvhan028's avatar
lvhan028 committed
110
你可以通过命令行方式与推理服务进行对话:
lvhan028's avatar
lvhan028 committed
111
112

```shell
113
python3 -m lmdeploy.serve.client {server_ip_addresss}:33337
lvhan028's avatar
lvhan028 committed
114
115
```

lvhan028's avatar
lvhan028 committed
116
也可以通过 WebUI 方式来对话:
AllentDan's avatar
AllentDan committed
117

vansin's avatar
vansin committed
118
```shell
119
python3 -m lmdeploy.serve.gradio.app {server_ip_addresss}:33337
AllentDan's avatar
AllentDan committed
120
```
lvhan028's avatar
lvhan028 committed
121

122
其他模型的部署方式,比如 LLaMA,LLaMA-2,vicuna等等,请参考[这里](docs/zh_cn/serving.md)
lvhan028's avatar
lvhan028 committed
123

WRH's avatar
WRH committed
124
125
### 基于 PyTorch 的推理

126
127
128
129
130
131
你必须确保环境中有安装 deepspeed:

```
pip install deepspeed
```

WRH's avatar
WRH committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#### 单个 GPU

```shell
python3 -m lmdeploy.pytorch.chat $NAME_OR_PATH_TO_HF_MODEL\
    --max_new_tokens 64 \
    --temperture 0.8 \
    --top_p 0.95 \
    --seed 0
```

#### 使用 DeepSpeed 实现张量并行

```shell
deepspeed --module --num_gpus 2 lmdeploy.pytorch.chat \
    $NAME_OR_PATH_TO_HF_MODEL \
    --max_new_tokens 64 \
    --temperture 0.8 \
    --top_p 0.95 \
    --seed 0
```

153
## 量化部署
lvhan028's avatar
lvhan028 committed
154

pppppM's avatar
pppppM committed
155
156
157
158
159
### Step 1. 获取量化参数

首先,执行量化脚本,获取量化参数

> 执行后,量化需要的各种参数会存放在 $WORK_DIR 中; 接下来的步骤中会用到
160
161

```
pppppM's avatar
pppppM committed
162
163

python3 -m lmdeploy.lite.apis.calibrate \
164
  --model $HF_MODEL \
pppppM's avatar
pppppM committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  --calib_dataset 'c4' \             # 校准数据集,支持 c4, ptb, wikitext2, pileval
  --calib_samples 128 \              # 校准集的样本数,如果显存不够,可以适当调小
  --calib_seqlen 2048 \              # 单条的文本长度,如果显存不够,可以适当调小
  --work_dir $WORK_DIR \             # 保存 Pytorch 格式量化统计参数和量化后权重的文件夹
```

### Step 2. 实际量化模型

目前支持对权重的 INT4 量化和 KV Cache 的 INT8 量化,根据需求执行对应脚本即可

#### 权重 INT4 量化

LMDeploy 使用 [AWQ](https://arxiv.org/abs/2306.00978) 算法对模型权重进行量化

> 需要输入第一步的 \`$WORK_DIR\`\` ,量化后的权重也会存在这个文件夹中

```
python3 -m lmdeploy.lite.apis.auto_awq \
  --w_bits 4 \                       # 权重量化的 bit 数
  --w_group_size 128 \               # 权重量化分组统计尺寸
  --work_dir $WORK_DIR \             # Step 1 保存量化参数的目录
```

#### KV Cache INT8 量化

首先,导出 TurboMind 格式的量化参数(KV Cache INT8 量化需要使用 `TurboMind`

> `$TURBOMIND_DIR` 为  `deploy.py` 转换得到的`workspace/triton_models/weights\` 目录

```
python3 -m lmdeploy.lite.apis.kv_qparams \
  --work_dir $WORK_DIR \              # Step 1 保存量化参数的目录
  --turbomind_dir $TURBOMIND_DIR \
  --kv_sym False \                    # 对称量化或非对称量化,默认为 False
  --num_tp 1  \                       # Tensor 并行使用的 GPU 数,和 deploy.py 保持一致
200
201
```

tpoisonooo's avatar
tpoisonooo committed
202
然后调整 `workspace/triton_models/weights/config.ini`
lvhan028's avatar
lvhan028 committed
203
204
205

- `use_context_fmha` 改为 0,表示关闭
- `quant_policy` 设置为 4。此参数默认为 0,表示不开启
206

207
208
这里是[量化测试结果](./docs/zh_cn/quantization.md)

209
210
211
> **Warning**<br />
> 量化部署不支持运行时 Tensor 并发。如果希望使用 Tensor 并发,需要在 deploy 时配置 tp 参数。

lvhan028's avatar
lvhan028 committed
212
213
## 贡献指南

lvhan028's avatar
lvhan028 committed
214
我们感谢所有的贡献者为改进和提升 LMDeploy 所作出的努力。请参考[贡献指南](.github/CONTRIBUTING.md)来了解参与项目贡献的相关指引。
lvhan028's avatar
lvhan028 committed
215
216
217
218

## 致谢

- [FasterTransformer](https://github.com/NVIDIA/FasterTransformer)
pppppM's avatar
pppppM committed
219
- [llm-awq](https://github.com/mit-han-lab/llm-awq)
lvhan028's avatar
lvhan028 committed
220
221
222
223

## License

该项目采用 [Apache 2.0 开源许可证](LICENSE)