LMDeploy 多模态模型 InternVL 部署指南

LMDeploy 多模态模型 InternVL 部署指南

lmdeploy LMDeploy is a toolkit for compressing, deploying, and serving LLMs. lmdeploy 项目地址: https://gitcode.com/gh_mirrors/lm/lmdeploy

引言

随着多模态大模型的快速发展,视觉语言模型(Vision-Language Model, VLM)已成为人工智能领域的重要研究方向。InternVL 系列模型作为其中的佼佼者,在图像理解、视频分析等任务上表现出色。本文将详细介绍如何使用 LMDeploy 高效部署 InternVL 系列多模态大模型。

InternVL 模型概览

LMDeploy 目前支持 InternVL 系列多个版本的模型,包括:

  • InternVL:13B-19B 参数规模,支持 TurboMind 推理引擎
  • InternVL1.5:2B-26B 参数规模,支持 TurboMind 和 PyTorch
  • InternVL2:4B 版本仅支持 PyTorch,1B-2B 和 8B-76B 版本支持双引擎
  • InternVL2.5/2.5-MPO/3:1B-78B 参数范围,支持双引擎
  • Mono-InternVL:2B 参数,仅支持 PyTorch

不同规模的模型适用于不同的硬件环境和应用场景,用户可根据实际需求选择合适的版本。

环境准备

基础依赖安装

部署 InternVL 模型前,需要安装必要的依赖:

pip install timm
# 根据环境选择合适的 flash-attention 版本
pip install flash-attn

Docker 环境构建(可选)

对于需要容器化部署的场景,LMDeploy 提供了专门的 Dockerfile:

  1. CUDA 12.4 及以上环境:
docker build --build-arg CUDA_VERSION=cu12 -t openmmlab/lmdeploy:internvl . -f ./docker/InternVL_Dockerfile
  1. CUDA 11 环境:
docker build --build-arg CUDA_VERSION=cu11 -t openmmlab/lmdeploy:internvl . -f ./docker/InternVL_Dockerfile

离线推理实践

基础图像描述

使用 pipeline 进行单图描述是最基础的应用场景:

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=32):
    """加载视频并提取关键帧"""
    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('demo.mp4', num_segments=8)

# 构建问题
question = ''.join([f'Frame{i+1}: {IMAGE_TOKEN}\n' for i in range(len(video_frames))])
question += 'What is happening in this video?'

# 发送请求
content = [{'type': 'text', 'text': question}]
for frame in video_frames:
    content.append({'type': 'image_url', 'image_url': {'url': encode_image_base64(frame)}})

response = pipe([dict(role='user', content=content)])

在线服务部署

基础 API 服务

启动基础 API 服务:

lmdeploy serve api_server OpenGVLab/InternVL2-8B

Docker 部署方案

  1. 直接运行:
docker run --gpus all -p 23333:23333 \
    -v ~/.cache/huggingface:/root/.cache/huggingface \
    openmmlab/lmdeploy:internvl \
    lmdeploy serve api_server OpenGVLab/InternVL2-8B
  1. 使用 Docker Compose(推荐):
version: '3.5'
services:
  lmdeploy:
    image: openmmlab/lmdeploy:internvl
    ports: ["23333:23333"]
    volumes: ["~/.cache/huggingface:/root/.cache/huggingface"]
    ipc: host
    command: lmdeploy serve api_server OpenGVLab/InternVL2-8B
    deploy:
      resources:
        reservations:
          devices: [{driver: nvidia, count: "all", capabilities: [gpu]}]

启动后可通过 http://localhost:23333 访问 API 服务。

性能优化建议

  1. 批处理:对于多图场景,尽量使用批处理提高吞吐量
  2. 动态分块:调整 max_dynamic_patch 参数平衡计算效率和内存占用
  3. 量化部署:考虑使用 LMDeploy 的量化功能减少显存占用
  4. 缓存机制:对重复图像使用缓存避免重复计算

结语

LMDeploy 为 InternVL 系列模型提供了高效的部署方案,无论是离线推理还是在线服务都能满足不同场景需求。通过本文介绍的方法,开发者可以快速搭建多模态应用,实现图像理解、视频分析等高级功能。随着模型不断迭代,建议持续关注 LMDeploy 的最新支持情况。

lmdeploy LMDeploy is a toolkit for compressing, deploying, and serving LLMs. lmdeploy 项目地址: https://gitcode.com/gh_mirrors/lm/lmdeploy

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤力赛Frederica

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值