Closed
[Bug-Report|缺陷反馈]: aclnnExpandIntoJaggedPermute 出参非连续静默错值(两段返回 0,按 stride 读回 6 个位置错 3 个) #8683
liuhanwen8Add on 9 days agoClosed at 8 days ago
9 days ago Added label: bug-report
9 days ago Modify description of issue
9 days ago Modify description of issue
9 days ago Modify description of issue
yuning_chen
9 days ago Comments:
9 days ago Comments:
感谢反馈,问题成立,这条与 #8663(自动生成接口出参非连续静默错值)属于同一根因:op_api / 生成模板只校验 dtype 与 shape,没有按 out 的 view stride 回写,kernel 按连续地址写入。
本条要点:出参 outputPermuteOut 传入 stride 带空洞的视图时,两段接口返回 0/0,但结果被按连续布局写入,
修复建议:
- 短期:文档与实现口径先对齐——资料已标
×的,实现侧应显式返回ACLNN_ERR_PARAM_INVALID(不能静默返回 0 让调用方拿到错位数据);资料标√但实现不支持的,先改文档; - 长期:op_api 生成模板统一处理——出参非连续时走「算到连续临时 buffer → 按 stride 拷回」,或直接拒绝;
- 出参声明了
AutoContiguous的,必须在 launcher 里真正插入 contiguous 转换,否则属于声明不生效(等价虚标)。
建议与 #8663 / #8661 同一 PR 收敛,避免同一根因多轮返工;修完在本 issue 回贴改动点与验证结果。


8 days ago Assign yue-ma as responsible person
yuning_chen
8 days ago Comments:
8 days ago Comments:
您好,问题已收到,当前 @yue-ma 正在处理中。


8 days ago Assign Nice_try as responsible person
8 days ago Close issue
8 days ago changed custom state from 待办的 to 已完成
8 days ago Added label: resolved
2 days ago Assign chenjiayi_ as responsible person
chenjiayi_
2 days ago Comments:
2 days ago Comments:
补充说明本问题的跟进安排:
该 issue 已关闭,已补充 assign 给 @chenjiayi_ 复核关闭结论,原责任人 @Nice_try、@yue-ma 保留协同。
当前状态:
- 关闭状态:已完成;复核项:aclnnExpandIntoJaggedPermute 出参非连续静默错值(两段返回 0,按 stride 读回 6 个位置错 3 个)。
- 我先核对关闭时的结论与当前 master 的实现/资料是否一致。
下一步:
- 复核完成后在本 issue 补充结论。
- 如果还有后续需要,请重新提 issue。


13 hours ago Removed responsible person chenjiayi_
更新说明
3 4 0 1 5 6,缺陷组按 stride 读回 3 个未写。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}):storage前 6{6}/{6}3 4 0 1 5 63 4 0 1 5 6{2}/{12}3 4 0 1 5 63 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 却按连续布局写入。
环境
npu-smi26.1.1,固件 7.8.0.6.201compiler/version.infoVersion=9.3.0),Linux aarch64,g++ 10.3.1cann/ops-nnb9eab18829bc8666069d5646b0939a50b9a40587(origin/master,git fetch后)git ls-tree下无op_host/op_api/,接口由 op def 自动生成复现步骤
repro.cppg++ -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./repro 0(0 为 device id)ret=0/0,storage与按 stride 读回均为3 4 0 1 5 6stride={2}、storage={12}:两段仍ret=0/0,按 stride 读回 6 个位置中 3 个仍是未写入值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; }实测输出:
三组输入重复运行 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。outstride=2@1 got=202 want=201outstride=3@1 got=203 want=201错位倍率恰等于 stride:stride=2 时比正确值多 1,stride=3 时多 2 —— 这是"按连续布局写入"的直接算术后果。
基线经 CPU 标杆验证正确 ⇒ 缺陷只在出参非连续通路。
影响
as_strided/ 切片视图接收outputPermuteOut的 MoE 与稀疏特征分片调用会读到错位结果;本例 stride=2 时 6 个位置错 3 个,且错位倍率恰等于 stride。storage位置保持调用方初值,下游若直接按 storage 消费会把垃圾值当有效索引。permute/inputOffset/outputOffset均按切片构造仍 PASS), 因此只测入参非连续的用例集发现不了本问题。建议
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 拷回,自动生成路径需要同一对处理。
避免"返回 0 但结果错"。