书生作业:LMDeploy

发布于:2024-05-11 ⋅ 阅读:(156) ⋅ 点赞:(0)

自己随便说几句。

关于模型部署,很有趣的一件事就是,它一路随着深度学习训练一起发展,尽管例如tensorrt等工具的出现,不断试图降低部署门槛,但是实际上,每一次AI的升级,似乎让这个细分领域没有因为工具易用性的提高而萎缩,反而量化,蒸馏,各个细分领域开支散叶。你说未来的模型部署是什么样呢?

作业链接:Tutorial/lmdeploy/homework.md at camp2 · InternLM/Tutorial · GitHub

LLDeploy项目链接: GitHub - InternLM/lmdeploy: LMDeploy is a toolkit for compressing, deploying, and serving LLMs.

项目纵览

看一下repo的更新日志可以看到:

支持的模型种类很多,提到的就包括:

Qwen-7B, Qwen1.5-MOE and dbrx.Baichuan2-7B, Qwen 1.5, Gemma, Mistral, Mixtral, Deepseek-MOE, LLama 1--3

支持的量化方法也很多: AWQ 4bit GQA, W4A16 inference for sm_75 multi-model

性能优化的方式也很多: flash-attention2, dynamic NTK-RoPE scaling, dynamic logN scaling,Paged Attention, faster attention kernels without sequence length limitation, 2x faster KV8 kernels, Split-K decoding (Flash Decoding)

online int8/int4 KV cache quantization; Support VLM offline inference pipeline and serving;

Support DeepSeek-VL offline inference pipeline and serving.

部署的硬件支持也很多: multi-machine, multi-card inference
 真的够卷。据说MMDeploy基本不维护了,这个转型也是迅雷不及掩耳。

安装

可以直接通过pip安装,也可以通过源码安装,源码安装的流程明显更复杂。

参考: lmdeploy/docs/en/build.md at main · InternLM/lmdeploy · GitHub

conda activate lmdeploy
pip install lmdeploy[all]==0.3.0

离线demo的调用也很直接

import lmdeploy
pipe = lmdeploy.pipeline("internlm/internlm-chat-7b")
response = pipe(["Hi, pls intro yourself", "Shanghai is"])
print(response)

 或者通过:

lmdeploy chat /root/internlm2-chat-1_8b

进阶作业:

以API Server方式启动 lmdeploy,开启 W4A16量化,调整KV Cache的占用比例为0.4,分别使用命令行客户端与Gradio网页客户端与模型对话。

基于下列指令,我们可以启动serve

lmdeploy serve api_server internlm2-chat-1_8b --model-format hf\
--quant-policy 0\
--server-name 0.0.0.0\
--server-port 23333\
--tp 1

然后,我们可以尝试性能优化

程序默认cache-max-entry-count = 0.8

原始参数下,显存占用为 7800MB, 改成 0.4后,显存占用为6000MB。可以推测,KV cache的降低将减小模型的显存占用。

lmdeploy chat /root/internlm2-chat-1_8b --cache-max-entry-count 0.4

接下来,针对awq功能,我们进行量化优化。awq的更多细节可以参考:EfficientAI Lab: 大模型AWQ量化-CSDN博客

time lmdeploy lite auto_awq \
   /root/internlm2-chat-1_8b \
  --calib-dataset 'ptb' \
  --calib-samples 128 \
  --calib-seqlen 1024 \
  --w-bits 4 \
  --w-group-size 128 \
  --work-dir /root/internlm2-chat-1_8b-4bit

可以在目录中找到新生成的文件,并运行

lmdeploy chat /root/internlm2-chat-1_8b-4bit --model-format awq

值得注意的是,目前该方法只支持图灵架构及之后的设备,也就是20系列之后的显卡。


网站公告

今日签到

点亮在社区的每一天
去签到