Closed
[Bug-Report|缺陷反馈]: aclnnDequantSwigluQuant 出参 yOut/scaleOut 尺寸未校验,小于推导尺寸时静默越界写 #7569
liuhanwen8Add on 24 days agoClosed at 16 days ago
24 days ago Added label: bug-report
yuning_chen
24 days ago Comments:
24 days ago Comments:
24 days ago Assign east_yang as responsible person
yuning_chen
24 days ago Comments:
24 days ago Comments:
您好,该问题已收到,当前问题 @east_yang 正在处理中


east_yang
19 days ago Comments:
19 days ago Comments:
/unassign @east_yang


19 days ago Removed responsible person east_yang
19 days ago Assign flag2233 as responsible person
17 days ago Associated pull request
16 days ago Close issue
16 days ago Added label: resolved
chenjiayi_
10 days ago Comments:
10 days ago Comments:
补充说明本问题的修复结果:
结论:已在 tiling 阶段补齐 yOut / scaleOut 的尺寸校验,声明尺寸与推导不一致时第一段直接失败,不再进入执行阶段造成越界写。错误码为 561002(ACLNN_ERR_INNER_TILING_ERROR),与资料错误码表「输入或输出的 shape 不满足约束要求」一致,不是 161002。
修复内容(文件/函数):
quant/dequant_swiglu_quant/op_host/dequant_swiglu_quant_tiling_base.cpp(Base 路径):三处只打日志不返回的校验补上return ge::GRAPH_FAILED——y 与 x 的 rank 不等、y 尾维不等于 x 尾维的一半、scaleOut元素数不等于rowLen;新增前导维逐维相等校验(x=[4,128] 配 y=[2,64] 这类前导维被压缩的形态同样会越界);尾维校验的日志改为打印实际 dim 值。quant/dequant_swiglu_quant/op_host/dequant_swiglu_quant_tiling.cpp(Dsk 路径,groupIndex非空时走这里):原代码只从 y 反推outDimy_,没有任何尺寸校验,现补——y rank 与 x 一致、前导维逐维相等、outDimy_ * 2 == inDimy_、scaleOut元素数等于 x 的行数,并对GetOutputShape(1)返回空指针的情况做保护后返回失败。
验证:
- 新增 7 个 UT:Base 4 例(y 尾维减半、y rank 不等、y 前导维减半、scaleOut 尺寸不符),Dsk 3 例(group 场景 y 尾维减半、y 前导维减半、scaleOut 尺寸不符);这 7 例在未打补丁时全部失败、打补丁后全部通过;
- dequant_swiglu_quant 全量 op_host UT 75 例通过。
该 issue 已随修复合入关闭。


现象
aclnnDequantSwigluQuant第一段接口aclnnDequantSwigluQuantGetWorkspaceSize不校验出参yOut/scaleOut的尺寸是否等于推导尺寸。把yOut声明成推导尺寸的一半时,两段接口都返回 0,内核按x推导的真实尺寸写满输出,越过声明张量边界 128 字节(静默越界写,无任何报错)。按资料
op_host/dequant_swiglu_quant_infershape.cpp:yOut.shape = x.shape(最后一维除 2),scaleOut.shape = yOut.shape去掉尾轴。x=[4,128]时yOut应为[4,64](256 元素)。实测(同一份数据、同一 die、连续两次一致):
[4,64](256 元素)[4,32](128 元素)scaleOut同型:声明[1]而真实应为[4]时ret1=0 ret2=0,边界外 3 个 float 被写。对照组越界恒为 0,排除"哨兵未初始化"误判。期望
第一段接口按资料声明的错误码表返回
ACLNN_ERR_PARAM_INVALID(161002)("输入或输出的 shape 不满足约束要求"),或在yOut/scaleOut尺寸小于推导尺寸时直接拒绝,不得进入执行阶段。环境
ascend-toolkit/latest)、npu-smi 26.1.1、Linux aarch64/glibc 2.35ops-nn/quant/dequant_swiglu_quant,源码提交711a4f5d复现步骤
repro.cpp,编译:g++ -std=c++17 -O1 repro.cpp -I$ASCEND/include -L$ASCEND/lib64 -L$ASCEND/aarch64-linux/lib64 -lascendcl -lnnopbase -lopapi -Wl,--allow-shlib-undefined -o repro($ASCEND指 CANN 安装根)。./repro base:出参yOut=[4,64],打印边界外被写字节=0(阴性对照)。./repro half:只把yOut改成[4,32],其余不变。ret1=0 ret2=0 声明边界外被写字节=128:两段都成功,越界写成立。复现脚本
#include <cstdio> #include <unistd.h> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_dequant_swiglu_quant.h" static aclTensor *mk(const std::vector<int64_t> &s, void *a, aclDataType dt) { std::vector<int64_t> st(s.size(), 1); for (int i = (int)s.size() - 2; i >= 0; i--) st[i] = s[i + 1] * st[i + 1]; return aclCreateTensor(s.data(), s.size(), dt, st.data(), 0, aclFormat::ACL_FORMAT_ND, s.data(), s.size(), a); } static int64_t numel(const std::vector<int64_t> &s) { int64_t n = 1; for (auto v : s) n *= v; return n; } int main(int argc, char **argv) { bool half = argc > 1 && argv[1][0] == 'h'; std::vector<int64_t> xs{4, 128}, qs{64}, ss{4}; std::vector<int64_t> ys = half ? std::vector<int64_t>{4, 32} : std::vector<int64_t>{4, 64}; aclInit(nullptr); aclrtSetDevice(0); aclrtStream stream = nullptr; aclrtCreateStream(&stream); const int64_t yAlloc = 4 * 64 * 2; // 缓冲开大 2 倍,全填 0xA5 哨兵 void *xd, *qd, *od, *yd, *sd; aclrtMalloc(&xd, numel(xs) * 2, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&qd, numel(qs) * 4, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&od, numel(qs) * 4, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&yd, yAlloc, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&sd, 64, ACL_MEM_MALLOC_HUGE_FIRST); std::vector<int8_t> sent(yAlloc, (int8_t)0xA5); std::vector<float> qv(64, 0.01f), ov(64, 0.0f); std::vector<aclFloat16> xh(numel(xs)); for (size_t i = 0; i < xh.size(); i++) xh[i] = aclFloatToFloat16(0.05f * (float)((int)i % 32)); aclrtMemcpy(xd, numel(xs) * 2, xh.data(), numel(xs) * 2, ACL_MEMCPY_HOST_TO_DEVICE); aclrtMemcpy(qd, 256, qv.data(), 256, ACL_MEMCPY_HOST_TO_DEVICE); aclrtMemcpy(od, 256, ov.data(), 256, ACL_MEMCPY_HOST_TO_DEVICE); aclrtMemcpy(yd, yAlloc, sent.data(), yAlloc, ACL_MEMCPY_HOST_TO_DEVICE); aclTensor *x = mk(xs, xd, ACL_FLOAT16), *q = mk(qs, qd, ACL_FLOAT), *o = mk(qs, od, ACL_FLOAT); aclTensor *y = mk(ys, yd, ACL_INT8), *s = mk(ss, sd, ACL_FLOAT); uint64_t ws = 0; aclOpExecutor *e = nullptr; char qm[] = "dynamic"; int r1 = (int)aclnnDequantSwigluQuantGetWorkspaceSize(x, nullptr, nullptr, nullptr, q, o, nullptr, true, qm, y, s, &ws, &e); int r2 = 127; if (r1 == 0) { void *w = nullptr; if (ws) aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST); r2 = (int)aclnnDequantSwigluQuant(w, ws, e, stream); if (r2 == 0) aclrtSynchronizeStream(stream); } std::vector<int8_t> got(yAlloc); aclrtMemcpy(got.data(), yAlloc, yd, yAlloc, ACL_MEMCPY_DEVICE_TO_HOST); int64_t decl = numel(ys), over = 0; for (int64_t i = 0; i < yAlloc; i++) if (got[i] != (int8_t)0xA5 && i >= decl) over++; printf("mode=%s yOut声明元素=%ld ret1=%d ret2=%d 边界外被写字节=%ld\n", half ? "half" : "base", (long)decl, r1, r2, (long)over); fflush(stdout); _exit(0); }实测输出:
影响
yOut尺寸申请显存时,内核会写到缓冲之外,破坏相邻张量且无报错。推理框架里出参常按x.numel()/2自算,一旦与内核推导不一致就是静默越界写。建议
dequant_swiglu_quant的第一段接口(或InferShape之后的 check 环节)增加出参校验:yOut.shape == x.shape(末维H/2)、scaleOut.shape == yOut.shape[:-1],不满足返回161002。scaleOut)一并纳入校验,避免只校验yOut时scaleOut仍可小尺寸越界。x=[4,128]+yOut=[4,32]期望161002;x=[4,128]+scaleOut=[1]期望161002。