Pull Request已成功合入, 合并人@CANN-robot
(感谢 babeiee 的贡献)变更摘要
本 PR 用于对齐 threshold_grad_v2_d(即 threshold_backward 反向算子)的比较语义与 ATen 参考实现。此前实现使用「大于阈值(COMPARE_MODE_GT)」配合「命中时选梯度、否则选零」的 Select 组合,与 ATen 的 threshold_backward(grad * (input > threshold))语义不一致;本次将比较模式改为「小于等于(COMPARE_MODE_LE)」,并交换 Select 的两个数据源顺序(命中时选零、否则选梯度),同时针对 INT32 输入在 tiling 侧将阈值先经 int32_t 截断再转回 float,使比较在同一数值语义下进行。测试 golden 脚本也改为直接调用 torch.ops.aten.threshold_backward(在 float32 上比较与选择后回落到输出 dtype),从而与内核行为保持一致。
主要改动
-
切换比较模式并调整
Select语义:在threshold_grad_v2_d_dag.h中,将ThresholdGradV2D8BDag、ThresholdGradV2DInt32Dag、ThresholdGradV2DDag的Compare由COMPARE_MODE_GT(> 阈值)改为COMPARE_MODE_LE(<= 阈值),并将Select的参数顺序由(Compare, OpCopyInGradCast, data_zero)改为(Compare, data_zero, OpCopyInGradCast),使输入小于等于阈值时输出零、否则透传梯度,与 ATen 语义对齐。 -
INT32 分支的阈值类型修正:在
threshold_grad_v2_d_tiling_arch35.cpp的DT_INT32分支中新增thresHold = static_cast<float>(static_cast<int32_t>(thresHold));,将阈值先截断为int32_t再转回float,消除浮点表示导致的比较偏差。 -
golden 参考实现对齐:在
golden.py中将手写的「广播 +self > threshold掩码 +torch.where」改为直接调用torch.ops.aten.threshold_backward,输入统一提升到torch.float32参与比较与选择,结果再转回原始输出 dtype,与内核的「提升到float32运算后回落输出 dtype」行为一致。


The MR can not be merged, because of CodeReview discussion not resolved
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.


描述
解决了nan用例判断失败的问题,修复了int在边界精度的问题
关联的Issue
[]https://gitcode.com/cann/ops-nn/issues/5562
测试
文档更新
类型标签
AI/Agent生成声明