Merged
fix(pow/sub): A2/A3补充out dtype白名单校验(#3964) #6800
baijinxiang_hwAdd on 9 days ago
fix(pow/sub): A2/A3补充out dtype白名单校验(#3964) #6800
Merged
baijinxiang_hwAdd on 9 days ago
baijinxiang_hwCollaborators
9 days ago

描述

问题

aclnnPowScalarTensor、aclnnPowTensorScalar、aclnnPowTensorTensor、aclnnSubs 和 aclnnSub 在 out 传入资料输出支持列表之外的 dtype(如 DT_INT4)时,A2/A3 会继续进入 Build/Cast 并返回内部错误码,而不是第一段接口约定的 161002。

根因

输出类型此前只经过可转换关系检查。部分非法 dtype 能被 CanCast 放行,随后在 Build/Cast 阶段失败,导致参数非法被降级为内部错误。

方案

  • 新增 out dtype 白名单校验,但仅在 NpuArch::DAV_2201(A2/A3)启用。
  • aclnn_pow.cpp 和 aclnn_pow_tensor_tensor.cpp 覆盖 aclnnPowScalarTensor、aclnnPowTensorScalar、aclnnPowTensorTensor 三个 out-of-place 入口。
  • aclnn_sub.cpp 在 CheckParamsScalar 和 CheckParams 中覆盖 aclnnSubs、aclnnSub;原地接口继续沿用原有输入别名校验。
  • A5 走 DAV_3510 路径,不启用新增白名单,保持原有 CanCast 及后续 Build 行为不变。
  • A2/A3 合法 out dtype 路径不变,仅将非法 dtype 的返回码修正为 ACLNN_ERR_PARAM_INVALID(161002)。

关联的Issue

https://gitcode.com/cann/ops-math/issues/3964

测试

A2:Ascend 910B3,CANN 9.2.0

  • math_op_api_ut:158/158 PASS,包含新增 A2 非法 out 校验和 A5 不拦截回归用例。
  • 当前补丁真机矩阵:PowScalarTensor/PowTensorScalar/PowTensorTensor/Subs/Sub 的 INT4/UINT1/HIFLOAT8/FLOAT8_E5M2/COMPLEX32 均返回 161002。
  • 合法路径未被误拦截:三个 Pow 入口的 INT64 -> UINT16/UINT32/UINT64 均返回 0;FLOAT/FLOAT16/DOUBLE/BF16/COMPLEX64/COMPLEX128 输出在对应可转换输入下保持 0。

A5:Ascend 950PR,CANN 9.2.0

  • math_op_api_ut:158/158 PASS,包含 Ascend950_out_dtype_check_not_applied 回归用例。
  • 当前补丁真机矩阵与基线一致:
out dtype PowScalarTensor PowTensorScalar / PowTensorTensor / Subs / Sub
INT4 / UINT1 / COMPLEX32 561000 561103
HIFLOAT8 / FLOAT8_E5M2 0 0
  • 新增 A2/A3 out 白名单在 A5 未生效;合法 INT64 -> UINT16/UINT32/UINT64 仍返回 0。

实现路径说明

A3 与 A2 均走 DAV_2201 路径,共用同一实现。本轮完成 A2 和 A5 真机验证。

文档更新

不涉及。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for baijinxiang_hw's contribution.)
Bbaijinxiang_hwCollaborators
9 days ago Created pull request, commit 6ab6023c
Bbaijinxiang_hwCollaborators
9 days ago Associated issue: [Bug-Report|缺陷反馈]: aclnnPowScalarTensor/aclnnSubs 的 out 非白名单 dtype 返回 561000/561103 而非 161002
atomgit-bot
atomgit-bot
9 days ago Comments:

变更摘要

本 PR 修复 aclnnPowScalarTensor、aclnnSubs / aclnnInplaceSubs 在 out dtype 非法时错误码降级为内部错误的问题:原先 dtype 校验只覆盖输入侧,out 仅经过 CanCast 判断,导致 DT_INT4 等不在资料支持列表内但 CanCast 放行的 dtype 漏到 Build 阶段,返回 561000 / 561113。变更通过补充 out dtype 白名单校验,使非法 out dtype 在第一段接口统一返回 ACLNN_ERR_PARAM_INVALID(161002),合法路径行为不变。

主要改动

  • 新增 pow 输出 dtype 白名单: aclnn_pow.cpp 与 aclnn_pow_tensor_tensor.cpp 各自新增 OUT_DTYPE_SUPPORT_LIST(输入列表基础上增加 DT_UINT16 / DT_UINT32 / DT_UINT64),与 API 资料的 out 支持列表对齐。
  • pow 参数校验接入 out 校验: aclnn_pow.cpp 在 CheckDtypeValid 中、aclnn_pow_tensor_tensor.cpp 在 CheckParams 中增加 out dtype 是否在 OUT_DTYPE_SUPPORT_LIST 内的判断,不满足时打印错误日志并返回 ACLNN_ERR_PARAM_INVALID,覆盖 aclnnPowScalarTensor / aclnnPowTensorScalar / aclnnPowTensorTensor。
  • sub 参数校验补充 out 校验: aclnn_sub.cpp 的 CheckParamsScalar(aclnnSubs 与 aclnnInplaceSubs 共用)在既有 OP_CHECK_DTYPE_NOT_SUPPORT(self, ...) 之后新增 OP_CHECK_DTYPE_NOT_SUPPORT(out, dtypeSupportList, ...),复用 soc 版本相关的 GetDtypeSupportListBySocVersion() 列表。
  • pow 单测新增异常与正常对照: test_aclnn_pow_scalar_tensor.cpp 新增 aclnnPowScalarTensor_out_dtype_not_in_support_list,覆盖 INT4、BOOL、列表内但不可转换的 UINT16 均返回 ACLNN_ERR_PARAM_INVALID,以及合法 FLOAT 返回 ACL_SUCCESS。
  • sub 单测新增异常与 inplace 对照: test_aclnn_sub.cpp 新增 abnormal_out_dtype_not_in_support_list,验证 aclnnSubs 的 INT4 out 报错、FLOAT out 成功,以及 aclnnInplaceSubs 场景不受新增校验影响。

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

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

代码审查

审查结论

发现的问题(共 4 项:P2 × 1,P3 × 3)

1. [P2] math/sub/op_api/aclnn_sub.cpp:435 — tensor-tensor 的 CheckParams 未同步 out 白名单
CheckParamsScalar 补了 OP_CHECK_DTYPE_NOT_SUPPORT(out, ...),但同文件 tensor-tensor 版 CheckParams(L248/L249)仍只有 self / other。aclnnSub / aclnnInplaceSub 传 out=DT_INT4 时 CanCast(fp32,int4) 放行,仍在 build 阶段 l0op::Cast 失败返回 561000/561113,与 aclnnSubs 现在返回 161002 形成入口分叉,本 issue 的根因在该入口未收敛(issue 自身锚点即指向 L240 的 CheckParams)。

2. [P3] math/sub/op_api/aclnn_sub.cpp:435 — out 校验复用输入侧 soc 列表,与 pow 的独立 OUT 列表做法不一致
pow 本次刻意新建 OUT_DTYPE_SUPPORT_LIST 并说明 out 列表 ≠ 输入列表;sub 却把 out 绑定到 GetDtypeSupportListBySocVersion()。该列表在非 regbase 的 Ascend910(DAV_1001)不含 BF16,可能把 out=BF16 这类调用从“通过校验”变为 161002,是否误拒取决于资料 out 列表,置信度较低,属可选改进。

3. [P3] math/pow/op_api/aclnn_pow.cpp:52 — 新增注释与实现不符
注释称 out 列表“比输入列表多 UINT16/UINT32/UINT64”,实际还去掉了 DT_BOOL(aclnn_pow_tensor_tensor.cpp:57 同样)。集合关系描述错误会误导后续维护者漏改 out 列表。可选的注释准确性改进。

4. [P3] math/pow/op_api/aclnn_pow_tensor_tensor.cpp:142-146 — 新分支无 UT 覆盖
本次只给 aclnnPowScalarTensor 加了用例,test_aclnn_pow_tensor_tensor.cpp / test_aclnn_pow_tensor_scalar.cpp 未新增用例;tensor-tensor 入口“非法 out 返回 161002 而非 561000”这一核心断言零覆盖。

逐文件审查确认

文件 结论
math/pow/op_api/aclnn_pow.cpp 新增 OUT_DTYPE_SUPPORT_LIST 与 CheckDtypeValid 中 out 校验,已核对两个调用方(CheckPowTensorScalarParams L288 / CheckPowScalarTensorParams L382)及 inplace 入口(out=self,self 先经 DTYPE_SUPPORT_LIST 校验,安全);BOOL out 在改动前已被 CanCast 拒绝(现有 UT L303 亦期望 PARAM_INVALID),无功能回退 → 仅问题 3
math/pow/op_api/aclnn_pow_tensor_tensor.cpp out 校验置于 CheckParams 的 null 检查之后(L138),逻辑正确;inplace 路径 out=self 同样安全 → 仅问题 3(同源)、4
math/pow/tests/ut/op_api/test_aclnn_pow_scalar_tensor.cpp 新增用例四组断言与实现一致、无重名 TEST_F,合法用例路径成立 → 无问题(覆盖范围缺口已在问题 4 记录)
math/sub/op_api/aclnn_sub.cpp out 校验在 null 检查(L430)之后,宏用法与同文件 L434 一致,inplace 场景安全 → 问题 1、2
math/sub/tests/ut/op_api/test_aclnn_sub.cpp 新增用例无重名 TEST_F,三组断言(INT4→161002、FLOAT→SUCCESS、inplace→SUCCESS)与实现一致;既有用例的 out dtype 均在支持列表内,不受影响 → 无问题(缺 aclnnSub 用例已在问题 1 中建议)

整体风险判断:改动方向正确、对既有合法路径基本无回退,主要风险集中在 sub 侧修复不对称——仅覆盖 scalar 入口,tensor-tensor 入口(aclnnSub / aclnnInplaceSub)仍保留原错误码降级问题,建议本 PR 内一并补上。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论


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

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
9 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
9 days ago Comments:

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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
math/pow ✅ 陈兴宇, yue-ma (2/2) ✅ 陈兴宇, yue-ma (2/1)
math/sub ✅ yue-ma, 陈兴宇 (2/2) ✅ 陈兴宇, yue-ma (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

doufloat, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
9 days ago Set FelixTang7,yue-ma,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,chenxingyu18,pingchuantang,llimwang,gubaocheng,andong_hw,xuejinghui,onanfield,zhanw_coding,clinglai0517 as approver
CANN-robotCANN-robotCollaborators
9 days ago Set FelixTang7,yue-ma,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,zl_hw,songkai111,chenxingyu18,pingchuantang,llimwang,gubaocheng,andong_hw,xuejinghui,onanfield,zhanw_coding,clinglai0517 as approver
atomgit-bot
atomgit-bot9 days agoStart code review2
math/sub/op_api/aclnn_sub.cpp
Outdated
@@ -432,6 +432,7 @@ static aclnnStatus CheckParamsScalar(const aclTensor* self, const aclScalar* oth
432432 // 2. 检查输入的数据类型是否在API支持的数据类型范围之内,需要根据api定义校验
433433 const std::initializer_list<op::DataType> dtypeSupportList = GetDtypeSupportListBySocVersion();
434434 OP_CHECK_DTYPE_NOT_SUPPORT(self, dtypeSupportList, return ACLNN_ERR_PARAM_INVALID);
435+ OP_CHECK_DTYPE_NOT_SUPPORT(out, dtypeSupportList, return ACLNN_ERR_PARAM_INVALID);
atomgit-bot
atomgit-bot9 days agoComments:

🟡 Medium Priority

变更点:math/sub/op_api/aclnn_sub.cpp:435 在 CheckParamsScalar 中新增了 OP_CHECK_DTYPE_NOT_SUPPORT(out, dtypeSupportList, ...),但同文件第 240-249 行的 tensor-tensor 版 CheckParams 只保留了 self / other 两项(L248/L249),没有同步补 out。

受影响契约:aclnnSubs / aclnnInplaceSubs 走 CheckParamsScalar,非法 out dtype 现在正确返回 161002;而 aclnnSub / aclnnInplaceSub 走 CheckParams,out 仍只经过 CheckPromoteType 里的 OP_CHECK_RESULT_DTYPE_CAST_FAILED(CANCAST 校验)。

失败模式:调用 aclnnSub(self=fp32, other=fp32, out=DT_INT4)(或 aclnnInplaceSub 同名同参路径)时,CanCast(fp32,int4) 返回 true 放行,进入 build 阶段 l0op::Cast(subOpOut, out->GetDataType())(L361)失败,返回 561000 / 561113 内部错误码——正是本 issue 要消除的“错误码语义漂移”,只是换到了另一个入口。同一文件内两个参数校验函数对 out 的处理出现分叉,且 issue #3964 的根因锚点本身就指向 aclnn_sub.cpp:240 CheckParams,说明该入口同样漏校验。

建议:在 tensor-tensor 版本的 CheckParams(L248/L249 之后)补同样的 out 白名单校验,使 aclnnSub / aclnnInplaceSub 与 aclnnSubs / aclnnInplaceSubs 对非法 out dtype 都给 161002,并补一条 aclnnSub 的 UT 用例。

likedislike
Incorrect?
System
System MessageSystem
8 days ago Comments:

changed this line on d803c07a view diff detail

Bbaijinxiang_hwCollaborators
9 days ago Pre merge succeeded (commit_id: e6a676fde04f2816f588471f8a2e34728e0a88b2)
Bbaijinxiang_hwCollaborators
9 days ago Force-pushed 1 commits:36cee2dc-fix(pow/sub): 补充out dtype白名单校验(#3964)
Bbaijinxiang_hwCollaborators
9 days ago Pre merge succeeded (commit_id: ea973c8aebe96cf1ee85d6e803c9b4bde08e12ff)
CANN-robotCANN-robotCollaborators
9 days ago Removed label: cann-cla/yes
CANN-robotCANN-robotCollaborators
9 days ago Added label: cann-cla/yes
baijinxiang_hwCollaborators
9 days ago Comments:

检视意见采纳(提交更新至 36cee2dc5):

已在 tensor-tensor 版 CheckParams(aclnnSub / aclnnInplaceSub 共用,aclnn_sub.cpp:248 之后)补充同样的 out 白名单校验 OP_CHECK_DTYPE_NOT_SUPPORT(out, dtypeSupportList, ...)。此前该入口的 out 仅经过 CheckPromoteType 的 CanCast 检查,aclnnSub(fp32, fp32, out=INT4) 确实会复现同根因的错误码漂移(CanCast 放行 → 构建阶段 Cast 失败 → 561000/561113)。

同步补充 UT:abnormal_out_dtype_not_in_support_list 新增 aclnnSub tensor-tensor 入口两组用例(out=INT4 → 161002;合法 out dtype → 不受影响)。

验证(Ascend 910B2,CANN 9.2.0,分支当前状态):

  • math_op_api_ut(--ops=pow,sub -u --opapi)154/154 通过(含全部新增用例,UT 直接编译修复后源码运行);
  • bash build.sh --ops=pow,sub --soc=ascend910b -j16 全量编译通过;
  • pre-commit 全绿。

至此 sub 全部四个入口(aclnnSub / aclnnInplaceSub / aclnnSubs / aclnnInplaceSubs)对资料未声明的 out dtype 均在第一段返回 161002,行为一致。

likedislike
Bbaijinxiang_hwCollaborators
9 days ago Resolved final issue
baijinxiang_hwCollaborators
9 days ago Comments:

/compile

likedislike
Bbaijinxiang_hwCollaborators
9 days ago Pre merge succeeded (commit_id: 16e53933633e70fee92d9c38128561d18b567025)
CANN-robot
CANN-robotCollaborators
9 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
9 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
9 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
9 days ago Added label: ci-pipeline-passed
chenxingyu18Collaborators
9 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
9 days ago Added label: approved
gubaochengCollaborators
9 days ago Comments:

[Major] Pow 输出白名单未纳入 BF16 平台约束

math/pow/op_api/aclnn_pow.cpp L53-57 与 math/pow/op_api/aclnn_pow_tensor_tensor.cpp L58-62 新增的输出白名单无条件包含 DT_BF16,但两处既有 CheckSocVersionIsSupportBf16 仍只检查 selfDtype/expDtype,没有检查 outDtype。

因此在文档声明不支持 BFLOAT16 的 Atlas 训练/推理及 Atlas 200I/500 A2 等平台上,使用 FLOAT 输入并指定 BF16 out 时,输入侧平台检查不会触发,新增白名单和后续不感知 SoC 的 CanCast 也都会放行;三个非原地 Pow 入口均无法在参数阶段稳定返回本 PR 约定的 161002,甚至可能接受资料禁止的调用。

请将 outDtype == DT_BF16 纳入两个实现的 BF16 SoC 防护(保留支持平台的合法路径),并补充“非 BF16 SoC + FLOAT 输入 + BF16 out → ACLNN_ERR_PARAM_INVALID”的入口级负例及支持平台正例。

agent检视,通过gubaocheng提交

likedislike
chenyifengCollaborators
8 days ago Comments:

您好,PR已满足合入指标,请尽快联系Committer进行合入。如暂时不需要合入,请在PR标题前加[WIP]

likedislike
Bbaijinxiang_hwCollaborators
8 days ago Pre merge succeeded (commit_id: 1d0bad76374337427d4382b3e3bad8b3fc6ea79a)
Bbaijinxiang_hwCollaborators
8 days ago Force-pushed 1 commits:d803c07a-fix(pow/sub): A2/A3补充out dtype白名单校验(#3964)
Bbaijinxiang_hwCollaborators
8 days ago Pre merge succeeded (commit_id: 88918c1c8f1ef37fd57c13526742cfbd5d35dd97)
CANN-robotCANN-robotCollaborators
8 days ago Removed label: cann-cla/yes
CANN-robotCANN-robotCollaborators
8 days ago Removed label: ci-pipeline-passed
CANN-robot
CANN-robotCollaborators
8 days ago Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): ci-pipeline-passed.

likedislike
CANN-robotCANN-robotCollaborators
8 days ago Added label: cann-cla/yes
CANN-robotCANN-robotCollaborators
8 days ago Removed label: approved
CANN-robot
CANN-robotCollaborators
8 days ago Comments:

Notice

New code changes of the pull request are detected and remove these labels approved. 😳

likedislike
baijinxiang_hwCollaborators
8 days ago Comments:

/compile

likedislike
Bbaijinxiang_hwCollaborators
8 days ago Pre merge succeeded (commit_id: b87af5e23125fb51ff74763868cd7a36c2db428a)
CANN-robot
CANN-robotCollaborators
8 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
8 days ago Added label: ci-pipeline-running
Bbaijinxiang_hwCollaborators
8 days ago Change title to "fix(pow/sub): A2/A3补充out dtype白名单校验(#3964)", originally titled "fix(pow/sub): 补充out dtype白名单校验,非法out dtype返回161002(#3964)".
Bbaijinxiang_hwCollaborators
8 days ago Modify description of pull request
CANN-robotCANN-robotCollaborators
8 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
8 days ago Added label: ci-pipeline-passed
Bbaijinxiang_hwCollaborators
7 days ago Modify description of pull request
chenxingyu18Collaborators
7 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: approved
yue-maCollaborators
7 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
7 days ago Merged pull request