Merged
[feat] unify TorchAO quantization converter for LI and metadata #800
[feat] unify TorchAO quantization converter for LI and metadata #800
Merged
zx1029Add on Sep 12
zx1029
zx1029
Sep 12

refactor: unify TorchAO quantization converter for LI and metadata

背景

TorchAO-NPU 原有量化转换流程主要面向 Linear、BatchedLinear 和 GroupedExperts。LightningIndexer(LI)及其 metadata 仍需要单独的转换入口,导致量化配置、quant_mode 映射和 metadata 替换逻辑分散,LI kernel 的接入和模型配置校验也容易出现不一致。

本提交将量化 LI module 与 LI metadata 纳入统一的 NpuQuantizeConverter,在保留原有高精度 LI 路径的前提下,为 DeepSeek-V4 提供 MXFP4、MXFP8 和 FP8 LI 量化配置。

主要改动

  • 扩展 NpuQuantizeConverter:
    • 支持 module-level swap 和 metadata config replacement。
    • 统一遍历 Linear、BatchedLinear、GroupedExperts、LightningIndexer 及 LightningIndexerMetadata 配置节点。
    • 为 metadata replacement 增加统一日志,便于确认实际替换结果。
  • 新增 QuantLightningIndexerConfig:
    • 支持 MXFP4、MXFP8、FP8 三种 Q/K 输入量化模式。
    • 统一 quant_mode 映射:FP8=1、MXFP8=3、HiFloat8=4、MXFP4=5。
    • 通过 input adapter 将 TorchTitan 的 varlen metadata、context parallel gather 结果和 LI 输入转换为 kernel 所需格式。
  • 新增量化 LI operator bridge 和 metadata provider:
    • 对接 quant_lightning_indexer 和 quant_lightning_indexer_metadata。
    • 支持 FP8、MXFP8、MXFP4 输入量化及对应 descale 格式。
  • 将 LightningIndexerMetadata 替换为量化 metadata adapter,使 metadata 构建与 LI kernel 的 quant_mode、layout、mask mode 和 compression ratio 保持一致。
  • 在 QuantizationExtensionConfig 中增加 li_quantization 配置,并校验其只能在量化训练开启时使用。
  • 移除 LightningIndexerKernelConfig 对固定 TND / mask_mode=3 / cmp_ratio=4 的强制校验,使其他模型能够提供自己的 LI kernel 参数;默认值保持不变。
  • 增加 LI converter、FP8 输入量化和配置替换相关单元测试,并补充 DeepSeek-V4 三种 LI 量化模式的集成测试配置。

行为边界

  • enable_quantized_training=false 或 li_quantization=null 时,不替换高精度 LI module 和 metadata。
  • LI 量化仅支持 mxfp4、mxfp8 和 fp8 配置值。
  • 当前 converter 的 LI 配置过滤针对 DeepSeek-V4 的 config-tree FQN;其他模型需要提供匹配的 LI 配置结构和 kernel contract。
  • 其他 layout、mask mode、compression ratio 是否被具体 CANN kernel 支持,仍由对应后端 kernel 契约决定。
  • 真实 NPU 数值、编译模式和多卡 context parallel 行为需要在匹配的 torch-npu、CANN 和 Ascend NPU 环境中验证。

验证

已完成功能验证:

MXFP8 + LI MXFP4 loss

2026-09-12 18:54:28,388 - root - INFO - step:  1  loss: 15.96178  grad_norm:  5.9943  memory: 42.96GiB(46.87%)  tps: 585  tflops: 54.15  mfu: 11.14%
2026-09-12 18:54:55,139 - root - INFO - step:  2  loss: 15.91458  grad_norm:  5.6147  memory: 42.96GiB(46.87%)  tps: 1,225  tflops: 113.31  mfu: 23.32%
2026-09-12 18:55:22,416 - root - INFO - step:  3  loss: 15.81922  grad_norm:  5.9411  memory: 42.96GiB(46.87%)  tps: 1,201  tflops: 111.13  mfu: 22.87%
2026-09-12 18:55:49,794 - root - INFO - step:  4  loss: 15.66073  grad_norm:  5.9806  memory: 42.96GiB(46.87%)  tps: 1,197  tflops: 110.72  mfu: 22.78%
2026-09-12 18:56:16,795 - root - INFO - step:  5  loss: 15.43990  grad_norm:  5.8961  memory: 42.96GiB(46.87%)  tps: 1,214  tflops: 112.26  mfu: 23.10%

MXFP8 + LI MXFP8 loss

2026-09-12 18:58:51,872 - root - INFO - step:  1  loss: 15.96181  grad_norm:  5.9935  memory: 42.96GiB(46.87%)  tps: 566  tflops: 52.37  mfu: 10.78%
2026-09-12 18:59:18,260 - root - INFO - step:  2  loss: 15.91471  grad_norm:  5.6120  memory: 42.96GiB(46.87%)  tps: 1,242  tflops: 114.87  mfu: 23.64%
2026-09-12 18:59:45,212 - root - INFO - step:  3  loss: 15.81970  grad_norm:  5.9392  memory: 42.96GiB(46.87%)  tps: 1,216  tflops: 112.47  mfu: 23.14%
2026-09-12 19:00:12,062 - root - INFO - step:  4  loss: 15.66053  grad_norm:  5.9777  memory: 42.96GiB(46.87%)  tps: 1,220  tflops: 112.89  mfu: 23.23%
2026-09-12 19:00:38,722 - root - INFO - step:  5  loss: 15.43978  grad_norm:  5.8965  memory: 42.96GiB(46.87%)  tps: 1,229  tflops: 113.70  mfu: 23.40%

MXFP8 + LI FP8 loss

2026-09-12 19:04:15,411 - root - INFO - step:  1  loss: 15.96179  grad_norm:  5.9938  memory: 42.96GiB(46.87%)  tps: 523  tflops: 48.42  mfu: 9.96%
2026-09-12 19:04:42,034 - root - INFO - step:  2  loss: 15.91478  grad_norm:  5.6136  memory: 42.96GiB(46.87%)  tps: 1,231  tflops: 113.86  mfu: 23.43%
2026-09-12 19:05:09,185 - root - INFO - step:  3  loss: 15.81918  grad_norm:  5.9386  memory: 42.96GiB(46.87%)  tps: 1,207  tflops: 111.64  mfu: 22.97%
2026-09-12 19:05:36,366 - root - INFO - step:  4  loss: 15.66041  grad_norm:  5.9787  memory: 42.96GiB(46.87%)  tps: 1,206  tflops: 111.52  mfu: 22.95%
2026-09-12 19:06:03,556 - root - INFO - step:  5  loss: 15.44001  grad_norm:  5.8963  memory: 42.96GiB(46.87%)  tps: 1,205  tflops: 111.48  mfu: 22.94%
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for zx1029's contribution.)
zx1029zx1029
Sep 12 Created pull request, commit ef8f6a7f
CANN-robotCANN-robotCollaborators
Sep 12 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Sep 12 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 12 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/torchtitan-npu ✅ zhuofanshen, wanghui0 (2/2) ✅ zhuofanshen (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

zhaoxi1029, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
zx1029zx1029
Sep 12 Pre merge succeeded (commit_id: 895a65cd42d787029cda996507ceae200108769d)
103 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Sep 15 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 15 Added label: ci-pipeline-failed
wanghui0wanghui0Collaborators
26 days ago Associated issue: [RFC] DeepSeek-V4/V4.1 LI模块支持A4C4、A8C8(MXFP8/HiF8)
wanghui0wanghui0Collaborators
26 days ago Deleted associated issue: [RFC] DeepSeek-V4/V4.1 LI模块支持A4C4、A8C8(MXFP8/HiF8)
wanghui0wanghui0Collaborators
26 days ago Associated issue: [RFC] DeepSeek-V4/V4.1 LI模块支持A4C4、A8C8(MXFP8/HiF8)