InternVL3_5-1B-Instruct微调指南:用SWIFT与XTuner定制专属多模态助手
InternVL3_5-1B-Instruct微调指南:用SWIFT与XTuner定制专属多模态助手
🤗 InternVL3_5-1B-Instruct 是 OpenGVLab 开源 InternVL3.5 系列中体积最小的指令模型(总参数仅 1.1B),支持看图对话、OCR、图表理解和视频理解。想训练一个属于自己的多模态助手?本文手把手教你用 SWIFT 和 XTuner 两大主流工具对它进行微调(LoRA),单张消费级显卡即可完成,文末附常见报错排查清单。
一、先认识 InternVL3_5-1B-Instruct:微调前必知
微调前,先搞清楚这个模型由哪几部分组成。InternVL3.5 系列遵循 ViT – MLP – LLM 三段式范式,1B 版本的具体构成可以从 config.json 中直接看到:
| 模块 | 配置 | 说明 |
|---|---|---|
| 视觉编码器 | InternViT-300M(24 层,隐藏维度 1024,patch 14px) | 负责把图片切成小块并提取特征 |
| 语言底座 | Qwen3(28 层,隐藏维度 1024,上下文 40960) | 负责理解和生成文本 |
| 图像分块 | 动态高分辨率:min_dynamic_patch=1,max_dynamic_patch=12 |
每张图最多切成 12 个 448×448 的块 |
💡 几个直接影响微调体验的参数:
- 动态分辨率(dynamic_image_size=true):图片越大切块越多、视觉 token 越多,显存占用越高。微调时建议限制
max_num以减少长序列; - 对话模板(template=internvl2_5):系统提示词和角色分隔符定义在 conversation.py 中,SWIFT/XTuner 已内置适配,无需自己处理;
- 视觉配置:定义于 configuration_intern_vit.py,modeling_intern_vit.py 中是视觉编码器的实现;
- 对话与生成入口:模型主体
InternVLChatModel(含chat方法)在 modeling_internvl_chat.py 中,整体配置类见 configuration_internvl_chat.py。
📌 版本建议:官方要求
transformers>=4.52.1(20B 版本需>=4.55.0),微调前请确认环境版本一致。
二、SWIFT vs XTuner:两款微调工具怎么选?
README 官方推荐(见 README.md 的 Finetune 一节):
Many repositories now support fine-tuning of the InternVL series models, including InternVL、SWIFT、XTuner and others.
两者定位略有差异:
| 对比项 | SWIFT(ms-swift) | XTuner |
|---|---|---|
| 出品方 | ModelScope 团队 | 书生(InternLM)团队 |
| 擅长 | 全参数微调 + LoRA + QLoRA,数据集格式统一、生态完善 | 多模态 LoRA 微调,配置直观,显存友好 |
| 交互方式 | swift sft 命令行 |
Python 脚本 + 配置字典 |
| 适合场景 | 想快速跑通、数据量大、后续要对接 ModelScope 生态 | 显存紧张、只做 LoRA、想精细控制训练超参 |
🧠 对 1.1B 这种小模型:LoRA 微调单卡 12GB 显存即可跑通(如 RTX 3060/4060),显存充裕时全参数微调也是完全可行的。
三、SWIFT 微调 InternVL3_5-1B-Instruct 步骤
1. 准备数据集(JSONL 格式)
SWIFT 的多模态数据集只需三列:messages(对话)、images(图片路径)。准备一个 train.jsonl:
{"messages": [{"role": "user", "content": "<image>这张图里有什么?"}, {"role": "assistant", "content": "图中是一只橘色的猫坐在窗台上。"}], "images": ["cat.jpg"]}
2. 安装并启动训练
pip install "ms-swift[all]" -U
典型 LoRA 训练命令(按需修改 model、dataset、train_type):
CUDA_VISIBLE_DEVICES=0 \
swift sft \
--model OpenGVLab/InternVL3_5-1B-Instruct \
--train_type lora \
--lora_rank 16 \
--lora_alpha 32 \
--dataset train.jsonl \
--num_train_epochs 3 \
--per_device_train_batch_size 1 \
--learning_rate 1e-4 \
--max_length 4096 \
--torch_dtype bfloat16 \
--output_dir output/internvl3_5-1b-lora
3. 合并权重并导出
训练完成后,SWIFT 会把 Adapter 保存在 output/internvl3_5-1b-lora 下,合并命令:
swift export \
--adapters output/internvl3_5-1b-lora \
--merge_lora true
四、XTuner 微调 InternVL3_5-1B-Instruct 步骤
XTuner 用「配置文件 + 启动脚本」的方式,更适合逐步调参。
1. 安装
pip install xtuner
2. 编写微调脚本
新建 finetune_internvl3_5_1b.py,核心配置如下(关键参数已标注):
from xtuner.dataset import build_dataset
from xtuner.model import build_model
from xtuner.trainers import build_trainer
# 1) 基础信息
model_path = 'OpenGVLab/InternVL3_5-1B-Instruct'
train_dict = dict(type='internvl', market_place='huggingface', path='train.jsonl')
# 2) 模型构建:LoRA 作用在 LLM 上
model = dict(
type='build_ernievl_moe', # 以 InternVL 对应 builder 为准
lora_a=32, lora_b=32, lora_dropout=0.05,
use_gradient_checkpointing=True)
# 3) 训练器
trainer = dict(
type='build_trainer',
max_epochs=3,
global_batch_size=8,
dataset=train_dict,
model=model)
if __name__ == '__main__':
trainer = build_trainer(trainer)
trainer.train()
实际运行时请以 XTuner 文档中 InternVL 对应的 model builder 名称为准(如
build_internvl),上表仅演示配置结构。
3. 启动训练
CUDA_VISIBLE_DEVICES=0 python finetune_internvl3_5_1b.py
五、微调效果不理想的 5 个排查点
| 问题现象 | 可能原因 | 解决建议 |
|---|---|---|
| 显存 OOM | 图片切块过多(最多 12 块) | 调低 max_length、减小 batch size、开启 gradient checkpointing |
| 回答乱码/复读 | 未开启采样参数不当 | 生成时设 do_sample=True、temperature=0.6(官方推荐) |
| 中文能力退化 | 数据全英文 | 中英混合数据,保证指令语言与目标一致 |
| 视觉理解变差 | LoRA 只挂了 LLM 而数据图质量低 | 提高图片分辨率、清洗标注错误样本 |
| 训练 loss 不降 | 学习率过大/数据量过少 | 1e-4 附近微调,数据量建议 ≥500 条 |
六、微调后如何部署使用
训练完的模型可以像官方权重一样直接加载。两种部署方式任选:
① Transformers 直接加载
from transformers import AutoModel, AutoTokenizer
import torch
model = AutoModel.from_pretrained(
'你的微调模型路径',
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
trust_remote_code=True,
device_map='auto').eval()
② LMDeploy 服务化部署(OpenAI 兼容接口)
lmdeploy serve api_server 你的微调模型路径 --server-port 23333 --tp 1 --backend pytorch
这样,你的专属多模态助手就上线了 🚀
七、写在最后
- 模型本体(本仓库):model.safetensors,配置见 config.json、generation_config.json;
- 对话模板系统提示词:conversation.py 中的
internvl2_5模板; - 若希望模型开启 Thinking 深度思考模式,微调数据中应包含
think标签的推理过程样本; - 完整模型家族(含 2B~241B)及官方评测图,见 README.md 的 InternVL3.5 Family 章节。
1.1B 的轻量身材 + 开源生态的成熟微调工具,意味着你完全可以在一张显卡上,把一个「通用助手」炼成「领域专家」。动手试试吧!