Merged
feat(fallback 2stages) #740
esok11Add on Aug 14
feat(fallback 2stages) #740
Merged
esok11Add on Aug 14
esok11
esok11Collaborators
Aug 14

描述

本 PR 为 op fallback 执行路径新增两阶段(2-stage)执行机制,将原有的单阶段 EXEC_OPAPI_CMD(Prepare + Launch 同步串行)拆分为独立的 Prepare 阶段与 Launch 阶段,使执行图能够在 Prepare 阶段完成算子执行器与 workspace 资源的准备,并在后续合适时机再触发 Launch,为调度优化提供基础。

改动内容

include/op_common/op_graph/op_fallback.h

  • 新增宏 CANN_OPS_OPB_ASYN_EXEC_ACLNN:仅完成 Prepare 阶段工作——获取 ResetCacheThreadLocal、{aclnn_api}GetWorkspaceSize、{aclnn_api} 三个函数地址;创建 OpApiParams 对象封装 executor、op_api_func 与转换后的参数列表;将 OpApiParams 通过 SetOpApiParamsWithDefaultDeleter 托管给 host_api_ctx 管理生命周期;调用 GetWorkspaceSize 获取 workspace 大小并通过 SetWorkspaceSizes 设置到上下文,供后续 Launch 阶段使用。

include/op_common/op_graph/op_fallback_internal.h

  • 新增 aclIntArray、aclFloatArray、aclBoolArray 的创建与销毁函数指针类型别名(_aclCreateIntArray/_aclDestroyIntArray 等),补齐原有仅覆盖 aclTensor/aclScalar/aclTensorList 的类型支持。
  • 新增两阶段执行所需的内部类型与接口:
    • OpApiAnyValue(带析构器的通用参数容器)与 OpApiAnyValueDeleter
    • OpApiFunc2Stage(两阶段 Launch 函数指针类型)
    • OpApiParams(封装 converted_params、executor、op_api_func,作为 Prepare 阶段产出传递给 Launch 阶段)
  • 新增 Collect 重载集与 CollectConvertedTypes 模板:为 aclTensor/aclScalar/aclIntArray/aclBoolArray/aclTensorList 等类型分别绑定对应的析构器并收集到 OpApiParams.converted_params,确保两阶段分离后参数资源在 Launch 完成后仍能正确释放。
  • 新增 op_execute_prepare_context.h、op_execute_launch_context.h 头文件依赖。

关联的Issue

  • #299

测试

文档更新

本 PR 未包含文档更新。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for esok11's contribution.)
esok11esok11Collaborators
Aug 14 Created pull request, commit e654813f
atomgit-bot
atomgit-bot
Aug 14 Comments:

变更摘要

本 PR 为 op fallback 执行路径新增两阶段(2-stage)执行机制,将原单阶段同步执行拆分为独立的 Prepare 与 Launch 阶段。核心改动集中在 include/op_common/op_graph/op_fallback.h 与 op_fallback_internal.h:新增 CANN_OPS_OPB_ASYN_EXEC_ACLNN 宏用于在 Prepare 阶段完成函数地址获取、OpApiParams 构造与托管、workspace 大小计算及写入上下文;同时补充两阶段执行所需的参数容器类型、函数指针类型以及带析构器的参数收集逻辑,确保 Prepare/Launch 分离后参数资源仍能正确释放。

主要改动

  • 新增 CANN_OPS_OPB_ASYN_EXEC_ACLNN 宏:该宏仅完成 Prepare 阶段工作,获取 ResetCacheThreadLocal、{aclnn_api}GetWorkspaceSize、{aclnn_api} 三个函数地址,构造 OpApiParams 并通过 SetOpApiParamsWithDefaultDeleter 托管给 ctx,随后调用 GetWorkspaceSize 并将结果经 SetWorkspaceSizes 写入上下文。
  • 新增两阶段内部类型定义:在 op_fallback_internal.h 中引入 OpApiAnyValue、OpApiAnyValueDeleter、OpApiFunc2Stage 和 OpApiParams,其中 OpApiParams 封装 converted_params、executor 与 op_api_func,作为 Prepare 阶段产出传递给后续 Launch 阶段。
  • 新增参数收集与析构逻辑:新增 Collect 重载集与 CollectConvertedTypes/CallCollect 模板,为 aclTensor、aclScalar、aclIntArray、aclBoolArray、aclTensorList 等类型绑定对应析构器并收集到 OpApiParams.converted_params,其他类型以 nullptr 占位。
  • 补齐数组类型的创建/销毁函数指针别名:新增 _aclCreateIntArray/_aclDestroyIntArray、_aclCreateFloatArray/_aclDestroyFloatArray、_aclCreateBoolArray/_aclDestroyBoolArray 等类型别名,覆盖原有仅支持 aclTensor/aclScalar/aclTensorList 的不足。
  • 新增头文件依赖与测试桩:op_fallback_internal.h 引入 op_execute_prepare_context.h、op_execute_launch_context.h;测试文件 op_info_record_utest.cpp 新增 Adx::AdumpSaveToFile 的桩实现。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 14 Comments:

代码审查

Closing Summary

审查完成,共报告 4 个问题:

  • P1 × 2、P2 × 1、P3 × 1

按文件逐一确认:

  1. include/op_common/op_graph/op_fallback.h —— 发现 2 个问题(CANN_OPS_OPB_ASYN_EXEC_ACLNN 宏中 OpApiFunc 未定义、new (std::nothrow) 结果未判空即解引用)。
  2. include/op_common/op_graph/op_fallback_internal.h —— 发现 1 个问题(Collect 重载集缺失 aclFloatArray*,导致 float 数组参数泄漏)。
  3. tests/nnopbase/ut/individual_op/op_info_record_utest.cpp —— 发现 1 个问题(新增 stub 引用了未显式声明的 SaveType 类型,存在编译失败风险)。

整体风险判断:本次变更新增的两阶段(Prepare)执行宏存在一处确定的编译级缺陷(OpApiFunc 未定义,一旦宏被使用即编译失败)和一处参数资源泄漏缺陷(aclFloatArray 析构缺失),此外测试文件存在潜在编译失败风险。建议在合入前修复 OpApiFunc → OpApiFunc2Stage、补齐 aclFloatArray 的 Collect 重载,并核对测试文件对 SaveType 的头文件依赖。整体属于中高风险变更,主要风险集中在新增宏的正确性与参数生命周期管理上。

类型 数量
🔴 阻塞 1
🟡 建议 1

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 14 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 14 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
include ✅ 王永光, 汤平川 (2/2) ✅ 王永光 (1/1)
repo-cann/opbase ✅ 宋恺, 汤平川 (2/2) ✅ 宋恺, 汤平川 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

esok11, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Aug 14 Set pengjiahao96,zhou-qilong,wangrui_,rxtfeng,loov1,tang-lei01,jia0liang,zl_hw,songkai111,pingchuantang,llimwang,gubaocheng,wangyongguang as approver
CANN-robotCANN-robotCollaborators
Aug 14 Set pengjiahao96,zhou-qilong,wangrui_,rxtfeng,loov1,tang-lei01,jia0liang,zl_hw,songkai111,pingchuantang,llimwang,gubaocheng,wangyongguang as approver
esok11esok11Collaborators
Aug 14 Modify description of pull request
esok11
esok11Collaborators
Aug 14 Comments:

/compile

likedislike
esok11esok11Collaborators
Aug 14 Pre merge succeeded (commit_id: a7defd541ba1e1b30c668b32249b875669306633)
CANN-robotCANN-robotCollaborators
Aug 14 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 14 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 14 Added label: ci-pipeline-passed
atomgit-bot
atomgit-botAug 14Start code review2
include/op_common/op_graph/op_fallback_internal.h
@@ -345,0 +406,4 @@
406+{
407+ (void)value;
408+ params.emplace_back(OpApiAnyValue{nullptr, nullptr});
409+}
atomgit-bot
atomgit-botAug 14Comments:

🟡 Medium Priority

变更行:op_fallback_internal.h 新增了 _aclCreateFloatArray/_aclDestroyFloatArray 别名(第 42、49 行),并新增了 Collect 重载集(第 368-402 行)覆盖 aclTensor*/aclScalar*/aclIntArray*/aclBoolArray*/aclTensorList*,但遗漏了 aclFloatArray* 的重载。

受影响的行为:当算子参数中包含 float 数组(对应 op 框架的 OPARG_FLOAT_LIST/aclFloatArray*,经 ConvertType 恒等模板原样保留为 aclFloatArray*)时,重载决议会落入通用模板 Collect(T value, ...)(第 404-409 行)。

失败模式:通用模板把该参数丢弃并写入 OpApiAnyValue{nullptr, nullptr},既不保存指针也不绑定 aclDestroyFloatArray 析构器。两阶段分离后,该 aclFloatArray 对象失去所有权、永远不被销毁 → 内存泄漏。这与 PR 描述中“补齐 aclIntArray/aclFloatArray/aclBoolArray 类型支持”的意图相矛盾(float 数组类型别名已加,但析构收集缺失)。

likedislike
Incorrect?
esok11
esok11Collaborators
Aug 17 Comments:

移植不涉及

likedislike
atomgit-bot
atomgit-botAug 14Start code review2
include/op_common/op_graph/op_fallback.h
Outdated
@@ -89,0 +96,4 @@
96+ auto* op_api_params = new (std::nothrow) OpApiParams(); \
97+ auto ResetCacheThreadLocalFunc = reinterpret_cast<ResetCacheThreadLocal>(ResetCacheThreadLocalAddr); \
98+ ResetCacheThreadLocalFunc(); \
99+ op_api_params->op_api_func = reinterpret_cast<OpApiFunc>(opApiFuncAddr); \
atomgit-bot
atomgit-botAug 14Comments:

🟠 High Priority

变更行:op_fallback.h:99 op_api_params->op_api_func = reinterpret_cast<OpApiFunc>(opApiFuncAddr);。

受影响的契约:OpApiFunc 并非命名空间级类型。全仓库中 OpApiFunc 仅有两处局部定义——ConvertToOpApiFunc 内部(op_fallback_internal.h:338 的局部 using)以及 SYN 宏 lambda 内部(op_fallback.h:62 的局部 using),二者都不可见于此宏展开处。新增的两阶段内部头里只定义了 OpApiFunc2Stage(op_fallback_internal.h:361),且 OpApiParams::op_api_func 字段类型正是 OpApiFunc2Stage。

失败模式:该宏一旦被任何调用点展开,OpApiFunc 未声明,编译报错(identifier not found),两阶段 Prepare 路径无法构建。当前仓库暂无该宏的调用点,所以暂不阻塞现有构建,但它是本 PR 的核心交付物,属确定的潜在编译错误。

Suggestions
99
- op_api_params->op_api_func = reinterpret_cast<OpApiFunc>(opApiFuncAddr); \
99
+ op_api_params->op_api_func = reinterpret_cast<OpApiFunc2Stage>(opApiFuncAddr);
Apply suggestion
likedislike
Incorrect?
System
System MessageSystem
Aug 15 Comments:

changed this line on f0406bd1 view diff detail

likedislike
esok11esok11Collaborators
Aug 15 Pre merge succeeded (commit_id: 5a4dfb10475c9459a8a802756b0256ddf46aa2d6)
5 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 15 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 15 Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): ci-pipeline-passed.

likedislike
esok11
esok11Collaborators
Aug 15 Comments:

/compile

likedislike
esok11esok11Collaborators
Aug 15 Pre merge succeeded (commit_id: 235a1174d355fef418ed4ef4377cb024bcba2924)
CANN-robotCANN-robotCollaborators
Aug 15 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 15 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 15 Added label: ci-pipeline-passed
wangyongguang
wangyongguangCollaborators
Aug 17 Comments:

/approve

likedislike
songkai111
songkai111Collaborators
Aug 17 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 17 Added label: approved
TangPC
TangPCCollaborators
Aug 17 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 17 Added label: lgtm
CANN-robot
CANN-robotCollaborators
Aug 17 Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
TangPCTangPCCollaborators
Aug 17 Resolved final issue
CANN-robotCANN-robotCollaborators
Aug 17 Merged pull request