Closed
[Bug-Report|缺陷反馈]: 仿真 AOT 控制流回调中 DEV_ASSERT 异常无法穿越 AOT 帧,进程 terminate 而非 Python 异常 #3580
anyangleAdd on  Oct 14Closed at  24 days ago
anyangle
anyangleCollaborators
Oct 14 Create

Describe the current behavior / 问题描述 (Mandatory / 必填)

仿真(host Machine + AOT control flow)跑用例时,业务侧出现 workspace / stitch 类失败后,进程不是抛出 Python 异常,而是:

  1. 终端裸打屏:ErrCode: F7…! Enum: … + Caught exception: '…'
  2. 进程 _Exit(1) 直接退出
  3. 无 Python traceback,未进入 pybind 异常通道

典型关联:AssignIncastAddresses 等路径上的 host DEV_ASSERT,以及更早的 DEV_ERROR(如 F73001 / CTRL_FLOW_EXEC_FAILED)。

根因(一层层)

1. 业务失败点(仿真控制流 / machine runtime)

CallRootFunctionStitch 做 workspace preflight 时,空 DeviceTask 塞不下 root,reason=runtime-outcast-metadata → DEV_ERROR(F73001)。
随后 AssignIncastAddresses 再触发 DEV_ASSERT → MACHINE_ASSERT → C++ throw Error。

(同类问题不限于这一条:凡在 AOT→host 回调里 DEV_ASSERT/MACHINE_ASSERT 抛出的,都会落到同一终止路径,例如 SlotMarkNeedAlloc、ShmemAllocator 等。)

2. 为什么不是「正常 Python 异常」

仿真跑控制流时走的是:

CallControlFlow
  → 跳进 AOT 机器码(aotCodePoolCodes)
  → 再回调 C++(DeviceExecuteRuntimeCallRootStitch / Alloc / MarkSlot / Shmem …)
  → 这里 throw

C++ 异常要穿过 AOT 那些栈帧才能回到 pybind、变成 Python 异常;AOT 裸代码没有可用的异常展开信息(无 EH unwind)→ 穿不过 → std::terminate。

对比:非 AOT、纯 C++ 栈上的同款 DEV_ASSERT,可以正常 unwind 到 pybind → RuntimeError。

3. 为什么是屏上这种裸格式

terminate 处理器(error.cpp / TerminateHandler)故意:

  • 写 plog(Caught exception: ...)
  • ErrorManager::OutputErrorMessage() → std::cerr 打队列里的 ErrCode: ...
  • 再 cerr << "Caught exception: '...'"
  • _Exit(1)

所以既进了 log,又打屏;且从未进入 Python 异常通道。

Environment / 环境信息 (Mandatory / 必填)

  • 运行模式:仿真 / eager(host Machine + AOT control flow)
  • 组件:PyPTO Machine runtime(device_execute_context / AOT code pool)
  • 与真机 AICPU 路径无关(device 侧 DEV_ASSERT 为 abort(),机制不同)

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

  1. 在仿真侧运行会触发 stitch/workspace 失败并走到 host DEV_ASSERT 的用例(例如 AssignIncastAddresses 空 fromSlotList,或 cell_match op_combo 等会进入 stitch / RUNTIME_SlotMarkNeedAlloc 的 case)。
  2. 观察进程退出方式与终端输出。

也可临时在 AOT 回调入口(如 MarkSlotNeedAlloc)强制 DEV_ASSERT 验证同一终止路径。

Describe the expected behavior / 预期结果 (Mandatory / 必填)

  1. AOT→host 回调中的 DEV_ASSERT throw 不应导致 std::terminate / _Exit。
  2. 应在返回 AOT 帧之前接住异常,置 SetErrorState,CF 正常返回后由既有错误态路径上报。
  3. 理想情况进一步转化为 Python 异常(可分阶段:先消灭 terminate,再补 leave 后 rethrow)。

修复方案(方案一 · 表级 trampoline)执行逻辑

在 RunControlFlow 组装 runtimeCallList 时(仅 host / #if !defined(__DEVICE__)),不直接挂裸的 DeviceExecuteRuntimeCall*,而是挂统一 Guard。

改前:

AOT
  → DeviceExecuteRuntimeCallRootAlloc / Stitch / MarkSlot / …
  → … → DEV_ASSERT → throw
  → 穿回 AOT(无 EH)→ std::terminate → Caught exception + _Exit

改后:

AOT
  → AotGuardedRuntimeCall<kRaw, kStage>     // 表里挂的是 Guard,不是裸实现
       → kRaw = DeviceExecuteRuntimeCall*   // 原业务实现
       → … → DEV_ASSERT → throw
       ↑ catch 在这里(仍在 host C++ 栈,尚未返回 AOT)
       → SetErrorState(DEVICE_MACHINE_ERROR)
       → DEV_ERROR("#ctrl.ctrlflow.<site>: caught host Error …")
       → 返回哨兵(Alloc/Stitch → RUNTIME_FUNCKEY_ERROR;多数其它 → nullptr;GetDieId 保持合法指针)
  → 正常 return 给 AOT
CallControlFlow 返回后
  → 原有 GetErrorState() 检查 → return finalErrorState

包装拆开是三层(以 Alloc 为例):

  1. DeviceExecuteRuntimeCallRootAlloc(kRaw):原来的真实现(业务逻辑),模板参数编译期固定。
  2. AotGuardedRuntimeCall<kRaw, kStage>:try { return kRaw(ctx, arg); } catch …;kStage 用于日志名与失败哨兵选择。
  3. &AotGuardedRuntimeCall<…>:取包装函数地址放进 runtimeCallList;AOT 调到的是 Guard,不是裸 Alloc。

改动文件:framework/src/machine/device/dynamic/context/device_execute_context.cpp(device 侧仍用原表)。

典型 terminate 打屏(修复前):

ErrCode: F7A006! Enum: DevCommonErr::PARAM_INVALID. false :TEMP force MarkSlotNeedAlloc assert, slotIndex=0 (verify AOT catch).
Caught exception: 'ErrCode: F7A006! Enum: DevCommonErr::PARAM_INVALID. false :TEMP force MarkSlotNeedAlloc assert, slotIndex=0 (verify AOT catch).'

业务失败关联日志中亦可见 F73001 / CTRL_FLOW_EXEC_FAILED 与 AssignIncastAddresses assert 行号叠加。

修复后期望日志形态示例:

#ctrl.ctrlflow.call.slot_mark_need_alloc: caught host Error, arg=..., what=...
#ctrl.ctrlflow.leave: Control flow execution failed with error code: ...

而不再出现 terminate 的 Caught exception + _Exit。

Special notes for this issue/备注 (Optional / 选填)

  • Host/仿真必须 Guard;Device(__DEVICE__)DEV_ASSERT 为 abort(),与本问题不同,保持原 runtimeCallList 即可。
  • 本修复优先消灭 terminate;若要稳定变成 Python RuntimeError,需在 CallControlFlow 返回后(普通 C++ 栈)再 throw / 上层转换,可作 follow-up。
  • 已知必须护住的入口:Alloc、Stitch、Shmem、MarkSlot;Log/GetDieId/SetDieId 可一并 Guard 或保持原函数。
likedislike
anyangleanyangleCollaborators
Oct 14 Added label: bug-report
anyangleanyangleCollaborators
Oct 14 Modify description of issue
anyangleanyangleCollaborators
Oct 15 changed custom category from 任务 to 缺陷
anyangleanyangleCollaborators
Oct 15 Assign quadrate as responsible person
anyangleanyangleCollaborators
Oct 15 Associated pull request
anyangle
anyangleCollaborators
Oct 26 Comments:

commit 2f800ae814bcd43343c916565722014d14ff3d37 (HEAD -> error_log)
Author: anyangle anyangle@huawei.com
Date: Wed Oct 14 17:59:54 2026 +0800

fix(machine): guard AOT runtime callbacks to catch host DEV_ASSERT before terminate

commit fda2a21e82292dec80a8c2544a3336972471ea7c
Author: zhaojunhan zhaojunhan1@huawei.com
Date: Wed Oct 14 14:32:05 2026 +0800

feat(ir): Sink a for loop after if into both branches

Co-authored-by: zhaojunhan<zhaojunhan1@huawei.com>



# message auto-generated for no-merge-commit merge:
!6872 merge sink_for_into_if into master

feat(ir): Sink a for loop after if into both branches

Created-by: zhaojunhan
Commit-by: zhaojunhan
Merged-by: cann-robot
Description: feat(ir): Sink a for loop after if into both branches
likedislike
anyangle
anyangleCollaborators
Oct 31 Comments:
anyangleanyangleCollaborators
24 days ago changed custom state from 待办的 to 已解决
anyangleanyangleCollaborators
24 days ago Close issue
CANN-robotCANN-robotCollaborators
24 days ago Added label: resolved