[transformers 源码解读] 量化

HfQuantizer 抽象基类统一 24 个量化后端,在权重加载前用模板方法替换 Linear 为量化层,避免先加载全精度再量化。本文解读量化介入点与 WeightConverter 协作设计。

src/pages/articles/_md/AI/Infra/Training/transformers/CodeWiki/5.15.0/10-quantizers.md

← 返回概览


模块定位

量化模块让大模型以 4bit/8bit/FP8 等低精度加载,大幅降显存(70B fp16 ~140GB → 4bit ~35GB)。HfQuantizer 抽象基类统一 24 个后端(bnb 4/8bit、GPTQ、AWQ、FP8、Quanto、HQQ、compressed-tensors 等),在 from_pretrained 流程的固定介入点替换模块。它独立成模块,是因为每种后端的模块替换/权重格式/校准不同,但介入点一致,用模板方法把公共流程固化、子类填两个钩子即可。

边界:量化只管”模块替换与权重格式适配”,不管具体模型结构(建模核心),并把后端库操作委托给 integrations/。

模块架构

quantizers/(4586 行,28 文件)按职责分:

  • HfQuantizer 抽象基类:base.py:73,持有 quantization_config(QuantizationConfigMixin 子类)与 pre_quantized(是否 checkpoint 已量化)。模板方法 preprocess_model(L155,加载前:设 is_quantized 属性、_convert_model_for_quantization、调 _process_model_before_weight_loading)与 postprocess_model(加载后:写回 config、_assign_is_quantized、调 _process_model_after_weight_loading)。钩子 _process_model_before_weight_loading/_process_model_after_weight_loading。
  • 工厂:auto.py 的 AutoHfQuantizer.from_config(L193)+ AUTO_QUANTIZER_MAPPING(24 后端)+ AutoQuantizationConfig.from_dict(L152,查 AUTO_QUANTIZATION_CONFIG_MAPPING)+ @register_quantizer/@register_quantization_config 装饰器。
  • 具体 quantizer:quantizer_bnb_4bit.py(Bnb4BitHfQuantizer)、quantizer_gptq.py、quantizer_awq.py、quantizer_finegrained_fp8.py 等共 24 个。
  • 共用工具:quantizers_utils.py(与 modeling_utils 权重替换协作)。
  • 顶层入口:get_hf_quantizer()(auto.py L330)被 from_pretrained 直接调,完成实例化+validate_environment+update_device_map+update_tp_plan。

调用链路

模型加载时量化介入的完整数据流:

from_pretrained()                                              modeling_utils.py:4225
├── get_hf_quantizer(config, quantization_config, ...)
│   ├── merge_quantization_configs(合并 model config 与用户传入的)
│   ├── AutoHfQuantizer.from_config → AUTO_QUANTIZER_MAPPING[quant_method]
│   ├── validate_environment(device_map, weights_only)
│   ├── update_device_map / update_tp_plan / update_ep_plan
│   └─ 返回 hf_quantizer
├── model = cls(config)  # meta device 实例化
├── hf_quantizer.preprocess_model(model, dtype, device_map)   # 权重加载前
│   ├── setattr(model, "is_quantized", True)
│   ├── _convert_model_for_quantization(model)  # 替换特殊模块(如 Llama4TextExperts → Sequential)
│   └── _process_model_before_weight_loading(model)           # 模块替换点
│       └── replace_with_bnb_linear → nn.Linear → bnb.nn.Linear4bit(meta 下创建)
├── weight_conversions = get_model_conversion_mapping(model, key_mapping, hf_quantizer)
│   └── hf_quantizer.update_weight_conversions(weight_conversions)  # 追加 quantizer 的 converter
├── _load_pretrained_model → WeightConverter.convert 时执行反序列化/反量化
└── hf_quantizer.postprocess_model(model)                     # 权重加载后
    ├── model.config.quantization_config = self.quantization_config
    ├── _assign_is_quantized(递归设 _is_quantized=True)
    └── _process_model_after_weight_loading → setattr(model, "is_loaded_in_4bit", True)

数据流:config → hf_quantizer → meta 模型替换 Linear 为量化层 → 权重加载(量化 converter 重组)→ model.config.quantization_config。

方法速查表
方法 一行职责 关键设计决策
get_hf_quantizer in auto.py:330 一站式实例化+校验 from_pretrained 唯一入口
AutoHfQuantizer.from_config in auto.py:193 工厂选 quantizer bnb 特殊拆 4bit/8bit
preprocess_model in base.py:155 加载前模板方法 固定设属性→转换→调钩子
_process_model_before_weight_loading 模块替换钩子 子类实现(如 replace_with_bnb_linear)
get_weight_conversions in base.py:295 返回自己的 converter 如 bnb 的 Bnb4bitDeserialize
update_weight_conversions 改写模型 converter 列表 默认追加,CompressedTensors 重写

核心实现

为何统一量化接口

24 种后端模块替换/权重格式/校准都不同,但在 from_pretrained 中的介入点完全一致:加载前替换模块 → 加载权重 → 加载后处理。HfQuantizer 用模板方法把公共流程(设 is_quantized、_convert_model_for_quantization、写回 config)固化在 preprocess_model/postprocess_model,子类只覆写 _process_model_before_weight_loading/_process_model_after_weight_loading 两钩子。这样 from_pretrained 只调统一接口,无需 if-elif 分支判断量化方法。

量化在权重加载”之前”替换模块

避免先加载全精度权重再量化(内存峰值翻倍 + 时间开销)。在 from_pretrained 中,模型先在 torch.device("meta") 实例化(所有参数 meta tensor 零内存),preprocess_model 在此阶段替换 nn.Linear 为 bnb.nn.Linear4bit(replace_with_bnb_linear 在 with torch.device("meta") 下),替换后量化层仍 meta 状态,随后 checkpoint 中已量化的权重直接加载到量化层参数。若先加载 fp32 再量化需:加载全精度(峰值=全精度大小)→ 逐层量化 → 丢弃全精度。70B 模型 fp16 ~140GB → 4bit ~35GB,差异巨大。

bnb 4bit 为何替换 Linear 层而非就地量化

bitsandbytes 4bit 依赖自定义 Params4bit 参数类型 + bnb.nn.Linear4bit 层实现,非标准 torch.Tensor。Params4bit 存压缩的 uint8 数据 + quant_state(scale/absmax/quant_map 元数据),Linear4bit.forward() 调 bnb.matmul_4bit 反量化+矩阵乘(bnb C++ 扩展专属 kernel)。标准 nn.Linear 的 weight 是 nn.Parameter(torch.Tensor) 无 quant_state。故必须用 Linear4bit 替换 Linear,权重加载时经 Bnb4bitDeserialize(WeightConverter)把 checkpoint 扁平量化字段(weight.nested_absmax/weight.quant_state.bitsandbytes__nf4)重组为 Params4bit 嵌套结构。bnb 4/8bit 共享一个 BitsAndBytesConfig 但拆两个 Quantizer——AutoHfQuantizer.from_config 据 load_in_8bit 追加 _8bit/_4bit 后缀查映射(auto.py:203),因 4bit/8bit 的模块替换/参数类型/反序列化完全不同。

quantizer 与 WeightConverter 协作

quantizer 通过两方法参与权重加载:get_weight_conversions() 返回自己的 WeightConverter(如 bnb 的 Bnb4bitDeserialize,source_patterns 列出 checkpoint 量化状态字段,operations 把它们重组为运行时格式);update_weight_conversions(weight_conversions)(base.py:297)默认追加,但 CompressedTensorsHfQuantizer 等重写此方法在 merge/concat converter 前插入 DecompressExperts 反量化(quantizer_compressed_tensors.py:185)。调用链:get_model_conversion_mapping(conversion_mapping.py:1765)收集模型自带 converter 后调 hf_quantizer.update_weight_conversions。

设计模式

模式 位置 为什么用
策略 HfQuantizer 子类(24 后端) 同接口多实现,按 quant_method 分派
工厂 AutoHfQuantizer.from_config(auto.py:193) 据 quant_method 选类实例化
注册表 AUTO_QUANTIZER_MAPPING + AUTO_QUANTIZATION_CONFIG_MAPPING(auto.py:73) quant_method→类 双表
装饰器注册 @register_quantizer/@register_quantization_config(auto.py:298) 外部扩展自定义后端
模板方法 preprocess_model/postprocess_model(base.py:155,173) 固定流程,钩子供子类覆写

模块间交互

quantizers 被 modeling_utils.from_pretrained 调用:L100 import get_hf_quantizer,L4225 调用获实例,L4308 调 preprocess_model(加载前替换),L4322 调 get_model_conversion_mapping(model, key_mapping, hf_quantizer) 让 quantizer 注入 converter,L4340 把 hf_quantizer 存入 LoadStateDictConfig,L4371 model.hf_quantizer = hf_quantizer,L4372 调 postprocess_model。与 core_model_loading 经 LoadStateDictConfig.hf_quantizer + WeightConverter.convert 中的可选 quantization_operation 协作。后端库操作委托给 integrations/(replace_with_bnb_linear ← integrations/bitsandbytes.py:162、Bnb4bitDeserialize/dequantize_and_replace)。

扩展方式

新增量化后端:建 quantizers/quantizer_xxx.py 继承 HfQuantizer 实现 validate_environment/_process_model_before_weight_loading/is_serializable/is_trainable;在 auto.py 的 AUTO_QUANTIZER_MAPPING 加映射(或 @register_quantizer("xxx") 装饰器);在 utils/quantization_config.py 加 XxxConfig(QuantizationConfigMixin) 与 QuantizationMethod 枚举;在 AUTO_QUANTIZATION_CONFIG_MAPPING 注册 config;后端库交互在 integrations/ 加辅助函数。自定义量化配置:操作 BitsAndBytesConfig(bnb_4bit_quant_type="nf4"/"fp4"、bnb_4bit_use_double_quant 等)经 AutoHfQuantizer.from_config → Bnb4BitHfQuantizer → replace_with_bnb_linear 读取。修改反序列化逻辑:改 Bnb4BitHfQuantizer.get_weight_conversions(quantizer_bnb_4bit.py:168)的 WeightConverter source_patterns 与 Bnb4bitDeserialize;MoE 场景覆写 update_weight_conversions 在 merge/concat 前插反量化(参考 quantizer_compressed_tensors.py:185)。