Closed
[Bug-Report|缺陷反馈]: aclnnMaskedSoftmaxWithRelPosBias 出参非连续静默错值(两段返回 0,64 个元素中 32 个未写入) #8684
liuhanwen8Add on  9 days agoClosed at  8 days ago
liuhanwen8
liuhanwen8
9 days ago Create

2026-11-12 更新:已在昇腾 910B3/910B2 + CANN 9.2.0 与 9.3.0 上复测并核对源码。实测结论与需要修正的计数、参数或根因描述见下方「更新说明」;原正文中已被证伪的原句已就地更正。

更新说明

  • 复现程序里 i 为 size_t,(i % 17) - 8 与 (i % 5) - 2 在负值分支发生无符号回绕,实际生成的是约 9.2e18 量级的输入值与 bias,而非表面上的小数。按该实际行为复算,日志中的 0.25/0/0.25 分布可由 softmax 解释。
  • 这不影响主结论:out 非连续时拼接/转置两种形态均返回 0/0,且 storage 前 8 与基线逐元素相同、按 stride 读回 32/64 为哨兵,证明写的是连续布局。
  • 建议改为先转有符号再减并重跑,以获得更接近实际的输入分布;4D 切片、转置两种补充用例需要附完整构造代码。

本单从 #8663 拆出单独立项(同一根因:无 op_host/op_api/ 的自动生成接口未把结果按出参 stride 拷回;#8663 现只保留 4 个未被单独覆盖的接口。本条独立原因:仅 aclnnMaskedSoftmaxWithRelPosBias 单接口,且资料另有「由框架自动转为连续Tensor」的明文承诺)

现象

出参 out 传入 stride 带空洞的视图时,两段接口返回 0/0,但结果被按连续布局写入,
调用方按 stride 读回即错位。构造 4 组 x=(1,1,1,4,16)、attenMaskOptional/relativePosBias 为
(1,4,16),只改 out 的最后一维 stride:

组 out stride / storage 两段返回 读回为哨兵(未写入)的元素 已写入位置最大绝对差
基线 连续 {64,64,64,16,1} / {1,1,1,4,16} 0 / 0 0 / 64 —
缺陷 {128,128,128,32,2} / {1,1,1,4,32} 0 / 0 32 / 64 0.5

缺陷组 storage 前 8 个元素与基线逐元素相同(0.2500 0.2500 0.0000 …),但按 stride 读回变成
0.25 0 0 0.25 0 0 … —— 写入位置按连续下标推进,与 stride 无关。与基线逐元素比对:
41 / 64 不一致,其中 32 处是根本没被写入的哨兵位。三组重复运行 3 次输出逐字节一致。

期望

资料 norm/masked_softmax_with_rel_pos_bias/docs/aclnnMaskedSoftmaxWithRelPosBias.md
「约束说明 · 非连续Tensor支持」原文:

x、attenMaskOptional、relativePosBias、out均支持非连续Tensor(即strides不满足连续存储条件的Tensor),
传入后由框架自动转为连续Tensor再参与计算。

该段无 npu= 分段限定(文件内 npu="310p" 分段均为其他条款),对 Atlas A2 适用。按此承诺,
out 非连续时必须与连续出参逐元素一致,不应返回 0 却按连续布局写入。

环境

  • 硬件:Atlas A2(910B3),npu-smi 26.1.1,固件 7.8.0.6.201
  • CANN:9.3.0(compiler/version.info Version=9.3.0),Linux aarch64,g++ 10.3.1
  • 仓库与提交:cann/ops-nn b9eab18829bc8666069d5646b0939a50b9a40587(origin/master,git fetch 后)
  • 该算子目录 git ls-tree 下无 op_host/op_api/(只有 *_def.cpp/*_infershape.cpp/*_tiling.cpp), 接口由 op def 自动生成
  • 复现日期:2026-11-12

复现步骤

  1. 存下方脚本为 repro.cpp
  2. g++ -std=c++17 -O1 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lopapi -lnnopbase -Wl,--allow-shlib-undefined
  3. ./repro 0(0 为 device id)
  4. 第 1 段为基线(out 连续):ret=0/0,未写入元素 0 / 64
  5. 第 2 段仅把 out 最后一维 stride 改为 2:两段仍 ret=0/0,未写入元素 32 / 64
  6. 对照:把第 2 段 oSt[4] 改回 1(其余不动),同一份代码立即恢复为 0 处未写入

复现脚本

// aclnnMaskedSoftmaxWithRelPosBias:out 出力非连续(stride 带空洞)静默错值
// 判据:同一组入参,基线(out 连续) vs 缺陷组(out 最后一维 stride=2),按各自 stride 读回后比对。
// 资料「约束说明」声明 x/attenMaskOptional/relativePosBias/out 均支持非连续Tensor。
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstdint>
#include <cstdlib>
#include "acl/acl.h"
#include "aclnnop/aclnn_masked_softmax_with_rel_pos_bias.h"

static aclrtStream g_s;
static int64_t P(const std::vector<int64_t>& s) { int64_t n = 1; for (auto v : s) n *= v; return n; }
static std::vector<int64_t> Cs(const std::vector<int64_t>& s)
{
    std::vector<int64_t> st(s.size(), 1);
    for (int64_t i = (int64_t)s.size() - 2; i >= 0; --i) { st[i] = s[i + 1] * st[i + 1]; }    return st;
}
static aclTensor* Mk(const std::vector<int64_t>& sh, const std::vector<int64_t>& st,
                     const std::vector<int64_t>& ss, const std::vector<float>& h, void** d)
{
    size_t b = h.size() * 4;
    if (aclrtMalloc(d, b ? b : 4, ACL_MEM_MALLOC_HUGE_FIRST) != 0) { return nullptr; }
    aclrtMemcpy(*d, b, h.data(), b, ACL_MEMCPY_HOST_TO_DEVICE);
    return aclCreateTensor(sh.data(), sh.size(), ACL_FLOAT, st.data(), 0, ACL_FORMAT_ND,
                           ss.data(), ss.size(), *d);
}

static void Run(bool nc, const char* tag)
{
    std::vector<int64_t> xS = {1, 1, 1, 4, 16}, mS = {1, 4, 16}, oS = {1, 1, 1, 4, 16};
    std::vector<float> xH(P(xS)), mH(P(mS), 0.0f), bH(P(mS));
    for (size_t i = 0; i < xH.size(); ++i) { xH[i] = (float)((i % 17) - 8) * 0.25f; }    for (size_t i = 0; i < bH.size(); ++i) { bH[i] = (float)((i % 5) - 2) * 0.5f; }

    // 出参 stride;缺陷组最后一维 1->2,storage 最后一维 16->32    std::vector<int64_t> oSt = Cs(oS), oSs = oS;
    if (nc) {
        oSt[4] = 2; oSt[3] = 32; oSt[2] = 4 * oSt[3]; oSt[1] = oSt[2]; oSt[0] = oSt[1];
        oSs[4] = 32;
    }
    std::vector<float> oH(P(oSs), -999000.0f);

    void *dx = nullptr, *dm = nullptr, *db = nullptr, *dv = nullptr;
    aclTensor* tx = Mk(xS, Cs(xS), xS, xH, &dx);
    aclTensor* tm = Mk(mS, Cs(mS), mS, mH, &dm);
    aclTensor* tb = Mk(mS, Cs(mS), mS, bH, &db);
    aclTensor* to = Mk(oS, oSt, oSs, oH, &dv);
    if (!tx || !tm || !tb || !to) { printf("%s alloc failed\n", tag); return; }

    uint64_t ws = 0;
    aclOpExecutor* ex = nullptr;
    int r1 = (int)aclnnMaskedSoftmaxWithRelPosBiasGetWorkspaceSize(tx, tm, tb, 1.0, 0, to, &ws, &ex);
    int r2 = -1;    if (r1 == 0) {
        void* wp = nullptr;
        if (ws) { aclrtMalloc(&wp, ws, ACL_MEM_MALLOC_HUGE_FIRST); }
        r2 = (int)aclnnMaskedSoftmaxWithRelPosBias(wp, ws, ex, g_s);
        aclrtSynchronizeStream(g_s);
        if (wp) { aclrtFree(wp); }
    }
    std::vector<float> sv(P(oSs), 0.0f);
    if (r2 == 0) { aclrtMemcpy(sv.data(), sv.size() * 4, dv, sv.size() * 4, ACL_MEMCPY_DEVICE_TO_HOST); }

    // 按逻辑下标配合各自 stride 取回(前 3 维均为 1)
    std::vector<float> lg;
    for (int64_t s1 = 0; s1 < oS[3]; ++s1) {
        for (int64_t s2 = 0; s2 < oS[4]; ++s2) { lg.push_back(sv[s1 * oSt[3] + s2 * oSt[4]]); }
    }
    int nw = 0;
    for (auto v : lg) { if (v == -999000.0f) { ++nw; } }    printf("%s ret=%d/%d\n", tag, r1, r2);
    printf("  storage 前 8:");
    for (size_t i = 0; i < 8; ++i) { printf(" %.4f", sv[i]); }
    printf("\n  按 stride 读回 前 8:");
    for (size_t i = 0; i < 8; ++i) { printf(" %.6f", lg[i]); }
    printf("\n  未写入(仍为哨兵)元素: %d / %lld\n", nw, (long long)lg.size());

    static std::vector<float> bLg;
    if (!nc) { bLg = lg; }
    else if (!bLg.empty()) {
        int diff = 0; int nwBoth = 0; double mx = 0;
        for (size_t i = 0; i < bLg.size(); ++i) {
            bool west = (lg[i] == -999000.0f);            if (west) { ++nwBoth; }
            if ((double)bLg[i] != (double)lg[i]) { ++diff; }
            // 最大差只统计「两组都已写入」的位置,避免哨兵位抬高数值
            if (!west) {
                double d = std::fabs((double)bLg[i] - (double)lg[i]);                if (d > mx) { mx = d; }
            }
        }
        printf("  [比对] 基线 vs 缺陷组:不一致 %d / %lld(其中 %d 处为未写入),"
               "已写入位置最大绝对差 %.6g\n",
               diff, (long long)bLg.size(), nwBoth, mx);
    }
    aclDestroyTensor(tx); aclDestroyTensor(tm); aclDestroyTensor(tb); aclDestroyTensor(to);
    aclrtFree(dx); aclrtFree(dm); aclrtFree(db); aclrtFree(dv);
}

int main(int argc, char** argv)
{
    int32_t dev = (argc > 1) ? atoi(argv[1]) : 0;
    aclInit(nullptr); aclrtSetDevice(dev); aclrtCreateStream(&g_s);
    Run(false, "[基线  out 连续]");
    Run(true,  "[缺陷组 out 最后一维 stride=2]");
    aclrtDestroyStream(g_s); aclrtResetDevice(dev); aclFinalize();
    return 0;
}

实测输出:

[基线  out 连续] ret=0/0
  storage 前 8: 0.2500 0.2500 0.0000 0.0000 0.0000 0.2500 0.2500 0.0000
  按 stride 读回 前 8: 0.250000 0.250000 0.000000 0.000000 0.000000 0.250000 0.250000 0.000000
  未写入(仍为哨兵)元素: 0 / 64
[缺陷组 out 最后一维 stride=2] ret=0/0
  storage 前 8: 0.2500 0.2500 0.0000 0.0000 0.0000 0.2500 0.2500 0.0000
  按 stride 读回 前 8: 0.250000 0.000000 0.000000 0.250000 0.000000 0.000000 0.000000 0.000000
  未写入(仍为哨兵)元素: 32 / 64
  [比对] 基线 vs 缺陷组:不一致 41 / 64(其中 32 处为未写入),已写入位置最大绝对差 0.5

CPU 标杆对照

另写独立于算子自身的 CPU 参考,严格照 docs/aclnnMaskedSoftmaxWithRelPosBias.md:
out = softmax(x * scaleValue + relativePosBias, 最后一维)(scaleValue=1.0,数值稳定版先减行最大值):

场景 与 CPU 标杆比对 与连续基线比对
基线(全连续) 0/64 ✓ 一致 —
out 4D 切片 {128,128,32,2} — 超差 63/64,worst=0.216795 @1
out 4D 转置 {256,256,1,4} — 超差 60/64,worst=0.205136 @1

基线经 CPU 标杆逐元素验证正确 ⇒ 缺陷只在出参非连续通路,与构造、与基线本身均无关。
两种不同的非连续形态(切片 / 转置)都失败,说明不是某个特定 stride 的参数问题。

影响

  • 两段返回 0,调用方无法感知。swinTransformer window attention 里对 out 做切片 / 转置视图 接收的场景会拿到整片错位结果:本例 64 个元素中 32 个根本没被写、9 个值错位。
  • softmax 结果恒 > 0,未写入位保持调用方初值,下游 matmul 会把垃圾值当概率继续算, 不会报错也不会 NaN,属于最难定位的一类静默错值。
  • 入参侧非连续正常(x/attenMaskOptional/relativePosBias 按切片构造仍 PASS), 只测入参非连续的用例集发现不了本问题。

建议

  1. 该算子 op_host/ 下无 op_api/,接口由 op def 自动生成。资料那句「由框架自动转为连续Tensor
    再参与计算」描述的是转换,但输出侧缺少对应的按 view 拷回:同仓
    activation/hard_swish/op_host/op_api/aclnn_hardswish.cpp:110,130 的写法是入参
    l0op::Contiguous + 出参 l0op::ViewCopy(castOut, out, ...),自动生成路径需要补齐后者。
  2. 若短期不支持,请把资料该条改为「入参支持非连续,出参不支持」并在 host 侧返回 161002,
    避免"返回 0 但结果错"。
  3. 建议补 ST:out 传 stride 带空洞的视图,按 stride 读回后与连续基线逐元素比对。
likedislike
liuhanwen8liuhanwen8
9 days ago Added label: bug-report
liuhanwen8liuhanwen8
9 days ago Modify description of issue
liuhanwen8liuhanwen8
9 days ago Modify description of issue
liuhanwen8liuhanwen8
9 days ago Modify description of issue
yuning_chen
yuning_chenCollaborators
9 days ago Comments:

感谢反馈,问题成立,这条与 #8663(自动生成接口出参非连续静默错值)属于同一根因:op_api / 生成模板只校验 dtype 与 shape,没有按 out 的 view stride 回写,kernel 按连续地址写入。

本条要点:出参 out 传入 stride 带空洞的视图时,两段接口返回 0/0,但结果被按连续布局写入,

修复建议:

  1. 短期:文档与实现口径先对齐——资料已标 × 的,实现侧应显式返回 ACLNN_ERR_PARAM_INVALID(不能静默返回 0 让调用方拿到错位数据);资料标 √ 但实现不支持的,先改文档;
  2. 长期:op_api 生成模板统一处理——出参非连续时走「算到连续临时 buffer → 按 stride 拷回」,或直接拒绝;
  3. 出参声明了 AutoContiguous 的,必须在 launcher 里真正插入 contiguous 转换,否则属于声明不生效(等价虚标)。

建议与 #8663 / #8661 同一 PR 收敛,避免同一根因多轮返工;修完在本 issue 回贴改动点与验证结果。

likedislike
yuning_chenyuning_chenCollaborators
8 days ago Assign yue-ma as responsible person
yuning_chen
yuning_chenCollaborators
8 days ago Comments:

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

likedislike
Nice tryNice tryCollaborators
8 days ago Assign Nice_try as responsible person
CANN-robotCANN-robotCollaborators
8 days ago Close issue
CANN-robotCANN-robotCollaborators
8 days ago changed custom state from 待办的 to 已完成
CANN-robotCANN-robotCollaborators
8 days ago Added label: resolved
chenjiayi_
chenjiayi_Collaborators
4 days ago Comments:

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

资料已按当前实现边界修正:out 明确为仅支持连续 Tensor。

处理结果:

  • norm/masked_softmax_with_rel_pos_bias/docs/aclnnMaskedSoftmaxWithRelPosBias.md 的参数表把 out 的非连续支持标记由 √ 改为 ×,约束说明改为“x、attenMaskOptional、relativePosBias 支持非连续 Tensor;out 必须为连续 Tensor,传入非连续 Tensor 时不保证结果”。
  • 该接口由 op def 自动生成,生成路径按连续地址写入 out;本次为资料口径收敛,接口侧对非连续 out 的显式拒绝仍待后续处理。
  • PR:!14401(同时覆盖 #8683)。

验证:

  • master 文档 norm/masked_softmax_with_rel_pos_bias/docs/aclnnMaskedSoftmaxWithRelPosBias.md:138、:263 当前为上述口径。
  • PR !14401 描述记录:已在 Atlas A2(910B3)+ CANN 9.2.0 上用复现程序确认非连续 out 按连续布局写入,与修正后的“仅支持连续 Tensor”约束对应。

说明:调用方传入非连续 out 时需先转为连续 Tensor;如需接口层返回 161002 或补齐 ViewCopy,请重新提 issue 跟踪实现侧改动。

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

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

likedislike
chenjiayi_chenjiayi_Collaborators
2 days ago Assign chenjiayi_ as responsible person
chenjiayi_chenjiayi_Collaborators
12 hours ago Removed responsible person chenjiayi_