Merged
fix(bce_with_logits_bwd): 空 self 不再跳过 shape 校验 #13156
fix(bce_with_logits_bwd): 空 self 不再跳过 shape 校验 #13156
Merged
sunhao_hwAdd on 22 days ago
sunhao_hw
sunhao_hwCollaborators
22 days ago

描述

aclnnBinaryCrossEntropyWithLogitsBackward 在 self 为空张量时,CheckShape 直接返回成功,跳过 self 与 target、out 的 shape 一致性以及 gradOutput、weight、posWeight 的 broadcast 校验;GetWorkspaceSize 随后短路返回 ACLNN_SUCCESS,第二段不写 out。非法 shape 因此返回 0,而不是资料要求的 161002。

修改 loss/sigmoid_cross_entropy_with_logits_grad_v2/op_api/aclnn_binary_cross_entropy_with_logits_backward.cpp:

  • 去掉 CheckShape 中 self 为空时的提前返回,0 尺寸维度仍走原有 shape 与 broadcast 校验。
  • shape 合法的空 self 仍在计算侧短路返回,不发起 kernel。

并在 tests/ut/op_host/test_aclnn_binary_cross_entropy_with_logits_backward.cpp 增加 case_empty_shape_invalid,覆盖 out、target shape 不一致以及 gradOutput 不能 broadcast 到空 self 的三种入参。

按检视意见,将 aclnn 资料中 self 的维度约束由 1-8 改为 0-8。

关联的Issue

关联Issue #7133(Close #7133)
https://gitcode.com/cann/ops-nn/issues/7133

测试

在 CANN 9.2.0、Atlas A2(910B3)上用修改后的 op_api 覆盖原库复现:

  • self 为 [2,3] 且 shape 一致:两段返回 0,workspace 为 1024。
  • self 非空且 out shape 不一致:GetWorkspaceSize 返回 161002。
  • self、target、gradOutput、out 均为 [0,4]:两段返回 0。
  • 空 self 且 out=[2,4]、target=[2,4]、gradOutput=[5]:GetWorkspaceSize 均返回 161002。

文档更新

更新了 loss/sigmoid_cross_entropy_with_logits_grad_v2/docs/aclnnBinaryCrossEntropyWithLogitsBackward.md 与 aclnnBinaryCrossEntropyWithLogitsBackward_en.md,self 的维度(shape)由 1-8 改为 0-8。

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for sunhao_hw's contribution.)
sunhao_hwsunhao_hwCollaborators
22 days ago Created pull request, commit 62ef8246
sunhao_hwsunhao_hwCollaborators
22 days ago Associated issue: [Bug-Report|缺陷反馈]: aclnnBinaryCrossEntropyWithLogitsBackward 空 self 跳过 shape 校验,非法入参返回 0 且不写输出
atomgit-bot
atomgit-bot
22 days ago Comments:

变更摘要

本 PR 修复 aclnnBinaryCrossEntropyWithLogitsBackward 在 self 为空张量时绕过参数校验的问题。此前 CheckShape 对空 self 直接返回成功,使 self 与 target、out 的 shape 一致性检查以及 gradOutput、weight、posWeight 的 broadcast 校验被跳过,非法入参因此返回成功而非 161002。改动后空张量的 0 尺寸维度仍参与 shape 与 broadcast 校验,类型提升校验也提前到空张量短路之前,同时在 UT 中新增空 shape 非法入参用例。

主要改动

  • CheckShape 移除空张量提前返回:删除 self->IsEmpty() 时直接返回 true 的分支,使 self 为空时仍执行与 target、out 的 shape 一致性检查及后续 broadcast 校验。
  • CheckParams 前置 CheckPromoteType:在返回成功前新增第 7 步 dtype 提升校验并映射为 ACLNN_ERR_PARAM_INVALID,避免空 self 短路导致跳过类型提升校验。
  • 新增 UT 用例 case_empty_shape_invalid:在 test_aclnn_binary_cross_entropy_with_logits_backward.cpp 中覆盖三种非法入参,分别是 out shape 不一致、target shape 不一致、gradOutput 无法 broadcast 到空 self,均断言返回 ACLNN_ERR_PARAM_INVALID。
  • 保持合法空张量的原有行为:shape 合法的空 self 仍在计算侧短路返回、不发起 kernel,仅校验收紧,合法场景行为不变。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
22 days ago Comments:

代码审查

✅ 未发现问题


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
22 days ago Added label: cann-cla/yes
41 messages are collapsed here。 See more
CANN-robot
CANN-robotCollaborators
19 days ago Comments:

The following users do not have permission to comment /lgtm or /approve on any module in this PR:
/approve: 杨阳

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
19 days ago Closed associated issue
CANN-robotCANN-robotCollaborators
19 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
19 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike