Closed
[Bug-Report|缺陷反馈]: aclnnGroupNormSilu 在 fp32 窄方差输入下 rstd 灾难性抵消为负,输出整块 NaN #7334
liuhanwen8Add on  26 days agoClosed at  22 days ago
liuhanwen8
liuhanwen8
26 days ago Create

环境信息

  • 硬件:Ascend 910B2(Atlas A2 训练系列),单 die
  • CANN:9.2.0(/usr/local/Ascend),驱动 npu-smi 26.1.1
  • OS:Ubuntu 22.04,kernel 5.15.0-94-generic,aarch64
  • 编译器:g++ (aarch64),链接 libopapi.so / libascendcl.so / libnnopbase.so

问题描述

aclnnGroupNormSilu 在 fp32 且组内方差远小于均值平方的合法输入下,rstdOut 被算成 NaN,
out 整块 NaN,而两段接口均返回 0(静默脏数据,无任何错误码)。

  • 输入:self=fp32[1,128],gamma=1,beta=0,group=1,eps=1e-5
  • 数值:用例 A x = 50 ± 0.001;用例 B x = 1000 ± 0.01(均为有限 fp32,组内方差 3.5e-7 / 3.5e-5)
  • 期望:rstd = 1/sqrt(var+eps) ≈ 310.7666 / 148.3512,out 有限
  • 实际:GetWorkspaceSize ret=0、aclnnGroupNormSilu ret=0,rstdOut=NaN,out 128/128 个 NaN;
    meanOut 正常(50 / 1000)

该输入满足资料全部约束(rank 2–8、group 整除第 2 维 C、eps>0、dtype fp32),不属于非法入参。

复现步骤

  1. 将下方代码保存为 repro.cpp
  2. 编译:g++ -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lopapi -lascendcl -lnnopbase -lpthread
  3. 运行:ASCEND_RT_VISIBLE_DEVICES=0 ./repro
  4. 观察 actual_rstd=nan、nan_out=128/128、RESULT=DEFECT_NAN

复现代码

// aclnnGroupNormSilu fp32 窄方差输入 -> rstd/out NaN(CANN 9.2.0 / Ascend910B2)
// 编译: g++ -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 \
//        -lopapi -lascendcl -lnnopbase -lpthread
// 运行: ASCEND_RT_VISIBLE_DEVICES=0 ./repro
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_group_norm_silu.h"
#define CK(x) do{auto _rc=(x); if(_rc!=ACL_SUCCESS){printf("FAIL L%d ret=%d\n",__LINE__,(int)_rc);return 2;}}while(0)
static aclTensor* mk(const std::vector<int64_t>& s, aclDataType dt, void* p){
    std::vector<int64_t> st(s.size(),1);
    for(int i=(int)s.size()-2;i>=0;--i) st[i]=s[i+1]*st[i+1];
    return aclCreateTensor(s.data(),s.size(),dt,st.data(),0,ACL_FORMAT_ND,s.data(),s.size(),p);
}
static int run(const char* tag,float off,float noise,uint32_t seed){
    const int C=128; std::vector<float> x(C),g(C,1.f),b(C,0.f),o(C,-7.f),m(1,-7.f),rst(1,-7.f);
    double s1=0,s2=0;
    for(int i=0;i<C;++i){ seed=seed*1664525u+1013904223u;
        x[i]=off+noise*(float)(2.0*((seed>>8)/16777216.0)-1.0); s1+=x[i]; s2+=(double)x[i]*x[i]; }
    double mu=s1/C, var=s2/C-mu*mu, re=1.0/std::sqrt(var+1e-5);
    void *dx,*dg,*db,*dout,*dm,*dr;
    CK(aclrtMalloc(&dx,C*4,ACL_MEM_MALLOC_HUGE_FIRST)); CK(aclrtMalloc(&dg,C*4,ACL_MEM_MALLOC_HUGE_FIRST));
    CK(aclrtMalloc(&db,C*4,ACL_MEM_MALLOC_HUGE_FIRST)); CK(aclrtMalloc(&dout,C*4,ACL_MEM_MALLOC_HUGE_FIRST));
    CK(aclrtMalloc(&dm,4,ACL_MEM_MALLOC_HUGE_FIRST)); CK(aclrtMalloc(&dr,4,ACL_MEM_MALLOC_HUGE_FIRST));
    CK(aclrtMemcpy(dx,C*4,x.data(),C*4,ACL_MEMCPY_HOST_TO_DEVICE));
    CK(aclrtMemcpy(dg,C*4,g.data(),C*4,ACL_MEMCPY_HOST_TO_DEVICE));
    CK(aclrtMemcpy(db,C*4,b.data(),C*4,ACL_MEMCPY_HOST_TO_DEVICE));
    std::vector<int64_t> xs={1,C}, gs={C}, ms={1,1};
    aclTensor *tx=mk(xs,ACL_FLOAT,dx),*tg=mk(gs,ACL_FLOAT,dg),*tb=mk(gs,ACL_FLOAT,db);
    aclTensor *to=mk(xs,ACL_FLOAT,dout),*tm=mk(ms,ACL_FLOAT,dm),*tr=mk(ms,ACL_FLOAT,dr);
    uint64_t ws=0; aclOpExecutor* ex=nullptr;
    aclnnStatus r1=aclnnGroupNormSiluGetWorkspaceSize(tx,tg,tb,1,1e-5,to,tm,tr,&ws,&ex);
    void* wp=nullptr; if(ws>0) CK(aclrtMalloc(&wp,ws,ACL_MEM_MALLOC_HUGE_FIRST));
    aclrtStream st=nullptr; CK(aclrtCreateStream(&st));
    aclnnStatus r2=(r1==ACL_SUCCESS)?aclnnGroupNormSilu(wp,ws,ex,st):(aclnnStatus)(-1);
    CK(aclrtSynchronizeStream(st));
    CK(aclrtMemcpy(o.data(),C*4,dout,C*4,ACL_MEMCPY_DEVICE_TO_HOST));
    CK(aclrtMemcpy(rst.data(),4,dr,4,ACL_MEMCPY_DEVICE_TO_HOST));
    int nan=0; for(int i=0;i<C;++i) if(std::isnan(o[i])) ++nan;
    printf("[%s] ret1=%d ret2=%d golden_rstd=%.4f actual_rstd=%.7g nan_out=%d/128 out[0]=%.7g\n",
           tag,(int)r1,(int)r2,re,rst[0],nan,o[0]);
    return std::isnan(rst[0])?1:0;
}
int main(){ CK(aclInit(nullptr)); CK(aclrtSetDevice(0));
    int bad=0; bad|=run("A_off50_noise1e-3",50.f,0.001f,7919);
    bad|=run("B_off1000_noise1e-2",1000.f,0.01f,7919);
    printf("RESULT=%s\n",bad?"DEFECT_NAN":"OK"); return bad?1:0; }

实测输出

[A_off50_noise1e-3] ret1=0 ret2=0 golden_rstd=310.7666 actual_rstd=nan nan_out=128/128 out[0]=nan
[B_off1000_noise1e-2] ret1=0 ret2=0 golden_rstd=148.3512 actual_rstd=nan nan_out=128/128 out[0]=nan
RESULT=DEFECT_NAN

期望行为

rstdOut 应为有限值(用例 A ≈310.7666,用例 B ≈148.3512),out 与 SiLU((x-mean)*rstd*gamma+beta)
一致且无 NaN;即使方差因浮点舍入被抵消,也不应把负值直接送进 sqrt。

根因分析

fp32 kernel 用「平方的均值减均值的平方」一遍法计算方差:

  • norm/group_norm_silu/op_kernel/group_norm_silu_b32.h:177
  • norm/group_norm_silu/op_kernel/group_norm_silu_small_b32.h:181
rstd = float(1.0) / sqrt(rstd * numRec - mean * mean + tiling->epsilon);

当 E[x^2] ≈ mean^2(公共偏置远大于组内扰动)时,两个相近的 fp32 数相减发生灾难性抵消,
差值可能为负;sqrt(负数) 得到 NaN,随后 scale = rstd * gamma 把 NaN 传播到整个输出。
meanOut 走同一路但只做乘法,故数值正常——这与实测「mean 正确、rstd/out 全 NaN」一致。
arch35 的同族实现已使用 Welford(group_norm_silu_regbase_base.h),b32/small_b32 路径未使用。

影响面:任何 fp32 激活带较大公共偏置、组内方差较小的真实网络层(归一化前常见)都会整层 NaN,
且不返回错误码。本机 1874 条 fp32/fp16/bf16 用例中 20 条 FAIL,全部为 fp32 小方差;
rstdOut 相对误差 7e-4~1.6e-2,定向用例中 rstd 实测 4.0(期望 148.35)。

关联:cann/ops-nn#6068(aclnnGroupNormSiluQuant 窄方差 NaN,已关闭)为同机理的量化接口版本;
本 issue 是非量化 aclnnGroupNormSilu 的独立复现。

修复建议:b32/small_b32 改两遍法或 Welford;或在开方前对 var 做负值钳位并记 warning(治标)。

likedislike
liuhanwen8liuhanwen8
26 days ago Added label: bug-report
liuhanwen8
liuhanwen8
26 days ago Comments:

同机理(one-pass 方差灾难性抵消)在量化兄弟接口 aclnnGroupNormSiluQuant 上也有 E1 复现:https://gitcode.com/cann/ops-nn/issues/7343(fp16 大偏置 rstdOut 相对误差 2.99%,C++ 探针 + fp64 对照)。本接口是 fp32 窄方差下 rstd 为负/NaN;两者 kernel 各自实现 one-pass 方差,建议一起修。

likedislike
yuning_chen
yuning_chenCollaborators
26 days ago Comments:

初步分析

感谢反馈,我们已开始核对这个问题。

现象理解:缺陷反馈;aclnnGroupNormSilu 在 fp32 窄方差输入下 rstd 灾难性抵消为负,输出整块 NaN。
当前判断:从描述看,这是缺陷反馈「aclnnGroupNormSilu 在 fp32 窄方差输入下 rstd 灾难性抵消为负,输出整块 NaN」。正文已给出对照用例与期望/实际差异,更像参数校验、错误码语义或静默错值类问题。以上为初步判断,待复现确认,不能当成已确认根因。
涉及范围:aclnnGroupNormSilu、aclnnop、aclnn_group_norm_silu
下一步:我们会对照资料契约与实现路径继续定位,并按需补充回归用例。

likedislike
yuning_chenyuning_chenCollaborators
25 days ago Assign east_yang as responsible person
yuning_chen
yuning_chenCollaborators
25 days ago Comments:

您好,问题已收到,当前 @east_yang 正在处理中。

likedislike
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:

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

根因:与您的分析一致。非 RegBase 的各 fp32 路径(实际复现命中的是 hw1_b32 模板)采用 one-pass 方差(Σx²/N − mean²),公共偏置远大于组内方差时发生灾难性抵消,方差可能为负,sqrt 得到 NaN 后经 scale=rstd×gamma
传播至整组输出;meanOut 仅做乘法故数值正常,与您观察到的现象一致。

修复:全部 9 个非 RegBase kernel 模板共 15 处,在开方前对方差钳位至 0(负方差场景 rstd=1/sqrt(eps)),消除 NaN 与静默错值;正常输入(方差≥0)结果位级不变,已经 14 例全路径×dtype 精度回归(fp32/fp16/bf16)与真机定向用例验证无回归。

说明:本次为消除 NaN 的修复,极端抵消场景下 rstd 数值精度仍受 one-pass 方差固有精度限制(您用例 B 的场景),两遍法/Welford 的精度优化我们另行评估排期,并在 GroupNormSiluQuant(#6068 同机理)上一并考虑。

修复已随 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