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
anyangleAdd on Aug 4
anyangle
anyangleCollaborators
Aug 4
  1. 多线程 Codegen 确定性 Kernel 编译
    Related Issue:https://gitcode.com/cann/pypto/issues/2913

  2. 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异常
    修改前:
    image.png
    修改后:
    image.png

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for anyangle's contribution.)
anyangleanyangleCollaborators
Aug 4 Created pull request, commit 144c993a
atomgit-bot
atomgit-bot
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() 将首个异常重新抛出,恢复异常的传播链。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 4 Comments:

代码审查

审查结束

对 framework/src/codegen/utils/parallel_execute.cpp 的变更进行了全面审查。

审查摘要

  • 发现问题数:1
    • P2:1(compare_exchange_strong 的 memory_order_acq_rel 单参数用法违反 C++ 标准)
  • 整体风险评估:低风险。该变更正确实现了异常传播的核心目标——将原本被吞掉的并行任务异常捕获、记录并通过 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

💬 仅评论

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 4 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
207 messages are collapsed here。 See more
gaoxingwang
gaoxingwangCollaborators
Aug 18 Comments:

/lgtm

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