已合并
feat: part2 完善HostCPU自定义算子运行时与常量折叠支持 #4538
duhua创建于 8 天前
feat: part2 完善HostCPU自定义算子运行时与常量折叠支持 #4538
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 duhua 的贡献)8 天前 添加了label:enhancement
atomgit-bot
8 天前 评论:
8 天前 评论:
变更摘要
本 PR 为自定义算子体系引入后端(kDevice / kHostCPU)维度,新增 Host CPU 自定义算子的注册、编译与运行时执行能力,并将自定义算子 pull registry ABI 升级到 V2(同时兼容历史 V1 SO 加载)。核心改动包括:CustomOpRegistry / CustomOpFactory 的注册与查询接口(RegisterCreator、CreateOrGetCustomOp、HasCreator、FindCustomOp)新增 OpBackend 参数并按后端分层存储;新增 HostCpuExecuteOp 接口与 HostCpuOpExecutionContext(含 MallocOutputTensor / MakeOutputRefInput);引擎选择、图分区与优化 pass 识别并路由 host CPU 自定义算子(kHostCpuCustomOpLowerFunc);runtime v2 新增 FindHostCpuCustomOp、ExecuteHostCustomOp、ExecuteHostCustomOpWithInferShape 内核与 LoweringHostCustomNode 转换,并配套安装打包、profiling 类型与大量测试更新。
主要改动
- 自定义算子注册表支持后端维度:
OpBackend枚举(kDevice/kHostCPU)引入后,CustomOpRegistry的creators_/custom_ops_改为按op_type -> backend -> creator/instance分层存储,RegisterCreator、CreateOrGetCustomOp、HasCreator、FindCustomOp、HasCustomOp等接口新增带 backend 的重载并校验非法后端;custom_op_registry_builder.cc按 ABI 版本(kCustomOpCreatorPullAbiVersion/kCustomOpCreatorPullAbiVersionV2)分派加载 V1/V2 的 creator。 - Pull registry ABI 升级到 V2:
custom_op_pull_registry.h/cc新增带backend字段的CustomOpTypeToCreatorV2(V1 布局保留用于加载历史 SO),GetRegisteredCustomOpCreators导出 V2 结构并校验OpBackend合法性,GetRegisteredCustomOpCreatorAbiVersion返回kCustomOpCreatorPullAbiVersionV2。 - 新增 Host CPU 自定义算子执行接口与上下文:
custom_op.h新增HostCpuExecuteOp接口、REG_OP_BACKEND注册宏及CustomOpCreatorRegister的 backend 重载,cast.h新增kHostCpuExecute能力位映射;新增HostCpuOpExecutionContext(含MallocOutputTensor通过 host 分配器申请输出内存、MakeOutputRefInput复用输入内存),并同步加入cmake/package.cmake与GeExecutorInc.xml的安装文件清单。 - 引擎选择与图流程适配 host CPU 自定义算子:
DNNEngineManager::GetDNNEngineName将 host CPU 自定义算子路由到 custom 引擎并设置kAttrLowingFunc = kHostCpuCustomOpLowerFunc;HostcpuEngineUpdatePass、EnginePartitioner、DynamicShapePartitioner、MarkGraphUnknownStatusPass新增IsCustomOpExecOnHostCpu识别逻辑(host 自定义算子保持 custom 引擎并标记为未知/主机执行,二次分区时挂靠DNN_VM_HOST_CPU流),CustomGraphOptimizer跳过 host CPU 自定义算子的设备编译。 - runtime v2 新增 Host CPU 执行链路与公共能力查询:
custom_node_converter.cc新增LoweringHostCustomNode(注册到kHostCpuCustomOpLowerFunc的 placement),custom_op_kernel.cc新增FindHostCpuCustomOp、ExecuteHostCustomOp、ExecuteHostCustomOpWithInferShape内核并注册ExecuteHostCustomOp等 profiling 类型;同时CustomOpRegistry新增GetCustomOpCommonCapability,model_custom_kernels_helper.cc、ge_root_model.cc、op_desc_utils_ex.cc、bg_infer_shape.cc等改为按kPortable/kShapeInfer/kInferMeta公共能力跨后端获取唯一实现(如不可序列化自定义算子不再报错而是标记为非可序列化)。


atomgit-bot
8 天前 评论:
8 天前 评论:
8 天前 添加了label:stat/needs-squash
此处折叠了385条消息 查看更多
4 天前 添加了label:approved
4 天前 合入了pull request
Pull Request
描述
本 PR 完善后端感知自定义算子的 HostCPU 执行链路,打通编译期识别与分流、Runtime V2 下沉执行、ShapeInfer、常量折叠及配套单元测试。HostCPU 自定义算子使用独立的 lowering 标识和执行上下文,避免被当作 Device 自定义算子编译或按 AI Core 归属分区。
主要修改点
1. HostCPU 自定义算子编译与分区
DNNEngineManager中识别注册到OpBackend::kHostCPU的自定义算子,并将其设置为 Custom Engine、Custom Op Kernel Lib,同时写入host_cpu_custom_op_lower_funclowering 属性。HostcpuEngineUpdatePass中保留 HostCPU 自定义算子的 Custom Engine 归属,避免被错误迁移到普通 HostCPU 或 Device 执行路径。EnginePartitioner二次分区时区分 Device 自定义算子和 HostCPU 自定义算子:前者跟随 AI Core,后者跟随DNN_VM_HOST_CPU。2. Runtime V2 HostCPU 自定义算子执行
host_cpu_custom_op_lower_func对应的节点 converter;ExecuteHostCustomOp和ExecuteHostCustomOpWithInferShape;HostCpuOpExecutionContext调用HostCpuExecuteOp::Execute;kShapeInfer能力获取ShapeInferOp,新增FindCustomShapeInferOpkernel,避免先获取通用BaseCustomOp再转换。3. HostCPU 自定义算子常量折叠
HostCpuExecuteOp的自定义算子。HostCpuOpExecutionContext构造逻辑。4. 执行上下文、构建及可观测性
MakeOutputRefInput调整为非 const 接口,移除内部const_cast,同步更新实现、头文件及离线 stub。gert的链接依赖。5. 测试覆盖
新增或扩展以下单元测试:
变更类型
如何测试
核对清单
其他信息
本 PR 相对目标分支涉及 33 个文件,新增约 1384 行、删除约 50 行。Device 自定义算子与 HostCPU 自定义算子通过后端注册信息和 lowering 属性区分,HostCPU 路径不会进入 Device 自定义算子编译流程。