Merged
fix: 量化 LI 适配 TND 格式 #1169
fix: 量化 LI 适配 TND 格式 #1169
Merged
zx1029Add on Nov 3
zx1029
zx1029
Nov 3

描述

变更 DeepSeekV4.1 量化 LI 支持 TND 的数据传入。

类型

Checklist:

如何测试

简要描述测试方案,并附上自验证记录。

拉起 examples/deepseek_v4_1/debug/deepseek_v4_1_flash_8p_qat_4k_a5.sh 脚本可测试,自测结果如下:

[rank0]:[titan] 2026-11-02 16:34:46,225 - root - INFO - step:  1  loss: 12.36928  grad_norm: 53.8278  memory: 63.77GiB(69.51%)  tps: 2  tflops: 0.25  mfu: 0.06%
[rank0]:[titan] 2026-11-02 16:34:46,225 - root - INFO - elapsed_time_per_step: 1751.142s
[rank0]:[titan] 2026-11-02 16:34:53,183 - root - INFO - step:  2  loss: 11.98497  grad_norm: 41.8379  memory: 65.32GiB(71.19%)  tps: 589  tflops: 62.80  mfu: 14.54%
[rank0]:[titan] 2026-11-02 16:34:53,184 - root - INFO - elapsed_time_per_step: 6.956s
[rank0]:[titan] 2026-11-02 16:35:00,393 - root - INFO - step:  3  loss: 11.92676  grad_norm: 32.8150  memory: 65.32GiB(71.19%)  tps: 568  tflops: 60.61  mfu: 14.03%
[rank0]:[titan] 2026-11-02 16:35:00,393 - root - INFO - elapsed_time_per_step: 7.208s
[rank0]:[titan] 2026-11-02 16:35:07,305 - root - INFO - step:  4  loss: 11.77176  grad_norm: 27.5992  memory: 65.36GiB(71.23%)  tps: 593  tflops: 63.22  mfu: 14.63%
[rank0]:[titan] 2026-11-02 16:35:07,305 - root - INFO - elapsed_time_per_step: 6.910s
[rank0]:[titan] 2026-11-02 16:35:14,230 - root - INFO - step:  5  loss: 10.53053  grad_norm: 34.6457  memory: 65.36GiB(71.23%)  tps: 592  tflops: 63.11  mfu: 14.61%
[rank0]:[titan] 2026-11-02 16:35:14,230 - root - INFO - elapsed_time_per_step: 6.922s
[rank0]:[titan] 2026-11-02 16:35:21,446 - root - INFO - step:  6  loss: 10.39476  grad_norm: 25.7359  memory: 65.36GiB(71.23%)  tps: 568  tflops: 60.55  mfu: 14.02%
[rank0]:[titan] 2026-11-02 16:35:21,446 - root - INFO - elapsed_time_per_step: 7.215s
[rank0]:[titan] 2026-11-02 16:35:28,933 - root - INFO - step:  7  loss: 10.72087  grad_norm: 23.6140  memory: 65.36GiB(71.23%)  tps: 547  tflops: 58.38  mfu: 13.51%
[rank0]:[titan] 2026-11-02 16:35:28,933 - root - INFO - elapsed_time_per_step: 7.483s
[rank0]:[titan] 2026-11-02 16:35:35,872 - root - INFO - step:  8  loss: 10.66032  grad_norm: 20.6096  memory: 65.36GiB(71.23%)  tps: 591  tflops: 63.00  mfu: 14.58%
[rank0]:[titan] 2026-11-02 16:35:35,872 - root - INFO - elapsed_time_per_step: 6.934s
[rank0]:[titan] 2026-11-02 16:35:42,819 - root - INFO - step:  9  loss:  9.44394  grad_norm: 28.4527  memory: 65.36GiB(71.23%)  tps: 590  tflops: 62.92  mfu: 14.56%
[rank0]:[titan] 2026-11-02 16:35:42,819 - root - INFO - elapsed_time_per_step: 6.943s
[rank0]:[titan] 2026-11-02 16:35:49,799 - root - INFO - step: 10  loss:  9.48571  grad_norm: 23.3999  memory: 65.36GiB(71.23%)  tps: 587  tflops: 62.61  mfu: 14.49%
[rank0]:[titan] 2026-11-02 16:35:49,799 - root - INFO - elapsed_time_per_step: 6.978s

精度和性能验证结果

添加精度和性能验证结果,建议以数据表格和曲线图形式展示。

其他信息

在这里可以添加任何与这个 Pull Request 相关的其他说明。

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for zx1029's contribution.)
zx1029zx1029
Nov 3 Created pull request, commit 6ce45274
atomgit-bot
atomgit-botCollaborators
Nov 3 Comments:

变更摘要

本 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(),保留原形状并统一转为浮点类型。
likedislike
Incorrect?
atomgit-bot
atomgit-botCollaborators
Nov 3 Comments:

代码审查

审查结论

结论概览

  • 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

💬 仅评论

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Nov 3 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Nov 3 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

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

likedislike
91 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Nov 3 Added label: lgtm
CANN-robotCANN-robotCollaborators
Nov 3 Merged pull request
CANN-robot
CANN-robotCollaborators
Nov 3 Comments:

Pull Request 已合并或已关闭。

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

likedislike
1Fire41Fire4Collaborators
24 days ago Associated issue: [RFC] TorchTitan v0.4.0 分阶段升级规划与进度跟踪
CANN-robot
CANN-robotCollaborators
24 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike