Merged
fix(codegen): Propagate parallel task exceptions instead of swallowing them #5266
anyangleAdd on Aug 4
fix(codegen): Propagate parallel task exceptions instead of swallowing them #5266
Merged
Merged fromquadrate/pypto_aylcompile_fixintocann/pyptomaster
Pull Request has been successfully merged by CANN-robot.
(Thank you for anyangle's contribution.)atomgit-bot
Aug 4 Comments:
Aug 4 Comments:
变更摘要
此 PR 修复了 codegen 模块中并行任务执行时异常被静默吞掉的问题。原实现中 TaskRunner 仅通过日志记录异常但不向上传播,导致调用方无法感知编译失败。改动引入了 TaskErrorState 类来跨线程传递异常状态,并在 ParallelExecuteAndWait 中任务完成后统一重新抛出首个捕获的异常,使上层能正确感知并行任务中的编译错误。
主要改动
- 新增
TaskErrorState类:使用std::atomic<bool>和std::exception_ptr实现线程安全的异常状态管理,通过compare_exchange_strong确保仅首个异常被记录,提供HasError()、SetError()和RethrowIfError()接口。 - 新增
RunTask函数:从原TaskRunner中提取单个任务的执行逻辑,捕获std::exception和未知异常后调用errState.SetError()记录异常,替代原先仅打日志便丢弃异常的做法。 - 修改
TaskRunner循环逻辑:增加TaskErrorState&参数,循环条件从while(true)改为while(!errState.HasError()),使工作线程在检测到任一任务出错后提前退出,避免无效执行。 - 修改
ParallelExecuteAndWait:单线程和多线程路径均引入TaskErrorState实例,任务执行完毕后调用errState.RethrowIfError()将首个异常重新抛出,恢复异常的传播链。


Incorrect?
atomgit-bot
Aug 4 Comments:
Aug 4 Comments:
代码审查
审查结束
对 framework/src/codegen/utils/parallel_execute.cpp 的变更进行了全面审查。
审查摘要
- 发现问题数:1
- P2:1(
compare_exchange_strong的memory_order_acq_rel单参数用法违反 C++ 标准)
- P2:1(
- 整体风险评估:低风险。该变更正确实现了异常传播的核心目标——将原本被吞掉的并行任务异常捕获、记录并通过
RethrowIfError()重新抛出。TaskErrorState的线程安全设计(CAS 保证首个异常唯一写入 + mutex 保护firstException的读写同步)在给定的调用上下文(worker 线程全部 join 后才调用RethrowIfError)下是正确的。唯一需要修复的是SetError中compare_exchange_strong的 memory order 参数写法,当前在 C++17 下已 deprecated,升级到 C++20 将变为 UB。
已审查文件
framework/src/codegen/utils/parallel_execute.cpp:发现 1 个问题(见上文)
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


Incorrect?
Aug 4 Added label: cann-cla/yes
CANN-robot
Aug 4 Comments:
Aug 4 Comments:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
For more, you also can visit HICANN.
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| docs | ✅ 尹杰, gaoxingwang (2/2) | ✅ 尹杰 (1/1) |
| repo-cann/pypto | ✅ 尹杰, gaoxingwang (2/2) | ✅ 尹杰 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
quadrate, thanks for your pull request. All authors of the commits have signed the CLA. 👍


207 messages are collapsed here。 See more
gaoxingwang
Aug 18 Comments:
Aug 18 Comments:
/lgtm


Aug 18 Added label: lgtm
Aug 18 Merged pull request
Aug 18 Deleted associated issue: [Requirement|需求建议]: 多线程 Codegen 确定性 Kernel 编号设计
Aug 18 Associated issue: [Bug-Report|缺陷反馈]: ParallelExecuteAndWait 没有正确做task错误处理,导致codegen编译适配未及时退出,产生后续二次异常
Aug 18 Associated issue: [Bug-Report|缺陷反馈]: ParallelExecuteAndWait 没有正确做task错误处理,导致codegen编译适配未及时退出,产生后续二次异常
多线程 Codegen 确定性 Kernel 编译
Related Issue:https://gitcode.com/cann/pypto/issues/2913
Issue


背景:GenCode 内的 ExecuteParallelCompile 编译失败抛出 COMPILE_CODE_FAILED 被吞掉,leafDict[hash] = leaf 未执行,leafDict不完整。下游 ConstructCodeInfo → RunCompileAicoreKernelStage → RunEncodeStage 继续访问不完整的 leafDict,触发 std::unordered_map::at() 越界,抛出 F00003 RuntimeError: _Map_base::at 二次异常,掩盖了真实的编译错误
根因:codeGen在并行编译遇到错误抛出异常时,只记录日志,并不传播异常和终止剩余task,导致报错在第二现场F00003 RuntimeError: _Map_base::at处
解决方法:修改 ParallelExecuteAndWait,使其在 task 失败时记录错误、提前终止剩余 task、并在所有线程 join 后重新抛出首个 COMPILE_CODE_FAILED异常
修改前:
修改后: