Closed
[Bug-Report|缺陷反馈]: aclnnExpandIntoJaggedPermute 出参非连续静默错值(两段返回 0,按 stride 读回 6 个位置错 3 个) #8683
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 上复测并核对源码。实测结论与需要修正的计数、参数或根因描述见下方「更新说明」;原正文中已被证伪的原句已就地更正。

更新说明

  • 主复现(6 元素、INT32)经独立复算成立:期望 3 4 0 1 5 6,缺陷组按 stride 读回 3 个未写。
  • 「CPU 标杆」段的非平凡用例 inputOffset={0,100,200} 只有 3 个元素,而文档要求长度为 inputLen+1(本例应为 4,且末值等于输入总元素数)。该用例属非法输入,建议改为如 {0,100,200,205} 后重跑,再引用其 11/12 超差结果。
  • 基线标签中的 stride={6} 与实际打印的 stride={1} 不一致,仅为标签笔误,不影响结果。

本单从 #8663 拆出单独立项(同一根因:无 op_host/op_api/ 的自动生成接口未把结果按出参 stride 拷回;#8663 现只保留 4 个未被单独覆盖的接口。本条独立原因:仅 aclnnExpandIntoJaggedPermute 单接口,out 为 INT32)

现象

出参 outputPermuteOut 传入 stride 带空洞的视图时,两段接口返回 0/0,但结果被按连续布局写入,
调用方按 stride 读回即错位。同一组入参下基线与缺陷组逐元素对比如下(out 逻辑 shape 均为 {6}):

组 出参 stride / storage 两段返回 storage 前 6 按 stride 读回
基线 {6} / {6} 0 / 0 3 4 0 1 5 6 3 4 0 1 5 6
缺陷 {2} / {12} 0 / 0 3 4 0 1 5 6 3 0 5 + 3 个未写入

入参 permute/inputOffset/outputOffset 三组完全一致,只改出参 stride。
缺陷组 storage 前 6 个元素与基线逐元素相同,而 stride 视图覆盖的 6 个下标里只有 3 个被写到 ——
写入位置按 0,1,2,... 连续推进,与 stride 无关。

期望

资料 index/expand_into_jagged_permute/docs/aclnnExpandIntoJaggedPermute.md 参数表中出参
(函数原型与「计算公式」写作 outputPermuteOut,参数表列名写作 outputPermute)的
「非连续Tensor」列为 √(该表无 npu= 分段限定,对 Atlas A2 同样适用)→
出参非连续时应得到与连续出参逐元素相同的结果,不应返回 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/,接口由 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 段为基线(出参连续):ret=0/0,storage 与按 stride 读回均为 3 4 0 1 5 6
  5. 第 2 段仅把出参改成 stride={2}、storage={12}:两段仍 ret=0/0,按 stride 读回 6 个位置中 3 个仍是未写入值
  6. 对照:把第 2 段的 outStrides 改回连续 {6},同一份代码立即恢复正确

复现脚本

// aclnnExpandIntoJaggedPermute 出参非连续(stride 带空洞)静默错值复现
// 判据:同一组入参,基线(出参连续)vs 缺陷组(出参 stride 带空洞),按各自 stride 读回后逐元素比对。
// 语义(资料「计算公式」):outputPermuteOut[outputOffset[i]:outputOffset[i+1]] = arange(inputOffset[permute[i]], ...+len)
// 取 permute={1,0,2},inputOffset={0,3,5,8},outputOffset={0,2,4,6},outputSize=6,期望 out={3,4,0,1,5,6}
#include <cstdio>
#include <vector>
#include <cstdint>
#include <cstdlib>
#include "acl/acl.h"
#include "aclnnop/aclnn_expand_into_jagged_permute.h"

static aclrtStream g_stream;
static constexpr int64_t NPRM = 3;   // permute 长度
static constexpr int64_t NOFF = 4;   // inputOffset / outputOffset 长度 = NPRM + 1
static constexpr int64_t OSIZE = 6;  // 出参长度

static std::vector<int64_t> ContigStrides(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[(size_t)i] = s[(size_t)(i + 1)] * st[(size_t)(i + 1)];
    }
    return st;
}

static int64_t Prod(const std::vector<int64_t>& s)
{
    int64_t n = 1;
    for (auto v : s) { n *= v; }
    return n;
}

// 创建 INT32 tensor。host 长度必须等于 Prod(storageShape)
static aclTensor* MakeInt32(const std::vector<int64_t>& shape, const std::vector<int64_t>& strides,
                            const std::vector<int64_t>& storageShape, const std::vector<int32_t>& host,
                            void** devOut)
{
    size_t bytes = host.size() * sizeof(int32_t);
    void* dev = nullptr;
    if (aclrtMalloc(&dev, bytes ? bytes : 4, ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS) { return nullptr; }
    aclrtMemcpy(dev, bytes, host.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE);
    aclTensor* t = aclCreateTensor(shape.data(), (uint64_t)shape.size(), ACL_INT32, strides.data(), 0,
                                   ACL_FORMAT_ND, storageShape.data(), (uint64_t)storageShape.size(), dev);
    *devOut = dev;
    return t;
}

// nc==false:基线,出参连续(stride={6})
// nc==true :缺陷组,出参 stride={2},storage 12(>= (6-1)*2+1),view 覆盖 6 个带空洞下标
static void Run(bool nc, const char* tag)
{
    std::vector<int64_t> prmS = {NPRM};
    std::vector<int64_t> offS = {NOFF};
    std::vector<int64_t> outS = {OSIZE};

    std::vector<int32_t> prmH = {1, 0, 2};
    std::vector<int32_t> inOffH = {0, 3, 5, 8};
    std::vector<int32_t> outOffH = {0, 2, 4, 6};

    std::vector<int64_t> outStrides = nc ? std::vector<int64_t>{2} : ContigStrides(outS);
    std::vector<int64_t> outStorage = nc ? std::vector<int64_t>{12} : outS;
    // 出参缓冲全填哨兵:非 view 下标若被写,说明实现按连续布局越界写
    std::vector<int32_t> outHost((size_t)Prod(outStorage), -999000);

    void *dPrm = nullptr, *dIn = nullptr, *dOut = nullptr, *dOutT = nullptr;
    aclTensor* tPrm = MakeInt32(prmS, ContigStrides(prmS), prmS, prmH, &dPrm);
    aclTensor* tIn = MakeInt32(offS, ContigStrides(offS), offS, inOffH, &dIn);
    aclTensor* tOut = MakeInt32(offS, ContigStrides(offS), offS, outOffH, &dOut);   // outputOffset
    aclTensor* tOutT = MakeInt32(outS, outStrides, outStorage, outHost, &dOutT);     // outputPermuteOut
    if (!tPrm || !tIn || !tOut || !tOutT) { printf("%s alloc failed\n", tag); return; }

    uint64_t ws = 0;
    aclOpExecutor* ex = nullptr;
    int r1 = (int)aclnnExpandIntoJaggedPermuteGetWorkspaceSize(tPrm, tIn, tOut, OSIZE, tOutT, &ws, &ex);
    int r2 = -1;    if (r1 == 0) {
        void* wp = nullptr;
        if (ws) { aclrtMalloc(&wp, ws, ACL_MEM_MALLOC_HUGE_FIRST); }
        r2 = (int)aclnnExpandIntoJaggedPermute(wp, ws, ex, g_stream);
        aclrtSynchronizeStream(g_stream);
        if (wp) { aclrtFree(wp); }
    }

    std::vector<int32_t> storage((size_t)Prod(outStorage), 0);
    if (r2 == 0) {
        aclrtMemcpy(storage.data(), storage.size() * sizeof(int32_t), dOutT,
                    storage.size() * sizeof(int32_t), ACL_MEMCPY_DEVICE_TO_HOST);
    }

    printf("%s ret=%d/%d\n", tag, r1, r2);
    printf("  storage(%lld):", (long long)Prod(outStorage));
    for (auto v : storage) { printf(" %d", v); }
    printf("\n  按 stride={%lld} 读回:", (long long)outStrides[0]);
    for (int64_t i = 0; i < OSIZE; ++i) { printf(" %d", storage[(size_t)(i * outStrides[0])]); }
    printf("\n  期望(连续基线语义): 3 4 0 1 5 6\n");

    aclDestroyTensor(tPrm); aclDestroyTensor(tIn); aclDestroyTensor(tOut); aclDestroyTensor(tOutT);
    aclrtFree(dPrm); aclrtFree(dIn); aclrtFree(dOut); aclrtFree(dOutT);
}

int main(int argc, char** argv)
{
    int32_t dev = (argc > 1) ? atoi(argv[1]) : 0;
    aclInit(nullptr);
    aclrtSetDevice(dev);
    aclrtCreateStream(&g_stream);
    Run(false, "[基线  出参连续  stride={6}]");
    Run(true,  "[缺陷组 出参非连续 stride={2} storage=12]");
    aclrtDestroyStream(g_stream);
    aclrtResetDevice(dev);
    aclFinalize();
    return 0;
}

实测输出:

[基线  出参连续  stride={6}] ret=0/0
  storage(6): 3 4 0 1 5 6
  按 stride={1} 读回: 3 4 0 1 5 6
[缺陷组 出参非连续 stride={2} storage=12] ret=0/0
  storage(12): 3 4 0 1 5 6 -999000 -999000 -999000 -999000 -999000 -999000  按 stride={2} 读回: 3 0 5 -999000 -999000 -999000     <-- 应为 3 4 0 1 5 6

三组输入重复运行 3 次输出逐字节一致,非竞争问题。

CPU 标杆对照

另写独立于算子自身的 CPU 参考,严格照 docs/aclnnExpandIntoJaggedPermute.md 公式:
out[outputOffset[i] : outputOffset[i+1]] = arange(inputOffset[permute[i]], +len)

采用非平凡构造(避免恒等退化为 out[i]=i):permute={2,0,1}、inputOffset={0,100,200}、
outputOffset={0,5,8,12},标杆应为 200 201 202 203 204 0 1 2 100 101 102 103。

场景 与 CPU 标杆比对 与连续基线比对
基线(全连续) 0/12 ✓ 逐元素一致 —
out stride=2 — 不一致 11/12,@1 got=202 want=201
out stride=3 — 不一致 11/12,@1 got=203 want=201

错位倍率恰等于 stride:stride=2 时比正确值多 1,stride=3 时多 2 —— 这是"按连续布局写入"的直接算术后果。
基线经 CPU 标杆验证正确 ⇒ 缺陷只在出参非连续通路。

影响

  • 两段返回 0,调用方无法感知。用 as_strided / 切片视图接收 outputPermuteOut 的 MoE 与稀疏特征分片调用会读到错位结果;本例 stride=2 时 6 个位置错 3 个,且错位倍率恰等于 stride。
  • 未覆盖的 storage 位置保持调用方初值,下游若直接按 storage 消费会把垃圾值当有效索引。
  • 入参侧非连续正常(permute/inputOffset/outputOffset 均按切片构造仍 PASS), 因此只测入参非连续的用例集发现不了本问题。

建议

  1. 该算子 op_host/ 下只有 arch22/arch35/config/*_def.cpp,无 op_api/,接口由 op def
    自动生成。同仓 activation/hard_swish/op_host/op_api/aclnn_hardswish.cpp:110,130 的手写实现给出了
    正确写法:入参 l0op::Contiguous 转连续、出参 l0op::ViewCopy(castOut, out, ...) 按 view 拷回,
    自动生成路径需要同一对处理。
  2. 若短期不支持,请把资料出参的「非连续Tensor」改为 × 并在 host 侧返回 161002,
    避免"返回 0 但结果错"。
  3. 建议补 ST:出参传 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 按连续地址写入。

本条要点:出参 outputPermuteOut 传入 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
2 days ago Assign chenjiayi_ as responsible person
chenjiayi_
chenjiayi_Collaborators
2 days ago Comments:

补充说明本问题的跟进安排:

该 issue 已关闭,已补充 assign 给 @chenjiayi_ 复核关闭结论,原责任人 @Nice_try、@yue-ma 保留协同。

当前状态:

  • 关闭状态:已完成;复核项:aclnnExpandIntoJaggedPermute 出参非连续静默错值(两段返回 0,按 stride 读回 6 个位置错 3 个)。
  • 我先核对关闭时的结论与当前 master 的实现/资料是否一致。

下一步:

  • 复核完成后在本 issue 补充结论。
  • 如果还有后续需要,请重新提 issue。
likedislike
chenjiayi_chenjiayi_Collaborators
13 hours ago Removed responsible person chenjiayi_