LMDeploy多模态模型InternVL系列部署指南
前言
随着大模型技术的发展,多模态大模型正成为人工智能领域的重要研究方向。InternVL系列作为开源社区中表现优异的多模态大模型,在图像理解、视频分析等任务上展现出强大能力。本文将详细介绍如何使用LMDeploy高效部署InternVL系列多模态大模型。
InternVL系列模型概览
LMDeploy目前支持的InternVL系列模型包括多个版本,各版本特点如下:
| 模型名称 | 参数量范围 | 支持的推理引擎 | |---------|-----------|--------------| | InternVL | 13B-19B | TurboMind | | InternVL1.5 | 2B-26B | TurboMind, PyTorch | | InternVL2 | 4B | PyTorch | | InternVL2 | 1B-2B, 8B-76B | TurboMind, PyTorch | | InternVL2.5/2.5-MPO/3 | 1B-78B | TurboMind, PyTorch | | Mono-InternVL | 2B | PyTorch |
其中,TurboMind是LMDeploy提供的高性能推理引擎,相比原生PyTorch能提供更好的推理性能。
环境准备
基础安装
首先需要安装LMDeploy核心包,然后安装InternVL所需的额外依赖:
pip install timm
# 建议根据环境选择匹配的flash-attention预编译包
pip install flash-attn
flash-attn
是加速注意力计算的优化库,能显著提升大模型推理速度。
Docker环境(可选)
为简化环境配置,LMDeploy提供了预配置的Docker镜像:
docker build --build-arg CUDA_VERSION=cu12 -t openmmlab/lmdeploy:internvl . -f ./docker/InternVL_Dockerfile
如果CUDA版本低于12.4,使用cu11参数:
docker build --build-arg CUDA_VERSION=cu11 -t openmmlab/lmdeploy:internvl . -f ./docker/InternVL_Dockerfile
离线推理实践
基础图像理解
以下代码展示了如何使用InternVL2-8B进行单张图像描述:
from lmdeploy import pipeline
from lmdeploy.vl import load_image
# 初始化pipeline
pipe = pipeline('OpenGVLab/InternVL2-8B')
# 加载图像
image = load_image('https://example.com/tiger.jpeg')
# 生成描述
response = pipe((f'describe this image', image))
print(response)
多图像交互对话
InternVL支持同时处理多张图像并进行多轮对话:
from lmdeploy import pipeline, GenerationConfig
from lmdeploy.vl.constants import IMAGE_TOKEN
pipe = pipeline('OpenGVLab/InternVL2-8B', log_level='INFO')
# 第一轮对话:描述两张图像
messages = [
dict(role='user', content=[
dict(type='text', text=f'{IMAGE_TOKEN}{IMAGE_TOKEN}\nDescribe the two images.'),
dict(type='image_url', image_url=dict(url='image1.jpg')),
dict(type='image_url', image_url=dict(url='image2.jpg'))
])
]
out = pipe(messages, gen_config=GenerationConfig(top_k=1))
# 第二轮对话:比较图像异同
messages.append(dict(role='assistant', content=out.text))
messages.append(dict(role='user', content='Compare these two images.'))
out = pipe(messages, gen_config=GenerationConfig(top_k=1))
视频理解分析
InternVL还能处理视频内容,通过提取关键帧进行分析:
import numpy as np
from decord import VideoReader
from PIL import Image
def load_video(video_path, num_segments=8):
"""加载视频并提取关键帧"""
vr = VideoReader(video_path, ctx=cpu(0))
frame_indices = np.linspace(0, len(vr)-1, num_segments, dtype=int)
return [Image.fromarray(vr[i].asnumpy()) for i in frame_indices]
# 加载视频帧
video_frames = load_video('red-panda.mp4')
# 构建问题
question = ''.join([f'Frame{i+1}: {IMAGE_TOKEN}\n' for i in range(len(video_frames))])
question += 'What is the red panda doing?'
# 执行推理
content = [{'type': 'text', 'text': question}]
for frame in video_frames:
content.append({'type': 'image_url', 'image_url': {'url': f'data:image/jpeg;base64,{encode_image_base64(frame)}'}})
response = pipe([dict(role='user', content=content)])
print(response)
在线服务部署
基础API服务
启动InternVL2-8B的API服务:
lmdeploy serve api_server OpenGVLab/InternVL2-8B
服务启动后默认监听23333端口。
Docker部署
使用预构建的Docker镜像部署服务:
docker run --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
-p 23333:23333 \
openmmlab/lmdeploy:internvl \
lmdeploy serve api_server OpenGVLab/InternVL2-8B
Docker Compose部署
创建docker-compose.yml
文件:
version: '3.5'
services:
lmdeploy:
image: openmmlab/lmdeploy:internvl
ports:
- "23333:23333"
volumes:
- ~/.cache/huggingface:/root/.cache/huggingface
command: lmdeploy serve api_server OpenGVLab/InternVL2-8B
deploy:
resources:
reservations:
devices:
- driver: nvidia
capabilities: [gpu]
启动服务:
docker-compose up -d
性能优化建议
- 对于大模型(>8B),建议使用TurboMind引擎以获得更好的推理性能
- 视频处理时,适当调整
num_segments
参数平衡精度与速度 - 多图像处理时,注意显存占用,可分批处理
结语
本文详细介绍了如何使用LMDeploy部署InternVL系列多模态大模型,涵盖了从环境配置到实际应用的完整流程。LMDeploy提供的高效推理引擎和便捷的API服务,使得多模态大模型的应用部署变得更加简单高效。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考