Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lmdeploy): README.md #148

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions inference-speed/GPU/lmdeploy_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,28 @@ lmdeploy 同样支持原始的 facebook 模型格式、支持 70B 模型分布

lmdeploy 实现了 kv cache int8 量化,同样的显存可以服务更多并发用户。

首先获取量化参数,结果保存到 fp16 转换好的 `workspace/triton_models/weights` 下,7B 模型也不需要 tensor parallel。
首先计算模型参数,结果是 pth 格式,保存到临时目录 minmax
```shell
mkdir minmax
python3 -m lmdeploy.lite.apis.calibrate \
--model /models/llama-2-7b-chat \ # huggingface llama2 模型。也支持 llama/vicuna/internlm/baichuan 等
--calib_dataset 'c4' \ # 校准数据集,支持 c4, ptb, wikitext2, pileval
--calib_samples 128 \ # 校准集的样本数,如果显存不够,可以适当调小
--calib_seqlen 2048 \ # 单条的文本长度,如果显存不够,可以适当调小
--work_dir minmax \ # 保存 pth 格式量化统计参数和量化后权重的文件夹
```

然后用 minmax 目录里的参数,计算量化参数,保存到 fp16 转换好的 `workspace/triton_models/weights` 下

```shell
python3 -m lmdeploy.lite.apis.kv_qparams \
--work_dir /models/llama-2-7b-chat \ # huggingface 格式模型
--work_dir minmax \ # 上一步计算的 minmax 结果
--turbomind_dir ./workspace/triton_models/weights \ # 结果保存目录
--kv_sym False \ # 用非对称量化
--num_tp 1 # tensor parallel GPU 个数
```

然后修改推理配置,开启 kv cache int8。编辑 `workspace/triton_models/weights/config.ini`
修改推理配置,开启 kv cache int8。编辑 `workspace/triton_models/weights/config.ini`
* 把 `use_context_fmha` 改为 0,表示关闭 flashattention
* 把 `quant_policy` 设为 4,表示打开 kv cache 量化

Expand All @@ -58,7 +69,7 @@ python3 -m lmdeploy.lite.apis.kv_qparams \
python3 -m lmdeploy.turbomind.chat ./workspace
```

[点击这里](https://github.com/InternLM/lmdeploy/blob/main/docs/zh_cn/quantization.md) 查看 kv cache int8 量化实现公式、精度和显存测试报告。
[点击这里](https://github.com/InternLM/lmdeploy/blob/main/docs/zh_cn/kv_int8.md) 查看 kv cache int8 量化实现公式、精度和显存测试报告。

## 四、weight int4 量化

Expand All @@ -70,13 +81,22 @@ lmdeploy 基于 [AWQ 算法](https://arxiv.org/abs/2306.00978) 实现了 weight
git clone https://huggingface.co/lmdeploy/llama2-chat-7b-w4
```

对于自己的模型,可以用`auto_awq`工具来优化,假设你的 huggingface 模型保存在 `/models/llama-2-7b-chat`
对于自己的模型,可以用`auto_awq`工具来优化
```shell
# 计算量化参数
python3 -m lmdeploy.lite.apis.calibrate \
--model $HF_MODEL \ # huggingface 模型位置
--calib_dataset 'c4' \ # 校准数据集,支持 c4, ptb, wikitext2, pileval
--calib_samples 128 \ # 校准集的样本数,如果显存不够,可以适当调小
--calib_seqlen 2048 \ # 单条的文本长度,如果显存不够,可以适当调小
--work_dir $WORK_DIR \ # 保存 Pytorch 格式量化统计参数和量化后权重的文件夹

# 量化模型
python3 -m lmdeploy.lite.apis.auto_awq \
--model /models/llama-2-7b-chat \
--model $HF_MODEL \ # huggingface 模型位置
--w_bits 4 \ # 权重量化的 bit 数
--w_group_size 128 \ # 权重量化分组统计尺寸
--work_dir ./llama2-chat-7b-w4 # 保存量化参数的目录
--work_dir $WORK_DIR \ # 上一条命令保存参数的目录
```


Expand Down