Pull Request已成功合入, 合并人@ascend-robot
(感谢 L1919_snow 的贡献)变更摘要
本次 PR 主要为 NPU 算子新增单元测试,覆盖 NpuDropPath 和 npu_renorm_backward 两个功能模块。测试均基于 torch_npu 运行在 NPU 设备上,验证前向传播、反向传播的正确性,以及与 PyTorch 原生自动微分结果的一致性。变更不涉及任何业务代码修改,仅新增 2 个测试文件。
主要改动
- 新增
NpuDropPath单元测试: 在test/test_base_ops/test_drop_path.py中新增TestNpuDropPath测试类,覆盖基础前向传播、drop_prob=0时输出与输入一致、反向传播 autograd 流程、以及训练/评估模式下丢弃行为的差异验证。 - 新增
npu_renorm_backward单元测试: 在test/test_base_ops/test_npu_renorm_backward.py中新增TestNPURenormBackward测试类,验证自定义反向算子torch_npu.npu_renorm_backward的基础调用,并与 PyTorch 原生torch.renorm自动微分结果进行精度对比。 - 多维度参数覆盖:
npu_renorm_backward测试涵盖多种 p 值(0.5、1、1.5、2、3)、不同 dim(含负数)、不同 maxnorm、3D/4D 张量、float16/bfloat16 低精度、全零输入及大张量等边界场景,确保算子实现的鲁棒性。


代码审查
审查总结
审查覆盖的文件:
- test/test_base_ops/test_drop_path.py — 发现 1 个问题(P2)
- test/test_base_ops/test_npu_renorm_backward.py — 未发现问题
按优先级统计:
| 优先级 | 数量 |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 1 |
| P3 | 0 |
整体风险评估:
此 PR 新增两个 NPU 算子单元测试文件,共 227 行代码。test_npu_renorm_backward.py 覆盖了多种 p 值、维度、maxnorm 值、不同精度(float16/bfloat16)、边界情况(零张量、负 dim、大张量),测试逻辑完整且正确。test_drop_path.py 存在一处测试质量问题:test_train_eval_mode 对训练模式的 dropout 行为未做断言,导致该测试在 NpuDropPath 训练模式存在 bug 时仍会通过,降低了测试的回归保护价值。整体风险较低,建议修复 test_train_eval_mode 中的缺失断言后再合入。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 2 |
💬 仅评论


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
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-Ascend/op-plugin | ✅ liangsongwei, culechan (2/2) | ✅ liangsongwei (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
l1919_snow, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Linking Issue Notice
@l1919_snow , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .


compile


ascend docs pipeline is running...


ascend docs pipeline is running...


✅ 跳过 docs ci 检查,没有需要检查的文档文件


✅ 跳过 docs ci 检查,没有需要检查的文档文件


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_master_ARM | ✅ | >>> |
| Build_v2_7_1_ARM | ✅ | >>> | |
| Build_v2_9_0_ARM | ✅ | >>> | |
| Build_v2_10_0_ARM | ✅ | >>> | |
| Build_v2_11_0_ARM | ✅ | >>> | |
| Build_v2_12_0_ARM | ✅ | >>> | |
| Build_master_x86 | ✅ | >>> | |
| Build_v2_7_1_x86 | ✅ | >>> | |
| Build_v2_9_0_x86 | ✅ | >>> | |
| Build_v2_10_0_x86 | ✅ | >>> | |
| Build_v2_11_0_x86 | ✅ | >>> | |
| Build_v2_12_0_x86 | ✅ | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| CodeCheck_pre-commit | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_master_ARM_01 | ✅ | >>> |
| UT_v2_7_1_ARM | ✅ | >>> | |
| UT_v2_9_0_ARM | ✅ | >>> | |
| UT_v2_10_0_ARM | ✅ | >>> | |
| UT_v2_11_0_ARM | ✅ | >>> | |
| UT_v2_12_0_ARM | ✅ | >>> | |
| 流水线 | PR-pipeline_op-plugin | ✅ | >>> |
- compile : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


🟡 Medium Priority
在 test_drop_path.py 的 test_train_eval_mode 方法中(第 41-47 行),has_zero_train = (train_output == 0).any() 计算了训练模式下输出中是否存在被丢弃(置零)的元素,但该变量仅在 print 中使用,从未通过 assertTrue 或其他断言进行验证。测试仅对 eval 模式的输出做了断言(第 46 行),而对训练模式的丢弃行为没有任何断言。这意味着如果训练模式存在 bug 导致完全不丢弃(与 eval 模式行为一致),该测试仍会通过,无法起到验证训练模式正确性的作用。
建议:对 has_zero_train 增加断言,例如 self.assertTrue(has_zero_train.item(), "训练模式下应有丢弃行为");或使用更严格的检查(如确认丢弃比例在合理范围内)。同时移除 print 语句或用日志替代。


🟡 Medium Priority
test_train_eval_mode (line 35-47) 声称测试"训练模式 vs 评估模式",但仅对评估模式做了断言 (self.assertTrue(torch.allclose(eval_output, x))),训练模式下的 has_zero_train 仅被 print 输出,未做任何断言。若 NpuDropPath 在训练模式下存在 bug(例如即使 drop_prob=0.5 也从不丢弃),该测试仍会通过,无法起到回归保护作用。
对于一个形状为 (68, 5) = 340 元素的张量,drop_prob=0.5 时没有任何元素被丢弃的概率为 (0.5)^340 ≈ 10⁻¹⁰²,实际不可能发生。因此可以安全地添加断言,例如 self.assertTrue(has_zero_train.item(), "training mode should drop elements") 或 self.assertFalse(torch.allclose(train_output, x))。
建议:在 test_train_eval_mode 中对训练模式的行为添加断言。推荐方案:检查 has_zero_train 为 True(训练模式下应有元素被置零),或检查 train_output 与 x 不 allclose(标准 DropPath 在训练时会对保留值做 scale,因此不可能与输入完全一致)。同时移除或保留 print 语句均可,但断言是必须的。


/approve


/lgtm


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


【合入来源】
【修改方案】
新增测试脚本
【资料变更】
“不涉及”
【接口变更】
“不涉及”
【功能验证】
【CheckList】