Pull Request has been successfully merged by CANN-robot.
(Thank you for zx1029's contribution.)变更摘要
本 PR 针对 DeepSeekV4.1 量化 LI(Lightning Indexer)适配 TND 格式数据传入的修复。在 experiments/torchao-npu/torchao_npu/quantized_modules/deepseek_v4_1/lightning_indexer.py 中,QuantV41LightningIndexer 的前向逻辑移除了对 batch 维必须为 1 的强制校验与 squeeze(0)/unsqueeze 的维度重排处理,改为将 idx_q_BLHiDi、idx_k_BNDi、weights_BLHi 以原始形状直接传入 TND 内核 _QuantV41LightningIndexerTND.apply,仅对 idx_k_BNDi 保留 unsqueeze(1) 以维持其所需的维度语义。
主要改动
- 移除 batch 维强校验:删除了当
idx_q_BLHiDi、idx_k_BNDi、weights_BLHi的第 0 维不为 1 时抛出ValueError("requires one packed TND row")的判断,使 batch 维不再被限制为 1。 - 取消查询张量的 squeeze:
idx_q_BLHiDi由idx_q_BLHiDi.squeeze(0)改为直接传入原张量,保持其携带的 batch 维信息。 - 调整 key 张量维度处理:
idx_k_BNDi由idx_k_BNDi.squeeze(0).unsqueeze(1)改为idx_k_BNDi.unsqueeze(1),只做单次维度扩展。 - 权重张量不再压缩且提前转 float:
weights_BLHi由weights_BLHi.squeeze(0).float()改为weights_BLHi.float(),保留原形状并统一转为浮点类型。


代码审查
审查结论
结论概览
- P0/P1:无
- P2:1 项
- P3:1 项
逐文件核查
experiments/torchao-npu/torchao_npu/quantized_modules/deepseek_v4_1/lightning_indexer.py:本文件改动本身在内部是自洽的——删除squeeze(0)/unsqueeze组合后,q 保持 rank-3[T,H,D](与_pack_mxfp4的ndim==3要求一致)、k 经.unsqueeze(1)变[N,1,Di](与同族测试test_sparse_attention.py:384-386直接调_QuantV41LightningIndexerTND.apply的形状一致)、w 保持 rank-2[T,H];autograd 边(unsqueeze/float 均为可微算子)未断,返回的reshape(1,-1,topk)输出契约也未变。发现 2 项问题(1×P2、1×P3),均与该单边契约变更带来的仓库内不一致/校验缺口有关。
风险判断
改动方向正确(LI 前端切到 TND 传参),核心计算路径无逻辑错误;主要风险是契约只在本文件单边变更:上游包装层命名与仓库内单测仍停留在 BLD 形状,而单测因 bypass 掉带 rank 校验的 _pack_mxfp4 而对旧形状“假通过”,新 TND 契约缺覆盖;另外 weights 失去了原有的唯一一致性校验。建议合并前补一条不 bypass rank 校验的形状用例,并统一形参命名。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


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
zhaoxi1029, 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.


描述
变更 DeepSeekV4.1 量化 LI 支持 TND 的数据传入。
类型
Checklist:
如何测试
简要描述测试方案,并附上自验证记录。
拉起 examples/deepseek_v4_1/debug/deepseek_v4_1_flash_8p_qat_4k_a5.sh 脚本可测试,自测结果如下:
精度和性能验证结果
添加精度和性能验证结果,建议以数据表格和曲线图形式展示。
其他信息
在这里可以添加任何与这个 Pull Request 相关的其他说明。