Merged
refactor: improve DeepSeek-V4.1 LI low-precision quantization #941
refactor: improve DeepSeek-V4.1 LI low-precision quantization #941
Merged
xiaomagemafeifeiAdd on Sep 29
xiaomagemafeifei
Sep 29

背景

本次重构将 DeepSeek-V4.1 Lightning Indexer(LI)的低精度实现从 BF16 override 中拆分,统一接入 TorchAO-NPU 量化转换流程。

重构后,BF16 override 仅作为默认 fallback,MXFP4 QLI/QSLI 通过量化配置独立启用,同时保留 sparse attention 到 indexer 的 teacher 梯度传递链路。

主要改动

1. 新增 V4.1 QLI/QSLI 量化模块

  • 新增 QuantV41LightningIndexer 及对应 module swap 配置。

  • 使用 torch_npu.npu_dynamic_mx_quant 将 LI 的 Q/K 输入转换为 MXFP4。

  • 根据 indexer 层角色自动选择:

    • QLI:生成 top-k selection 及 candidate pool。
    • QSLI:使用上游 candidate pool 完成稀疏检索。
  • 根据 candidate table 中连续的 -1 padding 恢复有效 candidate 长度。

  • 保持 candidate pool 的跨层布局及透传语义。

  • 对 top-k indices 统一排序,并保证无效位置位于末尾。

  • 前向使用 MXFP4 QLI/QSLI,反向继续使用 BF16 SLIKG 计算 indexer 梯度。

2. 重构 TorchAO-NPU converter

  • 为 DeepSeek-V4.1 Selector 墙加独立量化匹配规则。
  • 通过 --extension.quantization.li-quantization mxfp4 启用 V4.1 QLI/QSLI。
  • V4.1 当前仅支持 mxfp4,对 fp8、mxfp8 和 hif8 配置显式报错。
  • 保持 DeepSeek-V4 原有 LI 量化逻辑不变。
  • 为量化后的 selector 和 sparse attention 动态配置类补充 teacher consumer/provider 标记。

3. 完善 indexer teacher 梯度链路

  • sparse attention 反向通过 topk_scores 输出压缩 attention 的 softmax 边际,作为 SLIKG teacher。
  • 对 topk_indices == -1 的无效槽位将 teacher 值置零。
  • 仅在训练模式且存在 compressed KV 时构建 teacher 梯度链路。
  • 缺少 topk_scores、compressed KV 和 indices 不匹配时提前报错。
  • 支持以下四种组合:
    • BF16 LI + BF16 sparse attention
    • MXFP4 QLI/QSLI + BF16 sparse attention
    • BF16 LI + quantized sparse attention
    • MXFP4 QLI/QSLI + quantized sparse attention

4. 调整 V4.1 sparse attention 量化

  • sliding-window KV 使用 mxfp8_bf16 fake quant,group size 为 32。
  • compressed KV 由 source Compressor 完成一次 MXFP4 fake quant,消费层直接复用。
  • 前向与反向复用相同的量化后 SWA operand,避免计算路径不一致。
  • 修正 compress_ratio=1 时 residual tensor 的传递语义。
  • 保留 compress_ratio=0/1/2 对应的 window-only、full-resolution 和 compressed 路径。

5. 简化 BF16 override

  • 从 lightning_indexer.asc 中移除 QLI/QSLI 和 legacy 分支。
  • BF16 override 仅保留 LI forward 和 SLIKG backward。
  • 低精度实现统一由 TorchAO-NPU converter 管理,避免 override 与量化模块重复维护。
  • 保留 teacher provider/consumer 的构造期配对检查,防止只启用一侧后进入错误训练路径。

6. 更新脚本和文档

  • V4.1 QAT 脚本新增:

    --extension.quantization.li-quantization mxfp4

  • 单卡精简脚本改为通过 TorchAO-NPU 启用 QLI/QSLI,不再使用 override 参数选择量化实现。

  • 更新安装、快速上手、低精度训练和 override 文档。

  • 补充 QLI/QSLI、sparse attention、BF16 fallback、teacher 配对关系及启用限制说明。

使用方式

--extension.quantization.enable-quantized-training \
--extension.quantization.li-quantization mxfp4 \
--extension.quantization.enable-sparse-attention-quantization

li-quantization 与 enable-sparse-attention-quantization 可以独立替换对应的 BF16 fallback,但最终配置必须同时存在 indexer teacher provider 和 consumer。

限制

  • 当前面向 Ascend 950(A5)环境。

  • V4.1 LI 当前仅支持 MXFP4。

  • QLI/QSLI 当前要求 packed TND 的模型 batch size 为 1。

  • 依赖对应版本的 torch_npu、cann_ops_transformer 及自定义算子。

  • USE_GOLDEN=1 不会导入标准 A5 的 BF16 teacher fallback,需要确保替代实现仍保持 provider/consumer 配对。

    验证结果
    loss_curve_20261008_steps_1_100.png

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for xiaomagemafeifei's contribution.)
xiaomagemafeifeixiaomagemafeifei
Sep 29 Created pull request, commit c78ecc37
atomgit-bot
atomgit-botCollaborators
Sep 29 Comments:

变更摘要

本次重构将 DeepSeek-V4.1 Lightning Indexer(LI)的低精度实现从 BF16 override 中拆出,统一接入 TorchAO-NPU 量化转换流程:新增 QuantV41LightningIndexer 量化模块与 QuantV41LightningIndexerConfig module swap 配置,通过 torch_npu.npu_dynamic_mx_quant 将 Q/K 输入转为 MXFP4 并区分 QLI/QSLI 角色;interfaces/torchao_converter.py 为 V4.1 增加独立 LI 量化匹配规则并显式限制仅支持 mxfp4;lightning_indexer.ascendc.py 精简为只保留 BF16 LI forward 与 SLIKG backward,低精度实现交由量化模块管理;同时在 sparse attention 侧引入 topk_scores 作为 teacher 载体,打通 SMLAG 到 SLIKG 的梯度链路。

主要改动

  • 新增 V4.1 QLI/QSLI 量化模块: 新增 v41_lightning_indexer.py,QuantV41LightningIndexer.forward 根据 is_source 与 mode 选择 quant_lightning_indexer(生成候选池)或 quant_sparse_lightning_indexer(消费上游候选池),_pack_mxfp4 完成 MXFP4 打包,_QuantV41LightningIndexerTND 反向继续调用 BF16 sparse_lightning_indexer_kl_loss_grad 计算 indexer 梯度。
  • 新增 module swap 配置与注册: 新增 quant_v41_lightning_indexer.py 定义 QuantV41LightningIndexerConfig 与 _quant_v41_lightning_indexer_transform,并在 torchao_npu/__init__.py、configs/__init__.py、module_swap_configs/__init__.py 中导出该配置;quant_v41_sparse_attention.py 的 _quantized_forward 新增 topk_scores 参数透传,保持 SMLAG-to-SLIKG teacher 边。
  • 重构 TorchAO-NPU converter: interfaces/torchao_converter.py 将 V41Selector.Config 加入默认目标类型,为 lightning_indexer 增加 match_config_fqn_suffix(".attention.indexer.selector") 过滤规则;对 model_type == "v41" 使用 QuantV41LightningIndexerConfig 并对其非 mxfp4 配置抛出 ValueError,V4 原有 LI 量化路径保持不变;并由 V41Selector/CompressedSparseInnerAttention2 分别设置 consumes_indexer_teacher 与 provides_indexer_teacher 标记。
  • 简化 BF16 override: lightning_indexer/ascendc.py 移除 _pack_mxfp4、_quantized_geometry、_candidate_length、QLI/QSLI 与 legacy 分支,_LightningIndexerTND 仅保留 BF16 lightning_indexer forward,AscSelector.Config 删除 legacy 字段;lightning_indexer/__init__.py 的 asc override 去掉 legacy 参数,描述更新为仅驱动 BF16 LI forward。
  • 完善 sparse attention teacher 梯度链路: v41_sparse_attention.py 的 _SparseMLA 新增 topk_scores、wants_teacher 输入,反向将 cmp_softmax_l1_norm 对 topk_indices < 0 置零后作为 teacher 返回;QuantV41SparseAttention.forward 在训练且存在 cmp_k 时要求 topk_scores,并校验 cmp_k 与 topk_indices 必须同时提供。
likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
Sep 29 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 29 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 29 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 ✅ wanghui0, xuyujun (2/2) ✅ wanghui0 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
94 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Oct 10 Added label: lgtm
CANN-robotCANN-robotCollaborators
Oct 10 Merged pull request
CANN-robot
CANN-robotCollaborators
Oct 10 Comments:

Pull Request 已合并或已关闭。

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

likedislike
wanghui0wanghui0Collaborators
26 days ago Associated issue: [RFC] DeepSeek-V4/V4.1 LI模块支持A4C4、A8C8(MXFP8/HiF8)
CANN-robot
CANN-robotCollaborators
26 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike