快速开始#
本教程展示了LMDeploy在CUDA平台上的使用:
LLM模型和VLM模型的离线推理
通过OpenAI兼容服务器提供LLM或VLM模型服务
控制台CLI,用于与LLM模型进行交互式聊天
在继续阅读之前,请确保您已按照安装指南中的说明安装了lmdeploy。
离线批量推理#
LLM推理#
from lmdeploy import pipeline
pipe = pipeline('internlm/internlm2_5-7b-chat')
response = pipe(['Hi, pls intro yourself', 'Shanghai is'])
print(response)
在构建pipeline
时,如果没有在TurboMind引擎和PyTorch引擎之间指定推理引擎,LMDeploy将根据它们各自的能力自动分配一个,默认情况下TurboMind引擎优先。
然而,您可以选择手动选择一个引擎。例如,
from lmdeploy import pipeline, TurbomindEngineConfig
pipe = pipeline('internlm/internlm2_5-7b-chat',
backend_config=TurbomindEngineConfig(
max_batch_size=32,
enable_prefix_caching=True,
cache_max_entry_count=0.8,
session_len=8192,
))
或者,
from lmdeploy import pipeline, PytorchEngineConfig
pipe = pipeline('internlm/internlm2_5-7b-chat',
backend_config=PytorchEngineConfig(
max_batch_size=32,
enable_prefix_caching=True,
cache_max_entry_count=0.8,
session_len=8192,
))
注意
参数“cache_max_entry_count”显著影响GPU内存使用情况。 它表示在加载模型权重后,K/V缓存占用的空闲GPU内存的比例。
默认值为0.8。K/V缓存内存被一次性分配并重复使用,这就是为什么观察到内置管道和后面提到的“api_server”会消耗大量GPU内存的原因。
如果您遇到内存不足(OOM)错误,您可能需要考虑降低“cache_max_entry_count”的值。
当使用可调用的pipe()
来执行给定提示的令牌生成时,您可以通过GenerationConfig
设置采样参数,如下所示:
from lmdeploy import GenerationConfig, pipeline
pipe = pipeline('internlm/internlm2_5-7b-chat')
prompts = ['Hi, pls intro yourself', 'Shanghai is']
response = pipe(prompts,
gen_config=GenerationConfig(
max_new_tokens=1024,
top_p=0.8,
top_k=40,
temperature=0.6
))
在GenerationConfig
中,top_k=1
或temperature=0.0
表示贪婪搜索。
有关管道的更多信息,请阅读详细教程
VLM推理#
VLM推理管道的使用类似于LLMs,但增加了处理图像数据的能力。例如,您可以使用以下代码片段来使用InternVL模型进行推理:
from lmdeploy import pipeline
from lmdeploy.vl import load_image
pipe = pipeline('OpenGVLab/InternVL2-8B')
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe(('describe this image', image))
print(response)
在VLM管道中,默认的图像处理批次大小为1。这可以通过VisionConfig
进行调整。例如,您可以这样设置:
from lmdeploy import pipeline, VisionConfig
from lmdeploy.vl import load_image
pipe = pipeline('OpenGVLab/InternVL2-8B',
vision_config=VisionConfig(
max_batch_size=8
))
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe(('describe this image', image))
print(response)
然而,图像批量大小越大,发生OOM错误的风险就越大,因为VLM模型中的LLM组件会预先分配大量内存。
我们鼓励您根据TurboMind引擎和PyTorch引擎各自的性能手动选择,如支持模型矩阵中详细说明的。 此外,请按照LLM推理部分的说明减少与内存相关的参数值
服务#
如前一节离线批量推理所示,本部分介绍了LLMs和VLMs的相应服务方法。
服务一个LLM模型#
lmdeploy serve api_server internlm/internlm2_5-7b-chat
此命令将在本地主机的端口 23333
上启动一个与 OpenAI 兼容的服务器。您可以使用 --server-port
选项指定不同的服务器端口。
有关更多选项,请通过运行 lmdeploy serve api_server --help
查阅帮助文档。大多数这些选项与引擎配置一致。
要访问服务,您可以使用官方的OpenAI Python包pip install openai
。以下是一个示例,展示了如何使用入口点v1/chat/completions
from openai import OpenAI
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=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": " provide three suggestions about time management"},
],
temperature=0.8,
top_p=0.8
)
print(response)
我们鼓励您参考详细指南,以获取关于serving with Docker、function calls以及其他主题的更全面信息
服务一个VLM模型#
lmdeploy serve api_server OpenGVLab/InternVL2-8B
注意
LMDeploy 重用了上游 VLM 仓库的视觉组件。每个上游 VLM 模型可能有不同的依赖项。 因此,LMDeploy 决定不在其依赖列表中包含上游 VLM 仓库的依赖项。 如果您在使用 LMDeploy 进行 VLM 模型推理时遇到“ImportError”,请自行安装相关依赖项。
服务成功启动后,您可以通过修改api_key
和base_url
参数,以类似于访问gptv4
服务的方式访问VLM服务:
from openai import OpenAI
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=[{
'role':
'user',
'content': [{
'type': 'text',
'text': 'Describe the image please',
}, {
'type': 'image_url',
'image_url': {
'url':
'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg',
},
}],
}],
temperature=0.8,
top_p=0.8)
print(response)
使用命令行界面进行推理#
LMDeploy 提供了一个非常方便的 CLI 工具,供用户与本地 LLM 模型进行聊天。例如:
lmdeploy chat internlm/internlm2_5-7b-chat --backend turbomind
它旨在帮助用户检查和验证LMDeploy是否支持他们的模型,聊天模板是否正确应用,以及推理结果是否顺利交付。
另一个工具,lmdeploy check_env
,旨在收集基本的环境信息。在向我们报告问题时,这非常关键,因为它帮助我们更有效地诊断和解决问题。
如果您对它们的使用有任何疑问,可以尝试使用--help
选项来获取详细信息。