Closed
[RFC]支持DeepSeek V4 QAT的权重侧的实现 #80
caoshuyangAdd on Jul 14Closed at Sep 17
Jul 15 Associated pull request
张德鹏
Jul 15 Comments:
Jul 15 Comments:
欢迎贡献!可以提供一份使能QAT量化的参考配置,方便开发者更清晰地使用此特性。


Jul 16 Added label: RFC
Jul 16 Assign caoshuyang as responsible person
Jul 17 Modify description of issue
Jul 17 Modify description of issue
Jul 17 Modify description of issue
Jul 17 Modify description of issue
Jul 17 Modify description of issue
caoshuyang
Jul 17 Comments:
Jul 17 Comments:


Jul 17 Modify description of issue
Jul 17 Modify description of issue
Jul 17 Modify description of issue
Jul 17 Modify description of issue
Jul 17 Modify description of issue
Jul 28 Modify description of issue
Sep 17 changed custom state from 待办的 to 已验收
Sep 17 Close issue
Sep 17 Added label: resolved
作者:@caoshuyang
概述
简介
本提案为 torchtitan-npu 引入
torchao_npu模块,用于在 Ascend NPU 上基于torchao对 Transformer 模型权重进行低精度量化(感知)训练(MXFP8/MXFP4/Block FP8)。核心思路是
torch.mm/torch._grouped_mm/torch.nn.functional.linear等调用,替换为torch_npu的原生量化及低精度矩阵乘算子。部分实现直接使用torchao的功能。torchao进行模型的遍历和张量的替换。torchtitan.components.quantization.QuantizationConverter的ParameterConverter,将torchao_npu的功能和自定义 Tensor 子类暴露给 torchtitan 框架。主要组件包括:
torch_npu量化算子封装为torch.autograd.Function实现,提供可微的低精量化和矩阵乘。动机
一个直接的目标是为 DeepSeek-V4 QAT 的权重侧提供量化感知训练支持。在 NPU 上对 DeepSeek-V4 等大模型进行 QAT(量化感知训练)或低精度预训练,需要在前向/反向传播中使用硬件原生量化算子进行计算。DeepSeek-V4 QAT 中,各层量化策略如下:
float8_e4m3fn) 真量化float8_e4m3fn) 真量化上述策略中,MXFP8 和 Block FP8 需要在矩阵乘计算前将权重和激活同时量化为低精度,再调用
npu_quant_matmul/npu_grouped_matmul完成计算。这类需求仅靠上游torchao的 fake-quantize 机制无法满足。本提案基于torchtitan.components.quantization.QuantizationConverter接口提供统一的注册-替换流程,使量化策略的声明与模型代码解耦。核心设计
架构
量化算子映射
torch.mm/torch.matmulnpu_quant_matmul(量化后低精度矩阵乘)torch._grouped_mmnpu_grouped_matmul(MoE grouped 矩阵乘)torch.nn.functional.linear前向和反向路径均量化为低精度后计算。
关键设计决策
torchtitan侧利用Converter机制 +torchao侧利用quantize_ParameterConverter继承 torchtitan 的QuantizationConverter接口,模型遍历复用torchaoQuantizeConfig数据类驱动BlockQuantizeConfig+ 可选MXQuantizeConfig(for MXFP4 fake-quantize)使用示例
# other imports from torchtitan_npu.experiments.ao_npu.torchao_npu import ParamSwapConfig from torchtitan_npu.experiments.ao_npu.torchao_npu.interfaces.torchtitan import NpuQuantizeConverter, is_attention, is_shared_expert from torchtitan_npu.experiments.ao_npu.torchao_npu.quantization.quant_configs import BlockQuantizeConfig, MXQuantizeConfig from torchtitan_npu.experiments.ao_npu.torchao_npu.quantization.filters import any_filters # create a new model in config_registry.py def deepseek_v4_ao_qat() -> TrainerConfig: # cfg = some high precicion configuration from config_registry.py # add two new converters to high precision model configurations # Attention projections + shared experts: MXFP8 cfg.model_converters.converters.append( NpuQuantizeConverter.Config( base_config=ParamSwapConfig( weight_config=MXQuantizeConfig(), activation_config=MXQuantizeConfig(), ), filter_fn=any_filters(is_attention, is_shared_expert), ) ) # Routed experts: block FP8 + mxfp4 QAT cfg.model_converters.converters.append( NpuQuantizeConverter.Config( base_config=ParamSwapConfig( weight_config=BlockQuantizeConfig( mxfp4_fake_quantize_config=MXQuantizeConfig( elem_dtype=torch.float4_e2m1fn_x2, dst_type_max=7.0, ), ), activation_config=MXQuantizeConfig(elem_dtype=torch.float8_e4m3fn), ), filter_fn=is_routed_expert, ) ) return cfg