已合并
fix(threshold_grad_v2_d): align comparison semantics #9773
fix(threshold_grad_v2_d): align comparison semantics #9773
已合并
babeiee创建于 24 天前
babeiee
24 天前

描述

解决了nan用例判断失败的问题,修复了int在边界精度的问题

关联的Issue

[]https://gitcode.com/cann/ops-nn/issues/5562

测试

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 babeiee 的贡献)
Bbabeiee
24 天前 创建了 pull request,commit f493fd15
atomgit-bot
atomgit-bot
24 天前 评论:

变更摘要

本 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」行为一致。

likedislike
不准确?
atomgit-bot
atomgit-bot
24 天前 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
24 天前 添加了label:cann-cla/yes
Bbabeiee
24 天前 修改了pull request 的描述
此处折叠了56条消息 查看更多
CANN-robot
CANN-robot成员
22 天前 评论:

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.

likedislike
Zzhanglei842成员
22 天前 解决了最后一个问题
CANN-robotCANN-robot成员
22 天前 关闭了关联的issue
CANN-robotCANN-robot成员
22 天前 合入了pull request
CANN-robot
CANN-robot成员
22 天前 评论:

Pull Request 已合并或已关闭。

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

likedislike