README.md 9.05 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_zh-CN.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
## News 🎉
15

16
- \[2023/08\] TurboMind supports Qwen-7B, dynamic NTK-RoPE scaling and dynamic logN scaling
Chen Xin's avatar
Chen Xin committed
17
- \[2023/08\] TurboMind supports Windows (tp=1)
18
- \[2023/08\] TurboMind supports 4-bit inference, 2.4x faster than FP16, the fastest open-source implementation🚀. Check [this](./docs/en/w4a16.md) guide for detailed info
pppppM's avatar
pppppM committed
19
20
- \[2023/08\] LMDeploy has launched on the [HuggingFace Hub](https://huggingface.co/lmdeploy), providing ready-to-use 4-bit models.
- \[2023/08\] LMDeploy supports 4-bit quantization using the [AWQ](https://arxiv.org/abs/2306.00978) algorithm.
21
22
- \[2023/07\] TurboMind supports Llama-2 70B with GQA.
- \[2023/07\] TurboMind supports Llama-2 7B/13B.
q.yao's avatar
q.yao committed
23
- \[2023/07\] TurboMind supports tensor-parallel inference of InternLM.
24
25
26

______________________________________________________________________

lvhan028's avatar
lvhan028 committed
27
28
## Introduction

lvhan028's avatar
lvhan028 committed
29
30
LMDeploy is a toolkit for compressing, deploying, and serving LLM, developed by the [MMRazor](https://github.com/open-mmlab/mmrazor) and [MMDeploy](https://github.com/open-mmlab/mmdeploy) teams. It has the following core features:

tpoisonooo's avatar
tpoisonooo committed
31
- **Efficient Inference Engine (TurboMind)**: Based on [FasterTransformer](https://github.com/NVIDIA/FasterTransformer), we have implemented an efficient inference engine - TurboMind, which supports the inference of LLaMA and its variant models on NVIDIA GPUs.
lvhan028's avatar
lvhan028 committed
32

33
- **Interactive Inference Mode**: By caching the k/v of attention during multi-round dialogue processes, it remembers dialogue history, thus avoiding repetitive processing of historical sessions.
lvhan028's avatar
lvhan028 committed
34

tpoisonooo's avatar
tpoisonooo committed
35
- **Multi-GPU Model Deployment and Quantization**: We provide comprehensive model deployment and quantification support, and have been validated at different scales.
36
37

- **Persistent Batch Inference**: Further optimization of model execution efficiency.
lvhan028's avatar
lvhan028 committed
38

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

pppppM's avatar
pppppM committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
## Supported Models

`LMDeploy` has two inference backends, `Pytorch` and `TurboMind`.

### TurboMind

> **Note**<br />
> W4A16 inference requires Nvidia GPU with Ampere architecture or above.

|  Models  | Tensor Parallel | FP16 | KV INT8 | W4A16 | W8A8 |
| :------: | :-------------: | :--: | :-----: | :---: | :--: |
|  Llama   |       Yes       | Yes  |   Yes   |  Yes  |  No  |
|  Llama2  |       Yes       | Yes  |   Yes   |  Yes  |  No  |
| InternLM |       Yes       | Yes  |   Yes   |  Yes  |  No  |

### Pytorch

|  Models  | Tensor Parallel | FP16 | KV INT8 | W4A16 | W8A8 |
| :------: | :-------------: | :--: | :-----: | :---: | :--: |
|  Llama   |       Yes       | Yes  |   No    |  No   |  No  |
|  Llama2  |       Yes       | Yes  |   No    |  No   |  No  |
| InternLM |       Yes       | Yes  |   No    |  No   |  No  |

lvhan028's avatar
lvhan028 committed
64
65
## Performance

66
**Case I**: output token throughput with fixed input token and output token number (1, 2048)
lvhan028's avatar
lvhan028 committed
67

68
**Case II**: request throughput with real conversation data
lvhan028's avatar
lvhan028 committed
69

70
Test Setting: LLaMA-7B, NVIDIA A100(80G)
lvhan028's avatar
lvhan028 committed
71

72
73
The output token throughput of TurboMind exceeds 2000 tokens/s, which is about 5% - 15% higher than DeepSpeed overall and outperforms huggingface transformers by up to 2.3x.
And the request throughput of TurboMind is 30% higher than vLLM.
lvhan028's avatar
lvhan028 committed
74

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

lvhan028's avatar
lvhan028 committed
77
78
79
## Quick Start

### Installation
lvhan028's avatar
lvhan028 committed
80

81
Install lmdeploy with pip ( python 3.8+) or [from source](./docs/en/build.md)
lvhan028's avatar
lvhan028 committed
82
83

```shell
lvhan028's avatar
lvhan028 committed
84
pip install lmdeploy
lvhan028's avatar
lvhan028 committed
85
86
```

lvhan028's avatar
lvhan028 committed
87
### Deploy InternLM
lvhan028's avatar
lvhan028 committed
88

lvhan028's avatar
lvhan028 committed
89
#### Get InternLM model
lvhan028's avatar
lvhan028 committed
90
91

```shell
lvhan028's avatar
lvhan028 committed
92
# 1. Download InternLM model
lvhan028's avatar
lvhan028 committed
93

pppppM's avatar
pppppM committed
94
95
# Make sure you have git-lfs installed (https://git-lfs.com)
git lfs install
del-zhenwu's avatar
del-zhenwu committed
96
git clone https://huggingface.co/internlm/internlm-chat-7b /path/to/internlm-chat-7b
pppppM's avatar
pppppM committed
97
98
99
100
101

# 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
102
# 2. Convert InternLM model to turbomind's format, which will be in "./workspace" by default
103
python3 -m lmdeploy.serve.turbomind.deploy internlm-chat-7b /path/to/internlm-chat-7b
lvhan028's avatar
lvhan028 committed
104
105
106

```

lvhan028's avatar
lvhan028 committed
107
#### Inference by TurboMind
lvhan028's avatar
lvhan028 committed
108
109

```shell
lvhan028's avatar
lvhan028 committed
110
python -m lmdeploy.turbomind.chat ./workspace
lvhan028's avatar
lvhan028 committed
111
112
```

113
114
115
116
117
118
119
> **Note**<br />
> When inferring with FP16 precision, the InternLM-7B model requires at least 15.7G of GPU memory overhead on TurboMind. <br />
> It is recommended to use NVIDIA cards such as 3090, V100, A100, etc.
> Disable GPU ECC can free up 10% memory, try `sudo nvidia-smi --ecc-config=0` and reboot system.

> **Note**<br />
> Tensor parallel is available to perform inference on multiple GPUs. Add `--tp=<num_gpu>` on `chat` to enable runtime TP.
lvhan028's avatar
lvhan028 committed
120

121
122
123
124
125
126
127
128
129
#### Serving with gradio

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

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

#### Serving with Triton Inference Server
lvhan028's avatar
lvhan028 committed
130

lvhan028's avatar
lvhan028 committed
131
Launch inference server by:
lvhan028's avatar
lvhan028 committed
132
133

```shell
lvhan028's avatar
lvhan028 committed
134
bash workspace/service_docker_up.sh
lvhan028's avatar
lvhan028 committed
135
136
```

lvhan028's avatar
lvhan028 committed
137
Then, you can communicate with the inference server by command line,
lvhan028's avatar
lvhan028 committed
138
139

```shell
140
python3 -m lmdeploy.serve.client {server_ip_addresss}:33337
lvhan028's avatar
lvhan028 committed
141
142
```

lvhan028's avatar
lvhan028 committed
143
or webui,
AllentDan's avatar
AllentDan committed
144

vansin's avatar
vansin committed
145
```shell
146
python3 -m lmdeploy.serve.gradio.app {server_ip_addresss}:33337
AllentDan's avatar
AllentDan committed
147
148
```

149
For the deployment of other supported models, such as LLaMA, LLaMA-2, vicuna and so on, you can find the guide from [here](docs/en/serving.md)
lvhan028's avatar
lvhan028 committed
150

WRH's avatar
WRH committed
151
152
### Inference with PyTorch

153
For detailed instructions on Inference pytorch models, see [here](docs/en/pytorch.md).
154

WRH's avatar
WRH committed
155
156
157
#### Single GPU

```shell
WRH's avatar
WRH committed
158
python3 -m lmdeploy.pytorch.chat $NAME_OR_PATH_TO_HF_MODEL \
WRH's avatar
WRH committed
159
160
161
162
163
164
165
166
167
    --max_new_tokens 64 \
    --temperture 0.8 \
    --top_p 0.95 \
    --seed 0
```

#### Tensor Parallel with DeepSpeed

```shell
WRH's avatar
WRH committed
168
deepspeed --module --num_gpus 2 lmdeploy.pytorch.chat \
WRH's avatar
WRH committed
169
170
171
172
173
174
175
    $NAME_OR_PATH_TO_HF_MODEL \
    --max_new_tokens 64 \
    --temperture 0.8 \
    --top_p 0.95 \
    --seed 0
```

176
177
178
179
180
181
You need to install deepspeed first to use this feature.

```
pip install deepspeed
```

182
183
## Quantization

pppppM's avatar
pppppM committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
### Step 1. Obtain Quantization Parameters

First, run the quantization script to obtain the quantization parameters.

> After execution, various parameters needed for quantization will be stored in `$WORK_DIR`; these will be used in the following steps..

```
python3 -m lmdeploy.lite.apis.calibrate \
  --model $HF_MODEL \
  --calib_dataset 'c4' \             # Calibration dataset, supports c4, ptb, wikitext2, pileval
  --calib_samples 128 \              # Number of samples in the calibration set, if memory is insufficient, you can appropriately reduce this
  --calib_seqlen 2048 \              # Length of a single piece of text, if memory is insufficient, you can appropriately reduce this
  --work_dir $WORK_DIR \             # Folder storing Pytorch format quantization statistics parameters and post-quantization weight

```

### Step 2. Actual Model Quantization

`LMDeploy` supports INT4 quantization of weights and INT8 quantization of KV Cache. Run the corresponding script according to your needs.

#### Weight INT4 Quantization

LMDeploy uses AWQ algorithm for model weight quantization

> Requires input from the $WORK_DIR of step 1, and the quantized weights will also be stored in this folder.

```
python3 -m lmdeploy.lite.apis.auto_awq \
AllentDan's avatar
AllentDan committed
212
  --model $HF_MODEL \
pppppM's avatar
pppppM committed
213
214
215
216
217
218
219
  --w_bits 4 \                       # Bit number for weight quantization
  --w_group_size 128 \               # Group size for weight quantization statistics
  --work_dir $WORK_DIR \             # Directory saving quantization parameters from Step 1
```

#### KV Cache INT8 Quantization

220
In fp16 mode, kv_cache int8 quantization can be enabled, and a single card can serve more users.
tpoisonooo's avatar
tpoisonooo committed
221
First execute the quantization script, and the quantization parameters are stored in the `workspace/triton_models/weights` transformed by `deploy.py`.
222
223
224

```
python3 -m lmdeploy.lite.apis.kv_qparams \
pppppM's avatar
pppppM committed
225
226
227
228
  --work_dir $WORK_DIR \             # Directory saving quantization parameters from Step 1
  --turbomind_dir $TURBOMIND_DIR \
  --kv_sym False \                   # Whether to use symmetric or asymmetric quantization.
  --num_tp 1 \                       # The number of GPUs used for tensor parallelism
229
230
```

tpoisonooo's avatar
tpoisonooo committed
231
Then adjust `workspace/triton_models/weights/config.ini`
lvhan028's avatar
lvhan028 committed
232

lvhan028's avatar
lvhan028 committed
233
234
- `use_context_fmha` changed to 0, means off
- `quant_policy` is set to 4. This parameter defaults to 0, which means it is not enabled
lvhan028's avatar
lvhan028 committed
235

236
Here is [quantization test results](./docs/en/kv_int8.md).
237

238
> **Warning**<br />
tpoisonooo's avatar
tpoisonooo committed
239
> runtime Tensor Parallel for quantilized model is not available. Please setup `--tp` on `deploy` to enable static TP.
240

lvhan028's avatar
lvhan028 committed
241
## Contributing
lvhan028's avatar
lvhan028 committed
242

lvhan028's avatar
lvhan028 committed
243
We appreciate all contributions to LMDeploy. Please refer to [CONTRIBUTING.md](.github/CONTRIBUTING.md) for the contributing guideline.
244

lvhan028's avatar
lvhan028 committed
245
246
247
## Acknowledgement

- [FasterTransformer](https://github.com/NVIDIA/FasterTransformer)
pppppM's avatar
pppppM committed
248
- [llm-awq](https://github.com/mit-han-lab/llm-awq)
lvhan028's avatar
lvhan028 committed
249
250
251
252

## License

This project is released under the [Apache 2.0 license](LICENSE).