Pull Request has been successfully merged by CANN-robot.
(Thank you for xiaomagemafeifei's contribution.)变更摘要
本次重构将 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反向继续调用 BF16sparse_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仅保留 BF16lightning_indexerforward,AscSelector.Config删除legacy字段;lightning_indexer/__init__.py的ascoverride 去掉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必须同时提供。


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


背景
本次重构将 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 层角色自动选择:
根据 candidate table 中连续的 -1 padding 恢复有效 candidate 长度。
保持 candidate pool 的跨层布局及透传语义。
对 top-k indices 统一排序,并保证无效位置位于末尾。
前向使用 MXFP4 QLI/QSLI,反向继续使用 BF16 SLIKG 计算 indexer 梯度。
2. 重构 TorchAO-NPU converter
3. 完善 indexer teacher 梯度链路
4. 调整 V4.1 sparse attention 量化
5. 简化 BF16 override
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 配对。
验证结果
