InternVL3.5-241B-A28B快速入门:Transformers加载与推理全攻略
InternVL3.5-241B-A28B快速入门:Transformers加载与推理全攻略
InternVL3.5-241B-A28B 是 InternVL3.5 家族中规模最大的开源多模态大模型(Multimodal Large Language Model),也是目前开源阵营中综合表现最强的视觉语言模型(VLM)之一。它拥有 240.7B 总参数、28B 激活参数的 MoE 架构,在通用多模态理解、数学推理、OCR 文档、GUI 交互、视频理解等任务上全面对标商用旗舰。本文带你用 Transformers 框架快速完成模型加载、开启 Thinking 深度思考模式,并跑通图片推理全流程 🚀
上图来自仓库自带的示例图片 examples/image1.jpg,可直接作为"描述这张图片"的推理输入。
模型简介:InternVL3.5-241B-A28B 强在哪里?
InternVL3.5-241B-A28B 沿用 InternVL 系列经典的 "ViT–MLP–LLM" 架构:
- 视觉编码器:InternViT-6B(5.5B 参数),支持动态高分辨率切片(448×448 的 patch,最多 12 片)
- 语言模型:基于 Qwen3 系列 MoE 初始化,235.1B 参数,94 层、128 个专家、每 token 激活 8 个
- 关键创新:Cascade RL 级联强化学习(离线 MPO + 在线 GSPO)显著提升推理能力;ViR 视觉分辨率路由器 + DvD 解耦部署带来最高 4.05× 推理加速
📊 不同训练阶段的消融实验直观展示了 Cascade RL 的增益——从 Instruct 到完整 CascadeRL 版本,各尺寸模型得分普遍大幅提升,241B-A28B 在推理基准上达到 66.9:
完整训练流程为四个阶段:多模态持续预训练(CPT)→ 监督微调(SFT)→ 级联强化学习(CascadeRL)→ 视觉一致性学习(ViCO,仅 Flash 版):
本仓库为 HuggingFace 标准格式(HF Format) 权重,与 Transformers 官方 API 完全兼容,model_type 为 internvl,架构类为 InternVLForConditionalGeneration(详见 config.json)。相比 GitHub 格式,你无需自定义代码即可用 AutoModelForImageTextToText 一行加载。
硬件准备与获取模型:241B 多模态模型要几张卡?
官方给出的硬件建议(bf16 全精度部署):
| 模型规模 | GPU 需求 |
|---|---|
| ≤ 30B | 1× A100 |
| 38B | 2× A100 |
| 241B-A28B | 8× A100 |
💡 显存估算:bf16 权重约 480GB;若使用 8-bit 量化(load_in_8bit=True)可降至约 240GB,再配合多卡切分即可部署。
环境方面,官方要求 transformers>=4.52.1(本仓库按 transformers_version: 4.55.0 打包):
pip install "transformers>=4.52.1" torch accelerate
获取权重有三种方式:从 Hugging Face / ModelScope 下载,或在本地克隆本仓库后离线使用:
git clone https://gitcode.com/OpenGVLab/InternVL3_5-241B-A28B-HF
仓库内关键文件一览:
- 模型权重:97 个
model-XXXXX-of-00097.safetensors分片 + model.safetensors.index.json 索引 - 配置:config.json(模型结构)、preprocessor_config.json(图像预处理,448×448、最多 12 patches)、processor_config.json、generation_config.json
- 分词与对话模板:tokenizer.json、chat_template.jinja、vocab.json、merges.txt
完整介绍与更多变体见 README.md。
Transformers 加载模型:三种常用姿势
1️⃣ bf16 全精度加载(显存充足时首选)
import torch
from transformers import AutoTokenizer, AutoModelForImageTextToText
path = "OpenGVLab/InternVL3_5-241B-A28B-HF"
model = AutoModelForImageTextToText.from_pretrained(
path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
use_flash_attn=True,
trust_remote_code=True).eval()
2️⃣ 8-bit 量化加载(BNB,显存减半)
model = AutoModelForImageTextToText.from_pretrained(
path,
torch_dtype=torch.bfloat16,
load_in_8bit=True,
low_cpu_mem_usage=True,
use_flash_attn=True,
trust_remote_code=True).eval()
3️⃣ 多卡自动切分(241B 必选)
model = AutoModelForImageTextToText.from_pretrained(
path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
use_flash_attn=True,
trust_remote_code=True,
device_map="auto").eval()
⚠️ 三个关键点:
trust_remote_code=True必须开启;device_map="auto"会自动把 97 个权重分片切分到多张 GPU;use_flash_attn=True可显著降低注意力显存开销。
开启 Thinking 模式:解锁深度推理能力
InternVL3.5 支持 Thinking 模式——模型会先输出分步思考过程,再给出最终答案,特别适合数学、科学等多步推理任务。开启方法只有一步:将 system prompt 设为官方 Thinking System Prompt,并建议 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 to the user's question.
""".strip()
messages = [
{"role": "system",
"content": [{"type": "text", "text": R1_SYSTEM_PROMPT}]},
{"role": "user",
"content": [{"type": "text", "text": "你的问题"}]},
]
💡 进阶玩法(Test-Time Scaling):对推理类任务还可采用 Best-of-N 策略,用过程奖励模型(VisualPRM)从多个推理候选中挑选最优答案,进一步提升准确率。
跑通推理:图片问答实战
HF 格式权重与 Transformers 官方 API 完全一致,推理流程只有四步:建对话 → 处理器编码 → generate → 解码:
from transformers import AutoProcessor
processor = AutoProcessor.from_pretrained("OpenGVLab/InternVL3_5-241B-A28B-HF")
messages = [
{"role": "user", "content": [
{"type": "image", "image": "examples/image1.jpg"},
{"type": "text", "text": "这张图片里有什么?"},
]},
]
inputs = processor.apply_chat_template(
messages, add_generation_prompt=True,
tokenize=True, return_tensors="pt").to(model.device)
output_ids = model.generate(**inputs, max_new_tokens=512)
response = processor.batch_decode(
output_ids[:, inputs["input_ids"].shape[1]:],
skip_special_tokens=True)[0]
print(response)
🖼️ 使用小贴士:
- 动态高分辨率:图像按 448×448 切片、最多 12 片(见 preprocessor_config.json 中
max_patches: 12),细节丰富的文档、图表也能看清 - 多图对话:把多张图片放进
content列表即可,用 "Image-1 / Image-2" 编号提问效果更好;多图片会推高输入 token,注意上下文窗口(本模型支持 32K,见 config.json 中max_position_embeddings: 40960) - 视频理解:同样支持,示例视频见 examples/red-panda.mp4
生产部署:LMDeploy 与 DvD 加速
生产环境建议使用 LMDeploy 或 vLLM 部署(注意:这类推理框架使用 GitHub 格式权重 OpenGVLab/InternVL3_5-241B-A28B,且 241B 需设置 tp=8):
pip install lmdeploy>=0.9.1
lmdeploy serve api_server OpenGVLab/InternVL3_5-241B-A28B --server-port 23333 --tp 8 --backend pytorch
服务启动后即提供 OpenAI 兼容的 RESTful API,可直接对接现有应用。
📈 吞吐瓶颈主要在高分辨率场景。InternVL3.5 提出的 DvD(Decoupled Vision-Language Deployment,视觉-语言解耦部署) 把视觉编码器与语言模型分到不同 GPU、异步流水执行,避免互相阻塞。官方消融实验显示,241B-A28B 在 1344 分辨率下,DvD + ViR 组合相比基线带来 3.12× 请求吞吐提升(1.37 → 4.27 req/s):
常见问题速查(FAQ)
Q1:加载时报错或行为异常?
先确认 transformers>=4.52.1 且开启了 trust_remote_code=True。
Q2:Thinking 模式回答出现重复?
按官方建议设置 do_sample=True、temperature=0.6。
Q3:241B 显存放不下怎么办?
叠加 load_in_8bit=True + device_map="auto";追求吞吐则走 LMDeploy/vLLM 并启用量化 KV Cache。
Q4:后续想微调这个模型? InternVL 官方工具链、SWIFT、XTuner 等框架均已支持 InternVL 系列微调,可基于本权重继续训练。
相关文件导航
- 项目说明与完整 Quick Start:README.md
- 模型结构配置:config.json | 视觉编码器与 MoE 参数见其中
vision_config/text_config - 图像预处理配置:preprocessor_config.json
- 对话模板:chat_template.jinja
- 权重索引:model.safetensors.index.json
- 示例素材:examples/image1.jpg、examples/red-panda.mp4
掌握以上内容,你就可以用 Transformers 独立加载、推理并部署 InternVL3.5-241B-A28B 多模态大模型了。建议先跑通图片问答,再依次尝试 Thinking 模式与多卡部署,逐步解锁这个开源视觉语言模型的全部能力 ✨



