离线推理管道#

LMDeploy 将多模态视觉语言模型(VLM)的复杂推理过程抽象为一个易于使用的管道,类似于大型语言模型(LLM)推理的 pipeline

支持的模型列在这里。我们真诚地邀请社区为LMDeploy贡献新的VLM支持。您的参与将非常感激。

本文展示了使用liuhaotian/llava-v1.6-vicuna-7b模型作为案例研究的VLM管道。 您将了解利用管道的最简单方法,以及如何通过调整引擎参数和生成参数(如张量并行、上下文窗口大小、随机采样和聊天模板定制)逐步解锁更高级的功能。 此外,我们将提供针对多图像场景、批量提示等的实际推理示例。

使用管道接口推断其他VLM模型是类似的,主要区别在于模型的配置和安装依赖。您可以阅读这里了解不同模型的环境安装和配置方法。

一个‘Hello, world’示例#

from lmdeploy import pipeline
from lmdeploy.vl import load_image

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b')

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

如果在执行此案例时发生ImportError,请按照提示安装所需的依赖包。

在上面的例子中,推理提示是一个由 (prompt, image) 组成的元组结构。除了这种结构外,管道还支持 OpenAI 格式的提示:

from lmdeploy import pipeline

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b')

prompts = [
    {
        'role': 'user',
        'content': [
            {'type': 'text', 'text': 'describe this image'},
            {'type': 'image_url', 'image_url': {'url': 'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg'}}
        ]
    }
]
response = pipe(prompts)
print(response)

设置张量并行#

可以通过设置引擎参数tp来激活张量并行。

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

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b',
                backend_config=TurbomindEngineConfig(tp=2))

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

设置上下文窗口大小#

创建管道时,您可以通过设置引擎参数session_len来自定义上下文窗口的大小。

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

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b',
                backend_config=TurbomindEngineConfig(session_len=8192))

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

设置采样参数#

您可以通过传递GenerationConfig来更改管道的默认采样参数。

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

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b',
                backend_config=TurbomindEngineConfig(tp=2, session_len=8192))
gen_config = GenerationConfig(top_k=40, top_p=0.8, temperature=0.6)
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe(('describe this image', image), gen_config=gen_config)
print(response)

自定义图像令牌位置#

默认情况下,LMDeploy会根据上游算法仓库定义的聊天模板将特殊图像令牌插入用户提示中。然而,对于某些模型,如图像令牌位置不受限制的deepseek-vl,或者当用户需要自定义图像令牌位置时,需要手动将特殊图像令牌插入提示中。LMDeploy使用作为特殊图像令牌。

from lmdeploy import pipeline
from lmdeploy.vl import load_image
from lmdeploy.vl.constants import IMAGE_TOKEN

pipe = pipeline('deepseek-ai/deepseek-vl-1.3b-chat')

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

设置聊天模板#

在执行推理时,LMDeploy 会根据模型路径从其内置集合中识别出合适的聊天模板,并随后将此模板应用于输入提示。然而,当无法从模型路径中识别出聊天模板时,用户需要手动指定。例如,liuhaotian/llava-v1.5-7b 使用了 ‘llava-v1’ 聊天模板,如果用户有一个自定义的文件夹名称而不是官方的 ‘llava-v1.5-7b’,用户需要通过将 ‘llava-v1’ 设置为 ChatTemplateConfig 来指定它,如下所示:

from lmdeploy import pipeline, ChatTemplateConfig
from lmdeploy.vl import load_image
pipe = pipeline('local_model_folder',
                chat_template_config=ChatTemplateConfig(model_name='llava-v1'))
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe(('describe this image', image))
print(response)

有关自定义聊天模板的更多信息,请参考指南

设置视觉模型参数#

视觉模型的默认参数可以通过设置VisionConfig来修改。

from lmdeploy import pipeline, VisionConfig
from lmdeploy.vl import load_image
vision_config=VisionConfig(max_batch_size=16)
pipe = pipeline('liuhaotian/llava-v1.5-7b', vision_config=vision_config)
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe(('describe this image', image))
print(response)

计算logits#

我们提供对自定义输入的支持。用户可以利用‘prepare_inputs’来了解输入是如何组织的。

from lmdeploy import pipeline, TurbomindEngineConfig
from lmdeploy.vl import load_image
pipe = pipeline('internlm/internlm-xcomposer2-7b', backend_config=TurbomindEngineConfig(cache_max_entry_count=0.5))

# logits
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
inputs = pipe.prepare_inputs(('describe this image', image))
input_ids = inputs['input_ids']
embeddings = inputs['input_embeddings']
embedding_ranges = inputs['input_embedding_ranges']
logits = pipe.get_logits(input_ids, embeddings, embedding_ranges)

多图像推理#

处理多张图片时,您可以将它们全部放入一个列表中。请注意,多张图片会导致输入标记的数量增加,因此通常需要增加上下文窗口的大小。

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

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b',
                backend_config=TurbomindEngineConfig(session_len=8192))

image_urls=[
    'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/demo/resources/human-pose.jpg',
    'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/demo/resources/det.jpg'
]

images = [load_image(img_url) for img_url in image_urls]
response = pipe(('describe these images', images))
print(response)

批量提示推理#

使用批量提示进行推理非常简单;只需将它们放在列表结构中:

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

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b',
                backend_config=TurbomindEngineConfig(session_len=8192))

image_urls=[
    "https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/demo/resources/human-pose.jpg",
    "https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/demo/resources/det.jpg"
]
prompts = [('describe this image', load_image(img_url)) for img_url in image_urls]
response = pipe(prompts)
print(response)

多轮对话#

有两种方法可以使用管道进行多轮对话。一种是按照OpenAI的格式构建消息并使用上述介绍的方法,另一种是使用pipeline.chat接口。

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

pipe = pipeline('liuhaotian/llava-v1.6-vicuna-7b',
                backend_config=TurbomindEngineConfig(session_len=8192))

image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/demo/resources/human-pose.jpg')
gen_config = GenerationConfig(top_k=40, top_p=0.8, temperature=0.8)
sess = pipe.chat(('describe this image', image), gen_config=gen_config)
print(sess.response.text)
sess = pipe.chat('What is the woman doing?', session=sess, gen_config=gen_config)
print(sess.response.text)