[InternLM训练营第二期笔记]5. LMDeploy 量化部署 LLM 实践

9 篇文章 0 订阅
9 篇文章 0 订阅
本文介绍了如何使用LMDeploy进行大模型的量化部署,包括剪枝、蒸馏和量化技术的应用,以及实战演示如何配置和优化Chat1.8B模型,使用KVCache提升性能,以及将模型部署为API服务和Gradio前端应用。
摘要由CSDN通过智能技术生成

该系列是上海AI Lab举行的书生 浦语大模型训练营的相关笔记部分。
该笔记是第五节课,学习大语言模型量化的基本概念,以及利用LMDeploy工具进行微调。


0. 模型部署的概念

0.0 背景

如果要将大模型在特定平台(大到服务器集群,小到端侧设备比如说手机等),都需要经过部署步骤。然而,LLM的模型计算量是非常大的,具体如下图:

在这里插入图片描述

对于LLM的推理过程,计算量大概是2倍的参数量,加2倍的模型层数 * 上下文长度 * 注意力维度,单位为FLOPs

C f o r w a r d = 2 N + 2 n l a y e r n c o n t e x t n a t t n d i m C_{forward} = 2N + 2 n_{layer} n_{context} n_{attn dim} Cforward=2N+2nlayerncontextnattndim

此外,不仅推理时的计算量大,而且存储模型参数本身的开销也很大:

在这里插入图片描述

此外,在GPU的推理过程中,访存速度远小于计算速度,因此存在访存性能瓶颈。也就是说,由于Transformer的参数量比较大,因此它是只能保存在GPU周围的显存中,而不是速度较快的缓存中,而从显存中访问的速度又远小于计算速度。

在这里插入图片描述

因此,针对以上三个难点,我们就必须用一定的部署手段,来减少计算量或占用资源量。目前主要有三种流派:

0.1 剪枝

剪枝是一个很成熟的技术,就是在于将模型中不重要的参数去掉:

在这里插入图片描述

0.2 蒸馏

在这里插入图片描述
蒸馏就是用一个教师模型去指导学生模型的训练,通常学生模型是小参数模型。

0.3 量化

在这里插入图片描述
量化就是将模型参数转换为特定位数的整数,例如原来是32位的fp32,可以量化成8位int。

那么量化为什么可以降低计算量呢?一种错误的观点是,认为量化后是int型,计算机计算int型所需要的时钟周期更少。然而,我们只是按照int型存储模型,在计算的时候,我们仍然需要将其反量化恢复成float。

量化的主要意义是,可以降低访存量,让访存量和计算量更加平衡。也就是降低了数据传输的时间,对访存量的需求。

1. 实战:配置LMDeploy和chat 1.8B模型对话(基础作业)

LMDeploy的核心功能如下:

在这里插入图片描述
首先还是创建开发机,GPU选择10% * A100. 注意选择CUDA12.2的版本。

随后创建环境:

studio-conda -t lmdeploy -o pytorch-2.1.2

安装LMDeploy:

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

然后我们创建本次实验的目录,并创建预训练模型的软链接

cd ~
mkdir lmdeploy

ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b /root/lmdeploy

随后,我们用Huggingface社区中推出的Transformer库运行一下chat1.8B模型,然后感受一下推理速度。

我们首先创建文件

cd ~/lmdeploy

vim pipeline_transformer.py

然后插入以下内容:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("/root/lmdeploy/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/lmdeploy/internlm2-chat-1_8b", torch_dtype=torch.float16, trust_remote_code=True).cuda()  
# 以fp16加载模型
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 activate lmdeploy

python /root/lmdeploy/pipeline_transformer.py

运行结果如下:

在这里插入图片描述
时间应该在大概5s左右

之后,我们再用LMDeploy进行对话:

lmdeploy chat /root/lmdeploy/internlm2-chat-1_8b

在这里插入图片描述
这个生成速度是秒生成,可以说是快多了

2. 实战:W4A16量化(进阶作业)

2.1 什么是KV cache

KV Cache是一种缓存技术,通过存储键值对的形式来复用计算结果,以达到提高性能和降低内存消耗的目的。在大规模训练和推理中,KV Cache可以显著减少重复计算量,从而提升模型的推理速度。理想情况下,KV Cache全部存储于显存,以加快访存速度。当显存空间不足时,也可以将KV Cache放在内存,通过缓存管理器控制将当前需要使用的数据放入显存。

模型在运行时,占用的显存可大致分为三部分:模型参数本身占用的显存、KV Cache占用的显存,以及中间运算结果占用的显存。

2.2 实践

直接运行

pip install einops==0.7.0

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

可以看到,新的模型被保存了:

在这里插入图片描述

随后,我们将KV Cache最大占用比例为0.4,运行:

lmdeploy chat /root/lmdeploy/internlm2-chat-1_8b-4bit --model-format awq --cache-max-entry-count 0.4

在这里插入图片描述
对于同样的问题,回答速度非常快,此时显存占用为4.9G

在这里插入图片描述

2.3 API Server部署

我们都是在本地直接推理大模型,这种方式成为本地部署。在生产环境下,我们有时会将大模型封装为API接口服务,供客户端访问。

在这里插入图片描述
运行

lmdeploy serve api_server -h

查看api server的用法。

在这里插入图片描述
因此和lmdeploy chat一样,更改kv缓存就直接增加--cache-max-entry-count命令。

我们启动API Server服务:注意,我们启动刚刚量化后的4bit模型,并且把模型格式改为awq, 把KV缓存占用比例改为0.4

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

出现以下界面后,说明server启动成功。
在这里插入图片描述

然后新建terminal,运行

conda activate lmdeploy
lmdeploy serve api_client http://localhost:23333

运行结果:
在这里插入图片描述

可以看到运行结果和直接命令行运行是一样的,期间显存占用4.9G

在这里插入图片描述

2.4 Gradio前端部署

接下来用Gradio网页端进行部署

运行

lmdeploy serve gradio http://localhost:23333 \
    --server-name 0.0.0.0 \
    --server-port 6006

然后在Windows的powershell中运行:

ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p 45344

运行结果:

在这里插入图片描述

2.5 Python代码集成

Python代码集成就是将大模型的推理部分集成到某些代码里面

这部分主要是借助于lmdeploy中的pipeline包完成的。

运行以下命令:

cd ~/lmdeploy
conda activate lmdeploy

vim pipeline.py

复制以下内容:其中注意,要将KV cache比例调成0.4

from lmdeploy import pipeline

pipe = pipeline('/root/lmdeploy/internlm2-chat-1_8b-4bit', cache_max_entry_count=0.4)
response = pipe(['Hi, pls intro yourself', '上海是'])
print(response)

随后运行

python pipeline.py

结果:

在这里插入图片描述

3. 实战:使用LMDeploy运行多模态大模型Llava

注意将GPU调大一点,调成30% * A100. 首先安装依赖库:

conda activate lmdeploy
pip install git+https://github.com/haotian-liu/LLaVA.git@4e2277a060da264c4f21b364c867cc622c945874

随后新建文件

cd ~/lmdeploy

vim pipeline_llava.py

复制以下内容:

from lmdeploy.vl import load_image
from lmdeploy import pipeline, TurbomindEngineConfig


backend_config = TurbomindEngineConfig(session_len=8192) # 图片分辨率较高时请调高session_len
# pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b', backend_config=backend_config) 非开发机运行此命令
pipe = pipeline('/share/new_models/liuhaotian/llava-v1.6-vicuna-7b', backend_config=backend_config)

image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe(('describe this image', image))
print(response)

运行结果:

python pipeline_llava.py

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值