Closed
[Bug-Report|缺陷反馈]: aclnnNLLLoss2dBackward 的 totalWeight 资料写「输出」实为「输入」,mean 结果依赖调用方传入值 #7165
liuhanwen8Add on  Oct 22Closed at  8 days ago
liuhanwen8
liuhanwen8
Oct 22 Create

现象

aclnnNLLLoss2dBackward 的资料把 totalWeight 列为输出:
「仅当 reduction 为 mean 时,totalWeight 是通过 target 取相应位置的 weight,然后去掉 ignoreIndex 对应的 weight,
将剩下的 weight 求和;当 reduction 为其他值时,该参数默认不处理。」

实测(两段均返回 0):

reduction totalWeight 预填 执行后 totalWeight out 前 6 元素
mean 1.0 1.0(未变) [0, -0.84404, -0.84404, 0, 0, 0]
mean NaN NaN(未变) [0, nan, nan, 0, 0, 0]
sum 1.0 1.0(未变) 与 mean(预填 1.0) 相同

⇒ ① totalWeight 从未被写出;② mean 的分母取自调用方传入的 totalWeight。
按资料传未初始化/占位 tensor 时,mean 的结果直接取决于垃圾值(NaN 传播),且无任何报错。

源码侧佐证(master):op_host/nll_loss_grad_def.cpp 只注册一个输出 x_grad;
op_api/aclnn_nll_loss2d_backward.cpp 的 CheckShape/CheckDtypeValid 把 totalWeight 声明为
const aclTensor*,即实现侧按输入处理。

复现

gradOutput = fp32[1]  = [1.0]
self       = fp32[2x3x2x2]
target     = int64[2x2x2]  (取值 0..2)
weight     = fp32[3]
totalWeight= fp32[1] = [NaN]     # ← 关键:预填 NaN
out        = fp32[2x3x2x2]
reduction  = 1 (mean), ignoreIndex = -100

执行后 out 出现 NaN,totalWeight 仍为 NaN。

期望

二者取一并保持文档与实现一致:要么按资料计算并写出 totalWeight;
要么在资料里把它标为输入并说明 mean 场景必须由调用方给出 Σw。
当前形态下调用方按文档使用必然静默出错。

环境:Atlas A2(910B2)/ CANN 9.2.0 / aclnn 原生两段式直调(aclnnXxxGetWorkspaceSize + aclnnXxx)。
判定口径:第一段返回的第一个非零码与资料错误码表对照;返回值与输出逐元素比对 fp64 标杆。

likedislike
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

初步分析

感谢反馈。

现象理解:aclnnNLLLoss2dBackward 的资料把 totalWeight 写成输出,实现没有写它。reduction 为 mean 时,分母用的是调用方传进来的值,预填 NaN 会传进梯度。

当前判断:初步判断,待复现确认。

涉及范围:loss/nll_loss_grad 的 totalWeight 方向。

下一步:用 mean、totalWeight 预填 NaN 看它是否被改写。确认后,要么按资料写出,要么把资料改成输入。

likedislike
yuning_chenyuning_chenCollaborators
Oct 22 Assign huang-qiang as responsible person
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

您好,该问题已收到,当前问题 @huang-qiang 处理中

likedislike
黄强黄强Collaborators
16 days ago Assign caorenlei as responsible person
caorenleicaorenleiCollaborators
16 days ago Removed responsible person huang-qiang
caorenleicaorenleiCollaborators
16 days ago Removed responsible person caorenlei
jiygjiyg
10 days ago Associated pull request
yuning_chen
yuning_chenCollaborators
10 days ago Comments:

感谢反馈,问题成立且与源码一致。

核实结论:op_host/nll_loss_grad_def.cpp 只注册了 x_grad 一个输出;op_api/aclnn_nll_loss2d_backward.cpp 的 CheckShape/CheckDtypeValid 把 totalWeight 声明为 const aclTensor*。即实现侧把 totalWeight 当输入,而资料写的是输出,二者相反。

影响:调用方按资料准备未初始化/占位 totalWeight 时,mean 分支的分母直接取自这块内存(NaN 会传播到 out),全程返回 0,属于静默错值,比报错更难发现。

修复建议(二选一):

  1. 资料改为输入,并明确 mean 场景必须由调用方传入 Σw(推荐:不改接口、兼容性最好);
  2. 实现按资料计算并写出 totalWeight:需新增输出并同步改 GE 原型 / ACLNN 两段接口签名,属破坏性变更。

建议同时为 mean 分支补一条 totalWeight=NaN 的用例,把当前行为固定下来。

likedislike
yuning_chenyuning_chenCollaborators
10 days ago Assign yu-xinjie62 as responsible person
yuning_chen
yuning_chenCollaborators
10 days ago Comments:

已指派给 @yu-xinjie62 跟进。

指派依据:NLLLoss 责任田 arch22 接口人黄鑫当前不可指派,回退到 vfusion/loss 领域同代际 committer

likedislike
yuning_chenyuning_chenCollaborators
10 days ago Assign ji-songyuan as responsible person, remove original responsible person: yu-xinjie62
yuning_chen
yuning_chenCollaborators
10 days ago Comments:

更正指派:loss 类 arch22(A2/A3) 的统一责任人为 @ji-songyuan,本条改派给他,此前指派给 @yu-xinjie62 的通知作废。

likedislike
佐助大王佐助大王Collaborators
8 days ago changed custom state from 进行中 to 已完成
佐助大王佐助大王Collaborators
8 days ago Close issue
chenjiayi_
chenjiayi_Collaborators
4 days ago Comments:

补充说明本问题的后续处理:

资料已把 totalWeight 明确为输入,并与实现口径对齐。

处理结果:

  • loss/nll_loss_grad/docs/aclnnNLLLoss2dBackward.md 的参数表把 totalWeight 标为“输入”,说明改为“reduction 为 mean 时,调用方必须传入按 target、weight 和 ignoreIndex 计算得到的权重和;其他 reduction 下该参数不参与计算”。
  • 实现侧保持原状:op_host/nll_loss_grad_def.cpp 只注册 x_grad 一个输出,op_api/aclnn_nll_loss2d_backward.cpp 以 const aclTensor* 接收 totalWeight。
  • PR:!14323。

验证:

  • master 文档 loss/nll_loss_grad/docs/aclnnNLLLoss2dBackward.md:145 当前为“totalWeight是输入…”。
  • 中英文两份资料同步修改。

说明:该修复随资料 PR 合入关闭。

如果还有后续需要,请重新提 issue。

MR 链接:https://gitcode.com/cann/ops-nn/pull/14323

likedislike