Pull Request已成功合入, 合并人@CANN-robot
(感谢 yefeicoding 的贡献)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 |
|---|---|---|
| conversion/circular_pad_grad | ✅ zhanw_coding, 宋恺 (2/2) | ✅ zhanw_coding (1/1) |
| conversion/mirror_pad | ✅ zhanw_coding, 宋恺 (2/2) | ✅ zhanw_coding (1/1) |
| repo-cann/ops-math | ✅ zhanw_coding, 宋恺 (2/2) | ✅ zhanw_coding (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
yefeicoding, thanks for your pull request. All authors of the commits have signed the CLA. 👍


/compile


变更摘要
此 PR 为 pad、pad_v3、circular_pad_grad、mirror_pad、pad_v3_grad_replication 五个算子新增了测试用 golden 参考实现文件,主要目的是在测试用例中将 paddings 参数的 dtype 统一修正为 int64,确保与算子定义一致。每个 golden 文件通过 __golden__ 字典注册算子名到 golden 函数的映射,golden 函数内部将输入的 paddings 显式转换为 np.int64 后再进行运算,并支持多种填充模式(constant、reflect、symmetric、edge、circular)以及 bfloat16、uint8/16/32/64 等特殊数据类型的正确处理。
主要改动
-
新增
padgolden 测试文件:在conversion/pad/tests/assets/golden.py中实现pad_golden函数,使用torch.nn.functional.pad作为参考实现,并将paddings以np.int64传入torch进行前向对齐。 -
新增
pad_v3golden 测试文件:在conversion/pad_v3/tests/assets/golden.py中实现pad_v3_golden函数,支持constant(含constant_values)、reflect/symmetric/edge四种模式,处理paddings_contiguous参数和NC1HWC0格式的边界情况,且所有模式下都将paddings转为np.int64。 -
新增
circular_pad_grad反向 golden 测试文件:在conversion/circular_pad_grad/tests/assets/golden.py中实现circular_pad_grad_golden函数,低维(≤3)情况调用torch反向计算,高维情况使用自定义_numpy_pad_v3_grad_circular逐元素累加梯度,paddings统一转为np.int64。 -
新增
mirror_padgolden 测试文件:在conversion/mirror_pad/tests/assets/golden.py中实现mirror_pad_golden函数,支持REFLECT和SYMMETRIC两种模式,处理负 padding(裁剪)场景,通过_pad_and_slice辅助函数实现先填充后裁剪的逻辑。 -
新增
pad_v3_grad_replication反向 golden 测试文件:在conversion/pad_v3_grad_replication/tests/assets/golden.py中实现pad_v3_grad_replication_golden函数,通过逐元素索引映射(edge 复制模式)累加输入梯度,paddings同样转为np.int64。


代码审查
我已逐文件审查完所有 8 个变更文件。以下是我的审查结论:
审查发现
发现 1(P1,confidence 0.95)
fix: golden.squeeze(0) 返回值被丢弃,导致 circular_pad_grad 低维输入(ndim≤3)的 golden 输出 shape 错误
- 文件:
conversion/circular_pad_grad/tests/assets/golden.py - 位置:第 101 行,位于
_torch_direct_invoke_circular函数内 - 问题链:
- 第 90-91 行通过
unsqueeze(0)为 tensor 增加了 batch 维度(size=1) - 第 101 行
golden.squeeze(0)意图移除该 batch 维度 - 但 PyTorch 的
Tensor.squeeze(dim)返回新 tensor,不原地修改(原地版本为squeeze_(dim)) - 返回值被丢弃,
golden仍保持(1, *y_shape_list)的 shape
- 第 90-91 行通过
- 触发条件:
circular_pad_grad_golden第 142 行if grad_output.ndim <= 3分支走_torch_direct_invoke_circular路径时 - 失败模式:golden 输出多一个 size=1 的维度,与框架预期 output shape 不匹配,测试比较失败
- 修复:第 101 行改为
golden = golden.squeeze(0)
注:该 bug 同样存在于已有文件
conversion/pad_v3_grad/tests/assets/golden.py第 164 行(不在本次 diff 范围内),新文件复制了同一错误。
逐文件审查结论
| 文件 | 结论 |
|---|---|
conversion/circular_pad_grad/tests/assets/golden.py |
1 个 P1 问题(squeeze 返回值丢弃) |
conversion/mirror_pad/tests/assets/golden.py |
无问题 |
conversion/mirror_pad/tests/st/arch35/ttk_kernel_mirror_pad_st.csv |
无问题 |
conversion/pad/tests/assets/golden.py |
无问题 |
conversion/pad/tests/st/arch35/ttk_kernel_pad_st.csv |
无问题 |
conversion/pad_v3/tests/assets/golden.py |
无问题 |
conversion/pad_v3/tests/st/arch35/ttk_kernel_pad_v3_st.csv |
无问题 |
conversion/pad_v3_grad_replication/tests/assets/golden.py |
无问题 |
总体风险评估
- P0:0 | P1:1 | P2:0 | P3:0
- 总体风险:中等。
circular_pad_grad的 low-dim(ndim≤3)golden 计算路径存在确定性 shape 错误,会导致该算子对应测试用例失败。修复方式明确且安全(一行改动),不影响其他算子。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


🟠 High Priority
变更位置:conversion/circular_pad_grad/tests/assets/golden.py 第 101 行,函数 _torch_direct_invoke_circular 内部。
问题链:
- 第 90-91 行:
grad_output.unsqueeze(0)和x.unsqueeze(0)为 tensor 增加了 batch 维度(size=1)。 - 第 101 行:
golden.squeeze(0)意图移除该 batch 维度,使输出 shape 恢复为y_shape_list。 - PyTorch 的
Tensor.squeeze(dim)返回的是新的 tensor,不会原地修改 —— 原地版本是squeeze_(dim)。 - 返回值被丢弃,
golden仍然保持(1, *y_shape_list)的 shape。
触发条件:circular_pad_grad_golden 第 142 行 if grad_output.ndim <= 3 分支(低维输入)走 _torch_direct_invoke_circular 路径时。
失败模式:返回的 golden 值多一个 size=1 的维度,与框架预期的 output shape 不匹配,导致测试比较失败。
注:该 bug 模式同样存在于已有文件 conversion/pad_v3_grad/tests/assets/golden.py 第 164 行,该文件不在本次 diff 范围内,但新文件复制了同样的错误。
建议:将 golden.squeeze(0) 改为 golden = golden.squeeze(0),使 squeeze 返回的新 tensor 被正确赋值回 golden 变量。
| 101
| - golden.squeeze(0) |
|
101 | + golden = golden.squeeze(0) |


/compile


流水线任务触发成功
任务链接 [d28f2f173d9c435f9cd17b4731efa534][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_A5_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_harmony-infer | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_A5_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_harmony-infer-chs-math | ✅ SUCCESS | >>>>> | |
| Compile_kirin-harmony-infer-chs-math-1 | ✅ SUCCESS | >>>>> | |
| UT_Test | ✅ SUCCESS | ||
| UT_Test_experimental | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> |
[2026-07-02 09:39:30] CI执行结束


流水线任务触发成功
任务链接 [982704e8fcec4ed98064a0181129d08f][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_Pr | ✅ SUCCESS | ||
| StaticCheck_codespell_check | ✅ SUCCESS | ||
| StaticCheck_link_validity_check | ✅ SUCCESS | ||
| StaticCheck_resource_existence_check | ✅ SUCCESS | ||
| StaticCheck_tag_closed_check | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_codestyle | ⚠️ WARNING | >>>>> | |
| codecheck_precommit | ⚠️ WARNING | >>>>> | >>>>> |
[2026-07-02 09:28:57] CI执行结束


/lgtm
/approve


/lgtm


描述
关联的Issue
https://gitcode.com/cann/ops-math/issues/1961
测试
文档更新
类型标签