Closed
[Bug-Report|缺陷反馈]: aclnnSquaredRelu 放行非连续 out 且忽略 strides 导致输出静默错位 #7757
liuhanwen8Add on  23 days agoClosed at  18 days ago
liuhanwen8
liuhanwen8
23 days ago Create

aclnnSquaredRelu 放行非连续 out 且忽略 strides,输出静默错位

现象

out 传非连续张量(shape [2,4]、strides [8,1],底层 storage [2,8])时,第一段接口返回 0,内核按连续内存写入 8 个结果,而按 out 的逻辑 stride 读回时第二行仍是初始化哨兵值:8 个逻辑元素里有 4 个没有被写到。

用例 out strides 首段返回 逻辑视图错值数
对照 [4,1](连续) 0 0
缺陷 [8,1](非连续) 0(资料标 ×,应拒绝) 4

实测输出:

CTRL   return code = 0
CTRL   wrong elements in logical view = 0
DEFECT return code = 0
DEFECT wrong elements in logical view = 4

期望

资料 activation/squared_relu/docs/aclnnSquaredRelu.md 参数表中 out 的「非连续Tensor」列标注为 ×。既然不支持,第一段就应返回 161002 拒绝;若要支持,则必须按 out 的 strides 写入。当前既不拒绝、也不按 stride 写,调用方拿到的是静默错值。

环境

  • 昇腾 910B3(Atlas A2 训练系列),CANN 9.2.0
  • 算子仓 cann/ops-nn master 59c0720c
  • 资料:activation/squared_relu/docs/aclnnSquaredRelu.md

复现步骤

  1. 把下方脚本保存为 repro.cpp。
  2. 编译:g++ -O2 -std=c++17 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi_nn -lopapi_math。
  3. 运行:ASCEND_RT_VISIBLE_DEVICES=0 ./repro。
  4. 对照行错值应为 0;缺陷行首段返回 0 且逻辑视图错值 = 4,说明第二个逻辑行未写入。

复现脚本

#include <cmath>
#include <cstdio>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_squared_relu.h"

static const float SENT = -12345.0f;

static void *Dev(const std::vector<float> &v)
{
    void *p = nullptr;
    aclrtMalloc(&p, v.size() * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(p, v.size() * sizeof(float), v.data(), v.size() * sizeof(float), ACL_MEMCPY_HOST_TO_DEVICE);
    return p;
}

// outShape=[2,4],outStrides 由调用方给;storage 恒为 [2,8](16 个 float)
static void Run(const char *tag, std::vector<int64_t> outStrides, aclrtStream stream)
{
    std::vector<int64_t> shape = {2, 4};
    std::vector<int64_t> storage = {2, 8};
    std::vector<float> inVals = {1, -2, 3, -4, 5, -6, 7, -8};
    std::vector<float> outVals(16, SENT);
    void *iPtr = Dev(inVals), *oPtr = Dev(outVals);
    aclTensor *in = aclCreateTensor(shape.data(), 2, ACL_FLOAT, (std::vector<int64_t>{4, 1}).data(), 0,
                                    ACL_FORMAT_ND, shape.data(), 2, iPtr);
    aclTensor *out = aclCreateTensor(shape.data(), 2, ACL_FLOAT, outStrides.data(), 0,
                                     ACL_FORMAT_ND, storage.data(), 2, oPtr);
    uint64_t ws = 0;
    aclOpExecutor *exec = nullptr;
    int ret = aclnnSquaredReluGetWorkspaceSize(in, out, &ws, &exec);
    if (ret == 0) {
        void *w = nullptr;
        if (ws > 0) aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST);
        ret = aclnnSquaredRelu(w, ws, exec, stream);
        if (ret == 0) aclrtSynchronizeStream(stream);
        if (w) aclrtFree(w);
    }
    std::vector<float> back(16, 0.0f);
    aclrtMemcpy(back.data(), 64, oPtr, 64, ACL_MEMCPY_DEVICE_TO_HOST);
    int wrong = 0;
    for (int r = 0; r < 2; ++r) {
        for (int c = 0; c < 4; ++c) {
            float want = inVals[r * 4 + c] > 0 ? inVals[r * 4 + c] * inVals[r * 4 + c] : 0.0f;
            if (std::fabs(back[r * outStrides[0] + c * outStrides[1]] - want) > 1e-3) ++wrong;
        }
    }
    std::printf("%s return code = %d\n", tag, ret);
    std::printf("%s wrong elements in logical view = %d\n", tag, wrong);
    aclDestroyTensor(in);
    aclDestroyTensor(out);
    aclrtFree(iPtr);
    aclrtFree(oPtr);
}

int main()
{
    aclInit(nullptr);
    aclrtSetDevice(0);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);
    Run("CTRL  ", {4, 1}, stream);   // 连续 out:[2,4] 紧凑
    Run("DEFECT", {8, 1}, stream);   // 非连续 out:行间隔 8(资料标 ×,应拒绝)
    aclrtDestroyStream(stream);
    aclrtResetDevice(0);
    aclFinalize();
    return 0;
}

影响

框架侧把 out 传成视图(切片、转置后 buffer、narrow 结果)时很常见;此时算子返回成功但结果错位,误差被后续层放大,且没有任何告警。数据静默错误比报错更难排查。

建议

在 out 的校验里补连续性检查:非连续时返回 161002(与资料 × 对齐);如果产品上确实要支持非连续 out,则 kernel 侧按 out 的 strides 写,并补一条覆盖该形态的 UT/ST。

likedislike
liuhanwen8liuhanwen8
23 days ago Added label: bug-report
liuhanwen8liuhanwen8
23 days ago Modify description of issue
yuning_chenyuning_chenCollaborators
23 days ago Assign east_yang as responsible person
yuning_chen
yuning_chenCollaborators
23 days ago Comments:

初步分析

感谢反馈。

现象理解:aclnnSquaredRelu 放行了非连续 out(或未按约束拒绝)。
当前判断:初步判断,待复现确认。连续性约束若资料要求拒绝,当前实现可能漏检。
涉及范围:activation/squared_relu 连续性校验。
下一步:核对资料约束,补校验或改文档,并加用例。

likedislike
yuning_chen
yuning_chenCollaborators
23 days ago Comments:

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

likedislike
east_yang
east_yangCollaborators
19 days ago Comments:

/unassign @east_yang

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Removed responsible person east_yang
east_yang
east_yangCollaborators
19 days ago Comments:

/assign @guijianwei

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Assign guijianwei as responsible person
guijianweiguijianweiCollaborators
18 days ago Associated pull request
CANN-robotCANN-robotCollaborators
18 days ago Close issue
CANN-robotCANN-robotCollaborators
17 days ago Added label: resolved