InternVL3.5-2B-Pretrained快速上手:用transformers加载多模态大模型并实现图文对话实战
InternVL3.5-2B-Pretrained快速上手:用transformers加载多模态大模型并实现图文对话实战
InternVL3.5-2B-Pretrained 是 InternVL3.5 系列中仅 2.3B 参数的轻量级多模态大模型,由 Qwen3-1.7B 语言模型与 InternViT-300M 视觉编码器融合而成,单张消费级显卡即可加载运行。本文带你用 transformers 从零完成多模态大模型加载,一步步实现图片描述、多轮对话与思考模式,并带你读懂仓库里的每个文件。
一、认识 InternVL3.5-2B-Pretrained
🔍 先用 30 秒认识这位"选手":
| 项目 | 说明 |
|---|---|
| 总参数量 | 约 2.3B(视觉 0.3B + 语言 2.0B) |
| 视觉编码器 | InternViT-300M,支持 448px 动态分辨率 |
| 语言模型 | Qwen3-1.7B(28 层,32K 上下文) |
| 训练阶段 | 仅完成 CPT(多模态持续预训练) |
| 模型架构名 | InternVLChatModel(model_type: internvl_chat) |
⚠️ 一个必须知道的重点:本模型是 Pretrained(预训练版),只走完训练流程的第一步,还没有经过指令微调(SFT)和强化学习。它更适合作为继续预训练、二次微调的底座。如果你只是想要"拿来即聊"的对话体验,推荐同系列的 InternVL3.5-2B-Instruct(CPT + SFT)或无后缀的完整版 InternVL3.5-2B(CPT + SFT + CascadeRL)。
二、环境配置:transformers 版本与显卡要求
🛠️ 安装依赖只需一条命令:
pip install "transformers>=4.52.1" torch
官方明确要求 transformers>=4.52.1 才能保证模型正常工作,这是多模态大模型加载最常见的"坑",请一定留意。
硬件方面,2.3B 参数、bf16 精度下模型权重约 4.6GB,一张 6GB 显存的显卡(如 RTX 3060)即可运行;显存吃紧时下文会给出 8-bit 量化方案。
三、模型加载:一行 from_pretrained 跑通
16-bit 标准加载方式
import torch
from transformers import AutoTokenizer, AutoModel
path = "OpenGVLab/InternVL3_5-2B-Pretrained"
model = AutoModel.from_pretrained(
path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
trust_remote_code=True, # 关键:允许执行仓库中的自定义建模代码
).eval().cuda()
tokenizer = AutoTokenizer.from_pretrained(
path, trust_remote_code=True, use_fast=False
)
BNB 8-bit 量化加载(低显存首选)
model = AutoModel.from_pretrained(
path,
torch_dtype=torch.bfloat16,
load_in_8bit=True, # 8-bit 量化,显存占用更低
low_cpu_mem_usage=True,
trust_remote_code=True,
).eval()
多卡加载
model = AutoModel.from_pretrained(
path,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
trust_remote_code=True,
device_map="auto", # 自动切分到多张 GPU
).eval()
三个参数值得记住:
trust_remote_code=True:本仓库自带modeling_internvl_chat.py等自定义代码(见 config.json 中的auto_map配置),缺少它模型无法加载low_cpu_mem_usage=True:分片加载,避免 CPU 内存峰值过高use_fast=False(tokenizer):InternVL 系列分词器需使用慢速模式
四、图文对话实战:第一次问出图片内容
InternVL 的图像预处理(动态分辨率切块、缩略图)都内置在模型内部,调用 model.chat() 时只需传入图片文件路径或已处理的张量即可。下面是最核心的最小可用示例:
question = '<image>\n请简短描述这张图片。'
response = model.chat(tokenizer, './your_photo.jpg', question,
dict(max_new_tokens=512, do_sample=True))
print(response)
💡 <image> 是图像占位符,模型会自动把它替换为真实的视觉特征。
单图多轮对话
通过 history 参数即可维持上下文,实现"看图 + 连续追问":
question = '<image>\n请详细描述这张图片。'
response, history = model.chat(tokenizer, './your_photo.jpg', question,
dict(max_new_tokens=1024, do_sample=True),
history=None, return_history=True)
# 第二轮:基于同一张图片追问,传入上轮返回的 history
question = '图片里的主角在做什么?请展开说说。'
response, history = model.chat(tokenizer, './your_photo.jpg', question,
dict(max_new_tokens=1024, do_sample=True),
history=history, return_history=True)
多图对比与批量推理
把多张图在通道维拼接后传入,并用 num_patches_list 记录每张图占用的 patch 数:
pixel_values1 = load_image('./image1.jpg', max_num=12) # 见仓库内置预处理函数
pixel_values2 = load_image('./image2.jpg', max_num=12)
pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
question = 'Image-1: <image>\nImage-2: <image>\n对比这两张图的异同。'
response = model.chat(tokenizer, pixel_values, question,
dict(max_new_tokens=1024, do_sample=True),
num_patches_list=num_patches_list)
需要一次推理多条请求时,可改用 model.batch_chat()(定义于 modeling_internvl_chat.py)做批处理。
五、如何开启思考模式(Thinking Mode)
想让模型先"思考"再回答,只需设置系统提示词,并配合采样参数:
R1_SYSTEM_PROMPT = """
You are an AI assistant that rigorously follows this response protocol:
1. First, conduct a detailed analysis of the question. Consider different angles, potential solutions, and reason through the problem step-by-step. 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. Separate the answer from the think section with a newline.
Ensure that the thinking process is thorough but remains focused on the query. The final answer should be standalone and not reference the thinking section.
""".strip()
model.system_message = R1_SYSTEM_PROMPT
开启后建议设置 do_sample=True 且 temperature=0.6,可有效缓解输出重复的问题。
再次提醒:Pretrained 版本未做 SFT,对话与推理指令遵循能力有限,体验思考模式请优先使用 Instruct / 完整版权重。
六、仓库文件拆解:这份模型包里都有什么
本仓库是标准的多模态模型仓库,各文件各司其职:
| 文件 | 作用 |
|---|---|
| config.json | 模型架构配置:视觉、语言双配置及动态分辨率参数 |
| model.safetensors | 模型权重文件 |
| modeling_internvl_chat.py | 主模型实现,含对话入口 chat()(L264)与 batch_chat()(L215) |
| configuration_internvl_chat.py | 组合式配置类 InternVLChatConfig(L18) |
| modeling_intern_vit.py | InternViT 视觉编码器实现 |
| conversation.py | 对话模板,本模型使用 internvl2_5 模板(L384) |
| vocab.json / merges.txt | 词表与 BPE 合并规则 |
| special_tokens_map.json / added_tokens.json | 特殊与新增 token 定义 |
| generation_config.json | 默认生成参数 |
config.json 中有几个与多模态直接相关的配置值得留意:dynamic_image_size: true 开启动态分辨率;force_image_size: 448 表示图像块尺寸为 448px;max_dynamic_patch: 12 表示单图最多切分为 12 个块加缩略图——这就是"看得越细、token 越多"的由来。
七、常见问题排查清单(FAQ)
❓ 加载报"model type not recognized"?
缺少 trust_remote_code=True。仓库内自定义建模代码不被执行时,transformers 无法识别 internvl_chat 架构。
❓ OOM 显存不足?
按优先级尝试:加 load_in_8bit=True → 加 device_map="auto" 多卡分摊 → 调小 max_new_tokens 和图像 max_num。
❓ 分词报错或输出乱码?
确认加载 tokenizer 时指定了 use_fast=False。
❓ 回答质量不像"聊天机器人"? 这是预期行为——Pretrained 权重只完成了预训练阶段。请换用 InternVL3.5-2B-Instruct 或完整版 InternVL3.5-2B 权重。
❓ 生产部署怎么办?
除 transformers 外,LMDeploy(pip install "lmdeploy>=0.9.1")与 vLLM 均支持该系列模型,可用 lmdeploy serve api_server 一键拉起兼容 OpenAI 接口的推理服务。
写在最后
用 transformers 加载 InternVL3.5-2B-Pretrained 并完成图文对话,核心就三步:装对版本(≥4.52.1)→ trust_remote_code 加载 → model.chat() 传图提问。掌握这条链路后,无论是把它作为微调底座,还是换用 Instruct 权重做图文问答,都能快速上手。祝你的第一个多模态应用顺利跑起来 🚀