Skip to content

Commit 8e8629d

Browse files
authored
Check-in user guide for w4a16 LLM deployment (#224)
* tmp * update * update * update * update * update * remove * update * update
1 parent 6829684 commit 8e8629d

File tree

6 files changed

+214
-23
lines changed

6 files changed

+214
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ______________________________________________________________________
1313

1414
## News 🎉
1515

16-
- \[2023/08\] TurboMind supports 4-bit inference, 2.4x faster than FP16, the fastest open-source implementation🚀.
16+
- \[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
1717
- \[2023/08\] LMDeploy has launched on the [HuggingFace Hub](https://huggingface.co/lmdeploy), providing ready-to-use 4-bit models.
1818
- \[2023/08\] LMDeploy supports 4-bit quantization using the [AWQ](https://arxiv.org/abs/2306.00978) algorithm.
1919
- \[2023/07\] TurboMind supports Llama-2 70B with GQA.

README_zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ______________________________________________________________________
1313

1414
## 更新 🎉
1515

16-
- \[2023/08\] TurboMind 支持 4-bit 推理,速度是 FP16 的 2.4 倍,是目前最快的开源实现🚀
16+
- \[2023/08\] TurboMind 支持 4-bit 推理,速度是 FP16 的 2.4 倍,是目前最快的开源实现🚀。部署方式请看[这里](./docs/zh_cn/w4a16.md)
1717
- \[2023/08\] LMDeploy 开通了 [HuggingFace Hub](https://huggingface.co/lmdeploy) ,提供开箱即用的 4-bit 模型
1818
- \[2023/08\] LMDeploy 支持使用 [AWQ](https://arxiv.org/abs/2306.00978) 算法进行 4-bit 量化
1919
- \[2023/07\] TurboMind 支持使用 GQA 的 Llama-2 70B 模型

benchmark/profile_generation.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
from queue import Queue
55
from threading import Thread
6+
from typing import List
67

78
import fire
89
import numpy as np
@@ -29,25 +30,29 @@ def infer(model, session_id: int, input_ids: str, output_seqlen: int,
2930
tokens.append(token)
3031

3132
# TODO: ignore first token
32-
first_token_latency = timestamps[0] - start
33+
first_token_latency = np.round(timestamps[0] - start, 2)
3334
if len(timestamps) == 1:
34-
token_latency = timestamps[0] - start
35+
token_latency = np.round(timestamps[0] - start, 2)
3536
token = tokens[0]
3637
else:
37-
token_latency = timestamps[-1] - timestamps[0]
38+
token_latency = np.round(timestamps[-1] - timestamps[0], 2)
3839
token = tokens[-1] - tokens[0]
3940
stats.append([first_token_latency, token, token_latency])
4041
que.put((session_id, stats))
4142

4243

43-
def warmup(model, concurrency: int, output_seqlen: int, warmup_round: int = 4):
44+
def warmup(model,
45+
concurrency: int,
46+
input_ids: List[int],
47+
output_seqlen: int,
48+
warmup_round: int = 2):
4449
print('start to warmup ...')
4550

4651
def _infer(model, session_id):
4752
chatbot = model.create_instance()
4853
for _ in range(warmup_round):
4954
for _ in chatbot.stream_infer(session_id,
50-
input_ids=[1],
55+
input_ids=input_ids,
5156
request_output_len=output_seqlen,
5257
sequence_start=True,
5358
sequence_end=True,
@@ -82,11 +87,12 @@ def main(model_path: str,
8287
tokenizer = Tokenizer(tokenizer_model_path)
8388
tm_model = TurboMind(model_path=model_path, tp=tp)
8489

85-
warmup(tm_model, concurrency, output_seqlen)
86-
8790
# make up a prompt that can be tokenized into {input_seqlen} tokens
8891
prompt = '' if input_seqlen == 0 else 'hi' + ' hi' * (input_seqlen - 1)
8992
input_ids = tokenizer.encode(prompt)
93+
94+
warmup(tm_model, concurrency, input_ids, output_seqlen)
95+
9096
que = Queue()
9197
procs = []
9298
_start = time.perf_counter()
@@ -134,7 +140,7 @@ def main(model_path: str,
134140
f'{first_token_latency_ave:.2f}s\ntoken latency(min, max, ave): '
135141
f'{token_latency_min:.2f}s, {token_latency_max:.2f}s, '
136142
f'{token_latency_ave:.2f}s\n'
137-
f'throughput: {throughput} token/s\n{"-" * 50}')
143+
f'throughput: {throughput:.2f} token/s\n{"-" * 50}')
138144

139145

140146
if __name__ == '__main__':

docs/en/serving.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,6 @@ bash workspace/service_docker_up.sh
3434

3535
</details>
3636

37-
<details open>
38-
<summary><b>7B with INT4 weight only quantization</b></summary>
39-
40-
```shell
41-
python3 -m lmdeploy.serve.turbomind.deploy llama2 /path/to/llama-2-7b-chat-hf \
42-
--model_format awq \
43-
--group_size 128 \
44-
--quant_path /path/to/awq-quant-weight.pt
45-
bash workspace/service_docker_up.sh
46-
```
47-
48-
</details>
49-
5037
## Serving [LLaMA](https://github.com/facebookresearch/llama)
5138

5239
Weights for the LLaMA models can be obtained from by filling out [this form](https://docs.google.com/forms/d/e/1FAIpQLSfqNECQnMkycAp2jP4Z9TFX0cGR4uf7b_fBxjY_OjhJILlKGA/viewform)

docs/en/w4a16.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# W4A16 LLM Model Deployment
2+
3+
LMDeploy supports LLM model inference of 4-bit weight, with the minimum requirement for NVIDIA graphics cards being sm80, such as A10, A100, Geforce 30/40 series.
4+
5+
Before proceeding with the inference, please ensure that lmdeploy(>=v0.0.4) is installed.
6+
7+
```shell
8+
pip install lmdeploy
9+
```
10+
11+
## 4-bit LLM model Inference
12+
13+
You can download the pre-quantized 4-bit weight models from LMDeploy's [model zoo](https://huggingface.co/lmdeploy) and conduct inference using the following command.
14+
15+
Alternatively, you can quantize 16-bit weights to 4-bit weights following the ["4-bit Weight Quantization"](#4-bit-weight-quantization) section, and then perform inference as per the below instructions.
16+
17+
Take the 4-bit Llama-2-chat-7B model from the model zoo as an example:
18+
19+
```shell
20+
git-lfs install
21+
git clone https://huggingface.co/lmdeploy/llama2-chat-7b-w4
22+
```
23+
24+
As demonstrated in the command below, first convert the model's layout using `turbomind.deploy`, and then you can interact with the AI assistant in the terminal
25+
26+
```shell
27+
28+
## Convert the model's layout and store it in the default path, ./workspace.
29+
python3 -m lmdeploy.serve.turbomind.deploy \
30+
--model-name llama2 \
31+
--model-path ./llama2-chat-7b-w4 \
32+
--model-format awq \
33+
--group-size 128
34+
35+
## inference
36+
python3 -m lmdeploy.turbomind.chat ./workspace
37+
```
38+
39+
## Serve with gradio
40+
41+
If you wish to interact with the model via web ui, please initiate the gradio server as indicated below:
42+
43+
```shell
44+
python3 -m lmdeploy.serve.turbomind ./workspace --server_name {ip_addr} ----server_port {port}
45+
```
46+
47+
Subsequently, you can open the website `http://{ip_addr}:{port}` in your browser and interact with the model
48+
49+
## Inference Performance
50+
51+
We benchmarked the Llama-2-7B-chat and Llama-2-13B-chat models with 4-bit quantization on NVIDIA GeForce RTX 4090 using [profile_generation.py](https://github.com/InternLM/lmdeploy/blob/main/benchmark/profile_generation.py). And we measure the token generation throughput (tokens/s) by setting a single prompt token and generating 512 tokens. All the results are measured for single batch inference.
52+
53+
| model | llm-awq | mlc-llm | turbomind |
54+
| ---------------- | ------- | ------- | --------- |
55+
| Llama-2-7B-chat | 112.9 | 159.4 | 206.4 |
56+
| Llama-2-13B-chat | N/A | 90.7 | 115.8 |
57+
58+
Memory (GB) comparison results between 4-bit and 16-bit model with context size 2048 and 4096 respectively,
59+
60+
| model | 16bit(2048) | 4bit(2048) | 16bit(4096) | 4bit(4096) |
61+
| ---------------- | ----------- | ---------- | ----------- | ---------- |
62+
| Llama-2-7B-chat | 15.1 | 6.3 | 16.2 | 7.5 |
63+
| Llama-2-13B-chat | OOM | 10.3 | OOM | 12.0 |
64+
65+
```shell
66+
python benchmark/profile_generation.py \
67+
./workspace \
68+
--concurrency 1 --input_seqlen 1 --output_seqlen 512
69+
```
70+
71+
## 4-bit Weight Quantization
72+
73+
It includes two steps:
74+
75+
- generate quantization parameter
76+
- quantize model according to the parameter
77+
78+
### Step 1: Generate Quantization Parameter
79+
80+
```shell
81+
python3 -m lmdeploy.lite.apis.calibrate \
82+
--model $HF_MODEL \
83+
--calib_dataset 'c4' \ # Calibration dataset, supports c4, ptb, wikitext2, pileval
84+
--calib_samples 128 \ # Number of samples in the calibration set, if memory is insufficient, you can appropriately reduce this
85+
--calib_seqlen 2048 \ # Length of a single piece of text, if memory is insufficient, you can appropriately reduce this
86+
--work_dir $WORK_DIR \ # Folder storing Pytorch format quantization statistics parameters and post-quantization weight
87+
```
88+
89+
### Step2: Quantize Weights
90+
91+
LMDeploy employs AWQ algorithm for model weight quantization.
92+
93+
```shell
94+
python3 -m lmdeploy.lite.apis.auto_awq \
95+
--model $HF_MODEL \
96+
--w_bits 4 \ # Bit number for weight quantization
97+
--w_group_size 128 \ # Group size for weight quantization statistics
98+
--work_dir $WORK_DIR \ # Directory saving quantization parameters from Step 1
99+
```
100+
101+
After the quantization is complete, the quantized model is saved to `$WORK_DIR`. Then you can proceed with model inference according to the instructions in the ["4-Bit Weight Model Inference"](#4-bit-llm-model-inference) section.

docs/zh_cn/w4a16.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# W4A16 LLM 模型部署
2+
3+
LMDeploy 支持 4bit 权重模型的推理,**对 NVIDIA 显卡的最低要求是 sm80**,比如A10,A100,Gerforce 30/40系列。
4+
5+
在推理之前,请确保安装了 lmdeploy,版本 >= v0.0.4
6+
7+
```shell
8+
pip install lmdeploy
9+
```
10+
11+
## 4bit 权重模型推理
12+
13+
你可以直接从 LMDeploy 的 [model zoo](https://huggingface.co/lmdeploy) 下载已经量化好的 4bit 权重模型,直接使用下面的命令推理。也可以根据["4bit 权重量化"](#4bit-权重量化)章节的内容,把 16bit 权重量化为 4bit 权重,然后再按下述说明推理
14+
15+
以 4bit 的 Llama-2-chat-7B 模型为例,可以从 model zoo 直接下载:
16+
17+
```shell
18+
git-lfs install
19+
git clone https://huggingface.co/lmdeploy/llama2-chat-7b-w4
20+
```
21+
22+
执行以下命令,即可在终端与模型对话:
23+
24+
```shell
25+
26+
## 转换模型的layout,存放在默认路径 ./workspace 下
27+
python3 -m lmdeploy.serve.turbomind.deploy \
28+
--model-name llama2 \
29+
--model-path ./llama2-chat-7b-w4 \
30+
--model-format awq \
31+
--group-size 128
32+
33+
## 推理
34+
python3 -m lmdeploy.turbomind.chat ./workspace
35+
```
36+
37+
## 启动 gradio 服务
38+
39+
如果想通过 webui 与模型对话,请执行以下命令启动 gradio 服务
40+
41+
```shell
42+
python3 -m lmdeploy.serve.turbomind ./workspace --server_name {ip_addr} ----server_port {port}
43+
```
44+
45+
然后,在浏览器中打开 http://{ip_addr}:{port},即可在线对话
46+
47+
## 推理速度
48+
49+
我们在 NVIDIA GeForce RTX 4090 上使用 [profile_generation.py](https://github.com/InternLM/lmdeploy/blob/main/benchmark/profile_generation.py),分别测试了 4-bit Llama-2-7B-chat 和 Llama-2-13B-chat 模型的 token 生成速度。测试配置为 batch size = 1,(prompt_tokens, completion_tokens) = (1, 512)
50+
51+
| model | llm-awq | mlc-llm | turbomind |
52+
| ---------------- | ------- | ------- | --------- |
53+
| Llama-2-7B-chat | 112.9 | 159.4 | 206.4 |
54+
| Llama-2-13B-chat | N/A | 90.7 | 115.8 |
55+
56+
上述两个模型的16bit 和 4bit 权重,分别使用 turbomind 推理时,各自在context size 为 2048 和 4096 配置下,所占的显存对比如下:
57+
58+
| model | 16bit(2048) | 4bit(2048) | 16bit(4096) | 4bit(4096) |
59+
| ---------------- | ----------- | ---------- | ----------- | ---------- |
60+
| Llama-2-7B-chat | 15.1 | 6.3 | 16.2 | 7.5 |
61+
| Llama-2-13B-chat | OOM | 10.3 | OOM | 12.0 |
62+
63+
```shell
64+
python benchmark/profile_generation.py \
65+
./workspace \
66+
--concurrency 1 --input_seqlen 1 --output_seqlen 512
67+
```
68+
69+
## 4bit 权重量化
70+
71+
4bit 权重量化包括 2 步:
72+
73+
- 生成量化参数
74+
- 根据量化参数,量化模型权重
75+
76+
### 第一步:生成量化参数
77+
78+
```shell
79+
python3 -m lmdeploy.lite.apis.calibrate \
80+
--model $HF_MODEL \
81+
--calib_dataset 'c4' \ # 校准数据集,支持 c4, ptb, wikitext2, pileval
82+
--calib_samples 128 \ # 校准集的样本数,如果显存不够,可以适当调小
83+
--calib_seqlen 2048 \ # 单条的文本长度,如果显存不够,可以适当调小
84+
--work_dir $WORK_DIR \ # 保存 Pytorch 格式量化统计参数和量化后权重的文件夹
85+
```
86+
87+
### 第二步:量化权重模型
88+
89+
LMDeploy 使用 AWQ 算法对模型权重进行量化。在执行下面的命令时,需要把步骤1的`$WORK_DIR`传入。量化结束后,权重文件也会存放在这个目录中。然后就可以根据 ["4bit权重模型推理"](#4bit-权重模型推理)章节的说明,进行模型推理。
90+
91+
```shell
92+
python3 -m lmdeploy.lite.apis.auto_awq \
93+
--model $HF_MODEL \
94+
--w_bits 4 \ # 权重量化的 bit 数
95+
--w_group_size 128 \ # 权重量化分组统计尺寸
96+
--work_dir $WORK_DIR \ # 步骤 1 保存量化参数的目录
97+
```

0 commit comments

Comments
 (0)