Merged
[feat] Support Deepseek v4 HiFloat8 current-tensor scaling training through TorchAO #925
nancycyzlAdd on Sep 23
[feat] Support Deepseek v4 HiFloat8 current-tensor scaling training through TorchAO #925
Merged
Pull Request has been successfully merged by CANN-robot.
(Thank you for nancycyzl's contribution.)atomgit-bot
Sep 23 Comments:
Sep 23 Comments:
变更摘要
本变更在 experiments/torchao-npu/ 插件子包中新增 HiF8(torch_npu.hifloat8)量化后端,采用 current/constant tensor scaling(CTS,每次调用通过 npu_dynamic_quant 现算 scale,不持久化跨 step 的 scale),使 DeepSeek-V4 系列模型的普通线性层与 MoE 专家层可在 Ascend NPU 上以 HiF8 精度训练。HiF8 作为 ParamSwapConfig.weight_config/activation_config 的又一种取值,复用既有 NpuQuantizeConverter → torchao.quantize_() → 参数级 tensor-subclass 拦截链路;训练侧通过新增 recipe all_hif8 切换,模型代码无需改动。
主要改动
- 新增
all_hif8recipe 与配置类型: 在interfaces/torchao_converter.py的_SUPPORTED_RECIPES与torchtitan_npu/config/configs.py的QuantizationRecipe中加入all_hif8,并在_recipe_converters中新增分支,用_hif8_param_swap()构造ParamSwapConfig,经any_config_filter(dense_filter, filters["routed_expert"])匹配 dense 与 routed expert 模块。 - 新增 HiF8 量化算子与量化原语: 新增
ops/hif8_ops.py,提供to_hif8_then_mm、to_hif8_then_bmm、to_hif8_then_grouped_mm三个基于torch.autograd.Function的算子(分别走npu_quant_matmul/npu_grouped_matmul);新增quantize_hifloat8_pertensor,通过降序 stride 置换避免非稠密输入的隐式拷贝,并断言quant_mode == "pertensor"。 - 新增 HiF8 参数包装张量: 新增
HiF8TrainingWeightWrapperTensor,在__torch_function__中拦截F.linear/torch.addmm/torch.mm/torch.matmul/torch.bmm/torch._grouped_mm分派到对应算子,构造时校验weight_config/activation_config均为HiF8QuantizeConfig、quant_mode == "pertensor"且二者相等,并注册register_parameter_swap_handler完成参数替换。 - 扩展
ParamSwapConfig与HiF8QuantizeConfig校验/文档:ParamSwapConfig接受HiF8QuantizeConfig作为 weight/activation 配置(更新相应报错文案),HiF8QuantizeConfig补充说明其同时服务 Lightning Indexer Q/K 量化与 dense/MoE per-tensor 链路两种调用场景。 - 新增选择性激活检查点的保存算子开关: 在
interfaces/torchao_converter.py中新增_save_quant_codes_under_selective_ac(HIF8_SAVE_QUANT_CODES)与_save_block_ops_under_selective_ac(KIWI_SAVE_BLOCK_OPS),通过包装 torchtitan 的_get_default_save_ops将量化 GEMM 算子或 attention/mHC/MoE-routing 算子加入 selective-AC 保存列表,默认关闭。


Incorrect?
atomgit-bot
Sep 23 Comments:
Sep 23 Comments:
Sep 23 Added label: stat/needs-squash
Sep 23 Added label: cann-cla/yes
160 messages are collapsed here。 See more
Oct 31 Associated issue: [RFC] HiF8 delayed scaling 在 torchtitan-npu 中的实现方案——基于延迟历史幅度统计的动态量化缩放
CANN-robot
Oct 31 Comments:
Oct 31 Comments:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


Oct 31 Deleted associated issue: [RFC] HiF8 delayed scaling 在 torchtitan-npu 中的实现方案——基于延迟历史幅度统计的动态量化缩放
Oct 31 Deleted associated issue: [RFC] HiF8 delayed scaling 在 torchtitan-npu 中的实现方案——基于延迟历史幅度统计的动态量化缩放
Oct 31 Associated issue: [RFC] 通过 TorchAO 支持 DeepSeek-V4 模型的 HiFloat8 训练
CANN-robot
Oct 31 Comments:
Oct 31 Comments:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
本提案在 torchtitan-npu 已有的低精度训练框架基础上,新增 HiF8(
torch_npu.hifloat8)作为一种新的量化后端,采用 current/constant tensor scaling(CTS:每次调用都通过npu_dynamic_quant现算 scale,不做任何跨 step、跨调用的 scale 持久化),使 DeepSeek-V4 系列模型的普通线性层(nn.Linear/BatchedLinear)与 MoE 专家层(Grouped MM)能够在 Ascend NPU 上使用 HiF8 精度进行训练。本次实现落在实验性插件子包
experiments/torchao-npu/里。HiF8 与已有的 MX、Block FP8、Float8 row-wise 一样,只是ParamSwapConfig.weight_config/activation_config可以取的又一种值,复用同一套NpuQuantizeConverter→torchao.quantize_()→ 参数级 tensor-subclass 拦截 链路;训练脚本只需通过--extension.quantization.recipe all_hif8切换 recipe,模型代码无需改动。HiF8QuantizeConfig同时也是 Lightning Indexer(LI)Q/K 量化沿用的配置类(quant_mode可以是pertoken/pertensor/perchannel),dense/MoE 这条链路只使用quant_mode="pertensor"的场景,并在HiF8TrainingWeightWrapperTensor构造时显式校验这一点,避免把非 per-tensor 配置误接到这条只支持单一标量 scale 的链路上。实现逻辑
类型
Checklist
如何测试
使用
--extension.quantization.recipe all_hif84k 性能结果:
8k 性能结果: