Closed
[Bug-Report|缺陷反馈]: aclnnDivMod INT16 + mode=2 除数含 0 时两段返回 0 但同步抛 507018 FloorDiv aicpu exception #3934
liuhanwen8Add on  23 days agoClosed at  1 day ago
liuhanwen8
liuhanwen8
23 days ago Create

现象

aclnnDivMod 在 INT16 + mode=2(floor)+ 除数含 0 时,两段接口均返回 0,
但 aclrtSynchronizeStream 返回 507018(aicpu exception),输出未写。

同一份含 0 输入在 mode=0 / mode=1 下正常返回,说明输入本身不是非法入参。

组 dtype mode 除数含 0 预期 实测
对照 CTRL-1 INT16 2 否 成功 ret=0 out[0]=4
对照 CTRL-2 INT16 2 否(b=1) 成功 ret=0 out[0]=8
对照 CTRL-3 INT16 0 是 成功 ret=0 out[0]=-1
对照 CTRL-4 INT16 1 是 成功 ret=0 out[0]=-1
缺陷组 INT16 2 是 成功 phase1=0 phase2=0 sync=507018
对照 CTRL-5 INT16 2 否 成功 ret=0 out[0]=4

CTRL-5 在缺陷组之后执行仍成功,说明 device/stream 未被永久污染。

plog 根因行(三行):davinci_kernel_task.cc:767 AI CPU kernel execution failed,
soName=libtf_kernels.so funcName=TFOperateAPI kernelName=FloorDiv;
api_c_stream.cc:172 rtStreamSynchronize:ErrCode=507018, desc=[aicpu exception], InnerCode=0x715002a。

期望

除数为 0 在本接口中已有明确定义,资料与本仓实现一致:

  • math/div/docs/aclnnDivMod&aclnnInplaceDivMod.md 错误码表共 7 条 161002 子项
    (空指针 / dtype / 推导 / broadcast / 维度 / mode 越界 / complex),不含「除数为 0」;
  • 实测 mode=0(CTRL-3)与 mode=1(CTRL-4)对同一份含 0 输入均返回 0 并给出结果;
  • 本仓 math/div/op_api/aclnn_div.cpp:883-886 的注释也承认该路径的边界:
    // aicore is not supported, aicpu has problems when div 0。

因此 INT16 + mode=2 + 除数为 0 应与 mode=0 / mode=1 行为一致(返回结果),
或若确实不支持则应在第一段返回 161002,而不是第二段提交一个必抛异常的 kernel。

环境

Atlas A3(Ascend910_93)单 die;CANN 9.2.0(Ascend-cann-A3-ops_9.2.0);
driver npu-smi 26.1.1 / 固件 7.8.0.6.201;源码基线 cann/ops-math master 584b2ea6
(已 git fetch,local 与 origin 同 sha)。

复现步骤

  1. source /usr/local/Ascend/ascend-toolkit/set_env.sh
  2. 保存下方 repro.cpp
  3. g++ -std=c++14 -O2 -I$ASCEND_HOME_PATH/include -I$ASCEND_HOME_PATH/include/aclnnop repro.cpp -o repro -L$ASCEND_HOME_PATH/lib64 -lascendcl -lopapi -lnnopbase -lpthread
  4. ./repro
  5. 第 5 行 缺陷组 DEFECT int16 m2 b=0 输出 phase2=0 sync=507018,其余 5 行 对照组 均 OK

复现脚本

#include <acl/acl.h>
#include <aclnnop/aclnn_div.h>
#include <cstdio>
#include <vector>
#define CK(x, m) do { int _r = (x); if (_r != ACL_SUCCESS) { \
    printf("  [ERR] %s ret=%d\n", m, _r); return _r; } } while (0)
class Buf {
 public:
  void* p = nullptr; aclTensor* t = nullptr;
  ~Buf() { if (t) aclDestroyTensor(t); if (p) aclrtFree(p); }
};
static int MakeI16(Buf& b, const std::vector<int64_t>& sh, const std::vector<int16_t>& h) {
  size_t by = h.size() * sizeof(int16_t);
  CK(aclrtMalloc(&b.p, by, ACL_MEM_MALLOC_HUGE_FIRST), "malloc");
  CK(aclrtMemcpy(b.p, by, h.data(), by, ACL_MEMCPY_HOST_TO_DEVICE), "h2d");
  std::vector<int64_t> st(sh.size(), 1);
  for (int i = (int)sh.size() - 2; i >= 0; --i) st[i] = sh[i + 1] * st[i + 1];
  b.t = aclCreateTensor(sh.data(), sh.size(), ACL_INT16, st.data(), 0, ACL_FORMAT_ND,
                        sh.data(), sh.size(), b.p);
  return b.t ? 0 : -1; }
// a 全员恒定 8,只有 b[0] 可控 → 单变量隔离「b 是否含 0」
static int One(aclrtStream s, const char* tag, int mode, int16_t b0) {
  std::vector<int64_t> sh = {4, 8};
  std::vector<int16_t> a(32, 8), b(32, 2), o(32, 0);
  b[0] = b0;
  Buf A, B, O;
  if (MakeI16(A, sh, a) || MakeI16(B, sh, b) || MakeI16(O, sh, o)) {
    printf("  [%s] SETUP_FAIL\n", tag); return -1; }
  uint64_t ws = 0; aclOpExecutor* ex = nullptr;
  int r1 = (int)aclnnDivModGetWorkspaceSize(A.t, B.t, mode, O.t, &ws, &ex);
  if (r1 != 0) { printf("  [%s] phase1=%d\n", tag, r1); return r1; }
  void* wsp = nullptr; if (ws > 0) aclrtMalloc(&wsp, ws, ACL_MEM_MALLOC_HUGE_FIRST);
  int r2 = (int)aclnnDivMod(wsp, ws, ex, s);
  int r3 = (int)aclrtSynchronizeStream(s);
  if (wsp) aclrtFree(wsp);
  if (r2 || r3) { printf("  [%s] FAIL phase2=%d sync=%d\n", tag, r2, r3); return r2 ? r2 : r3; }
  int16_t v = 0; aclrtMemcpy(&v, 2, O.p, 2, ACL_MEMCPY_DEVICE_TO_HOST);
  printf("  [%s] OK   out[0]=%d\n", tag, (int)v);
  return 0; }
int main() {
  CK(aclInit(nullptr), "aclInit"); CK(aclrtSetDevice(0), "setDevice");
  aclrtStream s = nullptr; CK(aclrtCreateStream(&s), "createStream");
  One(s, "对照组 CTRL-1 int16 m2 b!=0", 2, 2);
  One(s, "对照组 CTRL-2 int16 m2 b=1", 2, 1);
  One(s, "对照组 CTRL-3 int16 m0 b=0", 0, 0);
  One(s, "对照组 CTRL-4 int16 m1 b=0", 1, 0);
  One(s, "缺陷组 DEFECT int16 m2 b=0", 2, 0);
  One(s, "对照组 CTRL-5 int16 m2 b!=0", 2, 2);
  aclrtDestroyStream(s); aclrtResetDevice(0); aclFinalize(); return 0; }

实测输出:
CTRL-1 OK 4 / CTRL-2 OK 8 / CTRL-3 OK -1 / CTRL-4 OK -1 /
DEFECT FAIL phase2=0 sync=507018 / CTRL-5 OK 4。

影响

目标检测/推荐场景中整型除法常以 INT16 存储缩放因子,除数列出现 0 是常见脏数据。
此时 mode=2 路径直接抛 507018,调用方拿到异常而非结果;而同一份数据换 mode=0/1 可用,
说明不是数据本身非法,而是该模式分支对除零无处理。

建议

INT16 + mode=2 走的是「提升到 INT32 再 floor + cast 回 INT16」路径
(见 aclnn_div.cpp 的 needToInt32 = (promoteType == DT_INT16 && mode == FLOOR_DIV))。
建议在该路径补除零处理,使其与 mode=0/1 行为一致;若确定不支持除零,则在第一段
返回 161002 并更新错误码表。同时补 INT16 + mode=2 + 除数为 0 的 UT。

likedislike
liuhanwen8liuhanwen8
23 days ago Added label: bug-report
陈思
陈思Collaborators
23 days ago Comments:

您好,感谢反馈!已按报告提交 584b2ea6 核对,INT16 floor 的分派路径与所附 FloorDiv AICPU 日志相符,但报告中关于 INT32 提升的定位需要调整。

问题分析

A3 的当前路径走 非 RegBase 兼容类型推导,mode=2 保留 INT16;FloorDiv 的 AICore 类型列表不含 INT16,因此进入 AICPU-tf 实现。报告引用的 needToInt32 及“aicpu has problems when div 0”注释位于 RegBase 分支,不能据此认定本次 A3 已经执行 INT16→INT32→floor→INT16。mode=0/1 的兼容推导则会将该 INT16 组合转为 FLOAT,计算路径不同,可以解释为何两类对照不能直接代表 floor 分支。

两段接口返回成功与随后同步报错并不矛盾:首段生成执行计划、执行段提交任务,设备异常可在同步时才暴露。当前已核对到分派与类型路径,尚未读取所用 libtf_kernels.so 的内核实现或新增硬件复测,故 507018 的内核内部触发点仍需确认。

处理方向

资料的错误表确实未列除数为零,但也没有定义整型 floor 除零必须返回何值;其他模式实测为 -1,不足以确定所有模式应采用相同结果。应先明确 INT16 floor 的除零语义,再评估设备侧处理或可行的计算路径调整,避免仅搬用 INT32 提升分支就宣称问题解决。若选择拒绝非法设备元素,也需明确异步错误上报方式:other 是 Device Tensor,不能靠普通 Host 参数检查直接读取其元素并保证首段返回 161002。

验证需保留本单的模式和非零对照,并覆盖正负被除数、零除数、INT16 极值、负数 floor 舍入、广播及异常后的后续调用。您提供的 CTRL-5 表明该次运行的后续非零调用成功,不应据此扩大为所有异常场景的恢复保证。后续将继续核对 AICPU 实现的除零行为与接口语义,跟踪可控处理方案。

likedislike
陈思陈思Collaborators
23 days ago Assign liu-wei as responsible person
陈思陈思Collaborators
23 days ago Added label: pending-confirmation
陈思陈思Collaborators
23 days ago Removed label: pending-confirmation
陈思陈思Collaborators
12 days ago Assign FelixTang7 as responsible person
陈思陈思Collaborators
12 days ago Removed responsible person liu-wei
CchenyifengCollaborators
7 days ago Removed responsible person FelixTang7
CchenyifengCollaborators
7 days ago Assign sakuraqqz as responsible person
CANN-robotCANN-robotCollaborators
1 day ago Close issue
CANN-robotCANN-robotCollaborators
1 day ago changed custom state from 进行中 to 已完成
CANN-robotCANN-robotCollaborators
1 day ago Added label: resolved