InternVL3_5-38B-Flash快速上手指南:10分钟跑通你的第一次多模态对话(附完整代码)

原创2026-10-18 20:06:282,189 阅读

InternVL3_5-38B-Flash快速上手指南:10分钟跑通你的第一次多模态对话(附完整代码)

InternVL3_5-38B-Flash 是 OpenGVLab 开源的 InternVL3.5 系列多模态大模型的轻量高效版本,基于 38B 参数规模(视觉编码器 5.5B + 语言模型 32.8B),支持图像理解、视频理解、多轮对话与推理任务。它通过**视觉分辨率路由器(ViR)**将视觉 token 减少 50%,同时保持近乎 100% 的原版性能,是资源受限场景下跑通多模态对话的理想选择。本文将带你用 10 分钟完成环境准备并跑通你的第一次多模态对话。


一、InternVL3_5-38B-Flash 是什么?

InternVL3.5 系列沿用 "ViT–MLP–LLM" 架构范式:视觉编码器采用 InternViT-6B,语言模型基于 Qwen3-32B。Flash 版本在此基础上新增了 Visual Resolution Router(视觉分辨率路由器):

版本 每个图像 patch 的 token 数 视觉 token 开销
InternVL3.5-38B 1024 → 压缩至 256 基准
InternVL3.5-38B-Flash 1024 → 按块路由至 256 或 64 减少约 50%

路由器会根据每个图像块的语义丰富程度动态选择压缩率:简单区域用 64 token,复杂区域保留 256 token。在模型配置中可以直接看到相关开关:

"flash_mode": true,
"flash_relative_threshold": 0.1,
"flash_absolute_threshold": 0.5

这些字段位于 config.json 中,也体现在核心模型的推理逻辑里,见 modeling_internvl_chat.py 中的路由选择实现。

📌 模型核心组件说明:


二、硬件需求与环境准备

1. 硬件要求

模型 最低显存配置
30B 及以下 单卡 A100
38B(本文主角) 双卡 A100
241B-A28B 八卡 A100

💡 如果你的显存不足,可以使用 8-bit 量化(load_in_8bit=True)或换用 14B 等更小的版本。

2. 下载模型

git clone https://gitcode.com/OpenGVLab/InternVL3_5-38B-Flash

3. 安装依赖

pip install transformers>=4.52.1 torch torchvision

⚠️ 请务必使用 transformers>=4.52.1,否则模型可能无法正常工作。


三、5 步跑通多模态对话

第 1 步:加载模型

38B 模型需要双卡,使用 device_map="auto" 自动分片即可,无需手动指定 GPU:

import torch
from transformers import AutoTokenizer, AutoModel

path = "OpenGVLab/InternVL3_5-38B-Flash"   # 可换成本地目录路径
model = AutoModel.from_pretrained(
    path,
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=True,
    use_flash_attn=True,
    trust_remote_code=True,
    device_map="auto").eval()

tokenizer = AutoTokenizer.from_pretrained(
    path, trust_remote_code=True, use_fast=False)

第 2 步:准备测试图片

模型的图像处理逻辑内置了动态高分辨率切块(默认 max_num=12 个块,块大小 448×448),代码与 README 中 load_image 函数完全一致,可直接从 README.md 复制。简化版如下:

from PIL import Image
# 复用 README 中的 build_transform 与 dynamic_preprocess 函数
pixel_values = load_image('./test.jpg', max_num=12).to(torch.bfloat16).cuda()
generation_config = dict(max_new_tokens=1024, do_sample=True)

第 3 步:纯文本对话(0 图片也能聊)

不需要图片时,pixel_values 传 None 即可:

question = '你好,介绍一下你自己?'
response, history = model.chat(
    tokenizer, None, question, generation_config,
    history=None, return_history=True)
print(f'用户: {question}\n助手: {response}')

第 4 步:图像 + 多轮对话

多轮对话的关键是把上一轮返回的 history 传回下一次调用:

question = '<image>\n请用一句话描述这张图片。'
response, history = model.chat(
    tokenizer, pixel_values, question, generation_config,
    history=None, return_history=True)
print(f'用户: {question}\n助手: {response}')

# 第二轮:追问图片细节,上下文自动延续
question = '针对上面的图片,帮我写一首短诗。'
response, history = model.chat(
    tokenizer, pixel_values, question, generation_config,
    history=history, return_history=True)
print(f'用户: {question}\n助手: {response}')

📎 更多用法(多图对话、视频理解、批量推理)的完整代码见 README.md 的 Inference with Transformers 章节;核心对话接口 chat 方法定义在 modeling_internvl_chat.py,批量接口 batch_chat 在 modeling_internvl_chat.py。

第 5 步(可选):开启思考模式

对于数学、科学等多步推理任务,可以设置系统提示词开启 Thinking 模式,官方推荐同时设置 do_sample=True 和 temperature=0.6 来避免输出重复:

R1_SYSTEM_PROMPT = """
You are an AI assistant that rigorously follows this response protocol:

1. First, conduct a detailed analysis of the question. ... Enclose this
   entire thinking process within  and  tags.

2. After the thinking section, provide a clear, concise, and direct answer.
""".strip()

model.system_message = R1_SYSTEM_PROMPT

完整的系统提示词原文见 README.md。


四、生产部署:LMDeploy 一键服务化

实际项目中建议使用 LMDeploy 部署,38B 版本设置 tp=2(双卡张量并行):

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

model = 'OpenGVLab/InternVL3_5-38B-Flash'
pipe = pipeline(model, backend_config=PytorchEngineConfig(session_len=32768, tp=2))

image = load_image('test.jpg')
response = pipe(('describe this image', image))
print(response.text)

一行命令即可启动兼容 OpenAI 接口的 API 服务:

lmdeploy serve api_server OpenGVLab/InternVL3_5-38B-Flash --server-port 23333 --tp 2 --backend pytorch

服务调用、多轮会话等完整示例见 README.md 的 Deployment 章节。


五、常见问题速查(FAQ)

问题 解决方案
显存不够加载 38B 使用 load_in_8bit=True 开启 8-bit 量化,或换 14B/8B 版本
模型加载报错、行为异常 升级 transformers>=4.52.1
多图对话效果差 用 Image-1: <image>\nImage-2: <image> 编号方式提问,并传入 num_patches_list
思考模式输出重复 设置 do_sample=True 且 temperature=0.6
想要流式输出 使用 TextIteratorStreamer,示例见 README.md

写在最后

至此,你已经完成了 InternVL3_5-38B-Flash 从模型下载到跑通第一次多模态对话的全过程:

  1. ✅ 下载模型并准备双卡 A100 环境
  2. ✅ 用 AutoModel + device_map="auto" 加载模型
  3. ✅ 纯文本对话 → 图像对话 → 多轮对话逐步解锁
  4. ✅ (可选)开启 Thinking 模式处理推理任务
  5. ✅ 用 LMDeploy 部署为 OpenAI 兼容服务

Flash 版本在视觉 token 上节省 50% 开销的特性,意味着同样的显存可以服务更多并发请求,非常适合落地到实际的多模态应用场景中。祝使用愉快!🚀

登录后查看全文
InternVL3_5-38B-Flash