Closed
[Bug-Report|缺陷反馈]: aclnnBernoulliTensor 未校验 offset%4 与 prob∈[0,1]:非法输入两段返回 0,prob 越界触发 507018 #3924
liuhanwen8Add on 27 days agoClosed at 3 days ago
27 days ago Added label: bug-report
liuhanwen8
27 days ago Comments:
27 days ago Comments:
四机复核(2026-10-25):同一复现脚本在 4 台机器 / 3 种芯片上输出逐行一致,排除单机或 CANN 安装差异。
| 机器 | 芯片 | CANN |
|---|---|---|
| 179.162.4.2 | Ascend 910B2 | 9.2.0 |
| 179.166.3.2 | Ascend 910B2 | 9.2.0 |
| 179.167.3.3 | Ascend 910B3 | 9.2.0 |
| 199.211.3.2 | Ascend 910 | 9.2.0 |
复现输出(bernoulli):control -> 0/0/0;offset=2 -> 0/0/0(资料要求调用失败);prob=-0.1 -> 0/0/507018。


陈思
26 days ago Comments:
26 days ago Comments:
已按报告提交 584b2ea6 核对,并结合补充的 910B2、910B3 和 910 测试结果分析。两个问题需要分别处理。
问题分析
offset:资料明确要求为 4 的倍数,但 Tensor 入口 的参数检查没有接收或检查offset,后续直接将其交给StatelessBernoulli。因此,offset=2未在首段拒绝与当前代码一致。原地接口直接转发到同一入口,也受影响。另外,该提交的同文件标量入口也没有CheckOffset定义或调用,修复时需要新增或复用经核实的检查,不能直接补调现有函数。prob:资料规定它是 Device 侧张量,元素值须在[0,1];当前CheckParamsTensor仅检查空指针、类型、形状和格式,没有读取元素做值域检查。A2 和 910 的该 Tensor 路径实际进入 AICPU StatelessBernoulli,与报告中的同步阶段异常现象相符;目前的源码核对尚不能确认507018在设备内核内的具体触发位置,也没有新增硬件复测。
处理方向
offset 是 Host 整数,适合在创建执行计划、处理空张量之前补齐对齐检查,并明确非法值对应 161002,同时覆盖原地/非原地入口及合法偏移的随机序列回归。
prob 的值域检查需要结合设备数据与异步接口设计:Host 不能直接解引用 Device 数据;在设备内核中增加断言,也不能使此前已返回成功的 GetWorkspaceSize 改为返回 161002。应评估设备端安全检查与可控的错误上报方式,核对执行、同步阶段及异常后续调用行为,并同步明确资料中的报错阶段和错误码。目前资料的 161002 表只列类型、格式、维度与形状错误,尚未明确承诺元素越界在首段返回该码。验证应包括负概率、超过 1、边界 0/1、合法对照及两个接口,避免仅让异常延后或改变合法随机序列。


26 days ago Assign liu-wei as responsible person
26 days ago Added label: pending-confirmation
26 days ago Removed label: pending-confirmation
16 days ago Removed responsible person liu-wei
16 days ago Assign chen_0715 as responsible person
16 days ago Added label: pending-confirmation
15 days ago Assign zhuzemao as responsible person
15 days ago Removed responsible person chen_0715
3 days ago Close issue
3 days ago changed custom state from 进行中 to 已完成
3 days ago Added label: resolved
现象
aclnnBernoulliTensor对资料明确约束的两个入参都没有校验,非法输入在两段式接口上均返回 0:prob=0.5, offset=0prob=0.5, offset=2offset%4==0、否则调用失败prob=-0.1, offset=0aclnnInplaceBernoulliTensor直接转发到同一实现(random/dsa_gen_bit_mask/op_host/op_api/aclnn_bernoulli.cpp:425),现象一致。期望
资料
random/stateless_bernoulli/docs/aclnnBernoulliTensor&aclnnInplaceBernoulliTensor.md:offset使用说明原文:「取值约束:offset % 4 == 0,例如可以取0、4、8 ...,不满足约束会调用失败」;因此
offset=2与prob=-0.1都应在第一段返回161002(ACLNN_ERR_PARAM_INVALID),不应进入执行段,更不应以507018(AICPU exception)暴露。环境
npu-smi26.1.1/usr/local/Ascend与/home/<user>/cann),Linux aarch64,g++ 11.4.0cann/ops-mathmaster584b2ea6cbaeba684a51a74296d82d4d50ab3b0f(git fetch origin master后核对;random/dsa_gen_bit_mask/op_host/op_api/aclnn_bernoulli.cpp:298的 Tensor 入口只调CheckParamsTensor,未调CheckOffset)复现步骤
repro.cpp。source $ASCEND_HOME_PATH/set_env.sh(或等价 CANN 9.2.0 环境)。g++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lopapi -lnnopbase./repro 0(0 为 device id)。161002(实测 0/0/507018)。复现脚本
#include <cstdio> #include <cstdlib> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_bernoulli.h" static aclTensor* Mk(const std::vector<int64_t>& shape, const std::vector<float>& host, void** dev, std::vector<int64_t>& strides) { strides.assign(shape.size(), 1); for (int i = (int)shape.size() - 2; i >= 0; --i) { strides[i] = shape[i + 1] * strides[i + 1]; } size_t bytes = host.size() * sizeof(float); aclrtMalloc(dev, bytes, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMemcpy(*dev, bytes, host.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE); return aclCreateTensor(shape.data(), shape.size(), ACL_FLOAT, strides.data(), 0, ACL_FORMAT_ND, shape.data(), shape.size(), *dev); } static void Run(aclrtStream stream, const char* tag, float prob0, int64_t offset) { std::vector<int64_t> shape = {1024}; std::vector<int64_t> st1, st2, st3; std::vector<float> self(1024, 0.0f), prob(1024, 0.5f), out(1024, 0.0f); prob[0] = prob0; void *ds = nullptr, *dp = nullptr, *do_ = nullptr; aclTensor* ts = Mk(shape, self, &ds, st1); aclTensor* tp = Mk(shape, prob, &dp, st2); aclTensor* to = Mk(shape, out, &do_, st3); uint64_t ws = 0; aclOpExecutor* ex = nullptr; int r1 = aclnnBernoulliTensorGetWorkspaceSize(ts, tp, 1, offset, to, &ws, &ex); int r2 = -1, r3 = -1; if (r1 == 0) { void* wsa = nullptr; if (ws > 0) { aclrtMalloc(&wsa, ws, ACL_MEM_MALLOC_HUGE_FIRST); } r2 = aclnnBernoulliTensor(wsa, ws, ex, stream); r3 = aclrtSynchronizeStream(stream); if (wsa) { aclrtFree(wsa); } } printf("%s: GetWorkspaceSize=%d Execute=%d Sync=%d\n", tag, r1, r2, r3); aclDestroyTensor(ts); aclDestroyTensor(tp); aclDestroyTensor(to); aclrtFree(ds); aclrtFree(dp); aclrtFree(do_); } int main(int argc, char** argv) { int32_t devId = argc > 1 ? atoi(argv[1]) : 0; aclrtStream stream = nullptr; aclInit(nullptr); aclrtSetDevice(devId); aclrtCreateStream(&stream); Run(stream, "control(prob=0.5,offset=0)", 0.5f, 0); Run(stream, "offset=2(not %4) ", 0.5f, 2); Run(stream, "prob=-0.1 ", -0.1f, 0); aclrtDestroyStream(stream); aclrtResetDevice(devId); aclFinalize(); return 0; }实测输出(两台机器一致):
影响
offset非 4 倍数时接口静默接受。资料声明「不满足约束会调用失败」,调用方无法发现自己传错偏移量,随机序列与预期不对齐;prob越界(负数或 >1)时首段与执行段都返回 0,错误只在aclrtSynchronizeStream以507018(AICPU exception)暴露,且同一进程后续调用可观察到异常/不稳定。非法概率直接把内核打异常,属于可被上层输入触发的稳定性问题;aclnnBernoulliTensor/aclnnInplaceBernoulliTensor)共用实现,影响面覆盖原地与非原地。建议
random/dsa_gen_bit_mask/op_host/op_api/aclnn_bernoulli.cpp:aclnnBernoulliTensorGetWorkspaceSize(约 298–312 行)在CheckParamsTensor之后补CHECK_RET(CheckOffset(offset), ACLNN_ERR_PARAM_INVALID);(同目录标量版已有CheckOffset,Tensor 版漏调);CheckParamsTensor(约 212–230 行)增加 prob 张量元素值域校验:CPU 侧可直接读 host 数据;device 侧建议在 kernel 入口加0<=prob<=1断言并返回 161002,而不是让 AICPU 抛 507018;offset%4与prob∈[0,1]各补一条 UT(正/负各一),防止再次漏调。