Closed
[Bug-Report|缺陷反馈]: aclnnMaskedSoftmaxWithRelPosBias 出参非连续静默错值(两段返回 0,64 个元素中 32 个未写入) #8684
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 按连续地址写入。
本条要点:出参 out 传入 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
chenjiayi_
4 days ago Comments:
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。


2 days ago Assign chenjiayi_ as responsible person
12 hours ago Removed responsible person chenjiayi_
更新说明
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 为哨兵,证明写的是连续布局。本单从 #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:outstride / storage{64,64,64,16,1}/{1,1,1,4,16}{128,128,128,32,2}/{1,1,1,4,32}缺陷组
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支持」原文:
该段无
npu=分段限定(文件内npu="310p"分段均为其他条款),对 Atlas A2 适用。按此承诺,out非连续时必须与连续出参逐元素一致,不应返回 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/(只有*_def.cpp/*_infershape.cpp/*_tiling.cpp), 接口由 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)out连续):ret=0/0,未写入元素0 / 64out最后一维 stride 改为 2:两段仍ret=0/0,未写入元素 32 / 64oSt[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; }实测输出:
CPU 标杆对照
另写独立于算子自身的 CPU 参考,严格照
docs/aclnnMaskedSoftmaxWithRelPosBias.md:out = softmax(x * scaleValue + relativePosBias, 最后一维)(scaleValue=1.0,数值稳定版先减行最大值):out4D 切片{128,128,32,2}out4D 转置{256,256,1,4}基线经 CPU 标杆逐元素验证正确 ⇒ 缺陷只在出参非连续通路,与构造、与基线本身均无关。
两种不同的非连续形态(切片 / 转置)都失败,说明不是某个特定 stride 的参数问题。
影响
out做切片 / 转置视图 接收的场景会拿到整片错位结果:本例 64 个元素中 32 个根本没被写、9 个值错位。> 0,未写入位保持调用方初值,下游matmul会把垃圾值当概率继续算, 不会报错也不会 NaN,属于最难定位的一类静默错值。x/attenMaskOptional/relativePosBias按切片构造仍 PASS), 只测入参非连续的用例集发现不了本问题。建议
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, ...),自动生成路径需要补齐后者。避免"返回 0 但结果错"。
out传 stride 带空洞的视图,按 stride 读回后与连续基线逐元素比对。