Merged
[fix](dsv4): avoid unbacked shape guards in fused Compressor #935
[fix](dsv4): avoid unbacked shape guards in fused Compressor #935
Merged
xuyujunAdd on Sep 24
xuyujun
xuyujunCollaborators
Sep 24

描述

修复 DeepSeek V4 融合 Compressor 在 CP + torch.compile 下的无提示动态尺寸守卫错误。

计划构建时记录空计划标记,供编译区选择空输入回退;空 rank 仍参与必需的 CP collective。状态表尺寸使用 torch.sym_min/max 表达,保留原有尺寸语义。

在 torchtitan_npu/ops/ascendc/compressor.py 提供按需加载的 CANN 兼容入口:通过公开 torch.library.register_fake 接口,为 _compressor_forward 注册支持符号尺寸的 fake 实现,TND 输出容量以严格等价的整数公式 (a + b - abs(a - b)) // 2 表达 min(a, b),同时避免 Python 比较守卫和 A5 上报告的 torch.sym_min 返回 NotImplemented 导致 new_empty 失败。保留二维/三维输出形状和辅助输出 FP32 契约,继续复用 CANN 的计算内核、autograd 和反向实现。无需修改 CANN 安装包;仅在融合 override 构造时注册,不增加训练热路径操作。

类型

Checklist:

如何测试

153 容器,CPU 测试,无需执行 A5 内核:

TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest -q \
  tests/unit_tests/ops/ascendc/test_compressor_fake.py \
  tests/unit_tests/override/deepseek_v4/test_compressor.py \
  --disable-warnings --no-header

前一版两文件合计 45 passed。本轮改用整数容量公式并补充 checkpoint 用例后,新增测试文件独立重跑 12 passed。另在不加载 NPU 扩展的 CPU Inductor + checkpoint 探针中,C4/C128、每种 3 组动态输入,共 6 组前后向与 eager 对照通过。

覆盖:

  • C4/C128、二维/三维及空输入的 fake 输出形状和 dtype。
  • 通过真实 custom-op fake 分发复现独立无提示符号的 Python min 失败,注册兼容 fake 后相同场景通过。
  • 重复加载兼容入口,以及有/无 checkpoint 的 aot_eager 前后向与 eager 一致性;模拟 sym_min 返回 NotImplemented,验证 fake 不依赖该接口。
  • CP/非 CP、连续/HeadTail 布局、空 rank、Compressor 前向和梯度对照。

独立检查 153 w0920 包内的真实 _compressor_forward_fake:原函数在 C4/C128 无提示符号输入上均复现 guard 错误,将 min 改为 torch.sym_min 后均通过;10 组具体尺寸检查通过。该探针未作为生产代码提交。

Ruff、格式、git diff --check 和新增模块的 Pyrefly 检查通过。pre-commit 基础检查通过;完整 OAT 因本机缺少 oat-py 且系统 Python 受管理而失败,项目 Pyrefly 因本机缺少仓库依赖失败,未声称完整检查通过。

其他信息

A5 真实融合算子的完整 compile 训练:
EP4FSDP4CP2不开启compressor算子:

[rank0]:[titan] 2026-10-09 09:24:44,371 - root - INFO - step:  1  loss: 12.32520  grad_norm: 10.2012  memory: 35.65GiB(38.90%)  tps: 13  tflops: 1.09  mfu: 0.22%
[rank0]:[titan] 2026-10-09 09:24:44,371 - root - INFO - elapsed_time_per_step: 163.713s
[rank0]:[titan] 2026-10-09 09:24:44,371 - root - INFO - Synchronizing and adjusting timeout for all ProcessGroups to 0:10:00
[rank0]:[titan] 2026-10-09 09:25:31,172 - root - INFO - NPU deterministic env enabled: HCCL_DETERMINISTIC, CLOSE_MATMUL_K_SHIFT
[rank0]:[titan] 2026-10-09 09:25:54,794 - root - INFO - NPU deterministic env enabled: HCCL_DETERMINISTIC, CLOSE_MATMUL_K_SHIFT
[rank0]:[titan] 2026-10-09 09:26:42,308 - root - INFO - NPU deterministic env enabled: HCCL_DETERMINISTIC, CLOSE_MATMUL_K_SHIFT
[rank0]:[titan] 2026-10-09 09:26:47,158 - root - INFO - step:  2  loss: 12.32486  grad_norm: 10.4411  memory: 35.65GiB(38.90%)  tps: 17  tflops: 1.45  mfu: 0.30%
[rank0]:[titan] 2026-10-09 09:26:47,158 - root - INFO - elapsed_time_per_step: 122.781s
[rank0]:[titan] 2026-10-09 09:27:14,686 - root - INFO - step:  3  loss: 12.27986  grad_norm:  9.3116  memory: 35.65GiB(38.90%)  tps: 74  tflops: 6.48  mfu: 1.33%
[rank0]:[titan] 2026-10-09 09:27:14,686 - root - INFO - elapsed_time_per_step: 27.517s
[rank0]:[titan] 2026-10-09 09:27:42,508 - root - INFO - step:  4  loss: 12.30932  grad_norm:  9.3119  memory: 35.65GiB(38.90%)  tps: 74  tflops: 6.41  mfu: 1.32%
[rank0]:[titan] 2026-10-09 09:27:42,509 - root - INFO - elapsed_time_per_step: 27.818s
[rank0]:[titan] 2026-10-09 09:27:46,713 - root - INFO - step:  5  loss: 12.25794  grad_norm:  9.2820  memory: 35.65GiB(38.90%)  tps: 488  tflops: 42.45  mfu: 8.73%
[rank0]:[titan] 2026-10-09 09:27:46,714 - root - INFO - elapsed_time_per_step: 4.200s
[rank0]:[titan] 2026-10-09 09:27:50,974 - root - INFO - step:  6  loss: 12.24232  grad_norm:  9.2438  memory: 35.65GiB(38.90%)  tps: 481  tflops: 41.90  mfu: 8.62%
[rank0]:[titan] 2026-10-09 09:27:50,974 - root - INFO - elapsed_time_per_step: 4.256s
[rank0]:[titan] 2026-10-09 09:27:55,153 - root - INFO - step:  7  loss: 12.23635  grad_norm: 11.5973  memory: 35.65GiB(38.90%)  tps: 491  tflops: 42.71  mfu: 8.79%
[rank0]:[titan] 2026-10-09 09:27:55,153 - root - INFO - elapsed_time_per_step: 4.175s
[rank0]:[titan] 2026-10-09 09:27:59,254 - root - INFO - step:  8  loss: 12.19272  grad_norm: 10.1203  memory: 35.65GiB(38.90%)  tps: 500  tflops: 43.53  mfu: 8.96%
[rank0]:[titan] 2026-10-09 09:27:59,254 - root - INFO - elapsed_time_per_step: 4.096s
[rank0]:[titan] 2026-10-09 09:28:03,435 - root - INFO - step:  9  loss: 12.13632  grad_norm: 10.1762  memory: 35.65GiB(38.90%)  tps: 490  tflops: 42.68  mfu: 8.78%
[rank0]:[titan] 2026-10-09 09:28:03,435 - root - INFO - elapsed_time_per_step: 4.178s
[rank0]:[titan] 2026-10-09 09:28:07,500 - root - INFO - step: 10  loss: 12.08651  grad_norm:  9.4147  memory: 35.65GiB(38.90%)  tps: 504  tflops: 43.92  mfu: 9.04%

EP4FSDP4CP2开启compressor算子:

[rank0]:[titan] 2026-10-09 09:35:09,116 - root - INFO - step:  1  loss: 12.32510  grad_norm: 10.2009  memory: 35.65GiB(38.89%)  tps: 10  tflops: 0.85  mfu: 0.17%
[rank0]:[titan] 2026-10-09 09:35:09,116 - root - INFO - elapsed_time_per_step: 209.792s
[rank0]:[titan] 2026-10-09 09:35:09,117 - root - INFO - Synchronizing and adjusting timeout for all ProcessGroups to 0:10:00
[rank0]:[titan] 2026-10-09 09:35:36,527 - root - INFO - NPU deterministic env enabled: HCCL_DETERMINISTIC, CLOSE_MATMUL_K_SHIFT
[rank0]:[titan] 2026-10-09 09:35:52,296 - root - INFO - NPU deterministic env enabled: HCCL_DETERMINISTIC, CLOSE_MATMUL_K_SHIFT
[rank0]:[titan] 2026-10-09 09:36:20,010 - root - INFO - NPU deterministic env enabled: HCCL_DETERMINISTIC, CLOSE_MATMUL_K_SHIFT
[rank0]:[titan] 2026-10-09 09:36:24,574 - root - INFO - step:  2  loss: 12.32494  grad_norm: 10.4393  memory: 35.65GiB(38.89%)  tps: 27  tflops: 2.36  mfu: 0.49%
[rank0]:[titan] 2026-10-09 09:36:24,574 - root - INFO - elapsed_time_per_step: 75.453s
[rank0]:[titan] 2026-10-09 09:36:43,959 - root - INFO - step:  3  loss: 12.27992  grad_norm:  9.3116  memory: 35.65GiB(38.89%)  tps: 106  tflops: 9.20  mfu: 1.89%
[rank0]:[titan] 2026-10-09 09:36:43,959 - root - INFO - elapsed_time_per_step: 19.383s
[rank0]:[titan] 2026-10-09 09:37:03,612 - root - INFO - step:  4  loss: 12.30960  grad_norm:  9.3120  memory: 35.65GiB(38.89%)  tps: 104  tflops: 9.07  mfu: 1.87%
[rank0]:[titan] 2026-10-09 09:37:03,613 - root - INFO - elapsed_time_per_step: 19.650s
[rank0]:[titan] 2026-10-09 09:37:07,544 - root - INFO - step:  5  loss: 12.25822  grad_norm:  9.2818  memory: 35.65GiB(38.89%)  tps: 521  tflops: 45.38  mfu: 9.34%
[rank0]:[titan] 2026-10-09 09:37:07,544 - root - INFO - elapsed_time_per_step: 3.930s
[rank0]:[titan] 2026-10-09 09:37:11,475 - root - INFO - step:  6  loss: 12.24234  grad_norm:  9.2434  memory: 35.65GiB(38.89%)  tps: 521  tflops: 45.40  mfu: 9.34%
[rank0]:[titan] 2026-10-09 09:37:11,476 - root - INFO - elapsed_time_per_step: 3.928s
[rank0]:[titan] 2026-10-09 09:37:15,409 - root - INFO - step:  7  loss: 12.23697  grad_norm: 11.5976  memory: 35.65GiB(38.89%)  tps: 521  tflops: 45.36  mfu: 9.33%
[rank0]:[titan] 2026-10-09 09:37:15,410 - root - INFO - elapsed_time_per_step: 3.931s
[rank0]:[titan] 2026-10-09 09:37:19,163 - root - INFO - step:  8  loss: 12.19271  grad_norm: 10.1193  memory: 35.65GiB(38.89%)  tps: 546  tflops: 47.53  mfu: 9.78%
[rank0]:[titan] 2026-10-09 09:37:19,163 - root - INFO - elapsed_time_per_step: 3.751s
[rank0]:[titan] 2026-10-09 09:37:23,090 - root - INFO - step:  9  loss: 12.13591  grad_norm: 10.1740  memory: 35.65GiB(38.89%)  tps: 522  tflops: 45.43  mfu: 9.35%
[rank0]:[titan] 2026-10-09 09:37:23,090 - root - INFO - elapsed_time_per_step: 3.925s
[rank0]:[titan] 2026-10-09 09:37:26,986 - root - INFO - step: 10  loss: 12.08647  grad_norm:  9.4152  memory: 35.65GiB(38.89%)  tps: 526  tflops: 45.78  mfu: 9.42%
[rank0]:[titan] 2026-10-09 09:37:26,986 - root - INFO - elapsed_time_per_step: 3.895s
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for xuyujun's contribution.)
xuyujunxuyujunCollaborators
Sep 24 Created pull request, commit 6041c37f
CANN-robotCANN-robotCollaborators
Sep 24 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 24 Comments:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Sep 24 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 ✅ panchao-gitcode, lrwei0709 (2/2) ✅ panchao-gitcode (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
xuyujunxuyujunCollaborators
Sep 24 Pre merge succeeded (commit_id: 100c402539e249457822b4990ffbced0709d66ca)
48 messages are collapsed here。 See more
weijinyi
weijinyiCollaborators
Oct 10 Comments:

/lgtm

likedislike
panchao-gitcode
panchao-gitcodeCollaborators
Oct 10 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Oct 10 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Oct 10 Merged pull request
xuyujunxuyujunCollaborators
Oct 24 Associated issue: [RFC] Compressor融合算子支持CP