Chapter 10
多模态:InternVL / LLaVA 微调
InternVL 是上海 AI Lab 自家的 VLM,XTuner 对它支持最深入。本章把 InternVL 微调流程过一遍。
10.1支持的多模态模型
| 模型族 | config 路径 |
|---|---|
| InternVL(v1.5 / v2 / v3) | xtuner/configs/internvl/ |
| LLaVA(v1.5 / NeXT) | xtuner/configs/llava/ |
| MiniGPT-4 | xtuner/configs/minigpt4/ |
| Cogvlm / Yi-VL(部分) | — |
10.2InternVL 架构回顾
┌──────────────┐ ┌──────────────┐
│ Vision │ Pixel │ LLM │
│ Encoder │ shuffle │ (InternLM2) │
│ (InternViT) │ ──────▶ │ │
└──────────────┘ MLP └──────────────┘
│ │
└── image tokens + text tokens ──▶ output
10.3InternVL 训练两阶段
| 阶段 | 解冻 | 数据 |
|---|---|---|
| Stage 1: Pretrain | 仅 MLP projector | image-text pair(caption) |
| Stage 2: SFT | LLM + projector(ViT 冻) | 多模态指令 |
10.4找配置 + 跑
xtuner list-cfg | grep internvl
# 例:
# internvl_v2_internlm2_5_8b_qlora_finetune
# internvl_v2_internlm2_5_8b_lora_finetune
# internvl_v2_internlm2_5_8b_full_finetune
xtuner copy-cfg internvl_v2_internlm2_5_8b_qlora_finetune ./
NPROC_PER_NODE=8 xtuner train \
./internvl_v2_internlm2_5_8b_qlora_finetune_copy.py \
--deepspeed deepspeed_zero2
10.5关键 config 段
model = dict(
type=InternVL_V1_5,
model_path=pretrained_model_name_or_path,
freeze_llm=False, # SFT 阶段解冻
freeze_visual_encoder=True,
quantization_llm=True, # QLoRA
quantization_vit=False,
use_activation_checkpointing=True,
max_position_embeddings=8192,
)
train_dataset = dict(
type=InternVL_V1_5_Dataset,
model_path=pretrained_model_name_or_path,
data_paths=data_path, # 多模态 jsonl 列表
image_folders=image_folder,
template=prompt_template,
max_length=8192,
)
10.6多模态数据格式
{
"id": "001",
"image": "./imgs/001.jpg",
"conversations": [
{"from": "human", "value": "<image>\n描述这张图"},
{"from": "gpt", "value": "图中..."}
]
}
<image> 占位符位置决定图片在文本中的位置。多图就有多个 <image> + "image": ["a.jpg", "b.jpg"]。
10.7显存预算
InternVL2-8B、单 A100-80GB、QLoRA、freeze_vit=True、seqlen=4K:
| 项 | 显存 |
|---|---|
| LLM 4bit | ~5 GB |
| ViT bf16 冻 | ~1 GB |
| LoRA 梯度 + optim | ~3 GB |
| activation | ~10 GB |
| 合计 | ~19 GB(单卡可以训) |
10.8常见踩坑
| 现象 | 处理 |
|---|---|
| image 找不到 | image_folders 设对;jsonl 里相对路径要相对 image_folders |
| image token 数量错 | InternVL 动态切 patch,每张图 token 数不定;检查 max_dynamic_patch |
| OOM | 开 use_activation_checkpointing;降图分辨率 |
| 训完不"看"图 | 检查数据 <image> 占位符是否在正确位置 |
10.9这章你需要带走的
- InternVL 是上海 AI Lab 自家 VLM,XTuner 支持最完整;
- 训练两阶段:projector pretrain → LLM SFT;
- 数据 jsonl 里 image 路径 +
<image>占位; - QLoRA + freeze_vit 可单卡 80GB 训 InternVL2-8B。