Open
feat(opdev): add compiler-neutral external op task SPI #1
su-yuemingAdd on Aug 30
feat(opdev): add compiler-neutral external op task SPI #1
Open
Merged fromcodex/aclnn-package-devintomaster
Total: 23 files changed+3622-2
| @@ -6,6 +6,7 @@ cmake-build-debugindocker/ | |||
| 6 | # distribution and packaging | 6 | # distribution and packaging |
| 7 | build/ | 7 | build/ |
| 8 | build_out/ | 8 | build_out/ |
| 9 | +build-production/ | ||
| 9 | output/ | 10 | output/ |
| 10 | dist/ | 11 | dist/ |
| 11 | *.egg-info/ | 12 | *.egg-info/ |
| @@ -67,3 +68,28 @@ third_party/ | |||
| 67 | oat_reports/ | 68 | oat_reports/ |
| 68 | .claude/ | 69 | .claude/ |
| 69 | .opencode/ | 70 | .opencode/ |
| 71 | + | ||
| 72 | +# local UT runtime byproducts (repo root) | ||
| 73 | +/built-in/ | ||
| 74 | +/custom_opp/ | ||
| 75 | +/test.json | ||
| 76 | +/test/ | ||
| 77 | + | ||
| 78 | +# local UT runtime byproducts (mock dir) | ||
| 79 | +tests/nnopbase/mock/conf/ | ||
| 80 | +tests/nnopbase/mock/debug_kernel/ | ||
| 81 | +tests/nnopbase/mock/scene.info | ||
| 82 | +tests/nnopbase/mock/user_define/ | ||
| 83 | +tests/nnopbase/mock/vendors/ | ||
| 84 | + | ||
| 85 | +# kernels generated by UT runs land at the SoC dir root; tracked fixtures | ||
| 86 | +# live one level deeper (<soc>/<op>/...) and are not matched by these patterns | ||
| 87 | +tests/nnopbase/mock/built-in/op_impl/ai_core/tbe/kernel/*/*.json | ||
| 88 | +tests/nnopbase/mock/built-in/op_impl/ai_core/tbe/kernel/*/*.o | ||
| 89 | +# binary_info_config.json is regenerated by UT runs; per-op <soc>/<op>.json | ||
| 90 | +# fixtures are tracked and not matched | ||
| 91 | +tests/nnopbase/mock/built-in/op_impl/ai_core/tbe/kernel/config/*/binary_info_config.json | ||
| 92 | +# static-kernel UT byproducts (tracked fixtures are the Conv2D/TestStatic/ | ||
| 93 | +# TestStaticAdd op dirs and are not matched) | ||
| 94 | +tests/nnopbase/mock/static_kernel/ai_core/config/ | ||
| 95 | +tests/nnopbase/mock/static_kernel/ai_core/static_kernel_202307261051/Flash/ | ||
| @@ -117,6 +117,8 @@ | |||
| 117 | - [AbandonCache](op_executor/AbandonCache.md) | 117 | - [AbandonCache](op_executor/AbandonCache.md) |
| 118 | - [预留接口](op_executor/reserved_interface_op_executor.md) | 118 | - [预留接口](op_executor/reserved_interface_op_executor.md) |
| 119 | 119 | ||
| 120 | + - [External Task算子接入架构](external_op_task_integration_architecture.md) | ||
| 121 | + | ||
| 120 | - [op\_log](op_log/op_log.md) | 122 | - [op\_log](op_log/op_log.md) |
| 121 | - [platform](platform/platform.md) | 123 | - [platform](platform/platform.md) |
| 122 | - [GetCurrentPlatformInfo](platform/GetCurrentPlatformInfo.md) | 124 | - [GetCurrentPlatformInfo](platform/GetCurrentPlatformInfo.md) |
| @@ -0,0 +1,264 @@ | |||
| 1 | +# ExternalOp DFX 关联方案 | ||
| 2 | + | ||
| 3 | +状态:当前开发版实现基线与后续演进方案。`ExternalOpTask` 仅接受 ABI v1 最新格式, | ||
| 4 | +不保留旧版兼容路径。本文明确标为“后续方案”的按需查询接口不在当前版本开发范围内。 | ||
| 5 | + | ||
| 6 | +## 1. 设计原则 | ||
| 7 | + | ||
| 8 | +opbase 将 producer 视为黑盒:框架负责 L2/L0 和 External Task 边界的 DFX,producer | ||
| 9 | +按需提供内部 DFX 子集,后续通过独立公共查询接口获取关联上下文,把两侧记录连接起来。 | ||
| 10 | + | ||
| 11 | +opbase 不解析 producer 的 tiling、kernel 参数、产物、workspace 布局,不接收内部 | ||
| 12 | +DFX payload,不代理内部阶段或 dispatch 的上报。producer 可以不提供内部 DFX, | ||
| 13 | +也可以只提供计时、日志或异常 dump;不要求所有 producer 达到原生 AiCore 的能力集合。 | ||
| 14 | + | ||
| 15 | +当前版本保留框架侧 DFX 和独立的执行复用约束;执行接口不携带 DFX 参数。公共 SPI 不提供 | ||
| 16 | +BeginStage/EndStage、BeginDispatch/EndDispatch、DumpRegion 等服务函数表。 | ||
| 17 | +producer 可直接对接具有稳定公共接口的 profiler、runtime 或自有诊断后端;Adump | ||
| 18 | +查询和异常登记目前仅有 `pkg_inc` 内部接口,不能作为独立 producer 的公共依赖。 | ||
| 19 | + | ||
| 20 | +后续方案提供 producer 所需的 DFX 上下文按需查询,并由 opbase 提供 ExternalOp | ||
| 21 | +异常登记 helper。下沉到 producer 的是“真实 dispatch 上报时机和动态元数据”,不是 | ||
| 22 | +Adump 内部实现及其私有头文件依赖。 | ||
| 23 | + | ||
| 24 | +关联上下文解决“属于哪个框架调用”,不等于后端已自动建立关联。每种后端的字段映射、 | ||
| 25 | +时间归属和真实设备任务身份仍需验证;无法建立关联时明确标注,不伪造记录。 | ||
| 26 | + | ||
| 27 | +## 2. 职责与能力子集 | ||
| 28 | + | ||
| 29 | +下表涉及 producer 父级关联的能力属于后续方案,当前不提供公共关联上下文。 | ||
| 30 | + | ||
| 31 | +| 诉求 | opbase 责任 | producer 可选责任 | | ||
| 32 | +| --- | --- | --- | | ||
| 33 | +| L2/L0 日志、接口耗时 | 保留公共宏与上下文 | 内部日志携带父调用标识 | | ||
| 34 | +| External launch 回调耗时 | 记录统一 Host 执行边界 | 无强制要求;producer 不得把整个回调伪装为 kernel | | ||
| 35 | +| 公共输入输出、属性 | 公共 L2/L0 日志与 dump;设备任务元数据后续接入 | 不重复承担框架边界 dump | | ||
| 36 | +| tiling/编译/加载耗时 | 提供父级关联 | 定义阶段、取得阶段 ID 并自行计时上报 | | ||
| 37 | +| 逐 kernel profiling | 提供 framework summary ID | 包围实际下发计时,自行上报任务详细信息 | | ||
| 38 | +| 设备执行耗时 | 提供框架归属 | 使用 runtime/profiler 设备事件及其关联机制 | | ||
| 39 | +| kernel 异常 dump | 当前不登记;后续提供公共查询和登记helper | 后续在真实dispatch后立即触发,提供参数/tiling等动态信息 | | ||
| 40 | +| 中间 tensor / workspace dump | 不解析 producer 私有内存 | 自选内容、快照时点、格式、后端和生命周期 | | ||
| 41 | +| printf/assert/产物调试 | 提供可引用的调用标识 | 使用自身产物协议与 runtime 机制 | | ||
| 42 | +| overflow | 保留计算任务的框架边界检查 | 如增加真实dispatch内部检查,需管理共享状态并避免重复 | | ||
| 43 | + | ||
| 44 | +不新增强制 DFX 能力注册协议。producer 应在自身文档/诊断输出中说明支持的子集, | ||
| 45 | +区分“未实现”“未开启”“当前模式不支持”;能力缺失不使 External Task 注册失败。 | ||
| 46 | +开关和运行模式限制由实际执行该项 DFX 的一方处理。当前没有公共查询能力时,不要求 | ||
| 47 | +producer 通过 `pkg_inc` 绕过模块边界访问 Adump。 | ||
| 48 | + | ||
| 49 | +## 3. 最小关联上下文(后续方案) | ||
| 50 | + | ||
| 51 | +逻辑归属为:框架 task 调用 → producer 自定义阶段/dispatch → runtime 设备任务。 | ||
| 52 | +summary ID 不承诺逐次调用唯一,task 调用 ID 也不是设备 task ID。当前版本不定义 | ||
| 53 | +公共关联结构,也不分配 producer task invocation ID;后续查询结果见 4.2。 | ||
| 54 | + | ||
| 55 | +prepare 与 launch 可以发生在不同线程。后续查询必须在实际 launch 回调线程进行, | ||
| 56 | +返回标量副本;异步或跨线程使用时由 producer 复制所需字段,不保存私有 TLS 指针。 | ||
| 57 | + | ||
| 58 | +## 4. 接口与版本 | ||
| 59 | + | ||
| 60 | +### 4.1 当前版本:三参数 launch | ||
| 61 | + | ||
| 62 | +当前尚未发布,ABI 版本保持为 1。producer 必须按当前三参数回调与 opbase 同步重编译; | ||
| 63 | +注册时精确校验 `structSize` 和 `abiVersion`,拒绝旧版本,不做隐式转换。 | ||
| 64 | + | ||
| 65 | +```cpp | ||
| 66 | +using ExternalOpPrepareFn = aclnnStatus (*)( | ||
| 67 | + const void* taskConfig, const OpArgContext* args, | ||
| 68 | + ExternalOpWorkspaceRequest* workspaceRequest, void** launchState) noexcept; | ||
| 69 | + | ||
| 70 | +using ExternalOpLaunchFn = aclnnStatus (*)( | ||
| 71 | + void* launchState, const OpArgContext* args, aclrtStream stream) noexcept; | ||
| 72 | +``` | ||
| 73 | + | ||
| 74 | +task 保留 prepare/launch/destroy、engineKind、taskConfig 和第 7 节的 cache 策略。 | ||
| 75 | +prepare 只负责 Host 侧参数校验、workspace 大小计算和 launchState 构造。 | ||
| 76 | +DFX 上下文不放入 taskConfig、launchState 或 OpArgContext,不向 producer 暴露私有对象。 | ||
| 77 | + | ||
| 78 | +### 4.2 后续方案:按需查询(当前版本不实现) | ||
| 79 | + | ||
| 80 | +后续查询作为独立 opbase 公共接口提供,不再修改三参数 launch 签名。 | ||
| 81 | + | ||
| 82 | +producer 或 samples adapter 仅在实际需要 profiling、异常登记等能力时查询当前 launch | ||
| 83 | +的上下文。概念接口如下,名称和最终布局以后续 ABI 评审为准: | ||
| 84 | + | ||
| 85 | +```cpp | ||
| 86 | +struct ExternalOpDfxContext { | ||
| 87 | + uint32_t structSize; | ||
| 88 | + uint32_t version; | ||
| 89 | + uint64_t enabledFeatures; | ||
| 90 | + uint64_t validFields; | ||
| 91 | + uint64_t taskInvocationId; | ||
| 92 | + uint64_t profilingSummaryId; | ||
| 93 | +}; | ||
| 94 | + | ||
| 95 | +aclnnStatus QueryExternalOpDfxContext(ExternalOpDfxContext* context) noexcept; | ||
| 96 | +``` | ||
| 97 | + | ||
| 98 | +- 查询只在 External launch callback 动态作用域内成功;回调外返回无当前上下文。 | ||
| 99 | +- opbase 在进入 callback 前安装 thread-local 上下文,退出时用 RAII 恢复;嵌套调用不能串扰。 | ||
| 100 | +- 查询结果按值复制,不向 producer 暴露 opbase 私有 TLS 指针。跨线程使用时由 producer | ||
| 101 | + 在原 callback 线程查询并复制所需标量;异常 task ID 登记仍必须发生在真实下发线程。 | ||
| 102 | +- `enabledFeatures` 是本次 launch 的 DFX 开关快照,和 `validFields` 的字段有效性语义分离。 | ||
| 103 | +- producer 不使用 DFX 时不调用查询接口;使用多个 dispatch 时通常每个 callback 查询一次, | ||
| 104 | + 再将结果复用于该 callback 内的逐 dispatch helper。 | ||
| 105 | +- 该变更主要用于简化回调 ABI 和隔离可选能力,不以性能收益为依据。传递一个指针本身 | ||
| 106 | + 通常只是一个参数寄存器;跨 SO 查询反而可能更贵。 | ||
| 107 | + | ||
| 108 | +当前 ABI v1 以 4.1 的三参数 launch 为唯一有效格式。当前头文件、实现、sample | ||
| 109 | +和测试不得提前使用上述查询函数,也不得把概念接口描述成已经对外提供。 | ||
| 110 | + | ||
| 111 | +## 5. 各类 DFX 的接入方式 | ||
| 112 | + | ||
| 113 | +### 5.1 Host 边界与逐 dispatch profiling | ||
| 114 | + | ||
| 115 | +opbase 仅在 External launch callback 外围通过私有实现建立 Host execution profiling 边界;该区间 | ||
| 116 | +表示 producer Host 执行耗时,不是 kernel launch,更不能作为一个虚构的 device kernel。 | ||
| 117 | +当前不调用 `ReportProfilingInfo` 或 `ReportCacheOpInfo`,也不生成设备任务的 | ||
| 118 | +basic/tensor/attr info。External 边界参数不等于真实 kernel 参数,engineKind 也不足以 | ||
| 119 | +描述内部各次 dispatch;不能读取遗留 TLS numBlocks 作为真实 blockDim。 | ||
| 120 | + | ||
| 121 | +后续 profiling 与 cache 元数据必须由真实 dispatch 提供,并验证名称、时间与任务身份的 | ||
| 122 | +对应关系。`aclrtCacheLastTaskOpInfo` 绑定最后一个 runtime 任务,不能在黑盒 callback | ||
| 123 | +返回后用一次调用代表全部设备工作;cache/capture 重放归属需单独设计和验证。 | ||
| 124 | + | ||
| 125 | +当前示例暂不提供 producer 逐 dispatch profiling helper,也不直接链接 msprofiler。 | ||
| 126 | +后续逐 kernel range 必须放在真实 dispatch 点,通过独立查询获取框架归属,再组合 | ||
| 127 | +blockDim、kernelType、stream 和参与本次 dispatch 的 tensor 等动态元数据。 | ||
| 128 | +一个 callback 可以下发多个不同 kernel;不得用通用 callback 外围的单个 kernel range | ||
| 129 | +代表整个黑盒回调。DFX 失败不改变计算返回值。 | ||
| 130 | + | ||
| 131 | +### 5.2 异常 dump | ||
| 132 | + | ||
| 133 | +当前 External launcher 不调用 `ReportExceptionDump`,也不清空或填充内部异常登记状态。 | ||
| 134 | +回调返回后只能取得 thread-last-task-id;多 dispatch、无实际下发、下发失败或后续辅助 | ||
| 135 | +下发都可能造成错误归属,因此异常登记留到真实 dispatch 的公共接口方案中实现。 | ||
| 136 | + | ||
| 137 | +后续版本由 producer 在每次真实下发后立即调用 opbase 提供的公共 ExternalOp exception | ||
| 138 | +report helper,并传入 dispatch name、blockDim、tiling 和参与该 dispatch 的 tensor 等 | ||
| 139 | +动态信息。helper 内部查询 Adump 异常开关、取得当前线程 last task ID、补充框架 L2/L0 | ||
| 140 | +归属并调用 Adump 内部登记接口。producer 不直接包含 `pkg_inc/dump/adump_pub.h`,也不 | ||
| 141 | +直接链接或依赖 Adump 私有 ABI。 | ||
| 142 | + | ||
| 143 | +exception report helper 可以由 producer 无条件调用,未开启时在 opbase 内快速返回; | ||
| 144 | +如果 producer 需要在构造昂贵元数据前判断开关,则使用 4.2 的按需上下文查询。当前版本 | ||
| 145 | +暂不新增该查询接口和 report helper,不提供 External 设备任务异常登记。 | ||
| 146 | + | ||
| 147 | +后续方案中,producer 在每次真实下发后确定该任务身份。使用 thread-last-task-id 的路径必须在正确 | ||
| 148 | +线程和下发位置查询,且先于可能覆盖身份的调试辅助任务;一次内部调用下发多个任务时, | ||
| 149 | +不能仅用最后一个 ID 代表所有任务。跨线程、跨 stream、capture 的关联由 producer 验证。 | ||
| 150 | + | ||
| 151 | +后续查询提供的 taskInvocationId 可进入后端支持的关联字段或 producer 伴随记录。Adump 接口不支持的字段不能强行映射或声称原生关联。 | ||
| 152 | +producer 负责确认后端复制/保留数据的契约,保留元数据、设备参数快照和产物直到不再使用。 | ||
| 153 | +普通算子 dump、异常 dump、参数异常 dump、overflow 是不同开关,不因有 context 就开启。 | ||
| 154 | + | ||
| 155 | +### 5.3 中间结果、workspace、printf 与 overflow | ||
| 156 | + | ||
| 157 | +后续 producer 自行选择区域、布局、格式和快照时点,并将输出携带查询所得 taskInvocationId 和可选 | ||
| 158 | +dispatch 标识。opbase 不提供区域描述 SPI,也不验证或解析其私有 arena schema。 | ||
| 159 | +producer 对访问范围、快照缓冲、stream 顺序以及异步释放负责。临时区域复用前应完成 | ||
| 160 | +必要快照排队;executor 析构不意味着设备已经完成。 | ||
| 161 | + | ||
| 162 | +printf/assert 保留 producer 的产物协议和解码方式,通过名称或关联记录接入排障流程。 | ||
| 163 | +计算任务保留现有 External task 框架边界 overflow 检查。producer 若增加内部逐dispatch检查, | ||
| 164 | +必须与公共检查明确分工;涉及清除共享设备溢出状态时需定义检查所有权,避免双重检测和 | ||
| 165 | +状态干扰。既有原生 AiCore overflow 路径不变。 | ||
| 166 | + | ||
| 167 | +producer 开启的同步调试行为必须遵守其后端和 capture 限制。未开启调试时,不因新增 | ||
| 168 | +关联上下文引入设备分配或 stream 同步。 | ||
| 169 | + | ||
| 170 | +## 6. 调用流程 | ||
| 171 | + | ||
| 172 | +```mermaid | ||
| 173 | +sequenceDiagram | ||
| 174 | + participant O as opbase | ||
| 175 | + participant P as producer | ||
| 176 | + participant R as runtime | ||
| 177 | + participant D as DFX 后端 | ||
| 178 | + O->>P: prepare(taskConfig, args, workspaceRequest, launchState) | ||
| 179 | + Note over P: 仅做 Host 准备 | ||
| 180 | + P-->>O: state 和 workspace 大小 | ||
| 181 | + O->>O: 内存规划;运行时恢复上下文、输入 dump | ||
| 182 | + O->>O: 创建框架侧 profiling summary | ||
| 183 | + O->>D: launch Host 范围开始、框架关联记录 | ||
| 184 | + O->>P: launch(launchState, args, stream) | ||
| 185 | + loop 每个真实 dispatch | ||
| 186 | + P->>R: 真实下发 | ||
| 187 | + end | ||
| 188 | + P-->>O: 计算状态 | ||
| 189 | + O->>D: launch Host 范围结束 | ||
| 190 | + O->>O: 输出 dump、按条件检查 overflow、恢复调用方上下文 | ||
| 191 | +``` | ||
| 192 | + | ||
| 193 | +内部事件没有回传 opbase 的过程。producer 不提供任何内部 DFX 时,框架流程同样成立。 | ||
| 194 | +后端可以相同,也可以不同;跨后端至少要输出可连接的关联字段,不承诺工具自动合并。 | ||
| 195 | + | ||
| 196 | +## 7. Cache、capture 与错误契约 | ||
| 197 | + | ||
| 198 | +关联 ID 不能解决重放绕过回调的问题,需要一个与 DFX payload 无关的执行策略: | ||
| 199 | + | ||
| 200 | +| ExternalOpCachePolicy | opbase 行为 | producer 承诺 | | ||
| 201 | +| --- | --- | --- | | ||
| 202 | +| Keep = 0 | 在公共cache检查允许时保留复用 | 计算及所声明DFX不依赖每次执行回调 | | ||
| 203 | +| Abandon = 1 | 禁cache,不禁止显式repeat | 显式repeat时仍须满足其计算及DFX契约 | | ||
| 204 | +| DisableRepeat = 2 | 禁cache/repeatable | 每次新建调用正常执行回调 | | ||
| 205 | + | ||
| 206 | +当前接口通过`ExternalOpTask::cachePolicy`声明策略,默认`DisableRepeat`(包括Host)。 | ||
| 207 | +使用`ExternalOpTask task{}`应用默认值,不得用memset清零。producer验证后可显式选择 | ||
| 208 | +`Keep`。opbase在注册时校验并映射到内部同名策略,outshape等框架限制优先。 | ||
| 209 | +“每次回调”指每次重新构建的调用,不改变原有executor消费和状态销毁语义。 | ||
| 210 | + | ||
| 211 | +后续提供关联查询时,对于 Keep,taskInvocationId 表示原始构建/捕获归属,重放不能被描述为又一次 | ||
| 212 | +producer 回调;runtime 可提供重放事件身份,但不保证再次生成 Host 埋点。若要求每次 | ||
| 213 | +调用都有可单独关联的 producer 记录,初版必须禁复用,不新增 opbase 内部元数据重放服务。 | ||
| 214 | + | ||
| 215 | +producer 私有 DFX 开关若要求每次回调,由 producer/Adapter 在注册前选择禁复用。 | ||
| 216 | +框架无法识别黑盒开关变化:复用 executor 生命周期内,producer 不得动态开启需要回调 | ||
| 217 | +的新能力;应重新构建,或从一开始禁复用。公共 DFX 的既有 cache 禁用条件仍生效。 | ||
| 218 | + | ||
| 219 | +capture 与 cache 独立。禁 cache 不意味着可以在 capture 中同步或插入任意 dump; | ||
| 220 | +producer 按其后端能力降级,框架只约束公共行为。 | ||
| 221 | + | ||
| 222 | +DFX 上报错误不覆盖计算状态,不能为诊断伪造 kernel 身份。运行时实际设备执行错误仍 | ||
| 223 | +按计算错误通道处理。DFX 缺失、后端失败和计算失败在日志中区分。 | ||
| 224 | + | ||
| 225 | +## 8. 框架实现范围 | ||
| 226 | + | ||
| 227 | +- 保留 L2/L0 宏、现有计算任务输入输出 dump、Host 不做 External L0 dump 等行为。 | ||
| 228 | +- 保留 External launch callback 的 Host 边界计时;不生成设备任务元数据。 | ||
| 229 | +- 当前 ABI 将非空 l0Name 复制为 task 名称,缺失时回退宏上下文;保留字符串的完整生命周期。 | ||
| 230 | + 名称不一致时诊断;传名称不替代 L2 初始化和 L0 参数打印。 | ||
| 231 | +- launch 只保留 launchState、args、stream;不定义公共 DFX 关联结构。 | ||
| 232 | + 注册 adapter 留在 samples,不进入 opbase 公共安装头。 | ||
| 233 | +- 修正有效 summary 的生成条件,保存并恢复调用方的日志、profiling 和计时上下文。 | ||
| 234 | +- 不新增 producer DFX 服务表、异常 payload 转换、workspace 快照或 printf 解码实现。 | ||
| 235 | +- 当前版本不新增按需 DFX 查询接口和 ExternalOp exception report helper;4.2 和 5.2 的 | ||
| 236 | + 后续方案需作为独立公共接口实现、测试并同步 producer。 | ||
| 237 | + | ||
| 238 | +## 9. 分阶段交付与验收 | ||
| 239 | + | ||
| 240 | +| 批次 | 范围 | 验收重点 | | ||
| 241 | +| --- | --- | --- | | ||
| 242 | +| A | 三参数 launch / ABI v1 / 复用策略与中立 fixture | 旧版本拒绝;名字所有权;禁复用有效 | | ||
| 243 | +| B | Host 边界与上下文恢复 | 开关组合及无效 ID;不生成 basic/attr 信息;多 task 不串名 | | ||
| 244 | +| C | 框架边界 dump 回归 | L0 dump 与 overflow;当前不做 External 异常登记 | | ||
| 245 | +| D | 异步、capture、cache 验收 | executor 生命周期;重放不伪称回调发生;私有开关变化约束 | | ||
| 246 | +| E(后续) | 按需 DFX 查询、逐 dispatch profiling/cache 元数据、公共 exception report helper | 查询作用域与嵌套恢复;跨线程副本;逐 dispatch task ID 准确 | | ||
| 247 | + | ||
| 248 | +opbase UT 不依赖特定 producer 产物或协议。设备测试由对应 producer 集成承担,验证 | ||
| 249 | +其选择实现的 DFX 子集;未实现的子集不作为失败。原生 AiCore/AiCpu 路径需要回归。 | ||
| 250 | +文档和示例须分别标明框架已有能力、producer 可选能力及后端限制。 | ||
| 251 | + | ||
| 252 | +落地前验证项:公开 Guard/名称 ID 接口的链接可用性;summary 与 task 调用记录的 | ||
| 253 | +后端映射;profiler Host 范围和设备节点去重;Adump 可用关联字段和数据所有权;不同 | ||
| 254 | +runtime 的任务身份与 capture 支持。未验证项不宣称已形成工具内自动关联。 | ||
| 255 | + | ||
| 256 | +## 10. 代码依据 | ||
| 257 | + | ||
| 258 | +- `include/nnopbase/opdev/external_op_task.h`:当前 SPI、版本校验与异步生命周期。 | ||
| 259 | +- `include/nnopbase/opdev/op_dfx.h`:保持不变的公共 OpDfxGuard 和接口宏。 | ||
| 260 | +- `src/nnopbase/composite_op/aclnn_engine/external_op_task.cpp`:当前 External DFX 边界。 | ||
| 261 | +- `src/nnopbase/common/utils/bridge_dfx.cpp`:已有 DFX 后端开关。 | ||
| 262 | +- `src/nnopbase/common/utils/op_cache.cpp`:公共 DFX 与 cache 约束。 | ||
| 263 | + | ||
| 264 | +与总体架构的关系见 [External Task 算子接入架构](external_op_task_integration_architecture.md)。 | ||
| @@ -0,0 +1,182 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#ifndef OP_API_COMMON_INC_OPDEV_EXTERNAL_OP_TASK_H_ | ||
| 12 | +#define OP_API_COMMON_INC_OPDEV_EXTERNAL_OP_TASK_H_ | ||
| 13 | + | ||
| 14 | +#include <cstdint> | ||
| 15 | + | ||
| 16 | +#include "acl/acl_base_rt.h" | ||
| 17 | +#include "aclnn/acl_meta.h" | ||
| 18 | + | ||
| 19 | +struct aclOpExecutor; | ||
| 20 | + | ||
| 21 | +namespace op { | ||
| 22 | + | ||
| 23 | +struct OpArgContext; | ||
| 24 | + | ||
| 25 | +inline constexpr uint32_t EXTERNAL_OP_TASK_ABI_VERSION = 1U; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * Executor scheduling category. It does not identify an artifact format, | ||
| 29 | + * compiler, package layout, or implementation routing policy. | ||
| 30 | + * | ||
| 31 | + * AiCore, AiCpu and Dvpp are compute tasks: op dump may record L0 inputs | ||
| 32 | + * and outputs, and successful launches may run the framework overflow check. | ||
| 33 | + * Host tasks skip these compute-only operations. All kinds share the External | ||
| 34 | + * callback Host profiling boundary. Device-task DFX is left to future APIs. | ||
| 35 | + */ | ||
| 36 | +enum class ExternalOpEngineKind : uint32_t { | ||
| 37 | + AiCore = 1U, | ||
| 38 | + AiCpu = 2U, | ||
| 39 | + Dvpp = 3U, | ||
| 40 | + Host = 4U, | ||
| 41 | +}; | ||
| 42 | + | ||
| 43 | +/** | ||
| 44 | + * Producer cache policy, resolved by opbase when registering the task. | ||
| 45 | + * Keep preserves framework eligibility checks; Abandon disables cache without | ||
| 46 | + * disabling explicit repeat; DisableRepeat disables both. Framework constraints | ||
| 47 | + * may strengthen this policy. This does not control runtime graph capture. | ||
| 48 | + */ | ||
| 49 | +enum class ExternalOpCachePolicy : uint32_t { | ||
| 50 | + Keep = 0U, | ||
| 51 | + Abandon = 1U, | ||
| 52 | + DisableRepeat = 2U, | ||
| 53 | +}; | ||
| 54 | + | ||
| 55 | +/** | ||
| 56 | + * Workspace buffers requested by a prepare callback. Each size becomes one | ||
| 57 | + * executor-allocated workspace tensor; alignment (512B) and memory reuse are | ||
| 58 | + * handled by the opbase memory planner, so producers only report byte sizes. | ||
| 59 | + * | ||
| 60 | + * The sizes array is borrowed: opbase consumes it synchronously after the | ||
| 61 | + * prepare callback returns and never retains the pointer. | ||
| 62 | + */ | ||
| 63 | +struct ExternalOpWorkspaceRequest { | ||
| 64 | + const uint64_t* sizes; | ||
| 65 | + uint32_t count; | ||
| 66 | + uint32_t reserved; | ||
| 67 | +}; | ||
| 68 | + | ||
| 69 | +/** | ||
| 70 | + * Prepare one executor-local task state during ACLNN phase 1. | ||
| 71 | + * | ||
| 72 | + * taskConfig is an optional opaque producer-owned preparation descriptor. It | ||
| 73 | + * carries producer-selected preparation data, such as an implementation plan, | ||
| 74 | + * resource handle, binding metadata, or static launch configuration, | ||
| 75 | + * without exposing that producer-private schema to opbase. opbase passes the | ||
| 76 | + * pointer through unchanged and never dereferences, copies, retains, or frees | ||
| 77 | + * it. The pointer and every object reachable through it are borrowed for this | ||
| 78 | + * synchronous call only; data needed after prepare returns must be copied or | ||
| 79 | + * otherwise retained by the producer in launchState. Tensor and workspace | ||
| 80 | + * addresses come from args rather than taskConfig. | ||
| 81 | + * | ||
| 82 | + * launchState is initialized to nullptr by opbase. A non-null state returned on | ||
| 83 | + * either success or failure is released exactly once through ExternalOpDestroyFn. | ||
| 84 | + * Each prepared task has its own state slot; the same pointer is passed to launch | ||
| 85 | + * on every execution and to destroy on cleanup. Stateless tasks may return null: | ||
| 86 | + * launch then receives null and destroy is not called. Shared producer resources | ||
| 87 | + * require producer-managed ownership (for example, reference counting). | ||
| 88 | + * | ||
| 89 | + * workspaceRequest is initialized to {nullptr, 0, 0} by opbase. The producer | ||
| 90 | + * may report the workspace byte sizes it needs; opbase allocates one workspace | ||
| 91 | + * tensor per size and appends them to args before graph building. | ||
| 92 | + * | ||
| 93 | + * The returned state must be address-independent: tensor and workspace | ||
| 94 | + * addresses are only read from the args passed to ExternalOpLaunchFn. This is | ||
| 95 | + * what allows opbase to cache and repeat the executor. | ||
| 96 | + */ | ||
| 97 | +using ExternalOpPrepareFn = aclnnStatus (*)(const void* taskConfig, const OpArgContext* args, | ||
| 98 | + ExternalOpWorkspaceRequest* workspaceRequest, | ||
| 99 | + void** launchState) noexcept; | ||
| 100 | + | ||
| 101 | +/** | ||
| 102 | + * Launch the prepared task during ACLNN phase 2. | ||
| 103 | + * | ||
| 104 | + * args contains the final tensor and workspace addresses, and stream is the | ||
| 105 | + * stream supplied to CommonOpExecutorRun. A producer may enqueue more than one | ||
| 106 | + * operation from a single callback; all device work must use this stream unless | ||
| 107 | + * the producer establishes the required cross-stream dependencies itself. | ||
| 108 | + * | ||
| 109 | + * opbase does not synchronize the stream or manage producer code images. When | ||
| 110 | + * launch enqueues asynchronous work, the producer must keep every module, | ||
| 111 | + * device image, and other resource needed by that work valid until the work has | ||
| 112 | + * completed. In particular, executor teardown is not proof of stream | ||
| 113 | + * completion. | ||
| 114 | + */ | ||
| 115 | +using ExternalOpLaunchFn = aclnnStatus (*)(void* launchState, const OpArgContext* args, | ||
| 116 | + aclrtStream stream) noexcept; | ||
| 117 | + | ||
| 118 | +/** | ||
| 119 | + * Release a non-null prepared state, including partially prepared state returned | ||
| 120 | + * on failure. Called exactly once on registration failure or launcher teardown, | ||
| 121 | + * not after each launch. All three callbacks are required, even for stateless | ||
| 122 | + * tasks, and must not throw exceptions. | ||
| 123 | + * | ||
| 124 | + * This callback is not a device-completion notification. Resources still used | ||
| 125 | + * by submitted work must remain alive until completion, even if launch returned | ||
| 126 | + * an error after submitting some work. The producer must establish completion | ||
| 127 | + * before releasing them, or transfer their ownership to deferred cleanup that | ||
| 128 | + * does not depend on the destroyed state. opbase does not wait for that cleanup. | ||
| 129 | + * Borrowed taskConfig data is no longer available; only data copied or explicitly | ||
| 130 | + * retained by the producer during prepare may be used here. | ||
| 131 | + */ | ||
| 132 | +using ExternalOpDestroyFn = void (*)(void* launchState) noexcept; | ||
| 133 | + | ||
| 134 | +/** | ||
| 135 | + * Initialize with ExternalOpTask task{} (not memset) to apply the default | ||
| 136 | + * DisableRepeat policy; set structSize to sizeof(ExternalOpTask) | ||
| 137 | + * and abiVersion to EXTERNAL_OP_TASK_ABI_VERSION. Reserved fields must be zero. | ||
| 138 | + * | ||
| 139 | + * The receiver requires the current ABI version and exact structure size. This | ||
| 140 | + * development interface intentionally has no compatibility path for earlier | ||
| 141 | + * layouts: producers and opbase must be built against the same header. These | ||
| 142 | + * checks assume a common platform ABI; they do not make pointers or callback | ||
| 143 | + * signatures portable across ABIs. | ||
| 144 | + */ | ||
| 145 | +struct ExternalOpTask { | ||
| 146 | + uint32_t structSize; | ||
| 147 | + uint32_t abiVersion; | ||
| 148 | + ExternalOpEngineKind engineKind; | ||
| 149 | + ExternalOpCachePolicy cachePolicy = ExternalOpCachePolicy::DisableRepeat; | ||
| 150 | + const void* taskConfig; | ||
| 151 | + ExternalOpPrepareFn prepare; | ||
| 152 | + ExternalOpLaunchFn launch; | ||
| 153 | + ExternalOpDestroyFn destroy; | ||
| 154 | + uint64_t reserved[4]; | ||
| 155 | +}; | ||
| 156 | + | ||
| 157 | +/** | ||
| 158 | + * Attach one already-selected producer task to an executor. | ||
| 159 | + * | ||
| 160 | + * args must be created by GetOpArgContext, which reserves one OpArg slot for | ||
| 161 | + * appending workspace and uses the allocator required by DestroyOpArgContext. | ||
| 162 | + * args is consumed on every return path. Input tensor arguments may be null | ||
| 163 | + * to represent absent optional inputs; output tensors must be non-null. The | ||
| 164 | + * workspace argument list must be absent or empty: workspace buffers are | ||
| 165 | + * requested through the prepare callback and allocated by opbase from the | ||
| 166 | + * executor. | ||
| 167 | + * | ||
| 168 | + * The task descriptor and taskConfig may be released when this function | ||
| 169 | + * returns. The launch and destroy function pointers are invoked later (during | ||
| 170 | + * CommonOpExecutorRun and executor teardown), so the module providing them | ||
| 171 | + * must remain loaded for the whole lifetime of the executor. If launch enqueues | ||
| 172 | + * asynchronous work, the producer is also responsible for extending the | ||
| 173 | + * lifetime of any code image or resource used by that work until device-side | ||
| 174 | + * completion; opbase neither synchronizes the stream nor unloads producer | ||
| 175 | + * modules. | ||
| 176 | + */ | ||
| 177 | +aclnnStatus AddExternalOpTask(const char* l0Name, uint32_t opType, const ExternalOpTask* task, | ||
| 178 | + aclOpExecutor* executor, OpArgContext* args); | ||
| 179 | + | ||
| 180 | +} // namespace op | ||
| 181 | + | ||
| 182 | +#endif // OP_API_COMMON_INC_OPDEV_EXTERNAL_OP_TASK_H_ | ||
| @@ -321,7 +321,15 @@ struct OpArgContext { | |||
| 321 | return nullptr; | 321 | return nullptr; |
| 322 | } | 322 | } |
| 323 | 323 | ||
| 324 | - inline bool ContainsOpArgType(OpArgDef type) | 324 | + inline const OpArgList* GetOpArg(OpArgDef type) const |
| 325 | + { | ||
| 326 | + if (type >= OP_INPUT_ARG && type <= OP_EXEC_MODE_ARG) { | ||
| 327 | + return &argLists[type]; | ||
| 328 | + } | ||
| 329 | + return nullptr; | ||
| 330 | + } | ||
| 331 | + | ||
| 332 | + inline bool ContainsOpArgType(OpArgDef type) const | ||
| 325 | { | 333 | { |
| 326 | if (type >= OP_INPUT_ARG && type <= OP_EXEC_MODE_ARG) { | 334 | if (type >= OP_INPUT_ARG && type <= OP_EXEC_MODE_ARG) { |
| 327 | return argLists[type].args && argLists[type].count; | 335 | return argLists[type].args && argLists[type].count; |
| @@ -0,0 +1,3 @@ | |||
| 1 | +/build/ | ||
| 2 | +/prof_out/ | ||
| 3 | +/opprof_out/ | ||
| @@ -0,0 +1,87 @@ | |||
| 1 | +# ---------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This file is a part of the CANN Open Software. | ||
| 4 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# ---------------------------------------------------------------------------- | ||
| 6 | + | ||
| 7 | +cmake_minimum_required(VERSION 3.16) | ||
| 8 | + | ||
| 9 | +# Target NPU architecture: dav-3510 (Ascend950) or dav-2201 (Ascend910B). | ||
| 10 | +if(NOT DEFINED NPU_ARCH OR NPU_ARCH STREQUAL "") | ||
| 11 | + set(NPU_ARCH dav-3510) | ||
| 12 | +endif() | ||
| 13 | + | ||
| 14 | +# Locate the Ascend toolkit. | ||
| 15 | +if(DEFINED ENV{ASCEND_HOME_PATH}) | ||
| 16 | + set(ASCEND_DIR $ENV{ASCEND_HOME_PATH}) | ||
| 17 | +elseif(EXISTS /usr/local/Ascend/ascend-toolkit/latest) | ||
| 18 | + set(ASCEND_DIR /usr/local/Ascend/ascend-toolkit/latest) | ||
| 19 | +elseif(EXISTS $ENV{HOME}/Ascend/ascend-toolkit/latest) | ||
| 20 | + set(ASCEND_DIR $ENV{HOME}/Ascend/ascend-toolkit/latest) | ||
| 21 | +else() | ||
| 22 | + message(FATAL_ERROR "Ascend toolkit not found. Please set ASCEND_HOME_PATH.") | ||
| 23 | +endif() | ||
| 24 | +message(STATUS "Using Ascend toolkit: ${ASCEND_DIR}") | ||
| 25 | + | ||
| 26 | +execute_process(COMMAND uname -m OUTPUT_VARIABLE _arch OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
| 27 | +set(SYSTEM_PREFIX "${_arch}-linux") | ||
| 28 | + | ||
| 29 | +# Use bisheng for every language, same as ops-samples, so <<<>>> in .asc files | ||
| 30 | +# and plain .cpp files link cleanly together. | ||
| 31 | +set(BISHENG "${ASCEND_DIR}/${SYSTEM_PREFIX}/ccec_compiler/bin/bisheng" CACHE FILEPATH "Bisheng compiler") | ||
| 32 | +set(CMAKE_C_COMPILER "${BISHENG}" CACHE FILEPATH "C compiler" FORCE) | ||
| 33 | +set(CMAKE_CXX_COMPILER "${BISHENG}" CACHE FILEPATH "CXX compiler" FORCE) | ||
| 34 | +set(CMAKE_LINKER "${BISHENG}" CACHE FILEPATH "Linker" FORCE) | ||
| 35 | + | ||
| 36 | +list(APPEND CMAKE_MODULE_PATH "${ASCEND_DIR}/${SYSTEM_PREFIX}/tikcpp/ascendc_kernel_cmake") | ||
| 37 | +find_package(ASC) | ||
| 38 | +project(external_op_axpy LANGUAGES C CXX ASC) | ||
| 39 | + | ||
| 40 | +set(CMAKE_CXX_STANDARD 17) | ||
| 41 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
| 42 | + | ||
| 43 | +# opbase: source-tree headers + freshly built production libnnopbase.so. | ||
| 44 | +get_filename_component(OPBASE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE) | ||
| 45 | +set(OPBASE_LIB_DIR "${OPBASE_ROOT}/build-production/src/nnopbase" CACHE PATH "Directory of libnnopbase.so") | ||
| 46 | +if(NOT EXISTS "${OPBASE_LIB_DIR}/libnnopbase.so") | ||
| 47 | + message(FATAL_ERROR "libnnopbase.so not found in ${OPBASE_LIB_DIR}. " | ||
| 48 | + "Build opbase production first (build.sh --production or cmake build-production).") | ||
| 49 | +endif() | ||
| 50 | + | ||
| 51 | +add_executable(external_op_axpy | ||
| 52 | + main.cpp | ||
| 53 | + aclnn_axpy.cpp | ||
| 54 | + axpy_l0.cpp | ||
| 55 | + axpy_kernel.asc | ||
| 56 | +) | ||
| 57 | + | ||
| 58 | +target_include_directories(external_op_axpy PRIVATE | ||
| 59 | + "${CMAKE_CURRENT_SOURCE_DIR}" | ||
| 60 | + "${OPBASE_ROOT}/include/nnopbase" | ||
| 61 | + "${ASCEND_DIR}/${SYSTEM_PREFIX}/include" | ||
| 62 | + "${ASCEND_DIR}/${SYSTEM_PREFIX}/include/ascendc" | ||
| 63 | +) | ||
| 64 | + | ||
| 65 | +# libnnopbase.so is built with the old GLIBCXX ABI; match it. | ||
| 66 | +target_compile_options(external_op_axpy PRIVATE | ||
| 67 | + -D_GLIBCXX_USE_CXX11_ABI=0 | ||
| 68 | + "$<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${NPU_ARCH}>" | ||
| 69 | + "$<$<COMPILE_LANGUAGE:ASC>:-O3>" | ||
| 70 | +) | ||
| 71 | + | ||
| 72 | +target_link_directories(external_op_axpy PRIVATE | ||
| 73 | + "${OPBASE_LIB_DIR}" | ||
| 74 | + "${ASCEND_DIR}/${SYSTEM_PREFIX}/lib64" | ||
| 75 | + "${ASCEND_DIR}/${SYSTEM_PREFIX}/devlib/${SYSTEM_PREFIX}" | ||
| 76 | +) | ||
| 77 | + | ||
| 78 | +target_link_libraries(external_op_axpy PRIVATE | ||
| 79 | + nnopbase | ||
| 80 | + ascendcl | ||
| 81 | + ge_common | ||
| 82 | + graph | ||
| 83 | +) | ||
| 84 | + | ||
| 85 | +set_target_properties(external_op_axpy PROPERTIES | ||
| 86 | + BUILD_RPATH "${OPBASE_LIB_DIR}:${ASCEND_DIR}/${SYSTEM_PREFIX}/lib64" | ||
| 87 | +) | ||
| @@ -0,0 +1,127 @@ | |||
| 1 | +# external_op_axpy 样例 | ||
| 2 | + | ||
| 3 | +演示把一个 AscendC `<<<>>>` 直调 kernel 通过 ExternalOp 薄 Adapter | ||
| 4 | +(样例目录内的 `external_op_launch.h`)接入 opbase 两段式(aclnn 风格)执行框架, | ||
| 5 | +编程界面与 ops-math 仓的常规 AscendC 算子一致,并用它验证 ExternalOp 的 | ||
| 6 | +功能完整性与 DFX 能力。 | ||
| 7 | + | ||
| 8 | +算子语义:`z = x * scale + y`(`scale` 为属性,用于演示属性传递)。 | ||
| 9 | + | ||
| 10 | +## 目录结构 | ||
| 11 | + | ||
| 12 | +``` | ||
| 13 | +external_op_axpy/ | ||
| 14 | +├── CMakeLists.txt # 独立构建(bisheng + ASC 语言,同 ops-samples 组织方式) | ||
| 15 | +├── external_op_launch.h # 独立 producer adapter:注册与参数访问 | ||
| 16 | +├── axpy_kernel.asc # AscendC kernel + 一个 launch 函数(唯一需要手写的部分) | ||
| 17 | +├── axpy_l0.cpp # l0op host_api:OP_TYPE_REGISTER + L0_DFX + | ||
| 18 | +│ # ADD_EXTERNAL_LAUNCH_LIST_AICORE(对齐 ops-math 写法) | ||
| 19 | +├── aclnn_axpy.cpp # L2 两段式入口:L2_DFX_PHASE_1 + CREATE_EXECUTOR + l0op 调用 | ||
| 20 | +├── aclnn_axpy.h | ||
| 21 | +├── main.cpp # aclInit -> 两段式调用 -> 结果校验;传入 dump 配置则使能 dump | ||
| 22 | +├── dump_config.json # aclmdlSetDump 的 dump 配置(L0 dump 验证用) | ||
| 23 | +└── run.sh # 构建并运行 | ||
| 24 | +``` | ||
| 25 | + | ||
| 26 | +## 与常规算子的编程界面对照 | ||
| 27 | + | ||
| 28 | +内置 AscendC 算子(ops-math l0op host_api): | ||
| 29 | + | ||
| 30 | +```cpp | ||
| 31 | +OP_TYPE_REGISTER(Xxx); | ||
| 32 | +const aclTensor* XxxAiCore(const aclTensor* in, aclTensor* out, aclOpExecutor* executor) | ||
| 33 | +{ | ||
| 34 | + L0_DFX(XxxAiCore, in); | ||
| 35 | + ADD_TO_LAUNCHER_LIST_AICORE(Xxx, OP_INPUT(in), OP_OUTPUT(out)); | ||
| 36 | + return out; | ||
| 37 | +} | ||
| 38 | +``` | ||
| 39 | + | ||
| 40 | +<<<>>> kernel 经 Adapter 接入(本样例 axpy_l0.cpp): | ||
| 41 | + | ||
| 42 | +```cpp | ||
| 43 | +OP_TYPE_REGISTER(Axpy); | ||
| 44 | +const aclTensor* AxpyAiCore(const aclTensor* x, const aclTensor* y, double scale, aclTensor* out, | ||
| 45 | + aclOpExecutor* executor) | ||
| 46 | +{ | ||
| 47 | + L0_DFX(AxpyAiCore, x, y, scale); | ||
| 48 | + ADD_EXTERNAL_LAUNCH_LIST_AICORE(Axpy, AxpyLaunch, OP_INPUT(x, y), OP_OUTPUT(out), OP_ATTR(scale)); | ||
| 49 | + return out; | ||
| 50 | +} | ||
| 51 | +``` | ||
| 52 | + | ||
| 53 | +唯一区别是宏换成 `ADD_EXTERNAL_LAUNCH_LIST_*` 并多传一个在 `.asc` 里实现的 | ||
| 54 | +launch 函数(`aclnnStatus Fn(const op::OpArgContext* args, aclrtStream stream)`), | ||
| 55 | +prepare/destroy 样板由 Adapter 以编译期绑定(NTTP 模板)方式提供,零运行时开销。 | ||
| 56 | +Adapter 还提供 `op::GetExternalKernelInput/Output(args, i)` 取 tensor。 | ||
| 57 | +该 adapter 是 producer 示例模块,不属于 opbase 安装头。 | ||
| 58 | +需要 workspace 或 prepare 态的任务不走 Adapter,直接用 `AddExternalOpTask` 全量接口。 | ||
| 59 | + | ||
| 60 | +## 参数边界 | ||
| 61 | + | ||
| 62 | +opbase launch 仅接收 `launchState`、`args`、`stream`;无状态 Adapter 的用户函数只接收 | ||
| 63 | +`args` 和 `stream`。prepare 负责 Host 侧参数校验、workspace 大小计算和 launchState 构造。 | ||
| 64 | +当前不提供 producer DFX 关联信息或逐 dispatch profiling helper;后续通过独立 opbase | ||
| 65 | +公共接口按需查询,不向执行参数添加 DFX 类型。ABI v1 要求 producer 与 opbase 同步重编译。 | ||
| 66 | + | ||
| 67 | +## 构建与运行 | ||
| 68 | + | ||
| 69 | +前提: | ||
| 70 | +- 已安装 CANN toolkit(提供 bisheng、libascendcl),设置 `ASCEND_HOME_PATH` | ||
| 71 | + 或 toolkit 位于默认路径; | ||
| 72 | +- 已构建 opbase production 库(`build-production/src/nnopbase/libnnopbase.so`)。 | ||
| 73 | + | ||
| 74 | +```bash | ||
| 75 | +# NPU_ARCH: dav-3510(Ascend950)或 dav-2201(Ascend910B),默认 dav-3510 | ||
| 76 | +cmake -S . -B build -DNPU_ARCH=dav-3510 | ||
| 77 | +cmake --build build -j | ||
| 78 | +./run.sh # 或者直接 LD_LIBRARY_PATH 设置后运行 ./build/external_op_axpy | ||
| 79 | +``` | ||
| 80 | + | ||
| 81 | +成功时输出: | ||
| 82 | + | ||
| 83 | +``` | ||
| 84 | +[PASS] external_op_axpy: z = x * 3.000000 + y verified on 4096 elements | ||
| 85 | +``` | ||
| 86 | + | ||
| 87 | +## DFX 能力验证 | ||
| 88 | + | ||
| 89 | +opbase 负责 L0 dump、上下文恢复和 External launch 回调的 | ||
| 90 | +Host 执行边界。producer 逐 dispatch profiling 待独立查询接口提供后实现。 | ||
| 91 | + | ||
| 92 | +### L0 dump(输入/输出 tensor) | ||
| 93 | + | ||
| 94 | +```bash | ||
| 95 | +mkdir -p /tmp/axpy_dump # dump_path 必须预先存在 | ||
| 96 | +./build/external_op_axpy "$(pwd)/dump_config.json" | ||
| 97 | +``` | ||
| 98 | + | ||
| 99 | +在 `dump_config.json` 的 `dump_path` 下生成 L0 dump 文件: | ||
| 100 | + | ||
| 101 | +``` | ||
| 102 | +/tmp/axpy_dump/<timestamp>/0/aclnnAxpy_0_L0.Axpy.* # 输入(x=1.0, y=2.0) 与 输出(z=5.0) | ||
| 103 | +``` | ||
| 104 | + | ||
| 105 | +### exception dump / overflow dump | ||
| 106 | + | ||
| 107 | +当前 External launcher 不登记设备异常信息;异常登记留待后续真实 dispatch 公共接口。 | ||
| 108 | +计算任务仍保留现有框架 overflow 检查,可通过对应 dump 配置启用。 | ||
| 109 | + | ||
| 110 | +### profiling(框架 Host 边界) | ||
| 111 | + | ||
| 112 | +```bash | ||
| 113 | +# 构建、校验并确保被采集进程加载当前 workspace 的 libnnopbase.so | ||
| 114 | +./profile.sh | ||
| 115 | +``` | ||
| 116 | + | ||
| 117 | +`profile.sh` 显式注入 workspace 库路径,检查被采集应用输出 `[PASS]`,以及最新结果 | ||
| 118 | +包含 `aclnnAxpy_Axpy_Axpy` 框架 summary 名称。这仅检查应用成功与框架名称输出,不验证 Host 时间线或设备任务元数据。 | ||
| 119 | +当前不提供 producer 逐 dispatch profiling、basic/tensor/attr info 或 cache 元数据。修改 ExternalOp ABI 后须先重建 `build-production` | ||
| 120 | +中的 `nnopbase` target,再重新构建示例。 | ||
| 121 | + | ||
| 122 | +## 已知边界 | ||
| 123 | + | ||
| 124 | +- launch 回调对框架是黑盒,设备任务 DFX 留待后续独立接口接入。 | ||
| 125 | +- `<<<>>>` 必须使用回调传入的 `stream`。 | ||
| 126 | +- Host 类任务有公共 External callback Host 区间,但不产生 L0 dump 或 compute-task | ||
| 127 | + addition info。 | ||
| @@ -0,0 +1,54 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file aclnn_axpy.cpp | ||
| 13 | + * \brief aclnn-style two-phase entry for axpy, organized exactly like the | ||
| 14 | + * aclnn ops in ops-math: L2_DFX_PHASE_1 + CREATE_EXECUTOR + l0op call. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include "aclnn_axpy.h" | ||
| 18 | + | ||
| 19 | +#include "opdev/make_op_executor.h" | ||
| 20 | +#include "opdev/op_dfx.h" | ||
| 21 | +#include "opdev/op_executor.h" | ||
| 22 | +#include "opdev/op_log.h" | ||
| 23 | + | ||
| 24 | +namespace l0op { | ||
| 25 | +const aclTensor* AxpyAiCore(const aclTensor* x, const aclTensor* y, double scale, aclTensor* out, | ||
| 26 | + aclOpExecutor* executor); | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +extern "C" { | ||
| 30 | + | ||
| 31 | +aclnnStatus aclnnAxpyGetWorkspaceSize(const aclTensor* x, const aclTensor* y, double scale, aclTensor* z, | ||
| 32 | + uint64_t* workspaceSize, aclOpExecutor** executor) | ||
| 33 | +{ | ||
| 34 | + L2_DFX_PHASE_1(aclnnAxpy, DFX_IN(x, y, scale), DFX_OUT(z)); | ||
| 35 | + auto uniqueExecutor = CREATE_EXECUTOR(); | ||
| 36 | + CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER); | ||
| 37 | + if (x == nullptr || y == nullptr || z == nullptr) { | ||
| 38 | + OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "x, y and z must not be null."); | ||
| 39 | + return ACLNN_ERR_PARAM_NULLPTR; | ||
| 40 | + } | ||
| 41 | + CHECK_RET(l0op::AxpyAiCore(x, y, scale, z, uniqueExecutor.get()) != nullptr, ACLNN_ERR_INNER); | ||
| 42 | + | ||
| 43 | + *workspaceSize = uniqueExecutor->GetWorkspaceSize(); | ||
| 44 | + uniqueExecutor.ReleaseTo(executor); | ||
| 45 | + return ACLNN_SUCCESS; | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +aclnnStatus aclnnAxpy(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream) | ||
| 49 | +{ | ||
| 50 | + L2_DFX_PHASE_2(aclnnAxpy); | ||
| 51 | + return CommonOpExecutorRun(workspace, workspaceSize, executor, stream); | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +} // extern "C" | ||
| @@ -0,0 +1,37 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file aclnn_axpy.h | ||
| 13 | + * \brief aclnn-style two-phase API for the ExternalOp axpy sample: z = x * scale + y. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef EXTERNAL_OP_AXPY_ACLNN_AXPY_H_ | ||
| 17 | +#define EXTERNAL_OP_AXPY_ACLNN_AXPY_H_ | ||
| 18 | + | ||
| 19 | +#include "acl/acl_base_rt.h" | ||
| 20 | +#include "aclnn/acl_meta.h" | ||
| 21 | +#include "aclnn/aclnn_base.h" | ||
| 22 | +#include "opdev/op_errno.h" | ||
| 23 | + | ||
| 24 | +#ifdef __cplusplus | ||
| 25 | +extern "C" { | ||
| 26 | +#endif | ||
| 27 | + | ||
| 28 | +aclnnStatus aclnnAxpyGetWorkspaceSize(const aclTensor* x, const aclTensor* y, double scale, aclTensor* z, | ||
| 29 | + uint64_t* workspaceSize, aclOpExecutor** executor); | ||
| 30 | + | ||
| 31 | +aclnnStatus aclnnAxpy(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream); | ||
| 32 | + | ||
| 33 | +#ifdef __cplusplus | ||
| 34 | +} | ||
| 35 | +#endif | ||
| 36 | + | ||
| 37 | +#endif // EXTERNAL_OP_AXPY_ACLNN_AXPY_H_ | ||
| @@ -0,0 +1,99 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file axpy_kernel.asc | ||
| 13 | + * \brief AscendC <<<>>> kernel plus its launch function. | ||
| 14 | + * | ||
| 15 | + * Through external_op_launch.h this is the ONLY file the kernel writer has to | ||
| 16 | + * provide: the ExternalOpTask prepare/destroy boilerplate is supplied by the | ||
| 17 | + * adapter. The launch function must live in an .asc/.cce file because it | ||
| 18 | + * contains the <<<>>> launch. | ||
| 19 | + */ | ||
| 20 | + | ||
| 21 | +#include "kernel_operator.h" | ||
| 22 | + | ||
| 23 | +#include "external_op_launch.h" | ||
| 24 | + | ||
| 25 | +namespace { | ||
| 26 | + | ||
| 27 | +constexpr uint32_t kNumBlocks = 8; | ||
| 28 | + | ||
| 29 | +// z[i] = x[i] * scale + y[i]. Each block handles one contiguous tile of | ||
| 30 | +// blockLength elements through the standard TPipe/LocalTensor flow. | ||
| 31 | +// Constraint: blockLength must stay 32B-aligned for DataCopy. | ||
| 32 | +template <typename T> | ||
| 33 | +__global__ __vector__ void axpy_kernel(GM_ADDR x, GM_ADDR y, GM_ADDR z, T scale, int64_t totalLength, | ||
| 34 | + int64_t blockLength) | ||
| 35 | +{ | ||
| 36 | + AscendC::TPipe pipe; | ||
| 37 | + AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueX; | ||
| 38 | + AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueY; | ||
| 39 | + AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueZ; | ||
| 40 | + pipe.InitBuffer(inQueX, 1, blockLength * sizeof(T)); | ||
| 41 | + pipe.InitBuffer(inQueY, 1, blockLength * sizeof(T)); | ||
| 42 | + pipe.InitBuffer(outQueZ, 1, blockLength * sizeof(T)); | ||
| 43 | + | ||
| 44 | + const int64_t offset = blockLength * static_cast<int64_t>(AscendC::GetBlockIdx()); | ||
| 45 | + int64_t curLen = totalLength - offset; | ||
| 46 | + curLen = curLen > blockLength ? blockLength : curLen; | ||
| 47 | + if (curLen <= 0) { | ||
| 48 | + return; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + AscendC::GlobalTensor<T> xGm; | ||
| 52 | + AscendC::GlobalTensor<T> yGm; | ||
| 53 | + AscendC::GlobalTensor<T> zGm; | ||
| 54 | + xGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(x) + offset); | ||
| 55 | + yGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(y) + offset); | ||
| 56 | + zGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(z) + offset); | ||
| 57 | + | ||
| 58 | + AscendC::LocalTensor<T> xLocal = inQueX.AllocTensor<T>(); | ||
| 59 | + AscendC::LocalTensor<T> yLocal = inQueY.AllocTensor<T>(); | ||
| 60 | + AscendC::DataCopy(xLocal, xGm, curLen); | ||
| 61 | + AscendC::DataCopy(yLocal, yGm, curLen); | ||
| 62 | + inQueX.EnQue(xLocal); | ||
| 63 | + inQueY.EnQue(yLocal); | ||
| 64 | + | ||
| 65 | + xLocal = inQueX.DeQue<T>(); | ||
| 66 | + yLocal = inQueY.DeQue<T>(); | ||
| 67 | + AscendC::LocalTensor<T> zLocal = outQueZ.AllocTensor<T>(); | ||
| 68 | + AscendC::Muls(zLocal, xLocal, scale, curLen); | ||
| 69 | + AscendC::Add(zLocal, zLocal, yLocal, curLen); | ||
| 70 | + outQueZ.EnQue(zLocal); | ||
| 71 | + inQueX.FreeTensor(xLocal); | ||
| 72 | + inQueY.FreeTensor(yLocal); | ||
| 73 | + | ||
| 74 | + zLocal = outQueZ.DeQue<T>(); | ||
| 75 | + AscendC::DataCopy(zGm, zLocal, curLen); | ||
| 76 | + outQueZ.FreeTensor(zLocal); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +} // namespace | ||
| 80 | + | ||
| 81 | +// Registered through ADD_EXTERNAL_LAUNCH_LIST_AICORE(Axpy, AxpyLaunch, ...) in | ||
| 82 | +// axpy_l0.cpp. Device addresses are only read from args here, never cached. | ||
| 83 | +aclnnStatus AxpyLaunch(const op::OpArgContext* args, aclrtStream stream) noexcept | ||
| 84 | +{ | ||
| 85 | + aclTensor* x = op::GetExternalKernelInput(args, 0U); | ||
| 86 | + aclTensor* y = op::GetExternalKernelInput(args, 1U); | ||
| 87 | + aclTensor* z = op::GetExternalKernelOutput(args, 0U); | ||
| 88 | + const op::OpArgList* attrs = args == nullptr ? nullptr : args->GetOpArg(op::OP_ATTR_ARG); | ||
| 89 | + if (x == nullptr || y == nullptr || z == nullptr || attrs == nullptr || attrs->count != 1U) { | ||
| 90 | + return ACLNN_ERR_PARAM_NULLPTR; | ||
| 91 | + } | ||
| 92 | + const float scale = static_cast<float>(attrs->args[0].value.data.dvalue); | ||
| 93 | + const int64_t totalLength = x->GetViewShape().GetShapeSize(); | ||
| 94 | + const int64_t blockLength = (totalLength + kNumBlocks - 1) / kNumBlocks; | ||
| 95 | + | ||
| 96 | + axpy_kernel<float><<<kNumBlocks, nullptr, stream>>>((GM_ADDR)x->GetData(), (GM_ADDR)y->GetData(), | ||
| 97 | + (GM_ADDR)z->GetData(), scale, totalLength, blockLength); | ||
| 98 | + return ACLNN_SUCCESS; | ||
| 99 | +} | ||
| @@ -0,0 +1,38 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file axpy_l0.cpp | ||
| 13 | + * \brief L0 host_api for axpy, same shape as a built-in AscendC op in | ||
| 14 | + * ops-math (OP_TYPE_REGISTER + L0_DFX + ADD_TO_LAUNCHER_LIST_AICORE), | ||
| 15 | + * except the kernel launch list is registered through the ExternalOp | ||
| 16 | + * adapter macro ADD_EXTERNAL_LAUNCH_LIST_AICORE. | ||
| 17 | + */ | ||
| 18 | + | ||
| 19 | +#include "opdev/op_dfx.h" | ||
| 20 | +#include "opdev/op_executor.h" | ||
| 21 | +#include "external_op_launch.h" | ||
| 22 | + | ||
| 23 | +// Implemented in axpy_kernel.asc (contains <<<>>>). | ||
| 24 | +aclnnStatus AxpyLaunch(const op::OpArgContext* args, aclrtStream stream) noexcept; | ||
| 25 | + | ||
| 26 | +namespace l0op { | ||
| 27 | + | ||
| 28 | +OP_TYPE_REGISTER(Axpy); | ||
| 29 | + | ||
| 30 | +const aclTensor* AxpyAiCore(const aclTensor* x, const aclTensor* y, double scale, aclTensor* out, | ||
| 31 | + aclOpExecutor* executor) | ||
| 32 | +{ | ||
| 33 | + L0_DFX(AxpyAiCore, x, y, scale); | ||
| 34 | + ADD_EXTERNAL_LAUNCH_LIST_AICORE(Axpy, AxpyLaunch, OP_INPUT(x, y), OP_OUTPUT(out), OP_ATTR(scale)); | ||
| 35 | + return out; | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +} // namespace l0op | ||
| @@ -0,0 +1,10 @@ | |||
| 1 | +{ | ||
| 2 | + "dump": { | ||
| 3 | + "dump_path": "/tmp/axpy_dump", | ||
| 4 | + "dump_mode": "all", | ||
| 5 | + "dump_op_switch": "on", | ||
| 6 | + "dump_list": [ | ||
| 7 | + { "model_name": "external_op_axpy" } | ||
| 8 | + ] | ||
| 9 | + } | ||
| 10 | +} | ||
| @@ -0,0 +1,158 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file external_op_launch.h | ||
| 13 | + * \brief Thin adapter between user kernels and ExternalOpTask. | ||
| 14 | + * | ||
| 15 | + * Lets a kernel written with <<<>>> plug into the aclnn two-phase flow with | ||
| 16 | + * the same programming interface as a built-in AscendC op: the user writes a | ||
| 17 | + * single launch function and registers it with one ADD_EXTERNAL_LAUNCH_LIST_XXX | ||
| 18 | + * macro, while the adapter supplies the prepare/destroy boilerplate of | ||
| 19 | + * ExternalOpTask. | ||
| 20 | + * | ||
| 21 | + * Scope: stateless tasks without workspace. Shapes and addresses are read from | ||
| 22 | + * the args passed to the launch function, so no executor-local state is needed. | ||
| 23 | + * Tasks that need workspace buffers or prepared state use AddExternalOpTask | ||
| 24 | + * directly. | ||
| 25 | + */ | ||
| 26 | + | ||
| 27 | +#ifndef SAMPLES_EXTERNAL_OP_AXPY_EXTERNAL_OP_LAUNCH_H_ | ||
| 28 | +#define SAMPLES_EXTERNAL_OP_AXPY_EXTERNAL_OP_LAUNCH_H_ | ||
| 29 | + | ||
| 30 | +#include "opdev/external_op_task.h" | ||
| 31 | +#include "opdev/op_arg_def.h" | ||
| 32 | +#include "opdev/op_executor.h" | ||
| 33 | +#include "opdev/op_log.h" | ||
| 34 | + | ||
| 35 | +namespace op { | ||
| 36 | + | ||
| 37 | +/** | ||
| 38 | + * Launch signature for adapter-integrated kernels. args carries the final | ||
| 39 | + * input/output/attribute arguments; tensor device addresses must be read here | ||
| 40 | + * (never cached). stream is the stream supplied to CommonOpExecutorRun; all | ||
| 41 | + * device work must be enqueued on it. Typically implemented in a .asc/.cce | ||
| 42 | + * file when it contains <<<>>>. | ||
| 43 | + */ | ||
| 44 | +using ExternalKernelLaunchFn = aclnnStatus (*)(const OpArgContext* args, aclrtStream stream) noexcept; | ||
| 45 | + | ||
| 46 | +// Fetch the index-th tensor of one arg kind, or nullptr when absent. | ||
| 47 | +inline aclTensor* GetExternalKernelTensor(const OpArgContext* args, OpArgDef argType, uint64_t index) | ||
| 48 | +{ | ||
| 49 | + if (args == nullptr) { | ||
| 50 | + return nullptr; | ||
| 51 | + } | ||
| 52 | + const OpArgList* list = args->GetOpArg(argType); | ||
| 53 | + if (list == nullptr || index >= list->count) { | ||
| 54 | + return nullptr; | ||
| 55 | + } | ||
| 56 | + const OpArg& arg = list->args[index]; | ||
| 57 | + if (arg.type != OpArgType::OPARG_ACLTENSOR) { | ||
| 58 | + return nullptr; | ||
| 59 | + } | ||
| 60 | + return static_cast<aclTensor*>(arg.value.data.pointer); | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +inline aclTensor* GetExternalKernelInput(const OpArgContext* args, uint64_t index) | ||
| 64 | +{ | ||
| 65 | + return GetExternalKernelTensor(args, OP_INPUT_ARG, index); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +inline aclTensor* GetExternalKernelOutput(const OpArgContext* args, uint64_t index) | ||
| 69 | +{ | ||
| 70 | + return GetExternalKernelTensor(args, OP_OUTPUT_ARG, index); | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +namespace internal { | ||
| 74 | + | ||
| 75 | +template <ExternalKernelLaunchFn Fn> | ||
| 76 | +aclnnStatus ExternalKernelPrepareAdaptor(const void* taskConfig, const OpArgContext* args, | ||
| 77 | + ExternalOpWorkspaceRequest* workspaceRequest, | ||
| 78 | + void** launchState) noexcept | ||
| 79 | +{ | ||
| 80 | + (void)taskConfig; | ||
| 81 | + (void)args; | ||
| 82 | + if (workspaceRequest == nullptr || launchState == nullptr) { | ||
| 83 | + return ACLNN_ERR_PARAM_NULLPTR; | ||
| 84 | + } | ||
| 85 | + workspaceRequest->count = 0U; | ||
| 86 | + *launchState = nullptr; | ||
| 87 | + return ACLNN_SUCCESS; | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +template <ExternalKernelLaunchFn Fn> | ||
| 91 | +aclnnStatus ExternalKernelLaunchAdaptor(void* launchState, const OpArgContext* args, aclrtStream stream) noexcept | ||
| 92 | +{ | ||
| 93 | + (void)launchState; // stateless: the launch fn is bound at compile time | ||
| 94 | + if (args == nullptr) { | ||
| 95 | + return ACLNN_ERR_PARAM_NULLPTR; | ||
| 96 | + } | ||
| 97 | + return Fn(args, stream); | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +inline void ExternalKernelDestroyAdaptor(void* launchState) noexcept | ||
| 101 | +{ | ||
| 102 | + (void)launchState; // stateless, nothing to release | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +} // namespace internal | ||
| 106 | + | ||
| 107 | +/** | ||
| 108 | + * Register a stateless <<<>>> kernel as an external task on the executor. | ||
| 109 | + * | ||
| 110 | + * Fn is the user launch function, bound at compile time. args is consumed on | ||
| 111 | + * every return path, same as AddExternalOpTask. | ||
| 112 | + */ | ||
| 113 | +template <ExternalKernelLaunchFn Fn> | ||
| 114 | +inline aclnnStatus AddExternalKernelLaunch(const char* l0Name, uint32_t opType, aclOpExecutor* executor, | ||
| 115 | + OpArgContext* args, | ||
| 116 | + ExternalOpEngineKind engineKind = ExternalOpEngineKind::AiCore, | ||
| 117 | + ExternalOpCachePolicy cachePolicy = ExternalOpCachePolicy::DisableRepeat) | ||
| 118 | +{ | ||
| 119 | + ExternalOpTask task{}; | ||
| 120 | + task.structSize = sizeof(ExternalOpTask); | ||
| 121 | + task.abiVersion = EXTERNAL_OP_TASK_ABI_VERSION; | ||
| 122 | + task.engineKind = engineKind; | ||
| 123 | + task.cachePolicy = cachePolicy; | ||
| 124 | + task.taskConfig = nullptr; | ||
| 125 | + task.prepare = &internal::ExternalKernelPrepareAdaptor<Fn>; | ||
| 126 | + task.launch = &internal::ExternalKernelLaunchAdaptor<Fn>; | ||
| 127 | + task.destroy = &internal::ExternalKernelDestroyAdaptor; | ||
| 128 | + return AddExternalOpTask(l0Name, opType, &task, executor, args); | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +} // namespace op | ||
| 132 | + | ||
| 133 | +// Mirrors ADD_TO_LAUNCHER_LIST_AICORE in make_op_executor.h: the executor | ||
| 134 | +// variable comes from the enclosing aclnn/l0op function scope. | ||
| 135 | +#define ADD_EXTERNAL_LAUNCH_LIST(KERNEL_NAME, LAUNCH_FN, KIND, op_args...) \ | ||
| 136 | + ({ \ | ||
| 137 | + aclnnStatus addExternalRet; \ | ||
| 138 | + do { \ | ||
| 139 | + op::OpArgContext* opArgCtx = op::GetOpArgContext(op_args); \ | ||
| 140 | + addExternalRet = op::AddExternalKernelLaunch<LAUNCH_FN>(#KERNEL_NAME, KERNEL_NAME##OpTypeId(), \ | ||
| 141 | + executor, opArgCtx, KIND); \ | ||
| 142 | + } while (0); \ | ||
| 143 | + addExternalRet; \ | ||
| 144 | + }) | ||
| 145 | + | ||
| 146 | +#define ADD_EXTERNAL_LAUNCH_LIST_AICORE(KERNEL_NAME, LAUNCH_FN, op_args...) \ | ||
| 147 | + ADD_EXTERNAL_LAUNCH_LIST(KERNEL_NAME, LAUNCH_FN, op::ExternalOpEngineKind::AiCore, op_args) | ||
| 148 | + | ||
| 149 | +#define ADD_EXTERNAL_LAUNCH_LIST_AICPU(KERNEL_NAME, LAUNCH_FN, op_args...) \ | ||
| 150 | + ADD_EXTERNAL_LAUNCH_LIST(KERNEL_NAME, LAUNCH_FN, op::ExternalOpEngineKind::AiCpu, op_args) | ||
| 151 | + | ||
| 152 | +#define ADD_EXTERNAL_LAUNCH_LIST_DVPP(KERNEL_NAME, LAUNCH_FN, op_args...) \ | ||
| 153 | + ADD_EXTERNAL_LAUNCH_LIST(KERNEL_NAME, LAUNCH_FN, op::ExternalOpEngineKind::Dvpp, op_args) | ||
| 154 | + | ||
| 155 | +#define ADD_EXTERNAL_LAUNCH_LIST_HOST(KERNEL_NAME, LAUNCH_FN, op_args...) \ | ||
| 156 | + ADD_EXTERNAL_LAUNCH_LIST(KERNEL_NAME, LAUNCH_FN, op::ExternalOpEngineKind::Host, op_args) | ||
| 157 | + | ||
| 158 | +#endif // SAMPLES_EXTERNAL_OP_AXPY_EXTERNAL_OP_LAUNCH_H_ | ||
| @@ -0,0 +1,142 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file main.cpp | ||
| 13 | + * \brief Runs z = x * scale + y on device through the ExternalOp-based aclnnAxpy | ||
| 14 | + * two-phase API and verifies the result on host. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include <cmath> | ||
| 18 | +#include <cstdint> | ||
| 19 | +#include <cstdio> | ||
| 20 | +#include <vector> | ||
| 21 | + | ||
| 22 | +#include "acl/acl.h" | ||
| 23 | +#include "aclnn/acl_meta.h" | ||
| 24 | + | ||
| 25 | +#include "aclnn_axpy.h" | ||
| 26 | + | ||
| 27 | +namespace { | ||
| 28 | + | ||
| 29 | +constexpr int64_t kNumElements = 4096; | ||
| 30 | +constexpr double kScale = 3.0; | ||
| 31 | +constexpr float kXValue = 1.0F; | ||
| 32 | +constexpr float kYValue = 2.0F; | ||
| 33 | +// expected: z = 1 * 3 + 2 = 5 | ||
| 34 | +constexpr float kExpected = kXValue * static_cast<float>(kScale) + kYValue; | ||
| 35 | + | ||
| 36 | +#define CHECK_ACL(expr) \ | ||
| 37 | + do { \ | ||
| 38 | + aclError _ret = (expr); \ | ||
| 39 | + if (_ret != ACL_SUCCESS) { \ | ||
| 40 | + std::fprintf(stderr, "[FAIL] %s ret=%d\n", #expr, (int)_ret); \ | ||
| 41 | + return 1; \ | ||
| 42 | + } \ | ||
| 43 | + } while (0) | ||
| 44 | + | ||
| 45 | +#define CHECK_ACLNN(expr) \ | ||
| 46 | + do { \ | ||
| 47 | + aclnnStatus _ret = (expr); \ | ||
| 48 | + if (_ret != ACLNN_SUCCESS) { \ | ||
| 49 | + std::fprintf(stderr, "[FAIL] %s ret=%d\n", #expr, (int)_ret); \ | ||
| 50 | + return 1; \ | ||
| 51 | + } \ | ||
| 52 | + } while (0) | ||
| 53 | + | ||
| 54 | +} // namespace | ||
| 55 | + | ||
| 56 | +int main(int argc, char* argv[]) | ||
| 57 | +{ | ||
| 58 | + // Optional: pass a dump config path to enable operator dump, e.g. | ||
| 59 | + // {"dump": {"dump_path": "/tmp/axpy_dump", "dump_mode": "all", | ||
| 60 | + // "dump_op_switch": "on", "dump_list": [{"model_name": "external_op_axpy"}]}} | ||
| 61 | + CHECK_ACL(aclInit(nullptr)); | ||
| 62 | + const bool dumpEnabled = argc > 1; | ||
| 63 | + if (dumpEnabled) { | ||
| 64 | + CHECK_ACL(aclmdlInitDump()); | ||
| 65 | + CHECK_ACL(aclmdlSetDump(argv[1])); | ||
| 66 | + } | ||
| 67 | + constexpr int32_t deviceId = 0; | ||
| 68 | + CHECK_ACL(aclrtSetDevice(deviceId)); | ||
| 69 | + aclrtStream stream = nullptr; | ||
| 70 | + CHECK_ACL(aclrtCreateStream(&stream)); | ||
| 71 | + | ||
| 72 | + const size_t bytes = static_cast<size_t>(kNumElements) * sizeof(float); | ||
| 73 | + std::vector<float> hostX(kNumElements, kXValue); | ||
| 74 | + std::vector<float> hostY(kNumElements, kYValue); | ||
| 75 | + std::vector<float> hostZ(kNumElements, 0.0F); | ||
| 76 | + | ||
| 77 | + void* devX = nullptr; | ||
| 78 | + void* devY = nullptr; | ||
| 79 | + void* devZ = nullptr; | ||
| 80 | + CHECK_ACL(aclrtMalloc(&devX, bytes, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 81 | + CHECK_ACL(aclrtMalloc(&devY, bytes, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 82 | + CHECK_ACL(aclrtMalloc(&devZ, bytes, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 83 | + CHECK_ACL(aclrtMemcpy(devX, bytes, hostX.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 84 | + CHECK_ACL(aclrtMemcpy(devY, bytes, hostY.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 85 | + | ||
| 86 | + const int64_t dims[] = {kNumElements}; | ||
| 87 | + aclTensor* x = aclCreateTensor(dims, 1, ACL_FLOAT, nullptr, 0, ACL_FORMAT_ND, dims, 1, devX); | ||
| 88 | + aclTensor* y = aclCreateTensor(dims, 1, ACL_FLOAT, nullptr, 0, ACL_FORMAT_ND, dims, 1, devY); | ||
| 89 | + aclTensor* z = aclCreateTensor(dims, 1, ACL_FLOAT, nullptr, 0, ACL_FORMAT_ND, dims, 1, devZ); | ||
| 90 | + if (x == nullptr || y == nullptr || z == nullptr) { | ||
| 91 | + std::fprintf(stderr, "[FAIL] aclCreateTensor failed\n"); | ||
| 92 | + return 1; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + // Phase 1: build the executor (ExternalOp prepare runs here). | ||
| 96 | + uint64_t workspaceSize = 0; | ||
| 97 | + aclOpExecutor* executor = nullptr; | ||
| 98 | + CHECK_ACLNN(aclnnAxpyGetWorkspaceSize(x, y, kScale, z, &workspaceSize, &executor)); | ||
| 99 | + void* workspace = nullptr; | ||
| 100 | + if (workspaceSize > 0U) { | ||
| 101 | + CHECK_ACL(aclrtMalloc(&workspace, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + // Phase 2: launch (ExternalOp launch callback runs here, <<<>>> inside). | ||
| 105 | + CHECK_ACLNN(aclnnAxpy(workspace, workspaceSize, executor, stream)); | ||
| 106 | + CHECK_ACL(aclrtSynchronizeStream(stream)); | ||
| 107 | + | ||
| 108 | + CHECK_ACL(aclrtMemcpy(hostZ.data(), bytes, devZ, bytes, ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 109 | + | ||
| 110 | + bool ok = true; | ||
| 111 | + for (int64_t i = 0; i < kNumElements; ++i) { | ||
| 112 | + if (std::fabs(hostZ[static_cast<size_t>(i)] - kExpected) > 1e-6F) { | ||
| 113 | + std::fprintf(stderr, "[FAIL] z[%ld] = %f, expect %f\n", static_cast<long>(i), | ||
| 114 | + static_cast<double>(hostZ[static_cast<size_t>(i)]), static_cast<double>(kExpected)); | ||
| 115 | + ok = false; | ||
| 116 | + break; | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + if (workspace != nullptr) { | ||
| 121 | + aclrtFree(workspace); | ||
| 122 | + } | ||
| 123 | + aclDestroyTensor(x); | ||
| 124 | + aclDestroyTensor(y); | ||
| 125 | + aclDestroyTensor(z); | ||
| 126 | + aclrtFree(devX); | ||
| 127 | + aclrtFree(devY); | ||
| 128 | + aclrtFree(devZ); | ||
| 129 | + aclrtDestroyStream(stream); | ||
| 130 | + if (dumpEnabled) { | ||
| 131 | + aclmdlFinalizeDump(); | ||
| 132 | + } | ||
| 133 | + aclrtResetDevice(deviceId); | ||
| 134 | + aclFinalize(); | ||
| 135 | + | ||
| 136 | + if (ok) { | ||
| 137 | + std::printf("[PASS] external_op_axpy: z = x * %f + y verified on %ld elements\n", kScale, | ||
| 138 | + static_cast<long>(kNumElements)); | ||
| 139 | + return 0; | ||
| 140 | + } | ||
| 141 | + return 1; | ||
| 142 | +} | ||
| @@ -0,0 +1,70 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This file is a part of the CANN Open Software. | ||
| 4 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + | ||
| 6 | +# Build, run, and collect profiling data with the workspace libnnopbase.so. | ||
| 7 | + | ||
| 8 | +set -euo pipefail | ||
| 9 | + | ||
| 10 | +SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) | ||
| 11 | +OPBASE_LIB_DIR="${SCRIPT_DIR}/../../build-production/src/nnopbase" | ||
| 12 | +OUTPUT_DIR=${1:-"${SCRIPT_DIR}/prof_out"} | ||
| 13 | +PROFILE_LOG=$(mktemp) | ||
| 14 | +trap 'rm -f "${PROFILE_LOG}"' EXIT | ||
| 15 | + | ||
| 16 | +"${SCRIPT_DIR}/run.sh" | ||
| 17 | + | ||
| 18 | +# msprof configures its own toolkit library paths. Prepend the workspace | ||
| 19 | +# library explicitly; otherwise the profiled process may load the installed | ||
| 20 | +# libnnopbase.so, which does not contain the ExternalOp API under development. | ||
| 21 | +export LD_LIBRARY_PATH="$(cd "${OPBASE_LIB_DIR}" && pwd):${LD_LIBRARY_PATH:-}" | ||
| 22 | + | ||
| 23 | +set +e | ||
| 24 | +msprof --application="${SCRIPT_DIR}/build/external_op_axpy" \ | ||
| 25 | + --output="${OUTPUT_DIR}" --task-time=l1 2>&1 | tee "${PROFILE_LOG}" | ||
| 26 | +MSPROF_STATUS=${PIPESTATUS[0]} | ||
| 27 | +set -e | ||
| 28 | + | ||
| 29 | +if [[ ${MSPROF_STATUS} -ne 0 ]]; then | ||
| 30 | + echo "[FAIL] msprof exited with status ${MSPROF_STATUS}." >&2 | ||
| 31 | + exit "${MSPROF_STATUS}" | ||
| 32 | +fi | ||
| 33 | + | ||
| 34 | +# Some msprof versions return success even when the application itself fails. | ||
| 35 | +if ! grep -Fq "[PASS] external_op_axpy" "${PROFILE_LOG}"; then | ||
| 36 | + echo "[FAIL] profiled application did not complete successfully." >&2 | ||
| 37 | + exit 1 | ||
| 38 | +fi | ||
| 39 | + | ||
| 40 | +shopt -s nullglob | ||
| 41 | +PROFILE_DIRS=("${OUTPUT_DIR}"/PROF_*) | ||
| 42 | +if [[ ${#PROFILE_DIRS[@]} -eq 0 ]]; then | ||
| 43 | + echo "[FAIL] msprof did not create a PROF_* directory under ${OUTPUT_DIR}." >&2 | ||
| 44 | + exit 1 | ||
| 45 | +fi | ||
| 46 | + | ||
| 47 | +LATEST_PROFILE=${PROFILE_DIRS[0]} | ||
| 48 | +for PROFILE_DIR in "${PROFILE_DIRS[@]:1}"; do | ||
| 49 | + if [[ "${PROFILE_DIR}" -nt "${LATEST_PROFILE}" ]]; then | ||
| 50 | + LATEST_PROFILE=${PROFILE_DIR} | ||
| 51 | + fi | ||
| 52 | +done | ||
| 53 | + | ||
| 54 | +# GenSummaryItemId combines the L2 API, L0 name and op type. The standalone | ||
| 55 | +# op-type hash is not emitted after removing device-task metadata reporting. | ||
| 56 | +SUMMARY_HASH_FOUND=false | ||
| 57 | +for HASH_FILE in "${LATEST_PROFILE}"/host/data/*hash_dic*.slice_*; do | ||
| 58 | + if [[ "${HASH_FILE}" != *.done ]] && grep -aFq ":aclnnAxpy_Axpy_Axpy" "${HASH_FILE}"; then | ||
| 59 | + SUMMARY_HASH_FOUND=true | ||
| 60 | + break | ||
| 61 | + fi | ||
| 62 | +done | ||
| 63 | + | ||
| 64 | +if [[ "${SUMMARY_HASH_FOUND}" != true ]]; then | ||
| 65 | + echo "[FAIL] framework profiling summary is missing in ${LATEST_PROFILE}." >&2 | ||
| 66 | + echo " Rebuild build-production/src/nnopbase/libnnopbase.so and retry." >&2 | ||
| 67 | + exit 1 | ||
| 68 | +fi | ||
| 69 | + | ||
| 70 | +echo "[PASS] application and framework profiling summary verified in ${LATEST_PROFILE}" | ||
| @@ -0,0 +1,20 @@ | |||
| 1 | +#!/bin/bash | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This file is a part of the CANN Open Software. | ||
| 4 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# | ||
| 6 | +# Build and run the external_op_axpy sample. | ||
| 7 | + | ||
| 8 | +set -e | ||
| 9 | + | ||
| 10 | +SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) | ||
| 11 | +NPU_ARCH=${NPU_ARCH:-dav-3510} | ||
| 12 | + | ||
| 13 | +cmake -S "${SCRIPT_DIR}" -B "${SCRIPT_DIR}/build" -DNPU_ARCH="${NPU_ARCH}" | ||
| 14 | +cmake --build "${SCRIPT_DIR}/build" -j | ||
| 15 | + | ||
| 16 | +# The freshly built opbase lib must win over the toolkit's libnnopbase.so. | ||
| 17 | +OPBASE_LIB_DIR="${SCRIPT_DIR}/../../build-production/src/nnopbase" | ||
| 18 | +export LD_LIBRARY_PATH="$(cd "${OPBASE_LIB_DIR}" && pwd):${LD_LIBRARY_PATH}" | ||
| 19 | + | ||
| 20 | +"${SCRIPT_DIR}/build/external_op_axpy" | ||
| @@ -183,6 +183,7 @@ | |||
| 183 | <file_info copy_type="source" src_path="" dst_path="$(TARGET_ENV)/include/aclnn/opdev" install_path="$(TARGET_ENV)/include/aclnn/opdev" install_type="all"> | 183 | <file_info copy_type="source" src_path="" dst_path="$(TARGET_ENV)/include/aclnn/opdev" install_path="$(TARGET_ENV)/include/aclnn/opdev" install_type="all"> |
| 184 | <file value="bfloat16.h" install_mod="550"/> | 184 | <file value="bfloat16.h" install_mod="550"/> |
| 185 | <file value="common_types.h" install_mod="550"/> | 185 | <file value="common_types.h" install_mod="550"/> |
| 186 | + <file value="external_op_task.h" install_mod="550"/> | ||
| 186 | <file value="data_type_utils.h" install_mod="550"/> | 187 | <file value="data_type_utils.h" install_mod="550"/> |
| 187 | <file value="fast_vector.h" install_mod="550"/> | 188 | <file value="fast_vector.h" install_mod="550"/> |
| 188 | <file value="float4_e1m2.h" install_mod="550"/> | 189 | <file value="float4_e1m2.h" install_mod="550"/> |
| @@ -0,0 +1,448 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +#include "opdev/external_op_task.h" | ||
| 12 | + | ||
| 13 | +#include <algorithm> | ||
| 14 | +#include <iterator> | ||
| 15 | +#include <limits> | ||
| 16 | +#include <new> | ||
| 17 | +#include <string> | ||
| 18 | + | ||
| 19 | +#include "bridge_graph.h" | ||
| 20 | +#include "kernel_launcher.h" | ||
| 21 | +#include "op_dfx_internal.h" | ||
| 22 | +#include "opdev/op_arg_def.h" | ||
| 23 | +#include "opdev/op_def.h" | ||
| 24 | +#include "opdev/op_dfx.h" | ||
| 25 | +#include "opdev/op_executor.h" | ||
| 26 | +#include "opdev/op_log.h" | ||
| 27 | + | ||
| 28 | +namespace op { | ||
| 29 | + | ||
| 30 | +namespace { | ||
| 31 | + | ||
| 32 | +// GetOpArg never returns null for the arg kinds used here; a malformed list | ||
| 33 | +// is one that claims entries without backing storage. | ||
| 34 | +bool IsWellFormedArgList(const OpArgList* list) | ||
| 35 | +{ | ||
| 36 | + return list != nullptr && (list->count == 0U || list->args != nullptr); | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +// launcher must own args. This function consumes launcher and args on every | ||
| 40 | +// return path: failure deletes launcher, success transfers it to executor. | ||
| 41 | +aclnnStatus RegisterKernelLauncher(uint32_t opType, aclOpExecutor* executor, OpArgContext* args, | ||
| 42 | + KernelLauncher* launcher, bool includeOutShape = false) | ||
| 43 | +{ | ||
| 44 | + if (launcher == nullptr) { | ||
| 45 | + DestroyOpArgContext(args); | ||
| 46 | + OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "Kernel launcher must not be null."); | ||
| 47 | + return ACLNN_ERR_PARAM_NULLPTR; | ||
| 48 | + } | ||
| 49 | + if (executor == nullptr || args == nullptr) { | ||
| 50 | + delete launcher; | ||
| 51 | + OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "Executor and launcher arguments must not be null."); | ||
| 52 | + return ACLNN_ERR_PARAM_NULLPTR; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + OpArgList* inputs = args->GetOpArg(OP_INPUT_ARG); | ||
| 56 | + OpArgList* outputs = args->GetOpArg(OP_OUTPUT_ARG); | ||
| 57 | + OpArgList* workspaces = args->GetOpArg(OP_WORKSPACE_ARG); | ||
| 58 | + OpArgList* outShapes = args->GetOpArg(OP_OUTSHAPE_ARG); | ||
| 59 | + if (!IsWellFormedArgList(inputs) || !IsWellFormedArgList(outputs) || !IsWellFormedArgList(workspaces) || | ||
| 60 | + (includeOutShape && !IsWellFormedArgList(outShapes))) { | ||
| 61 | + delete launcher; | ||
| 62 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Launcher graph arguments are malformed."); | ||
| 63 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + aclnnStatus status = includeOutShape ? internal::BuildGraph(executor->GetGraph(), opType, *inputs, *outputs, | ||
| 67 | + *workspaces, *outShapes) : | ||
| 68 | + internal::BuildGraph(executor->GetGraph(), opType, *inputs, *outputs, | ||
| 69 | + *workspaces); | ||
| 70 | + if (status != ACLNN_SUCCESS) { | ||
| 71 | + delete launcher; | ||
| 72 | + return status; | ||
| 73 | + } | ||
| 74 | + executor->AddToKernelLauncherList(launcher); | ||
| 75 | + return ACLNN_SUCCESS; | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +bool ResolveCoreType(ExternalOpEngineKind kind, CoreType& coreType) | ||
| 79 | +{ | ||
| 80 | + switch (kind) { | ||
| 81 | + case ExternalOpEngineKind::AiCore: | ||
| 82 | + coreType = AI_CORE; | ||
| 83 | + return true; | ||
| 84 | + case ExternalOpEngineKind::AiCpu: | ||
| 85 | + coreType = AI_CPU; | ||
| 86 | + return true; | ||
| 87 | + case ExternalOpEngineKind::Dvpp: | ||
| 88 | + coreType = DVPP; | ||
| 89 | + return true; | ||
| 90 | + case ExternalOpEngineKind::Host: | ||
| 91 | + coreType = NO_CALC; | ||
| 92 | + return true; | ||
| 93 | + default: | ||
| 94 | + return false; | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +bool IsValidCachePolicy(ExternalOpCachePolicy policy) | ||
| 99 | +{ | ||
| 100 | + switch (policy) { | ||
| 101 | + case ExternalOpCachePolicy::Keep: | ||
| 102 | + case ExternalOpCachePolicy::Abandon: | ||
| 103 | + case ExternalOpCachePolicy::DisableRepeat: | ||
| 104 | + return true; | ||
| 105 | + default: | ||
| 106 | + return false; | ||
| 107 | + } | ||
| 108 | +} | ||
| 109 | + | ||
| 110 | +bool IsValidTask(const ExternalOpTask& task, CoreType& coreType) | ||
| 111 | +{ | ||
| 112 | + return task.structSize == sizeof(ExternalOpTask) && task.abiVersion == EXTERNAL_OP_TASK_ABI_VERSION && | ||
| 113 | + IsValidCachePolicy(task.cachePolicy) && | ||
| 114 | + std::all_of(std::begin(task.reserved), std::end(task.reserved), | ||
| 115 | + [](uint64_t value) { return value == 0U; }) && | ||
| 116 | + task.prepare != nullptr && task.launch != nullptr && task.destroy != nullptr && | ||
| 117 | + ResolveCoreType(task.engineKind, coreType); | ||
| 118 | +} | ||
| 119 | + | ||
| 120 | +uint64_t MakeProfilingSummaryId(const internal::OpLogInfo& logInfo, const char* opTypeName) | ||
| 121 | +{ | ||
| 122 | + if (!internal::opProfilingSwitch.kernelLaunchFlag) { | ||
| 123 | + return 0U; | ||
| 124 | + } | ||
| 125 | + return internal::GenSummaryItemId(logInfo.l2ApiName, logInfo.l0Name, opTypeName); | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +class ExternalOpHostProfilingGuard { | ||
| 129 | +public: | ||
| 130 | + explicit ExternalOpHostProfilingGuard(uint64_t summaryId) | ||
| 131 | + { | ||
| 132 | + if ((!internal::opProfilingSwitch.reportFlag && !internal::opProfilingSwitch.kernelLaunchFlag) || | ||
| 133 | + summaryId == 0U) { | ||
| 134 | + return; | ||
| 135 | + } | ||
| 136 | + enabled_ = true; | ||
| 137 | + info_.beginTime = MsprofSysCycleTime(); | ||
| 138 | + info_.threadId = internal::OpGetTid(); | ||
| 139 | + info_.type = MSPROF_REPORT_NODE_HOST_OP_EXEC_TYPE; | ||
| 140 | + info_.magicNumber = MSPROF_REPORT_DATA_MAGIC_NUM; | ||
| 141 | + info_.reserve = 0U; | ||
| 142 | + info_.itemId = summaryId; | ||
| 143 | + info_.level = MSPROF_REPORT_NODE_LEVEL; | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + ~ExternalOpHostProfilingGuard() | ||
| 147 | + { | ||
| 148 | + if (!enabled_) { | ||
| 149 | + return; | ||
| 150 | + } | ||
| 151 | + info_.endTime = MsprofSysCycleTime(); | ||
| 152 | + const int32_t result = MsprofReportApi(1U, &info_); | ||
| 153 | + if (result != 0) { | ||
| 154 | + OP_LOGW("Failed to report ExternalOp Host execution profiling, result = %d.", result); | ||
| 155 | + } | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | +private: | ||
| 159 | + bool enabled_{false}; | ||
| 160 | + MsprofApi info_{}; | ||
| 161 | +}; | ||
| 162 | + | ||
| 163 | +// Matches the framework graph builder: a null input tensor means an absent | ||
| 164 | +// optional input and is tolerated; outputs must be present. | ||
| 165 | +enum class TensorRequirement : uint32_t { | ||
| 166 | + OptionalInput = 0U, | ||
| 167 | + Required = 1U, | ||
| 168 | +}; | ||
| 169 | + | ||
| 170 | +bool IsValidTensor(const aclTensor* tensor, TensorRequirement requirement) | ||
| 171 | +{ | ||
| 172 | + if (tensor == nullptr) { | ||
| 173 | + return requirement == TensorRequirement::OptionalInput; | ||
| 174 | + } | ||
| 175 | + return true; | ||
| 176 | +} | ||
| 177 | + | ||
| 178 | +bool IsValidTensorArg(const OpArg& arg, TensorRequirement requirement) | ||
| 179 | +{ | ||
| 180 | + if (arg.type == OpArgType::OPARG_ACLTENSOR) { | ||
| 181 | + return IsValidTensor(static_cast<const aclTensor*>(arg.value.data.pointer), requirement); | ||
| 182 | + } | ||
| 183 | + if (arg.type != OpArgType::OPARG_ACLTENSOR_LIST) { | ||
| 184 | + return false; | ||
| 185 | + } | ||
| 186 | + const auto* tensors = static_cast<const aclTensorList*>(arg.value.data.pointer); | ||
| 187 | + if (tensors == nullptr) { | ||
| 188 | + return false; | ||
| 189 | + } | ||
| 190 | + for (uint64_t index = 0U; index < tensors->Size(); ++index) { | ||
| 191 | + if (!IsValidTensor((*tensors)[index], requirement)) { | ||
| 192 | + return false; | ||
| 193 | + } | ||
| 194 | + } | ||
| 195 | + return true; | ||
| 196 | +} | ||
| 197 | + | ||
| 198 | +bool IsValidTensorList(const OpArgList& args, TensorRequirement requirement) | ||
| 199 | +{ | ||
| 200 | + if (args.count != 0U && args.args == nullptr) { | ||
| 201 | + return false; | ||
| 202 | + } | ||
| 203 | + for (size_t index = 0U; index < args.count; ++index) { | ||
| 204 | + if (!IsValidTensorArg(args.args[index], requirement)) { | ||
| 205 | + return false; | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | + return true; | ||
| 209 | +} | ||
| 210 | + | ||
| 211 | +bool IsValidArgs(const OpArgContext& args) | ||
| 212 | +{ | ||
| 213 | + const OpArgList* inputs = args.GetOpArg(OP_INPUT_ARG); | ||
| 214 | + const OpArgList* outputs = args.GetOpArg(OP_OUTPUT_ARG); | ||
| 215 | + const OpArgList* attributes = args.GetOpArg(OP_ATTR_ARG); | ||
| 216 | + const OpArgList* workspaces = args.GetOpArg(OP_WORKSPACE_ARG); | ||
| 217 | + // Workspace buffers are requested through the prepare callback and | ||
| 218 | + // allocated by opbase; the entry workspace list must be absent or empty. | ||
| 219 | + return inputs != nullptr && outputs != nullptr && attributes != nullptr && | ||
| 220 | + (attributes->count == 0U || attributes->args != nullptr) && | ||
| 221 | + IsValidTensorList(*inputs, TensorRequirement::OptionalInput) && | ||
| 222 | + IsValidTensorList(*outputs, TensorRequirement::Required) && | ||
| 223 | + (workspaces == nullptr || workspaces->count == 0U); | ||
| 224 | +} | ||
| 225 | + | ||
| 226 | +// Mirrors the AI Core GetWorkspace flow: one DT_UINT8 tensor per requested | ||
| 227 | +// size, allocated from the executor so the memory planner applies its | ||
| 228 | +// alignment and lifetime reuse, then appended as the workspace argument. | ||
| 229 | +aclnnStatus AppendRequestedWorkspaces(aclOpExecutor* executor, const ExternalOpWorkspaceRequest& request, | ||
| 230 | + OpArgContext* args) | ||
| 231 | +{ | ||
| 232 | + if (request.count == 0U) { | ||
| 233 | + return ACLNN_SUCCESS; | ||
| 234 | + } | ||
| 235 | + if (request.sizes == nullptr) { | ||
| 236 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Workspace request count is non-zero but sizes is null."); | ||
| 237 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 238 | + } | ||
| 239 | + FVector<aclTensor*> workspaces; | ||
| 240 | + for (uint32_t index = 0U; index < request.count; ++index) { | ||
| 241 | + const uint64_t size = request.sizes[index]; | ||
| 242 | + if (size == 0U || size > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { | ||
| 243 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Workspace request contains an invalid size %llu at index %u.", | ||
| 244 | + static_cast<unsigned long long>(size), index); | ||
| 245 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 246 | + } | ||
| 247 | + aclTensor* tensor = executor->AllocTensor(Shape({static_cast<int64_t>(size)}), DataType::DT_UINT8); | ||
| 248 | + if (tensor == nullptr) { | ||
| 249 | + OP_LOGE(ACLNN_ERR_INNER, "Failed to allocate requested workspace of %llu bytes.", | ||
| 250 | + static_cast<unsigned long long>(size)); | ||
| 251 | + return ACLNN_ERR_INNER; | ||
| 252 | + } | ||
| 253 | + workspaces.push_back(tensor); | ||
| 254 | + } | ||
| 255 | + aclTensorList* workspaceList = executor->AllocTensorList(workspaces.data(), workspaces.size()); | ||
| 256 | + if (workspaceList == nullptr) { | ||
| 257 | + OP_LOGE(ACLNN_ERR_INNER, "Failed to allocate requested workspace list."); | ||
| 258 | + return ACLNN_ERR_INNER; | ||
| 259 | + } | ||
| 260 | + args->AppendOpWorkspaceArg(workspaceList); | ||
| 261 | + return ACLNN_SUCCESS; | ||
| 262 | +} | ||
| 263 | + | ||
| 264 | +class ExternalOpKernelLauncher final : public KernelLauncher { | ||
| 265 | +public: | ||
| 266 | + ExternalOpKernelLauncher(uint32_t opType, CoreType coreType, const aclOpExecutor* executor, | ||
| 267 | + const ExternalOpTask& task, void* launchState, OpArgContext* args, | ||
| 268 | + const internal::OpLogInfo& opLogInfo) | ||
| 269 | + : KernelLauncher(opType, coreType, executor, internal::ProfilingInfoId{}), | ||
| 270 | + launch_(task.launch), | ||
| 271 | + destroy_(task.destroy), | ||
| 272 | + launchState_(launchState), | ||
| 273 | + args_(args), | ||
| 274 | + l2Name_(opLogInfo.l2ApiName == nullptr ? "" : opLogInfo.l2ApiName), | ||
| 275 | + l0Name_(opLogInfo.l0Name == nullptr ? "" : opLogInfo.l0Name), | ||
| 276 | + opTypeName_(OpTypeDict::ToString(opType).GetString()) | ||
| 277 | + { | ||
| 278 | + // KernelLauncher captures raw TLS pointers. External tasks may outlive | ||
| 279 | + // the caller's l0Name storage, so own both names and repoint the copy. | ||
| 280 | + opLogInfo_ = opLogInfo; | ||
| 281 | + opLogInfo_.l2ApiName = l2Name_.empty() ? nullptr : l2Name_.c_str(); | ||
| 282 | + opLogInfo_.l0Name = l0Name_.empty() ? nullptr : l0Name_.c_str(); | ||
| 283 | + } | ||
| 284 | + | ||
| 285 | + ~ExternalOpKernelLauncher() override | ||
| 286 | + { | ||
| 287 | + if (launchState_ != nullptr) { | ||
| 288 | + destroy_(launchState_); | ||
| 289 | + launchState_ = nullptr; | ||
| 290 | + } | ||
| 291 | + DestroyOpArgContext(args_); | ||
| 292 | + args_ = nullptr; | ||
| 293 | + } | ||
| 294 | + | ||
| 295 | + aclnnStatus Launch() override | ||
| 296 | + { | ||
| 297 | + if (launch_ == nullptr || args_ == nullptr) { | ||
| 298 | + OP_LOGE(ACLNN_ERR_INNER, "External task launcher is not initialized."); | ||
| 299 | + return ACLNN_ERR_INNER; | ||
| 300 | + } | ||
| 301 | + // Restore the complete captured context so profiling attribution cannot | ||
| 302 | + // inherit another launcher's framework context. | ||
| 303 | + auto& threadLocalCtx = op::internal::GetThreadLocalContext(); | ||
| 304 | + const internal::OpLogInfo previousLogInfo = threadLocalCtx.logInfo_; | ||
| 305 | + const internal::ProfilingInfoId previousProfilingInfoId = threadLocalCtx.profilingInfoId_; | ||
| 306 | + const uint64_t previousKernelStartTime = threadLocalCtx.kernelLauncherStartTime_; | ||
| 307 | + threadLocalCtx.logInfo_ = opLogInfo_; | ||
| 308 | + threadLocalCtx.profilingInfoId_ = profilingInfoId_; | ||
| 309 | + | ||
| 310 | + // Host tasks have no device compute semantics and skip L0 dump. | ||
| 311 | + const bool isComputeTask = coreType_ != NO_CALC; | ||
| 312 | + | ||
| 313 | + if (isComputeTask && threadLocalCtx.opConfigInfo_.isOpDumpEnable_) { | ||
| 314 | + op::internal::DumpL0(*args_->GetOpArg(op::OP_INPUT_ARG), opLogInfo_, OpInputType, executor_->GetStream()); | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + threadLocalCtx.profilingInfoId_.summaryItemId_ = MakeProfilingSummaryId(opLogInfo_, opTypeName_.c_str()); | ||
| 318 | + | ||
| 319 | + aclnnStatus ret = ACLNN_SUCCESS; | ||
| 320 | + { | ||
| 321 | + // This is a host execution boundary only. Kernel ranges belong at | ||
| 322 | + // each producer-owned real dispatch where launch metadata is known. | ||
| 323 | + threadLocalCtx.kernelLauncherStartTime_ = 0U; | ||
| 324 | + ExternalOpHostProfilingGuard hostExecGuard(threadLocalCtx.profilingInfoId_.summaryItemId_); | ||
| 325 | + ret = launch_(launchState_, args_, executor_->GetStream()); | ||
| 326 | + } | ||
| 327 | + if (isComputeTask && threadLocalCtx.opConfigInfo_.isOpDumpEnable_) { | ||
| 328 | + op::internal::DumpL0(*args_->GetOpArg(op::OP_OUTPUT_ARG), opLogInfo_, OpOutputType, executor_->GetStream()); | ||
| 329 | + } | ||
| 330 | + if (ret == ACLNN_SUCCESS && isComputeTask && op::internal::IsOverflowDumpEnable()) { | ||
| 331 | + CheckOverflowDump(); | ||
| 332 | + } | ||
| 333 | + // The launcher owns the name storage used above. Never leave borrowed | ||
| 334 | + // TLS pointers referring to it after the callback/executor can die. | ||
| 335 | + threadLocalCtx.logInfo_ = previousLogInfo; | ||
| 336 | + threadLocalCtx.profilingInfoId_ = previousProfilingInfoId; | ||
| 337 | + threadLocalCtx.kernelLauncherStartTime_ = previousKernelStartTime; | ||
| 338 | + return ret; | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + internal::OpKernelBin* GetBin() override { return nullptr; } | ||
| 342 | + | ||
| 343 | + // The task contract requires launchState to be address-independent, so | ||
| 344 | + // repeatability only depends on the argument storages, same as the AI CPU | ||
| 345 | + // path. | ||
| 346 | + bool CheckRepeatable(const std::unordered_map<const aclStorage*, const aclStorage*>& relation, | ||
| 347 | + const std::vector<const aclStorage*>& oriStorage) override | ||
| 348 | + { | ||
| 349 | + if (args_ == nullptr) { | ||
| 350 | + return false; | ||
| 351 | + } | ||
| 352 | + LauncherRepeatableChecker checker(relation, oriStorage); | ||
| 353 | + return checker.CheckLauncherRepeatable(args_); | ||
| 354 | + } | ||
| 355 | + | ||
| 356 | +private: | ||
| 357 | + void CheckOverflowDump() | ||
| 358 | + { | ||
| 359 | + aclmdlRICaptureStatus status; | ||
| 360 | + aclmdlRI captureMdl; | ||
| 361 | + if ((aclmdlRICaptureGetInfo(executor_->GetStream(), &status, &captureMdl) == ACL_SUCCESS) && | ||
| 362 | + (status == ACL_MODEL_RI_CAPTURE_STATUS_ACTIVE)) { | ||
| 363 | + OP_LOGI("No need to perform overflow check in the capture scenario."); | ||
| 364 | + return; | ||
| 365 | + } | ||
| 366 | + (void)op::internal::OverflowDumpProcess(args_, const_cast<aclOpExecutor*>(executor_), executor_->GetStream(), | ||
| 367 | + opLogInfo_); | ||
| 368 | + } | ||
| 369 | + | ||
| 370 | + ExternalOpLaunchFn launch_; | ||
| 371 | + ExternalOpDestroyFn destroy_; | ||
| 372 | + void* launchState_; | ||
| 373 | + OpArgContext* args_; | ||
| 374 | + std::string l2Name_; | ||
| 375 | + std::string l0Name_; | ||
| 376 | + std::string opTypeName_; | ||
| 377 | +}; | ||
| 378 | + | ||
| 379 | +} // namespace | ||
| 380 | + | ||
| 381 | +aclnnStatus AddExternalOpTask(const char* l0Name, uint32_t opType, const ExternalOpTask* task, | ||
| 382 | + aclOpExecutor* executor, OpArgContext* args) | ||
| 383 | +{ | ||
| 384 | + if (task == nullptr || executor == nullptr || args == nullptr) { | ||
| 385 | + DestroyOpArgContext(args); | ||
| 386 | + OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "External task, executor and arguments must not be null."); | ||
| 387 | + return ACLNN_ERR_PARAM_NULLPTR; | ||
| 388 | + } | ||
| 389 | + | ||
| 390 | + CoreType coreType = NO_CALC; | ||
| 391 | + if (opType == 0U || opType >= OpTypeDict::GetAllOpTypeSize() || !IsValidTask(*task, coreType) || | ||
| 392 | + !IsValidArgs(*args)) { | ||
| 393 | + DestroyOpArgContext(args); | ||
| 394 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Invalid external task or arguments."); | ||
| 395 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 396 | + } | ||
| 397 | + | ||
| 398 | + internal::OpLogInfo taskLogInfo = internal::GetThreadLocalContext().logInfo_; | ||
| 399 | + if (l0Name != nullptr) { | ||
| 400 | + taskLogInfo.l0Name = l0Name; | ||
| 401 | + } | ||
| 402 | + ExternalOpWorkspaceRequest workspaceRequest{nullptr, 0U, 0U}; | ||
| 403 | + void* launchState = nullptr; | ||
| 404 | + aclnnStatus status = task->prepare(task->taskConfig, args, &workspaceRequest, &launchState); | ||
| 405 | + if (status != ACLNN_SUCCESS) { | ||
| 406 | + if (launchState != nullptr) { | ||
| 407 | + task->destroy(launchState); | ||
| 408 | + } | ||
| 409 | + DestroyOpArgContext(args); | ||
| 410 | + return status; | ||
| 411 | + } | ||
| 412 | + | ||
| 413 | + status = AppendRequestedWorkspaces(executor, workspaceRequest, args); | ||
| 414 | + if (status != ACLNN_SUCCESS) { | ||
| 415 | + if (launchState != nullptr) { | ||
| 416 | + task->destroy(launchState); | ||
| 417 | + } | ||
| 418 | + DestroyOpArgContext(args); | ||
| 419 | + return status; | ||
| 420 | + } | ||
| 421 | + | ||
| 422 | + auto* launcher = new (std::nothrow) ExternalOpKernelLauncher( | ||
| 423 | + opType, coreType, executor, *task, launchState, args, taskLogInfo); | ||
| 424 | + if (launcher == nullptr) { | ||
| 425 | + if (launchState != nullptr) { | ||
| 426 | + task->destroy(launchState); | ||
| 427 | + } | ||
| 428 | + DestroyOpArgContext(args); | ||
| 429 | + return ACLNN_ERR_INNER; | ||
| 430 | + } | ||
| 431 | + | ||
| 432 | + // Tasks with dynamic output shapes must not be cached, same as the AI | ||
| 433 | + // Core path (CreatAiCoreKernelLauncher abandons the cache for them). | ||
| 434 | + const bool includeOutShape = args->ContainsOpArgType(OP_OUTSHAPE_ARG); | ||
| 435 | + status = RegisterKernelLauncher(opType, executor, args, launcher, includeOutShape); | ||
| 436 | + if (status != ACLNN_SUCCESS) { | ||
| 437 | + return status; | ||
| 438 | + } | ||
| 439 | + ExternalOpCachePolicy cachePolicy = includeOutShape ? ExternalOpCachePolicy::DisableRepeat : task->cachePolicy; | ||
| 440 | + if (cachePolicy == ExternalOpCachePolicy::Abandon) { | ||
| 441 | + executor->AbandonCache(); | ||
| 442 | + } else if (cachePolicy == ExternalOpCachePolicy::DisableRepeat) { | ||
| 443 | + executor->AbandonCache(true); | ||
| 444 | + } | ||
| 445 | + return ACLNN_SUCCESS; | ||
| 446 | +} | ||
| 447 | + | ||
| 448 | +} // namespace op | ||
| @@ -10,6 +10,7 @@ | |||
| 10 | set(STUB_SRC_LIST | 10 | set(STUB_SRC_LIST |
| 11 | ${CMAKE_CURRENT_BINARY_DIR}/stub_aicpu_task.cc | 11 | ${CMAKE_CURRENT_BINARY_DIR}/stub_aicpu_task.cc |
| 12 | ${CMAKE_CURRENT_BINARY_DIR}/stub_common_types.cc | 12 | ${CMAKE_CURRENT_BINARY_DIR}/stub_common_types.cc |
| 13 | + ${CMAKE_CURRENT_BINARY_DIR}/stub_external_op_task.cc | ||
| 13 | ${CMAKE_CURRENT_BINARY_DIR}/stub_data_type_utils.cc | 14 | ${CMAKE_CURRENT_BINARY_DIR}/stub_data_type_utils.cc |
| 14 | ${CMAKE_CURRENT_BINARY_DIR}/stub_format_utils.cc | 15 | ${CMAKE_CURRENT_BINARY_DIR}/stub_format_utils.cc |
| 15 | ${CMAKE_CURRENT_BINARY_DIR}/stub_framework_op.cc | 16 | ${CMAKE_CURRENT_BINARY_DIR}/stub_framework_op.cc |
| @@ -43,7 +44,7 @@ add_custom_command( | |||
| 43 | OUTPUT ${STUB_SRC_LIST} | 44 | OUTPUT ${STUB_SRC_LIST} |
| 44 | COMMAND echo "Generating nnopbase stub files." | 45 | COMMAND echo "Generating nnopbase stub files." |
| 45 | && ${HI_PYTHON} ${CMAKE_CURRENT_LIST_DIR}/gen_stubapi.py ${OPS_BASE_PATH} ${CMAKE_CURRENT_BINARY_DIR} > /dev/null 2>&1 | 46 | && ${HI_PYTHON} ${CMAKE_CURRENT_LIST_DIR}/gen_stubapi.py ${OPS_BASE_PATH} ${CMAKE_CURRENT_BINARY_DIR} > /dev/null 2>&1 |
| 46 | - && echo "Generating nnopbase stub files end." | 47 | + && echo "Generating nnopbase stub files end." |
| 47 | ) | 48 | ) |
| 48 | 49 | ||
| 49 | 50 | ||
| @@ -78,6 +78,7 @@ white_list_for_debug = [ | |||
| 78 | "format_utils.h", | 78 | "format_utils.h", |
| 79 | "data_type_utils.h", | 79 | "data_type_utils.h", |
| 80 | "common_types.h", | 80 | "common_types.h", |
| 81 | + "external_op_task.h", | ||
| 81 | "aicpu/aicpu_task.h", # aicpu | 82 | "aicpu/aicpu_task.h", # aicpu |
| 82 | "individual_op_api.h", # nnopbase | 83 | "individual_op_api.h", # nnopbase |
| 83 | "aclnn/acl_meta.h", | 84 | "aclnn/acl_meta.h", |