Merged
[feat] Support HiFloat8 current-tensor scaling training with torchao #551
[feat] Support HiFloat8 current-tensor scaling training with torchao #551
Merged
nancycyzlAdd on Aug 12
nancycyzl
nancycyzl
Aug 12

描述

本提案在 torchtitan-npu 已有的低精度训练框架基础上,新增 HiF8(torch_npu.hifloat8)作为一种新的量化后端,使 DeepSeek-V4 等大模型的普通线性层(nn.Linear)与 MoE 专家层(Grouped MM)能够在 Ascend 950 NPU 上使用 HiF8 精度进行训练。

本次实现落在实验性插件子包 torchtitan_npu/experiments/ao_npu/ 里。HiF8 与已有的 MX、Block FP8、Float8 row-wise 一样,只是 ParamSwapConfig.weight_config/activation_config 可以取的又一种值,复用同一套 NpuQuantizeConverter → torchao.quantize_() → 参数级 tensor-subclass 拦截 链路。

实现逻辑

训练启动(config_registry.py 中的 debug_deepseek_v4_flash_single_node_hif8_qat 等)
        │
        ▼
  TrainerConfig.model_converters 追加 NpuQuantizeConverter.Config(...)
        │
        ▼
  ModelConvertersContainer 按序调用各 Converter.convert(model)
        │
        ▼
  NpuQuantizeConverter.convert(model)                 ← torchtitan_npu/experiments/ao_npu/torchao_npu/interfaces/torchtitan.py
        │
        └── torchao.quantize_(model, base_config=ParamSwapConfig(...), filter_fn=...)
              │  (filter_fn 按 FQN 匹配到目标模块)
              ▼
        register_quantize_module_handler(ParamSwapConfig) 注册的
        `_param_swap_config_transform`                       ← torchtitan_npu/experiments/ao_npu/torchao_npu/configs.py
              │  (QATStep.PREPARE)
              ├── handler = _PARAM_SWAP_QUANTIZE_CONFIG_HANDLER[type(config.weight_config)]
              │             = register_parameter_swap_handler(HiF8QuantizeConfig) 注册的函数
              │             ← torchtitan_npu/experiments/ao_npu/torchao_npu/wrapper_tensors/hif8_wrapper_tensor.py
              ▼
        _replace_params_with_custom_fn_if_matches_filter(module, handler, config.params_filter_fn, extra_args=(config,))
        (params_filter_fn 默认 `_is_parameter`;extra_args 把 config 传给 handler,供其取出 weight_config/activation_config)
              │
              ▼
        module.weight = nn.Parameter(HiF8TrainingWeightWrapperTensor(weight, weight_config, activation_config))
        (权重被替换成 wrapper tensor 子类,训练循环里其余代码无感知)
        │
        ▼
  训练前向/反向:nn.Linear(x, weight)/grouped_mm(x, weight, offs) 触发
        │
        └── HiF8TrainingWeightWrapperTensor.__torch_function__ 拦截
              ├── F.linear/torch.addmm/torch.mm/torch.matmul → to_hif8_then_mm(x, weight, ...)
              └── torch._grouped_mm                          → to_hif8_then_grouped_mm(x, weight, offs, ...)
                    │
                    └── torch_npu.npu_dynamic_quant(quant_mode="pertensor")
                        + torch_npu.npu_quant_matmul / npu_grouped_matmul
                        ← torchtitan_npu/experiments/ao_npu/torchao_npu/ops/hif8_ops.py

类型

Checklist:

如何测试

  1. profliing 中可以看到训练时成功进行了 HiF8 替换

image.png

  1. 对比 HiF8 与 BF16 的 loss 和性能

使用 debug_deepseek_v4_flash_single_node_hif8_qat 进行 HiF8 测试:

[rank0]:[titan] 2026-08-11 19:25:20,451 - root - INFO - step:  1  loss: 15.93523  grad_norm:  8.1593  memory: 46.70GiB(37.92%)  tps: 742  tflops: 162.22  mfu: N/A  elapsed_time_per_step: 44.163s
[rank0]:[titan] 2026-08-11 19:25:20,458 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:25:20,458 - root - INFO - Synchronizing and adjusting timeout for all ProcessGroups to 0:01:40
[rank0]:[titan] 2026-08-11 19:25:50,804 - root - INFO - step:  2  loss: 15.43785  grad_norm:  7.4791  memory: 46.70GiB(37.92%)  tps: 1,080  tflops: 236.08  mfu: N/A  elapsed_time_per_step: 30.345s
[rank0]:[titan] 2026-08-11 19:25:50,805 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:26:21,447 - root - INFO - step:  3  loss: 14.68658  grad_norm:  6.6486  memory: 46.70GiB(37.92%)  tps: 1,070  tflops: 233.84  mfu: N/A  elapsed_time_per_step: 30.636s
[rank0]:[titan] 2026-08-11 19:26:21,448 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:26:21,512 - root - WARNING - Dataset c4_test is being re-looped
[rank0]:[titan] 2026-08-11 19:26:52,206 - root - INFO - step:  4  loss: 14.10184  grad_norm: 12.5218  memory: 46.70GiB(37.92%)  tps: 1,066  tflops: 232.96  mfu: N/A  elapsed_time_per_step: 30.752s
[rank0]:[titan] 2026-08-11 19:26:52,207 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:26:52,208 - torch_npu.env - INFO - get env ASCEND_WORK_PATH = /home/z84448474/torchtitan-npu/torchtitan_npu/experiments/ao_npu/benchmarks/e2e/dsv4_flash_single_node/outputs/A5_PR_ep8_hif8_20260811_192408/profile_traces
[rank0]:[titan] 2026-08-11 19:27:22,718 - root - INFO - step:  5  loss: 13.98096  grad_norm: 18.3722  memory: 46.70GiB(37.92%)  tps: 1,074  tflops: 234.84  mfu: N/A  elapsed_time_per_step: 30.506s
[rank0]:[titan] 2026-08-11 19:27:22,721 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:27:54,895 - root - INFO - step:  6  loss: 13.73508  grad_norm: 13.1807  memory: 46.70GiB(37.92%)  tps: 1,019  tflops: 222.65  mfu: N/A  elapsed_time_per_step: 32.176s
[rank0]:[titan] 2026-08-11 19:27:54,904 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:27:55,453 - torch_npu.env - INFO - get env PYTORCH_NPU_ALLOC_CONF = expandable_segments:True
[rank0]:[titan] 2026-08-11 19:28:25,785 - root - INFO - step:  7  loss: 13.51933  grad_norm:  6.4743  memory: 46.70GiB(37.92%)  tps: 1,061  tflops: 231.98  mfu: N/A  elapsed_time_per_step: 30.883s
[rank0]:[titan] 2026-08-11 19:28:25,789 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:28:25,794 - root - WARNING - Dataset c4_test is being re-looped
[rank0]:[titan] 2026-08-11 19:28:56,429 - root - INFO - step:  8  loss: 13.30869  grad_norm:  6.2143  memory: 46.70GiB(37.92%)  tps: 1,070  tflops: 233.81  mfu: N/A  elapsed_time_per_step: 30.641s
[rank0]:[titan] 2026-08-11 19:28:56,437 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:29:27,026 - root - INFO - step:  9  loss: 13.13290  grad_norm:  4.9959  memory: 46.70GiB(37.92%)  tps: 1,072  tflops: 234.21  mfu: N/A  elapsed_time_per_step: 30.589s
[rank0]:[titan] 2026-08-11 19:29:27,028 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:29:57,671 - root - INFO - step: 10  loss: 13.03679  grad_norm:  4.3206  memory: 46.70GiB(37.92%)  tps: 1,070  tflops: 233.80  mfu: N/A  elapsed_time_per_step: 30.641s
[rank0]:[titan] 2026-08-11 19:29:57,678 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-11 19:29:57,679 - root - INFO - Profiling raw data for rank 0 (pid 2572371) saved at /home/z84448474/torchtitan-npu/torchtitan_npu/experiments/ao_npu/benchmarks/e2e/dsv4_flash_single_node/outputs/A5_PR_ep8_hif8_20260811_192408/profile_traces/profiling_data/localhost.localdomain_2572371_20260811192652208_ascend_pt
[rank0]:[titan] 2026-08-11 19:29:57,679 - root - INFO - For offline parsing, see scripts/parse_profiling_data.py usage in docs/feature_guides/metrics_and_debugging.md.
[rank0]:[titan] 2026-08-11 19:29:57,679 - root - INFO - Sleeping 2 seconds for other ranks to complete
[rank0]:[titan] 2026-08-11 19:29:59,679 - root - INFO - Training completed
[rank0]:[titan] 2026-08-11 19:29:59,804 - root - INFO - Process group destroyed

使用 debug_deepseek_v4_flash_single_node 进行 BF16 测试:

[rank0]:[titan] 2026-08-07 12:54:44,543 - root - INFO - step:  1  loss: 15.91495  grad_norm:  8.8308  memory: 47.73GiB(38.76%)  tps: 764  tflops: 167.04  mfu: 53.54%  elapsed_time_per_step: 42.887s
[rank0]:[titan] 2026-08-07 12:54:44,550 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:54:44,551 - root - INFO - Synchronizing and adjusting timeout for all ProcessGroups to 0:01:40
[rank0]:[titan] 2026-08-07 12:55:14,849 - root - INFO - step:  2  loss: 15.37333  grad_norm:  8.1227  memory: 47.73GiB(38.76%)  tps: 1,082  tflops: 236.40  mfu: 75.77%  elapsed_time_per_step: 30.305s
[rank0]:[titan] 2026-08-07 12:55:14,854 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:55:45,740 - root - INFO - step:  3  loss: 14.58730  grad_norm:  6.2097  memory: 47.73GiB(38.76%)  tps: 1,061  tflops: 231.95  mfu: 74.34%  elapsed_time_per_step: 30.886s
[rank0]:[titan] 2026-08-07 12:55:45,742 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:55:45,803 - root - WARNING - Dataset c4_test is being re-looped
[rank0]:[titan] 2026-08-07 12:56:16,791 - root - INFO - step:  4  loss: 14.15199  grad_norm: 17.5912  memory: 47.73GiB(38.76%)  tps: 1,056  tflops: 230.79  mfu: 73.97%  elapsed_time_per_step: 31.042s
[rank0]:[titan] 2026-08-07 12:56:16,795 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:56:16,796 - torch_npu.env - INFO - get env ASCEND_WORK_PATH = /home/z84448474/torchtitan-npu/torchtitan_npu/experiments/ao_npu/benchmarks/e2e/dsv4_flash_single_node/outputs/A5_PR_ep8_bf16_20260807_125336/profile_traces
[rank0]:[titan] 2026-08-07 12:56:55,293 - root - INFO - step:  5  loss: 14.05346  grad_norm: 21.0675  memory: 47.73GiB(38.76%)  tps: 852  tflops: 186.12  mfu: 59.65%  elapsed_time_per_step: 38.492s
[rank0]:[titan] 2026-08-07 12:56:55,304 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:57:26,037 - root - INFO - step:  6  loss: 13.70714  grad_norm: 10.2875  memory: 47.73GiB(38.76%)  tps: 1,066  tflops: 233.04  mfu: 74.69%  elapsed_time_per_step: 30.742s
[rank0]:[titan] 2026-08-07 12:57:26,044 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:57:26,615 - torch_npu.env - INFO - get env PYTORCH_NPU_ALLOC_CONF = expandable_segments:True
[rank0]:[titan] 2026-08-07 12:57:57,199 - root - INFO - step:  7  loss: 13.51913  grad_norm:  6.4207  memory: 47.73GiB(38.76%)  tps: 1,052  tflops: 229.96  mfu: 73.71%  elapsed_time_per_step: 31.153s
[rank0]:[titan] 2026-08-07 12:57:57,201 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:57:57,204 - root - WARNING - Dataset c4_test is being re-looped
[rank0]:[titan] 2026-08-07 12:58:27,809 - root - INFO - step:  8  loss: 13.26751  grad_norm:  5.5168  memory: 47.73GiB(38.76%)  tps: 1,071  tflops: 234.08  mfu: 75.03%  elapsed_time_per_step: 30.605s
[rank0]:[titan] 2026-08-07 12:58:27,813 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:58:58,412 - root - INFO - step:  9  loss: 13.11628  grad_norm:  6.4751  memory: 47.73GiB(38.76%)  tps: 1,071  tflops: 234.17  mfu: 75.06%  elapsed_time_per_step: 30.593s
[rank0]:[titan] 2026-08-07 12:58:58,414 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:59:28,729 - root - INFO - step: 10  loss: 13.00519  grad_norm:  4.3564  memory: 47.73GiB(38.76%)  tps: 1,081  tflops: 236.38  mfu: 75.76%  elapsed_time_per_step: 30.308s
[rank0]:[titan] 2026-08-07 12:59:28,731 - torchtitan_npu.models.deepseek_v4.parallelize - INFO - indexer loss: 0.0
[rank0]:[titan] 2026-08-07 12:59:28,732 - root - INFO - Profiling raw data for rank 0 (pid 1682168) saved at /home/z84448474/torchtitan-npu/torchtitan_npu/experiments/ao_npu/benchmarks/e2e/dsv4_flash_single_node/outputs/A5_PR_ep8_bf16_20260807_125336/profile_traces/profiling_data/localhost.localdomain_1682168_20260807125616797_ascend_pt
[rank0]:[titan] 2026-08-07 12:59:28,732 - root - INFO - For offline parsing, see scripts/parse_profiling_data.py usage in docs/feature_guides/metrics_and_debugging.md.
[rank0]:[titan] 2026-08-07 12:59:28,732 - root - INFO - Sleeping 2 seconds for other ranks to complete
[rank0]:[titan] 2026-08-07 12:59:30,732 - root - INFO - Training completed
[rank0]:[titan] 2026-08-07 12:59:30,856 - root - INFO - Process group destroyed

对比 HiF8 与 BF16:

  • 开启确定性,seed均设置为42
  • 训练100步

bf16_vs_hif8_0817_loss_gradnorm.png

HiF8 与 BF16 相比 loss gap 为 0.163%
Note:不同服务器测试数据可能会有略微差异

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for nancycyzl's contribution.)
nancycyzlnancycyzl
Aug 12 Created pull request, commit f0701010
nancycyzlnancycyzl
Aug 12 Associated issue: [RFC] 通过 TorchAO 支持 DeepSeek-V4 模型的 HiFloat8 训练
atomgit-bot
atomgit-botCollaborators
Aug 12 Comments:

变更摘要

本 PR 在 torchtitan_npu/experiments/ao_npu/ 实验性插件子包中新增 HiF8(torch_npu.hifloat8)作为量化后端,使 DeepSeek-V4 等模型能在 Ascend 950 NPU 上使用 per-tensor HiF8 精度进行训练。HiF8 与已有的 MX、Block FP8、Float8 row-wise 一样,复用同一套 NpuQuantizeConverter → torchao.quantize_() → 参数级 tensor-subclass 拦截 链路,只是 ParamSwapConfig.weight_config/activation_config 可取的一种新值。核心实现包括自定义 autograd Function 封装 torch_npu.npu_dynamic_quant + npu_quant_matmul/npu_grouped_matmul、对应的 wrapper tensor 子类以拦截 torch.mm/F.linear/torch._grouped_mm 等算子、以及配套的配置类与参数替换处理器。

主要改动

  • 新增 HiF8QuantizeConfig 配置类:在 quant_configs.py 中新增 HiF8QuantizeConfig,其 elem_dtype 固定为 torch_npu.hifloat8,无 block size 或 rounding mode 等可配置项,仅用于接口统一。

  • 新增 HiF8 量化算子 _HiF8QuantMM 与 _HiF8QuantGroupedMM:在 hif8_ops.py 中实现两个自定义 torch.autograd.Function,分别封装 per-tensor 的普通矩阵乘和 grouped 矩阵乘,前向使用 npu_dynamic_quant(quant_mode="pertensor") + npu_quant_matmul/npu_grouped_matmul,反向同样经过量化计算 dA 和 dB。

  • 新增 HiF8TrainingWeightWrapperTensor:在 hif8_wrapper_tensor.py 中新增继承自 BaseTrainingWeightWrapperTensor 的 wrapper tensor 子类,通过 __torch_function__ 拦截 torch.mm、torch.matmul、F.linear、torch.addmm、torch._grouped_mm,调用 to_hif8_then_mm/to_hif8_then_grouped_mm 实现真实的 HiF8 量化计算;同时通过 @register_parameter_swap_handler(HiF8QuantizeConfig) 注册参数替换处理器。

  • 扩展 ParamSwapConfig 支持 HiF8:在 configs.py 中修改 __post_init__ 的类型检查逻辑,将 HiF8QuantizeConfig 纳入 weight_config 和 activation_config 的合法类型列表,同时更新对应的错误消息;test_param_swap_config.py 中同步更新了测试用例的错误消息断言。

  • 新增 E2E 训练配置与启动脚本:在 config_registry.py 中新增 debug_deepseek_v4_flash_single_node_hif8_qat 和 debug_deepseek_v4_flash_single_node_mxfp8_qat 两个训练配置函数,分别使用 HiF8QuantizeConfig 和纯 MXQuantizeConfig 覆盖 attention、shared expert、routed expert 及 e_proj/h_proj 等模块;同时新增 debug_deepseek_v4_single_node_qat_hif8.sh 启动脚本。

likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
Aug 12 Comments:

代码审查

现在我已审查完所有 8 个文件,下面进行最终汇总。

审查结束摘要

优先级 数量
P0 0
P1 0
P2 0
P3 2

整体风险评估:低风险。 此次变更在既有 ParamSwapConfig / NpuQuantizeConverter 框架上新增了 HiF8QuantizeConfig 作为新的量化后端选项,改动范围清晰、结构对称。核心逻辑(configs.py 中的类型白名单、__init__.py 中的导出、config_registry.py 中的训练配置函数)均与已有的 MX/Block FP8 模式完全一致。新增的测试覆盖了 ops 和 wrapper tensor 的正反向正确性、错误输入拒绝、子类保持、FSDP 钩子以及端到端训练步骤。Shell 启动脚本使用了 set -euo pipefail,变量引用均正确加引号。两个 P3 发现均为测试覆盖面的小改进建议,不影响功能正确性。

逐文件审查结果:

  • config_registry.py — 无问题
  • debug_deepseek_v4_single_node_qat_hif8.sh — 无问题
  • test_hif8_ops.py — P3:grouped dtype 测试参数化不一致
  • test_everything.sh — 无问题
  • test_param_swap_config.py — P3:缺少 HiF8QuantizeConfig 正向接受性测试
  • test_hif8_wrapper_tensor.py — 无问题
  • configs.py — 无问题
  • ops/__init__.py — 无问题

所有文件审查完毕。下面是我最终的总结。


审查总结

各文件审查结果

文件 结果
torchtitan_npu/experiments/ao_npu/torchao_npu/ops/hif8_ops.py 发现 1 个 P2 问题:_HiF8QuantGroupedMM 缺少 _ensure_bf16_or_fp16 调用
torchtitan_npu/experiments/ao_npu/torchao_npu/quantization/quant_configs.py 无问题
torchtitan_npu/experiments/ao_npu/torchao_npu/wrapper_tensors/__init__.py 无问题
torchtitan_npu/experiments/ao_npu/torchao_npu/wrapper_tensors/hif8_wrapper_tensor.py 无问题

按严重度统计

  • P0: 0
  • P1: 0
  • P2: 1(_HiF8QuantGroupedMM 正向/反向缺少 dtype 保护,与 _HiF8QuantMM 行为不一致)
  • P3: 0

整体风险判断

低风险。本次新增的 HiF8 量化训练代码整体质量良好,与现有 MX/Block FP8 框架保持一致的架构模式。唯一的问题是 _HiF8QuantGroupedMM 路径遗漏了 _ensure_bf16_or_fp16 的前置 dtype 转换,但在当前 bf16 混合精度训练场景下不会触发(所有张量已是 bf16)。该问题在非 bf16 配置(如 fp32 训练)下才会暴露,建议在合并前修复以保持两条路径的行为一致性。

类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 12 Added label: cann-cla/no
63 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 20 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Aug 20 Merged pull request
刘原辰刘原辰Collaborators
Oct 31 Deleted associated issue: [RFC] 通过 TorchAO 支持 DeepSeek-V4 模型的 HiFloat8 训练
刘原辰刘原辰Collaborators
Oct 31 Associated issue: [RFC] HiF8 delayed scaling 在 torchtitan-npu 中的实现方案——基于延迟历史幅度统计的动态量化缩放
CANN-robot
CANN-robotCollaborators
Oct 31 Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike