书生大模型实战营第4期——进阶篇3 LMDeploy

301 阅读6分钟

1 环境安装

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

pip install datasets==2.19.2

创建共享目录权重的软连接:

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

2 LMDeploy & InternLM2.5

2.1 kv-cache

先测试是否能和正常对话(输入prompt后按两次回车):

lmdeploy chat /root/models/internlm2_5-1_8b-chat

image.png

image.png

可以看到默认设置显存占用20g:

image.png

默认参数cache-max-entry-count为0.8,kv cache占据剩余显存的80%。当前是1.8B的模型,占3.6G显存,剩余20G左右,kv cache占用80%为16G,加上模型显存刚好20g左右。

接下来试试cache-max-entry-count=0.3,总的显存应该不超过10g:

lmdeploy chat /root/models/internlm2_5-1_8b-chat --cache-max-entry-count 0.3 

1734515296822.png

结果和估算的差不多,推理的速度显著变快了。

2.2 kv cache量化

使用LMDeploy对kv cache进行在线量化,只需要指定quant_policy参数,quant_policy=4表示int4量化,quant_policy=8为int8量化,其中int4需要四位,默认的bf16要16位,int4能够存的元素数量是bf16的四倍。(quant_policy不会改变显存的占用,显存占用和模型权重大小,和cache-max-entry-count百分比有关系,quant_policy设置后在预留的kv cache大小下装了更多的内容。)

lmdeploy chat /root/models/internlm2_5-1_8b-chat --cache-max-entry-count 0.3 --quant-policy 4

image.png

2.3 W4A16量化

接下来我们进行awq量化,2.2是对kv cache进行在线量化,还可以对模型的权重进行量化,减小模型占用的显存从而提升运行速度。 这里使用W4A16量化方法,其中W表示权重,使用int4进行量化,体积缩小4倍;A16是指对激活值(输入、输出)保持bf16格式。

下面的命令是对internlm2_5-1_8b-chat进行离线量化,下次可以直接使用量化权重internlm2_5-1_8b-chat-w4a16-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

image.png

awq结束后结果如下,1.8B共24层,平均每层需要2分钟,总共1h左右。 1734520533524.png

接下来使用awq版本的权重进行chat,默认kv cache占80%

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

image.png

模型权重0.9G,剩余显存23.1G,kv cache占用80%为18.48G,权重+kv cache为19.38G,还有其他项有0.84G左右。注意激活值还是bf16的,实际显存占用会比1/4大些。

在测试一组,除了模型权重外,其他参数与2.2的最后一个例子保持一致:

lmdeploy chat  /root/models/internlm2_5-1_8b-chat-w4a16-4bit --model-format awq --cache-max-entry-count 0.3  --quant-policy 4

image.png

显存为8.9G,比2.2最后一个例子要少1G左右。模型权重0.9G,剩余显存23.1G,kv cache占用30%为6.93G,权重+kv cache为7.83G,还有其他项有1.1G左右。

2.4 W4A16 量化+ KV cache+KV cache 量化

接下来使用api的形式部署量化的internlm2_5-1_8b,并且使用kv-cache百分比为0.4%(cache-max-entry-count 0.4),并且开启kv-cache int4量化(quant-policy 4):

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

3 API调用 & Function call

3.1 API调用

LMDeploy使用fastapi封装了LLM服务,可以通过openai包进行调用:

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

使用下面的client代码调用部署的api:

# 导入openai模块中的OpenAI类,这个类用于与OpenAI API进行交互
from openai import OpenAI


# 创建一个OpenAI的客户端实例,需要传入API密钥和API的基础URL
client = OpenAI(
    api_key='YOUR_API_KEY',  
    # 替换为你的OpenAI API密钥,由于我们使用的本地API,无需密钥,任意填写即可
    base_url="http://0.0.0.0:23333/v1"  
    # 指定API的基础URL,这里使用了本地地址和端口
)

# 调用client.models.list()方法获取所有可用的模型,并选择第一个模型的ID
# models.list()返回一个模型列表,每个模型都有一个id属性
model_name = client.models.list().data[0].id

# 使用client.chat.completions.create()方法创建一个聊天补全请求
# 这个方法需要传入多个参数来指定请求的细节
response = client.chat.completions.create(
  model=model_name,  
  # 指定要使用的模型ID
  messages=[  
  # 定义消息列表,列表中的每个字典代表一个消息
    {"role": "system", "content": "你是一个友好的小助手,负责解决问题."},  
    # 系统消息,定义助手的行为
    {"role": "user", "content": "帮我讲述一个关于狐狸和西瓜的小故事"},  
    # 用户消息,询问时间管理的建议
  ],
    temperature=0.8,  
    # 控制生成文本的随机性,值越高生成的文本越随机
    top_p=0.8  
    # 控制生成文本的多样性,值越高生成的文本越多样
)

# 打印出API的响应结果
print(response.choices[0].message.content)

调用结果如下:

image.png

3.2 函数调用

Function call即函数调用功能,它允许开发者在调用模型时,详细说明函数的作用,并使模型能够智能地根据用户的提问来输入参数并执行函数。完成调用后,模型会将函数的输出结果作为回答用户问题的依据。

接下来测试一个简单的"加"与"乘"函数调用,模型给出需要调用的函数和参数后,调用函数执行结果,并且把结果追加到历史里,模型根据计算结果给出最终答案:

from openai import OpenAI


def add(a: int, b: int):
    return a + b


def mul(a: int, b: int):
    return a * b


tools = [{
    'type': 'function',
    'function': {
        'name': 'add',
        'description': 'Compute the sum of two numbers',
        'parameters': {
            'type': 'object',
            'properties': {
                'a': {
                    'type': 'int',
                    'description': 'A number',
                },
                'b': {
                    'type': 'int',
                    'description': 'A number',
                },
            },
            'required': ['a', 'b'],
        },
    }
}, {
    'type': 'function',
    'function': {
        'name': 'mul',
        'description': 'Calculate the product of two numbers',
        'parameters': {
            'type': 'object',
            'properties': {
                'a': {
                    'type': 'int',
                    'description': 'A number',
                },
                'b': {
                    'type': 'int',
                    'description': 'A number',
                },
            },
            'required': ['a', 'b'],
        },
    }
}]
messages = [{'role': 'user', 'content': 'Compute (3+5)*2'}]

client = OpenAI(api_key='YOUR_API_KEY', base_url='http://0.0.0.0:23333/v1')
model_name = client.models.list().data[0].id
response = client.chat.completions.create(
    model=model_name,
    messages=messages,
    temperature=0.8,
    top_p=0.8,
    stream=False,
    tools=tools)
print(response)
func1_name = response.choices[0].message.tool_calls[0].function.name
func1_args = response.choices[0].message.tool_calls[0].function.arguments
func1_out = eval(f'{func1_name}(**{func1_args})')
print(func1_out)

messages.append({
    'role': 'assistant',
    'content': response.choices[0].message.content
})
messages.append({
    'role': 'environment',
    'content': f'3+5={func1_out}',
    'name': 'plugin'
})
response = client.chat.completions.create(
    model=model_name,
    messages=messages,
    temperature=0.8,
    top_p=0.8,
    stream=False,
    tools=tools)
print(response)
func2_name = response.choices[0].message.tool_calls[0].function.name
func2_args = response.choices[0].message.tool_calls[0].function.arguments
func2_out = eval(f'{func2_name}(**{func2_args})')
print(func2_out)

image.png