在终端中,让我们输入以下指令,来创建一个名为lmdeploy的conda环境,python版本为3.10,创建成功后激活环境并安装0.5.3版本的lmdeploy及相关包。
conda create -n lmdeploy python=3.10 -y conda activate lmdeploy conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y pip install timm==1.0.8 openai==1.40.3 lmdeploy[all]==0.5.3
InternStudio环境获取模型
为方便文件管理,我们需要一个存放模型的目录,本教程统一放置在/root/models/
目录。
运行以下命令,创建文件夹并设置开发机共享目录的软链接。
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
此时,我们可以看到/root/models
中会出现internlm2_5-7b-chat
和InternVL2-26B
文件夹。
LMDeploy验证启动模型文件
在量化工作正式开始前,我们还需要验证一下获取的模型文件能否正常工作,以免竹篮打水一场空。
让我们进入创建好的conda环境并启动InternLM2_5-7b-chat!
conda activate lmdeploy
lmdeploy chat /root/models/internlm2_5-7b-chat
稍待片刻,启动成功后,会显示如下。
2 LMDeploy与InternLM2.5
2.1 LMDeploy API部署InternLM2.5
在上一章节,我们直接在本地部署InternLM2.5。而在实际应用中,我们有时会将大模型封装为API接口服务,供客户端访问。
2.1.1 启动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服务器
关闭http://127.0.0.1:23333
网页,但保持终端和本地窗口不动,按箭头操作新建一个终端。
运行如下命令,激活conda环境并启动命令行客户端。
conda activate lmdeploy
lmdeploy serve api_client http://localhost:23333
稍待片刻,等出现double enter to end input >>>
的输入提示即启动成功,此时便可以随意与InternLM2.5对话,同样是两下回车确定,输入exit
退出。
2.1.3 以Gradio网页形式连接API服务器
保持第一个终端不动,在新建终端中输入exit
退出。
输入以下命令,使用Gradio作为前端,启动网页。
lmdeploy serve gradio http://localhost:23333 \
--server-name 0.0.0.0 \
--server-port 6006
我使用的是50%A100。
占用了23GB,那么试一试执行以下命令,再来观看占用显存情况。
lmdeploy chat /root/models/internlm2_5-7b-chat --cache-max-entry-count 0.4
稍待片刻,观测显存占用情况,可以看到减少了约4GB的显存。
2.2.2 设置在线 kv cache int4/int8 量化
我们通过2.1 LMDeploy API部署InternLM2.5的实践为例,输入以下指令,启动API服务器。
lmdeploy serve api_server \
/root/models/internlm2_5-7b-chat \
--model-format hf \
--quant-policy 4 \
--cache-max-entry-count 0.4\
--server-name 0.0.0.0 \
--server-port 23333 \
--tp 1
2.2.3 W4A16 模型量化和部署
让我们回到LMDeploy,在最新的版本中,LMDeploy使用的是AWQ算法,能够实现模型的4bit权重量化。输入以下指令,执行量化工作。(本步骤耗时较长,请耐心等待)
lmdeploy lite auto_awq \
/root/models/internlm2_5-7b-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-7b-chat-w4a16-4bit
等待推理完成,便可以直接在你设置的目标文件夹看到对应的模型文件。
那么推理后的模型和原本的模型区别在哪里呢?最明显的两点是模型文件大小以及占据显存大小。
我们可以输入如下指令查看在当前目录中显示所有子目录的大小。
cd /root/models/
du -sh *
原模型大小输入以下指令查看。
cd /root/share/new_models/Shanghai_AI_Laboratory/
du -sh *
那么显存占用情况对比呢?输入以下指令启动量化后的模型。
lmdeploy chat /root/models/internlm2_5-7b-chat-w4a16-4bit/ --model-format awq
我使用的是50%A100。
输入以下指令,让我们同时启用量化后的模型、设定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
3.2 LMDeploy API部署InternVL2
4 LMDeploy之FastAPI与Function call
之前在2.1.1 启动API服务器与3.2 LMDeploy API部署InternVL2均是借助FastAPI封装一个API出来让LMDeploy自行进行访问,在这一章节中我们将依托于LMDeploy封装出来的API进行更加灵活更具DIY的开发。
4.1 API开发
4.2 Function call
关于Function call,即函数调用功能,它允许开发者在调用模型时,详细说明函数的作用,并使模型能够智能地根据用户的提问来输入参数并执行函数。完成调用后,模型会将函数的输出结果作为回答用户问题的依据。
首先让我们进入创建好的conda环境并启动API服务器。
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
目前LMDeploy在0.5.3版本中支持了对InternLM2, InternLM2.5和llama3.1这三个模型,故我们选用InternLM2.5 封装API。
让我们使用一个简单的例子作为演示。输入如下指令,新建internlm2_5_func.py
。
touch /root/internlm2_5_func.py