LMDeploy 量化部署实践闯关任务

tmux使用:

由于程序运行时间较长,推荐使用tmux

apt-get install tmux

创建新的tmux会话:

tmux new -s lmdeploy
#第一次连接
tmux a -t lmdeploy

断开tmux会话:按住ctrl+b,然后d键,可以从tmux会话中断开,但会话和其中的程序会继续运行。列出可用的tmux会话

tmux ls

重新附加到tmux会话

tmux attach -t lmdeploy

InternStudio环境获取模型

mkdir /root/models
ln -s /root/share/new_models//Shanghai_AI_Laboratory/internlm2_5-7b-chat /root/models
ln -s /root/share/new_models/OpenGVLab/InternVL2-26B /root/models

进入创建好的conda环境并启动InternLM2_5-7b-chat!

此时,我们可以在CLI(“命令行界面” Command Line Interface的缩写)中和InternLM2.5尽情对话了,注意输入内容完成后需要按两次回车才能够执行

LMDeploy API部署InternLM2.5

实际应用中,我们有时会将大模型封装为API接口服务,供客户端访问,启动API服务器,首先让我们进入创建好的conda环境,并通下命令启动API服务器,部署InternLM2.5模型:

conda activate lmdeploy
lmdeploy serve api_server \
    /root/models/internlm2_5-7b-chat \
    --model-format hf \
    --quant-policy 0 \
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

以命令行形式连接API服务器

以Gradio网页形式连接API服务器

大模型压缩技术:

随着模型变得越来越大,我们需要一些大模型压缩技术来降低模型部署的成本,并提升模型的推理性能。LMDeploy 提供了权重量化和 k/v cache两种策略。

 设置最大kv cache缓存大小

模型在运行时,占用的显存可大致分为三部分:模型参数本身占用的显存、kv cache占用的显存,以及中间运算结果占用的显存。LMDeploy的kv cache管理器可以通过设置--cache-max-entry-count参数,控制kv缓存占用剩余显存的最大比例。默认的比例为0.8。

如何使用:

lmdeploy chat /root/models/internlm2_5-7b-chat --cache-max-entry-count 0.4

设置在线 kv cache int4/int8 量化

可以看到此时显存占用约19GB,相较于 LMDeploy验证启动模型文件直接启动模型的显存占用情况(23GB)减少了4GB的占用。此时4GB显存的减少逻辑与设置最大kv cache缓存大小中4GB显存的减少一致,均因设置kv cache占用参数cache-max-entry-count至0.4而减少了4GB显存占用。

19GB的显存占用与2.2.1 设置最大kv cache缓存大小19GB的显存占用区别:

由于都使用BF16精度下的internlm2.5 7B模型,故剩余显存均为10GB,且 cache-max-entry-count 均为0.4,这意味着LMDeploy将分配40%的剩余显存用于kv cache,即10GB*0.4=4GB。但quant-policy 设置为4时,意味着使用int4精度进行量化。因此,LMDeploy将会使用int4精度提前开辟4GB的kv cache。相比使用BF16精度的kv cache,int4的Cache可以在相同4GB的显存下只需要4位来存储一个数值,而BF16需要16位。这意味着int4的Cache可以存储的元素数量是BF16的四倍。

W4A16 模型量化和部署:

  • W4:这通常表示权重量化为4位整数(int4)。这意味着模型中的权重参数将从它们原始的浮点表示(例如FP32、BF16或FP16,Internlm2.5精度为BF16)转换为4位的整数表示。这样做可以显著减少模型的大小。
  • A16:这表示激活(或输入/输出)仍然保持在16位浮点数(例如FP16或BF16)。激活是在神经网络中传播的数据,通常在每层运算之后产生。
  • 因此,W4A16的量化配置意味着:权重被量化为4位整数。激活保持为16位浮点数。

使用1.8B模型进行量化:

在最新的版本中,LMDeploy使用的是AWQ算法,能够实现模型的4bit权重量化。

lmdeploy lite auto_awq \
   /root/models/internlm2_5-1_8b-chat \
  --calib-dataset 'ptb' \
  --calib-samples 128 \
  --calib-seqlen 2048 \
  --w-bits 4 \
  --w-group-size 128 \
  --batch-size 1 \
  --search-scale False \
  --work-dir /root/models/internlm2_5-1_8b-chat-w4a16-4bit

问题:

无法找到模型:在models文件夹中进行一次软链接。

ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2_5-1_8b-chat /root/models

报错:ValueError: The repository for ptb_text_only contains custom code which must be executed to correctly load the dataset. You can inspect the repository content at https://hf.co/datasets/ptb_text_only.
Please pass the argument `trust_remote_code=True` to allow custom code to be run.

分析:4bit量化时ptb_text_only在连接huggingface时无法下载

解决方法:

终端输入

export HF_ENDPOINT=https://hf-mirror.com

#查看在当前目录中显示所有子目录的大小
cd /root/models/
du -sh *

#原模型大小
cd /root/share/new_models/Shanghai_AI_Laboratory/
du -sh *

模型大小由原来的15G到1.5G

那么显存占用情况对比呢?输入以下指令启动量化后的模型。

lmdeploy chat /root/models/internlm2_5-7b-chat-w4a16-4bit/ --model-format awq

报错:

分析:模型使用错误

解决措施:

lmdeploy chat /root/models/internlm2_5-1_8b-chat-w4a16-4bit --model-format awq

 W4A16 量化+ KV cache+KV cache 量化

lmdeploy serve api_server \
    /root/models/internlm2_5-1_8b-chat-w4a16-4bit/ \
    --model-format awq \
    --quant-policy 4 \
    --cache-max-entry-count 0.4\
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

输入以下指令,让我们同时启用量化后的模型、设定kv cache占用和kv cache int4量化。

lmdeploy serve api_server \
    /root/models/internlm2_5-7b-chat-w4a16-4bit/ \
    --model-format awq \
    --quant-policy 4 \
    --cache-max-entry-count 0.4\
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

LMDeploy与InternVL2

针对InternVL系列模型,让我们先进入conda环境,并输入以下指令,执行模型的量化工作。(本步骤耗时较长,请耐心等待)

conda activate lmdeploy
lmdeploy lite auto_awq \
   /root/models/InternVL2-26B \
  --calib-dataset 'ptb' \
  --calib-samples 128 \
  --calib-seqlen 2048 \
  --w-bits 4 \
  --w-group-size 128 \
  --batch-size 1 \
  --search-scale False \
  --work-dir /root/models/InternVL2-26B-w4a16-4bit

报错:FlashAttention is not installed.

解决:pip install flash_attn

(注意版本匹配问题)

W4A16 量化+ KV cache+KV cache 量化

输入以下指令,让我们启用量化后的模型。

lmdeploy serve api_server \
    /root/models/InternVL2-26B-w4a16-4bit \
    --model-format awq \
    --quant-policy 4 \
    --cache-max-entry-count 0.1\
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

LMDeploy API部署InternVL2

lmdeploy serve api_server \
    /root/models/InternVL2-26B-w4a16-4bit/ \
    --model-format awq \
    --quant-policy 4 \
    --cache-max-entry-count 0.1 \
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

LMDeploy之FastAPI与Function call

API开发

conda activate lmdeploy
lmdeploy serve api_server \
    /root/models/internlm2_5-1_8b-chat-w4a16-4bit \
    --model-format awq \
    --cache-max-entry-count 0.4 \
    --quant-policy 4 \
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

此时代表我们成功地使用本地API与大模型进行了一次对话,如果切回第一个终端窗口,会看到如下信息,这代表其成功的完成了一次用户问题GET与输出POST。

Function call

conda activate lmdeploy
lmdeploy serve api_server \
    /root/models/internlm2_5-1_8b-chat \
    --model-format hf \
    --quant-policy 0 \
    --server-name 0.0.0.0 \
    --server-port 23333 \
    --tp 1

  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值