LMDeploy 量化部署 LLM-VLM 实践

参考文档:https://github.com/InternLM/Tutorial/blob/camp2/lmdeploy/README.md

作业一:1.配置 LMDeploy 运行环境

InternStudio开发机创建conda环境
由于环境依赖项存在torch,下载过程可能比较缓慢。InternStudio上提供了快速创建conda环境的方法。
打开命令行终端,创建一个名为lmdeploy的环境:
studio-conda -t lmdeploy -o pytorch-2.1.2

接下来,激活刚刚创建的虚拟环境。

conda activate lmdeploy

安装0.3.0版本的lmdeploy。

pip install lmdeploy[all]==0.3.0

等待安装就可以了:

2.LMDeploy模型对话(chat)

Transformer来直接运行InternLM2-Chat-1.8B模型:

我们已经下载好了InternLM2-Chat-1.8B的HF模型。下面我们先用Transformer来直接运行InternLM2-Chat-1.8B模型,后面对比一下LMDeploy

在终端中输入如下指令,新建pipeline_transformer.py

touch /root/pipeline_transformer.py

回车执行指令,可以看到侧边栏多出了pipeline_transformer.py文件,点击打开。后文中如果要创建其他新文件,也是采取类似的操作。

将以下内容复制粘贴进入pipeline_transformer.py

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("/root/internlm2-chat-1_8b", trust_remote_code=True)

# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and cause OOM Error.
model = AutoModelForCausalLM.from_pretrained("/root/internlm2-chat-1_8b", torch_dtype=torch.float16, trust_remote_code=True).cuda()
model = model.eval()

inp = "hello"
print("[INPUT]", inp)
response, history = model.chat(tokenizer, inp, history=[])
print("[OUTPUT]", response)

inp = "please provide three suggestions about time management"
print("[INPUT]", inp)
response, history = model.chat(tokenizer, inp, history=history)
print("[OUTPUT]", response)

回到终端,激活conda环境。

conda activate lmdeploy
运行python代码:
python /root/pipeline_transformer.py
得到输出:

使用LMDeploy与模型对话(注意这里要按两下回车)

先激活创建好的conda环境:

conda activate lmdeploy

使用LMDeploy与模型进行对话的通用命令格式为:

lmdeploy chat [HF格式模型路径/TurboMind格式模型路径]

例如,您可以执行如下命令运行下载的1.8B模型:

lmdeploy chat /root/internlm2-chat-1_8b

进阶作业:

设置KV Cache最大占用比例为0.4,开启W4A16量化,以命令行方式与模型对话

量化是一种以参数或计算中间结果精度下降换空间节省(同时带来性能提升)

首先保持不加该参数(默认0.8),运行1.8B模型。(从图中可以看出显存占用为7856MB)

lmdeploy chat /root/internlm2-chat-1_8b

把参数改为0.4后显存占用明显降低(变成6052MB)

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

 使用W4A16量化(量化后显存明显占比降低)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值