Qwen2-VL#
LMDeploy 支持以下 Qwen-VL 系列模型,详细信息如下表所示:
模型 |
大小 |
支持的推理引擎 |
|---|---|---|
Qwen-VL-Chat |
- |
TurboMind |
Qwen2-VL |
2B, 7B |
PyTorch |
下一章将演示如何使用LMDeploy部署一个Qwen-VL模型,以Qwen2-VL-7B-Instruct为例。
安装#
请按照安装指南安装LMDeploy,并安装Qwen2-VL所需的其他包
pip install qwen_vl_utils
或者,您可以构建一个docker镜像来设置推理环境。如果您的宿主机上的CUDA版本是>=12.4,您可以运行:
git clone https://github.com/InternLM/lmdeploy.git
cd lmdeploy
docker build --build-arg CUDA_VERSION=cu12 -t openmmlab/lmdeploy:qwen2vl . -f ./docker/Qwen2VL_Dockerfile
否则,你可以选择:
docker build --build-arg CUDA_VERSION=cu11 -t openmmlab/lmdeploy:qwen2vl . -f ./docker/Qwen2VL_Dockerfile
离线推理#
以下示例代码展示了VLM管道的基本用法。有关详细信息,请参阅VLM离线推理管道
from lmdeploy import pipeline
from lmdeploy.vl import load_image
pipe = pipeline('Qwen/Qwen2-VL-2B-Instruct')
image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
response = pipe((f'describe this image', image))
print(response)
更多示例如下:
multi-image multi-round conversation, combined images
from lmdeploy import pipeline, GenerationConfig
pipe = pipeline('Qwen/Qwen2-VL-2B-Instruct', log_level='INFO')
messages = [
dict(role='user', content=[
dict(type='text', text='Describe the two images in detail.'),
dict(type='image_url', image_url=dict(url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Beijing_Small.jpeg')),
dict(type='image_url', image_url=dict(url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Chongqing_Small.jpeg'))
])
]
out = pipe(messages, gen_config=GenerationConfig(top_k=1))
messages.append(dict(role='assistant', content=out.text))
messages.append(dict(role='user', content='What are the similarities and differences between these two images.'))
out = pipe(messages, gen_config=GenerationConfig(top_k=1))
image resolution for performance boost
from lmdeploy import pipeline, GenerationConfig
pipe = pipeline('Qwen/Qwen2-VL-2B-Instruct', log_level='INFO')
min_pixels = 64 * 28 * 28
max_pixels = 64 * 28 * 28
messages = [
dict(role='user', content=[
dict(type='text', text='Describe the two images in detail.'),
dict(type='image_url', image_url=dict(min_pixels=min_pixels, max_pixels=max_pixels, url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Beijing_Small.jpeg')),
dict(type='image_url', image_url=dict(min_pixels=min_pixels, max_pixels=max_pixels, url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Chongqing_Small.jpeg'))
])
]
out = pipe(messages, gen_config=GenerationConfig(top_k=1))
messages.append(dict(role='assistant', content=out.text))
messages.append(dict(role='user', content='What are the similarities and differences between these two images.'))
out = pipe(messages, gen_config=GenerationConfig(top_k=1))
在线服务#
你可以通过lmdeploy serve api_server CLI启动服务器:
lmdeploy serve api_server Qwen/Qwen2-VL-2B-Instruct
你也可以使用上述构建的docker镜像来启动服务:
docker run --runtime nvidia --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HUGGING_FACE_HUB_TOKEN=<secret>" \
-p 23333:23333 \
--ipc=host \
openmmlab/lmdeploy:qwen2vl \
lmdeploy serve api_server Qwen/Qwen2-VL-2B-Instruct
Docker compose 是另一种选择。在 lmdeploy 项目的根目录下创建一个 docker-compose.yml 配置文件,如下所示:
version: '3.5'
services:
lmdeploy:
container_name: lmdeploy
image: openmmlab/lmdeploy:qwen2vl
ports:
- "23333:23333"
environment:
HUGGING_FACE_HUB_TOKEN: <secret>
volumes:
- ~/.cache/huggingface:/root/.cache/huggingface
stdin_open: true
tty: true
ipc: host
command: lmdeploy serve api_server Qwen/Qwen2-VL-2B-Instruct
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: "all"
capabilities: [gpu]
然后,您可以执行如下启动命令:
docker-compose up -d
如果你在运行docker logs -f lmdeploy后看到以下日志,这意味着服务已成功启动。
HINT: Please open http://0.0.0.0:23333 in a browser for detailed api usage!!!
HINT: Please open http://0.0.0.0:23333 in a browser for detailed api usage!!!
HINT: Please open http://0.0.0.0:23333 in a browser for detailed api usage!!!
INFO: Started server process [2439]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:23333 (Press CTRL+C to quit)
lmdeploy serve api_server 的参数可以通过 lmdeploy serve api_server -h 详细查看。
有关api_server的更多信息以及如何访问该服务,可以从这里找到