Closed
[Bug-Report|缺陷反馈]: aclnnGeluBackward 不校验出参 gradInput 的 dtype,INT8/INT32/BOOL 两段均返回 0 而非 161002 #7157
liuhanwen8Add on  Oct 22Closed at  22 days ago
liuhanwen8
liuhanwen8
Oct 22 Create

一、问题描述

aclnnGeluBackward 对出参 gradInput 的 dtype 不做校验:传入资料明确不支持的 INT8 / INT32 / BOOL 时,aclnnGeluBackwardGetWorkspaceSize 与 aclnnGeluBackward 两段均返回 0(ACLNN_SUCCESS),未按资料返回 161002。

资料(activation/gelu_grad/docs/aclnnGeluBackward.md):

  • 参数表 gradInput 的数据类型列只列了 FLOAT、FLOAT16、BFLOAT16;
  • 错误码表 E161002 原文:gradOutput、self、gradInput的数据类型和数据格式不在支持的范围之内。

实测该子项未被拦截。同接口的 shape 类非法入参是会被拦的(见下"边界确认"),所以这不是"整个校验都不生效",而是出参 dtype 这一路漏了。

二、环境信息

  • 硬件:Atlas A2 训练系列产品(910B2,DAV_2201)
  • CANN:9.2.0(npu-smi 26.1.1)
  • 接口:aclnnGeluBackward(gradOutput, self, gradInput, workspaceSize, executor)
  • 源码位置:activation/gelu_grad/op_api/aclnn_gelu_backward.cpp(master)

三、重现步骤

最小复现(g++ 直编,不依赖任何测试框架;完整代码见附件 repro_gelu_backward_output_dtype.cpp):

source $ASCEND_HOME/set_env.sh
g++ -std=c++17 repro_gelu_backward_output_dtype.cpp -o repro \
    -I$ASCEND_HOME/include -I$ASCEND_HOME/aarch64-linux/include \
    -L$ASCEND_HOME/aarch64-linux/lib64 -L$ASCEND_HOME/lib64 \
    -lascendcl -lopapi -lnnopbase
./repro

核心逻辑(生效部分):

// gradOutput / self 固定 fp32,shape=[8];只改 gradInput 的 dtype
aclTensor* gradOutput = MakeTensor(devGrad, {8}, ACL_FLOAT);
aclTensor* self       = MakeTensor(devSelf, {8}, ACL_FLOAT);
aclTensor* gradInput  = MakeTensor(devOut,  {8}, outDtype);   // outDtype 依次取 FLOAT/INT8/INT32/BOOL

uint64_t ws = 0; aclOpExecutor* ex = nullptr;
aclnnStatus r1 = aclnnGeluBackwardGetWorkspaceSize(gradOutput, self, gradInput, &ws, &ex);
if (r1 == 0) { r2 = aclnnGeluBackward(workspace, ws, ex, stream); }

实测输出:

=== aclnnGeluBackward 出参 dtype 校验 ===
baseline (合法)   gradInput dtype=float32 -> GetWorkspaceSize=0  Execute=0
非法: INT8        gradInput dtype=int8    -> GetWorkspaceSize=0  Execute=0
非法: INT32       gradInput dtype=int32   -> GetWorkspaceSize=0  Execute=0
非法: BOOL        gradInput dtype=bool    -> GetWorkspaceSize=0  Execute=0

期望:后三行均为 161002。

四、根因

activation/gelu_grad/op_api/aclnn_gelu_backward.cpp:60:

static bool CheckPromoteType(const aclTensor* gradOutput, const aclTensor* self,
                             const aclTensor* /* gradInput */)   // ← 形参被注释掉
{
    op::DataType promoteType;
    if (!CheckPromoteTypeGeluBackward(gradOutput, self, promoteType, DTYPE_SUPPORT_LIST)) {
        return false;
    }
    return true;                                                 // ← 从不使用 gradInput
}

CheckPromoteTypeGeluBackward(common/inc/op_api/level2_base_nn.h:349)只做两件事:
① 用 PromoteType(gradOutput->GetDataType(), self->GetDataType()) 推导 promote 类型;
② 检查该 promote 类型在 DTYPE_SUPPORT_LIST 内。

gradInput 除了判空之外没有被读取,因此 CheckParams 第 2 步注释所写的
「检查gradOutput和self的dtype能否作类型推导, 类型推导后的结果需要与gradInput一致」
后半句实际未执行。gradInput=int8 时 promote 类型仍是 fp32,白名单自然通过。

五、边界确认(排除误判)

同一套复现程序里追加的对照项,说明拦截机制本身是工作的、只缺出参 dtype 这一路:

非法入参 首段返回 是否符合资料
gradInput shape [3x5](与 self [8] 不匹配) 161002 ✅ 拦截
self shape [16](与 gradOutput [8] 不匹配) 161002 ✅ 拦截
gradOutput/self/gradInput 任一为 nullptr 161001 ✅ 拦截
gradInput dtype INT8 / INT32 / BOOL 0 ❌ 未拦截

两条确定性开关腿(默认 / ACL 确定性开启)行为一致,均返回 0。

六、影响

出参 dtype 配错时接口层不给任何信号,两段都返回 0。调用方把 fp32 梯度缓冲误声明成整型时,
错误要到后续消费环节才可能暴露,且难以归因到接口契约。属「契约承诺的拦截未兑现」,
不影响合法路径的正确性。

七、建议修复

在 CheckPromoteType(或新增 CheckGradInputDtype)里补齐出参校验,二选一:

  1. 按 promote 一致性(贴合现有注释语义):gradInput->GetDataType() 必须等于 promoteType;
  2. 按支持列表:gradInput->GetDataType() 必须在 DTYPE_SUPPORT_LIST 内。

任一不满足返回 ACLNN_ERR_PARAM_INVALID(161002)。建议顺带补 UT:
gradInput 取 INT8/INT32/BOOL 各一条,断言首段返回 161002。

八、日志与验证

  • 首段返回码经 aclnnStatus 直接打印,未经任何包装;
  • 出参显存按「元素数 × 该 dtype 字节数」分配,排除因越界导致的现象;
  • 复现程序在 A2 真机连续运行多次结果稳定;
  • 该现象由 ascend-op-error-scenario 线(资料错误码子项单点变异)在两腿各命中 2 条 ACCEPTED 后发现,
    再用上述独立 C 程序逐 dtype 复核确认。
likedislike
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

初步分析

感谢反馈。

现象理解:aclnnGeluBackward 没有校验出参 gradInput 的 dtype。INT8、INT32、BOOL 两段都返回成功,资料要求 161002。shape 类非法入参是会被拦住的。

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

涉及范围:activation/gelu_grad 的出参 dtype 检查。

下一步:在 A2 上用 INT8 的 gradInput 看返回码。确认后按白名单在入口拒绝。

likedislike
yuning_chenyuning_chenCollaborators
Oct 22 Assign shilulu as responsible person
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

您好,该问题已收到,当前问题 @east_yang 处理中

likedislike
yuning_chenyuning_chenCollaborators
Oct 22 Removed responsible person shilulu
yuning_chenyuning_chenCollaborators
Oct 22 Assign east_yang as responsible person
east_yangeast_yangCollaborators
24 days ago Assign shilulu as responsible person
east_yangeast_yangCollaborators
24 days ago Removed responsible person east_yang
shilulu
shiluluCollaborators
22 days ago Comments:

感谢反馈,问题已确认并修复。

根因:与您的分析一致。CheckPromoteType 仅校验 gradOutput 与 self 的类型推导,gradInput 除判空外未被读取,出参 dtype 不在支持列表时两段均返回 0,契约中「类型推导结果与 gradInput 一致」的校验未实现。

修复:补齐出参 gradInput 的 dtype 白名单校验(FLOAT/FLOAT16/BFLOAT16,与资料参数表一致),不在列表内返回 161002。采用白名单而非「等于推导类型」的口径,以保持出参在支持列表内、依赖 Cast 兜底转换的现有合法用法不受影响。

修复已随 PR https://gitcode.com/cann/ops-nn/pull/13166 提交,请基于该 PR 验证。

likedislike
CANN-robotCANN-robotCollaborators
22 days ago Close issue
CANN-robotCANN-robotCollaborators
22 days ago Added label: resolved