如何用Transformers加载InternVL3_5-38B-Flash?16位、8位量化与多GPU并行三种方式详解

原创2026-10-18 17:00:341,910 阅读

如何用Transformers加载InternVL3_5-38B-Flash?16位、8位量化与多GPU并行三种方式详解

InternVL3_5-38B-Flash 是商汤 OpenGVLab 开源的 InternVL3.5 系列多模态大模型的高效版,总参数 38.4B,支持图文对话、OCR、图表理解与视频问答。本文手把手教你用 Transformers 以 16 位 bf16 加载、8 位量化加载、多 GPU 并行加载 三种方式跑通 InternVL3_5-38B-Flash,并附上显存需求对比与常见报错排查,新手也能一次配对环境。

一、项目速览:这个仓库里都有什么

在动手前,先弄清楚 InternVL3_5-38B-Flash 的“家底”:

组成模块 说明 关键证据
视觉编码器 InternViT-6B(约 5.5B 参数,448px 输入) config.json
语言模型 Qwen3-32B(约 32.8B 参数,40960 上下文) config.json
Flash 视觉路由器 ViR 动态压缩视觉 token,节省约 50% 视觉开销 flash_mode、downsample_ratio 字段
模型权重 17 个 safetensors 分片(model.safetensors.index.json) model-00001-of-00017.safetensors
自定义建模代码 加载时需 trust_remote_code=True modeling_internvl_chat.py、configuration_internvl_chat.py

⚡ Flash 版本的核心卖点:在标准 InternVL3.5 基础上引入 Visual Resolution Router(ViR),根据每个图像 patch 的语义丰富度动态选择 1/4 或 1/16 的压缩率,在视觉 token 数量减少 50% 的同时保留接近 100% 的性能,更适合显存和算力受限的场景。

📌 官方建议的版本要求:transformers >= 4.52.1,且必须开启 trust_remote_code=True(模型使用了 custom_code 自定义实现)。

二、加载前的准备工作:环境与关键参数

先安装依赖(use_flash_attn=True 时需要 flash-attn):

pip install "transformers>=4.52.1" accelerate bitsandbytes
pip install flash-attn --no-build-isolation   # 可选,未装可将 use_flash_attn 设为 False

三种加载方式共用的核心参数如下,后文会反复用到:

参数 作用 三种方式
torch_dtype 权重精度,推荐 torch.bfloat16 ✅ 全部使用
low_cpu_mem_usage 用 accelerate 低内存方式加载,避免 CPU 内存爆掉 ✅ 全部使用
use_flash_attn 启用 FlashAttention-2,省显存提速 ✅ 全部使用
trust_remote_code 允许执行仓库内的自定义建模代码 ✅ 必须为 True
load_in_8bit bitsandbytes 8 位量化 方式二专用
device_map 多卡自动切分权重 方式三专用

三、方式一:16位(bf16)加载 —— 精度无损的基准方案

这是精度最高的方案,适合显存充足(约 80GB×2) 的机器。38B 参数在 bf16 下约占 77GB 显存,官方建议至少 2 张 A100 80GB。

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).eval().cuda()

对应官方示例见 README.md。

适合人群:双卡以上服务器、追求最高生成质量的研究者。

四、方式二:BNB 8位量化加载 —— 小显存也能跑

如果只有单张 80GB 显卡,可以把 38B 模型量化到 8 位:显存占用从约 77GB 降到 约 40GB,单卡即可容纳,性能损失通常可忽略。

import torch
from transformers import AutoTokenizer, AutoModel

path = "OpenGVLab/InternVL3_5-38B-Flash"
model = AutoModel.from_pretrained(
    path,
    torch_dtype=torch.bfloat16,   # 量化由 bitsandbytes 完成,此参数保持不变
    load_in_8bit=True,            # 关键参数:开启 8 位量化
    low_cpu_mem_usage=True,
    use_flash_attn=True,
    trust_remote_code=True).eval()

对应官方示例见 README.md。

💡 两个新手容易踩的坑:

  1. load_in_8bit=True 时不要再手动 .cuda(),bitsandbytes 会自动把权重放卡上;
  2. 需要安装 bitsandbytes 且 CUDA 环境可用(import bitsandbytes 不报错即可)。

五、方式三:多GPU并行(device_map="auto")—— 38B 最推荐的日常姿势

单卡放不下、又不想到处改代码时,把 device_map="auto" 交给 accelerate 自动把 17 个权重分片切到多张卡上即可:

import math
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()

对应官方示例见 README.md。

工作原理:device_map="auto" 会按显存余量把 model-00001-of-00017.safetensors 到 model-00017-of-00017.safetensors 各分片分配到不同 GPU。2×80GB 双卡跑 bf16 是最典型的配置;如果显存紧张,可以叠加 load_in_8bit=True 组成“8位 + 多卡”的组合拳。

六、三种加载方式怎么选?一张表看懂

对比维度 方式一:16位 bf16 方式二:8位量化 方式三:多GPU auto
显存占用 约 77GB 约 40GB 约 77GB(分摊到多卡)
最低硬件 2×80GB 单卡不够 1×80GB 2×48GB 起
精度损失 无 极小 无
代码复杂度 低 低(加一个参数) 低(加一个参数)
适合场景 追求无损精度 单卡跑大模型 日常部署、新手首选

📌 结论:单卡 80GB 选方式二;双卡及以上直接选方式三(bf16 精度),追求极致显存优化再叠加 load_in_8bit=True。

七、加载完成后:跑通第一次多模态对话

模型加载成功后,用 chat() 接口即可对话。多模态输入前需要把图片切成 448px 的 patch 并转为张量,官方提供了完整的图像处理函数,核心调用如下(完整可运行代码见 README.md):

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

# 加载图像(动态高分辨率切 patch,max_num 控制最大 patch 数)
pixel_values = load_image('./examples/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
generation_config = dict(max_new_tokens=1024, do_sample=True)

# 单图对话:问题前缀写 <image>
question = '<image>\nPlease describe the image shortly.'
response = model.chat(tokenizer, pixel_values, question, generation_config)
print(response)

chat() 的完整签名与实现位于 modeling_internvl_chat.py,它会自动处理对话模板拼接(模板定义在 conversation.py)、<img> 上下文 token 展开和历史维护。

💡 两个实用技巧:

  • 多轮对话:调用时加 return_history=True,把返回的 history 传给下一轮即可;
  • 多图输入:把多张图的 pixel_values 沿 dim=0 拼接,并传入 num_patches_list=[图1的patch数, 图2的patch数] 告诉模型各图占多少 patch。

八、常见问题排查清单

报错 / 现象 原因 解决方法
OSError: ... does not recognize this architecture 缺少自定义代码或版本过低 确认 trust_remote_code=True 且 transformers ≥ 4.52.1
RuntimeError: CUDA out of memory 单卡显存不足 换 8 位量化(load_in_8bit=True)或多卡(device_map="auto")
flash-attn 相关报错 flash-attn 未装或版本不匹配 临时改 use_flash_attn=False,或重装匹配 CUDA 的 flash-attn
加载极慢、CPU 内存告急 未启用低内存加载 保留 low_cpu_mem_usage=True

结语

InternVL3_5-38B-Flash 的加载流程可以概括为一句话:bf16 双卡求精度,8 位量化省显存,device_map="auto" 多卡最省心。三种方式只差一两个参数,建议从方式三起步,遇到显存不足再叠加 8 位量化,就能快速把这个 38B 多模态大模型跑起来。更多细节可查阅仓库内的 README.md 与 config.json。

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