Merged
feat: 自定义算子注册支持 engine and priority,常量折叠适配 #4918
duhuaAdd on Sep 11
feat: 自定义算子注册支持 engine and priority,常量折叠适配 #4918
Merged
duhuaAdd on Sep 11
Total: 21 files changed+738-89
@@ -15,6 +15,7 @@
15#include "opskernel/ops_kernel_info_types.h"15#include "opskernel/ops_kernel_info_types.h"
16#include "graph/utils/args_format_desc_utils.h"16#include "graph/utils/args_format_desc_utils.h"
17#include "graph/args_format_desc.h"17#include "graph/args_format_desc.h"
18+#include "framework/common/debug/ge_log.h"
18#include "common/checker.h"19#include "common/checker.h"
19 20 
20namespace ge {21namespace ge {
@@ -19,6 +19,7 @@
19#include <unistd.h>19#include <unistd.h>
20#include <securec.h>20#include <securec.h>
21 21 
22+#include "common/ge_common/ge_inner_error_codes.h"
22#include "ops_kernel_builder/task_builder/task_builder.h"23#include "ops_kernel_builder/task_builder/task_builder.h"
23#include "graph/utils/args_format_desc_utils.h"24#include "graph/utils/args_format_desc_utils.h"
24#include "graph/debug/ge_attr_define.h"25#include "graph/debug/ge_attr_define.h"
@@ -189,6 +189,33 @@ Status BuildHostCpuOpContext(const NodePtr &node, const std::vector<ConstGeTenso
189 }189 }
190 return SUCCESS;190 return SUCCESS;
191}191}
192+ 
193+Status ExecuteHostCpuCustomOp(const NodePtr &node, BaseCustomOp *base_custom_op,
194+ const std::vector<ConstGeTensorPtr> &inputs, std::vector<GeTensorPtr> &outputs) {
195+ GE_ASSERT_NOTNULL(base_custom_op, "Host cpu custom op for node %s is null.", node->GetName().c_str());
196+ auto *host_custom_op = CustomOpCast<HostCpuExecuteOp>(base_custom_op);
197+ GE_ASSERT_NOTNULL(host_custom_op, "Host cpu custom op for node %s does not implement HostCpuExecuteOp.",
198+ node->GetName().c_str());
199+ 
200+ std::vector<gert::Tensor> input_tensors;
201+ std::vector<gert::Tensor> output_tensors;
202+ HostCpuConstFoldingMemGertAllocator allocator;
203+ gert::KernelContextHolder context_holder;
204+ GE_ASSERT_SUCCESS(BuildHostCpuOpContext(node, inputs, input_tensors, output_tensors, allocator, context_holder));
205+ auto *host_context = reinterpret_cast<gert::HostCpuOpExecutionContext *>(context_holder.GetKernelContext());
206+ GE_ASSERT_NOTNULL(host_context);
207+ GE_ASSERT_SUCCESS(host_custom_op->Execute(host_context));
208+ 
209+ outputs.clear();
210+ outputs.reserve(output_tensors.size());
211+ for (const auto &output_tensor : output_tensors) {
212+ GeTensorPtr output = MakeShared<GeTensor>();
213+ GE_ASSERT_NOTNULL(output);
214+ GE_ASSERT_SUCCESS(TensorTransUtils::GertTensor2GeTensor(output_tensor, *output));
215+ outputs.emplace_back(std::move(output));
216+ }
217+ return SUCCESS;
218+}
192} // namespace219} // namespace
193 220 
194bool ConstantFoldingPass::NeedIgnorePass(const NodePtr &node) {221bool ConstantFoldingPass::NeedIgnorePass(const NodePtr &node) {
@@ -276,29 +303,7 @@ Status ConstantFoldingPass::ComputeWithHostCpuCustomOp(const NodePtr &node, cons
276 auto base_custom_op = CustomOpFactory::CreateOrGetCustomOp(op_type_str, OpBackend::kHostCPU);303 auto base_custom_op = CustomOpFactory::CreateOrGetCustomOp(op_type_str, OpBackend::kHostCPU);
277 GE_ASSERT_NOTNULL(base_custom_op, "Op %s is registered as host cpu custom op but create instance failed.",304 GE_ASSERT_NOTNULL(base_custom_op, "Op %s is registered as host cpu custom op but create instance failed.",
278 op_type.c_str());305 op_type.c_str());
279- auto *host_custom_op = CustomOpCast<HostCpuExecuteOp>(base_custom_op);306+ return ExecuteHostCpuCustomOp(node, base_custom_op, inputs, outputs);
280- GE_ASSERT_NOTNULL(host_custom_op,
281- "Op %s is registered as host cpu custom op but does not implement HostCpuExecuteOp.",
282- op_type.c_str());
283- 
284- std::vector<gert::Tensor> input_tensors;
285- std::vector<gert::Tensor> output_tensors;
286- HostCpuConstFoldingMemGertAllocator allocator;
287- gert::KernelContextHolder context_holder;
288- GE_ASSERT_SUCCESS(BuildHostCpuOpContext(node, inputs, input_tensors, output_tensors, allocator, context_holder));
289- auto *host_context = reinterpret_cast<gert::HostCpuOpExecutionContext *>(context_holder.GetKernelContext());
290- GE_ASSERT_NOTNULL(host_context);
291- GE_ASSERT_SUCCESS(host_custom_op->Execute(host_context));
292- 
293- outputs.clear();
294- outputs.reserve(output_tensors.size());
295- for (const auto &output_tensor : output_tensors) {
296- GeTensorPtr output = MakeShared<GeTensor>();
297- GE_ASSERT_NOTNULL(output);
298- GE_ASSERT_SUCCESS(TensorTransUtils::GertTensor2GeTensor(output_tensor, *output));
299- outputs.emplace_back(std::move(output));
300- }
301- return SUCCESS;
302}307}
303 308 
304Status ConstantFoldingPass::ComputeWithBuiltInKernel(NodePtr &node, const vector<ConstGeTensorPtr> &inputs,309Status ConstantFoldingPass::ComputeWithBuiltInKernel(NodePtr &node, const vector<ConstGeTensorPtr> &inputs,
@@ -341,6 +346,20 @@ Status ConstantFoldingPass::ComputeWithHostCpuKernel(const NodePtr &node, const
341Status ConstantFoldingPass::RunOpKernel(const NodePtr &node, const std::vector<ConstGeTensorPtr> &inputs,346Status ConstantFoldingPass::RunOpKernel(const NodePtr &node, const std::vector<ConstGeTensorPtr> &inputs,
342 std::vector<GeTensorPtr> &outputs) {347 std::vector<GeTensorPtr> &outputs) {
343 const std::string op_type = NodeUtils::GetNodeType(node);348 const std::string op_type = NodeUtils::GetNodeType(node);
349+ const AscendString op_type_str(op_type.c_str());
350+ if (CustomOpFactory::IsExistOp(op_type_str, OpBackend::kHostCPU, OpRegistrationPriority::kBottom,
351+ OpEngine::kHostCpu)) {
352+ GELOGD("Bottom priority host cpu op is registered. op type = %s, engine = %s.", op_type.c_str(), "HOST_CPU");
353+ auto *bottom_op = CustomOpFactory::CreateOrGetCustomOp(op_type_str, OpBackend::kHostCPU,
354+ OpRegistrationPriority::kBottom, OpEngine::kHostCpu);
355+ if (bottom_op == nullptr) {
356+ GELOGE(GRAPH_FAILED, "Failed to create bottom priority host cpu op. op type = %s, engine = %s.", op_type.c_str(),
357+ "HOST_CPU");
358+ return UNSUPPORTED;
359+ }
360+ return ExecuteHostCpuCustomOp(node, bottom_op, inputs, outputs);
361+ }
362+ 
344 auto kernel = OpKernelRegistry::GetInstance().CreateHostCpuOp(op_type);363 auto kernel = OpKernelRegistry::GetInstance().CreateHostCpuOp(op_type);
345 if (kernel == nullptr) {364 if (kernel == nullptr) {
346 GELOGD("Op of type %s is not supported by host cpu engine", op_type.c_str());365 GELOGD("Op of type %s is not supported by host cpu engine", op_type.c_str());
@@ -17,17 +17,30 @@
17#include "graph/custom_op_pull_registry.h"17#include "graph/custom_op_pull_registry.h"
18 18 
19namespace ge {19namespace ge {
20+namespace {
21+bool NeedRegisterLocalCreator(const OpRegistrationPriority priority, const OpEngine engine) {
22+ return (priority == OpRegistrationPriority::kTop) && (engine == OpEngine::kCustom);
23+}
24+} // namespace
25+ 
20CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, const CustomOpCreateFunc op_creator)26CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, const CustomOpCreateFunc op_creator)
21 : CustomOpCreatorRegister(operator_type, OpBackend::kDevice, op_creator) {}27 : CustomOpCreatorRegister(operator_type, OpBackend::kDevice, op_creator) {}
22 28 
23CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend,29CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend,
30+ const CustomOpCreateFunc op_creator)
31+ : CustomOpCreatorRegister(operator_type, backend, OpRegistrationPriority::kTop, OpEngine::kCustom, op_creator) {}
32+ 
33+CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend,
34+ OpRegistrationPriority priority, OpEngine engine,
24 const CustomOpCreateFunc op_creator) {35 const CustomOpCreateFunc op_creator) {
25- RegisterCustomOpLocalCreator(operator_type.GetString(), backend, op_creator);36+ if (NeedRegisterLocalCreator(priority, engine)) {
37+ RegisterCustomOpLocalCreator(operator_type.GetString(), backend, op_creator);
38+ }
26 if ((op_creator == nullptr) || IsOfflineCustomOpSoLoading()) {39 if ((op_creator == nullptr) || IsOfflineCustomOpSoLoading()) {
27 return;40 return;
28 }41 }
29- CustomOpFactory::RegisterCustomOpCreator(operator_type, backend, [op_creator]() -> std::unique_ptr<BaseCustomOp> {42+ CustomOpFactory::RegisterCustomOpCreator(
30- return std::unique_ptr<BaseCustomOp>(op_creator());43+ operator_type, backend, priority, engine,
31- });44+ [op_creator]() -> std::unique_ptr<BaseCustomOp> { return std::unique_ptr<BaseCustomOp>(op_creator()); });
32}45}
33} // namespace ge46} // namespace ge
@@ -18,7 +18,6 @@
18#include <memory>18#include <memory>
19 19 
20namespace ge {20namespace ge {
21- 
22CustomOpRegistry &CustomOpFactory::GetGlobalRegistry() {21CustomOpRegistry &CustomOpFactory::GetGlobalRegistry() {
23 return *GetGlobalRegistryPtr();22 return *GetGlobalRegistryPtr();
24}23}
@@ -37,6 +36,12 @@ graphStatus CustomOpFactory::RegisterCustomOpCreator(const AscendString &op_type
37 return GetGlobalRegistry().RegisterCreator(op_type, backend, op_creator);36 return GetGlobalRegistry().RegisterCreator(op_type, backend, op_creator);
38}37}
39 38 
39+graphStatus CustomOpFactory::RegisterCustomOpCreator(const AscendString &op_type, OpBackend backend,
40+ OpRegistrationPriority priority, OpEngine engine,
41+ const BaseOpCreator &op_creator) {
42+ return GetGlobalRegistry().RegisterCreator(op_type, backend, priority, engine, op_creator);
43+}
44+ 
40BaseCustomOp *CustomOpFactory::CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend) {45BaseCustomOp *CustomOpFactory::CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend) {
41 return GetGlobalRegistry().CreateOrGetCustomOp(op_type, backend);46 return GetGlobalRegistry().CreateOrGetCustomOp(op_type, backend);
42}47}
@@ -45,6 +50,11 @@ BaseCustomOp *CustomOpFactory::CreateOrGetCustomOp(const AscendString &op_type)
45 return CreateOrGetCustomOp(op_type, OpBackend::kDevice);50 return CreateOrGetCustomOp(op_type, OpBackend::kDevice);
46}51}
47 52 
53+BaseCustomOp *CustomOpFactory::CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend,
54+ OpRegistrationPriority priority, OpEngine engine) {
55+ return GetGlobalRegistry().CreateOrGetCustomOp(op_type, backend, priority, engine);
56+}
57+ 
48void CustomOpFactory::RemoveCustomOps(const std::vector<AscendString> &op_types) {58void CustomOpFactory::RemoveCustomOps(const std::vector<AscendString> &op_types) {
49 GetGlobalRegistry().RemoveCustomOps(op_types);59 GetGlobalRegistry().RemoveCustomOps(op_types);
50}60}
@@ -61,6 +71,11 @@ bool CustomOpFactory::IsExistOp(const AscendString &op_type, OpBackend backend)
61 return GetGlobalRegistry().HasCreator(op_type, backend);71 return GetGlobalRegistry().HasCreator(op_type, backend);
62}72}
63 73 
74+bool CustomOpFactory::IsExistOp(const AscendString &op_type, OpBackend backend, OpRegistrationPriority priority,
75+ OpEngine engine) {
76+ return GetGlobalRegistry().HasCreator(op_type, backend, priority, engine);
77+}
78+ 
64bool CustomOpFactory::IsAddressRefreshable(const AscendString &op_type) {79bool CustomOpFactory::IsAddressRefreshable(const AscendString &op_type) {
65 return GetGlobalRegistry().IsAddressRefreshable(op_type);80 return GetGlobalRegistry().IsAddressRefreshable(op_type);
66}81}
@@ -77,10 +92,15 @@ CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_ty
77 : CustomOpCreatorRegister(operator_type, OpBackend::kDevice, op_creator) {}92 : CustomOpCreatorRegister(operator_type, OpBackend::kDevice, op_creator) {}
78 93 
79CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend,94CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend,
95+ BaseOpCreator const &op_creator)
96+ : CustomOpCreatorRegister(operator_type, backend, OpRegistrationPriority::kTop, OpEngine::kCustom, op_creator) {}
97+ 
98+CustomOpCreatorRegister::CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend,
99+ OpRegistrationPriority priority, OpEngine engine,
80 BaseOpCreator const &op_creator) {100 BaseOpCreator const &op_creator) {
81 if (IsOfflineCustomOpSoLoading()) {101 if (IsOfflineCustomOpSoLoading()) {
82 return;102 return;
83 }103 }
84- CustomOpFactory::RegisterCustomOpCreator(operator_type, backend, op_creator);104+ CustomOpFactory::RegisterCustomOpCreator(operator_type, backend, priority, engine, op_creator);
85}105}
86} // namespace ge106} // namespace ge
Mgraph_metadef/graph/normal_graph/custom_op_registry.cc+244-53File content is under review, please try again later
@@ -29,6 +29,29 @@ enum class OpBackend : uint32_t {
29 kHostCPU = 1,29 kHostCPU = 1,
30};30};
31 31 
32+enum class OpRegistrationPriority : uint32_t {
33+ kTop = 0,
34+ kBottom = 1,
35+};
36+ 
37+enum class OpEngine : uint32_t {
38+ kCustom = 0,
39+ kAiCore,
40+ kFftsPlus,
41+ kVectorCore,
42+ kDsa,
43+ kRts,
44+ kRtsFftsPlus,
45+ kHccl,
46+ kAiCpuFftsPlus,
47+ kAiCpuAscendFftsPlus,
48+ kAiCpuAscend,
49+ kAiCpu,
50+ kDvpp,
51+ kGeLocal,
52+ kHostCpu = 14,
53+};
54+ 
32/**55/**
33 * 自定义算子能力接口的公共基类。56 * 自定义算子能力接口的公共基类。
34 * 用户可按需组合继承 CompilableOp、EagerExecuteOp、ShapeInferOp,57 * 用户可按需组合继承 CompilableOp、EagerExecuteOp、ShapeInferOp,
@@ -164,14 +187,19 @@ using CustomOpCreateFunc = ge::BaseCustomOp *(*)();
164 187 
165/**188/**
166 * 自定义算子创建器注册辅助类。189 * 自定义算子创建器注册辅助类。
167- * 通常配合 REG_AUTO_MAPPING_OP 宏静态注册算子类型和创建函数。190+ * 用于通过注册宏静态注册算子类型、创建函数以及 backend、priority 和 engine 属性。
191+ * 支持 REG_AUTO_MAPPING_OP、REG_OP_BACKEND 和 REG_OP_WITH_PRIORITY 宏。
168 */192 */
169class CustomOpCreatorRegister {193class CustomOpCreatorRegister {
170 public:194 public:
171 CustomOpCreatorRegister(const AscendString &operator_type, const BaseOpCreator &op_creator);195 CustomOpCreatorRegister(const AscendString &operator_type, const BaseOpCreator &op_creator);
172 CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend, const BaseOpCreator &op_creator);196 CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend, const BaseOpCreator &op_creator);
197+ CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend, OpRegistrationPriority priority,
198+ OpEngine engine, const BaseOpCreator &op_creator);
173 CustomOpCreatorRegister(const AscendString &operator_type, const CustomOpCreateFunc op_creator);199 CustomOpCreatorRegister(const AscendString &operator_type, const CustomOpCreateFunc op_creator);
174 CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend, const CustomOpCreateFunc op_creator);200 CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend, const CustomOpCreateFunc op_creator);
201+ CustomOpCreatorRegister(const AscendString &operator_type, OpBackend backend, OpRegistrationPriority priority,
202+ OpEngine engine, const CustomOpCreateFunc op_creator);
175 ~CustomOpCreatorRegister() = default;203 ~CustomOpCreatorRegister() = default;
176};204};
177} // namespace ge205} // namespace ge
@@ -189,4 +217,14 @@ class CustomOpCreatorRegister {
189 static const ge::CustomOpCreatorRegister REG_JOIN(custom_op_register, ctr)(op_type, backend, \217 static const ge::CustomOpCreatorRegister REG_JOIN(custom_op_register, ctr)(op_type, backend, \
190 REG_JOIN(custom_op_pull_creator, ctr))218 REG_JOIN(custom_op_pull_creator, ctr))
191 219 
220+#define REG_OP_WITH_PRIORITY(custom_op_class, op_type, backend, priority, engine) \
221+ REG_OP_WITH_PRIORITY_UNIQ(__COUNTER__, custom_op_class, op_type, backend, priority, engine)
222+ 
223+#define REG_OP_WITH_PRIORITY_UNIQ(ctr, custom_op_class, op_type, backend, priority, engine) \
224+ static ge::BaseCustomOp *REG_JOIN(custom_op_pull_creator, ctr)() { \
225+ return new custom_op_class(); \
226+ } \
227+ static const ge::CustomOpCreatorRegister REG_JOIN(custom_op_register, ctr)(op_type, backend, priority, engine, \
228+ REG_JOIN(custom_op_pull_creator, ctr))
229+ 
192#endif // METADEF_CXX_INC_GRAPH_BASE_CUSTOM_OP_H230#endif // METADEF_CXX_INC_GRAPH_BASE_CUSTOM_OP_H
@@ -14,7 +14,6 @@
14#include <string>14#include <string>
15#include <vector>15#include <vector>
16 16 
17-#include "framework/common/debug/ge_log.h"
18#include "graph/error_codes.h"17#include "graph/error_codes.h"
19#include "graph/op_desc.h"18#include "graph/op_desc.h"
20#include "graph/node.h"19#include "graph/node.h"
@@ -27,9 +27,14 @@ class CustomOpFactory {
27 static graphStatus RegisterCustomOpCreator(const AscendString &op_type, const BaseOpCreator &op_creator);27 static graphStatus RegisterCustomOpCreator(const AscendString &op_type, const BaseOpCreator &op_creator);
28 static graphStatus RegisterCustomOpCreator(const AscendString &op_type, OpBackend backend,28 static graphStatus RegisterCustomOpCreator(const AscendString &op_type, OpBackend backend,
29 const BaseOpCreator &op_creator);29 const BaseOpCreator &op_creator);
30+ static graphStatus RegisterCustomOpCreator(const AscendString &op_type, OpBackend backend,
31+ OpRegistrationPriority priority, OpEngine engine,
32+ const BaseOpCreator &op_creator);
30 33 
31 static BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type);34 static BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type);
32 static BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend);35 static BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend);
36+ static BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend,
37+ OpRegistrationPriority priority, OpEngine engine);
33 38 
34 template <typename T>39 template <typename T>
35 static T *GetCustomOpCommonCapability(const AscendString &op_type) {40 static T *GetCustomOpCommonCapability(const AscendString &op_type) {
@@ -41,6 +46,16 @@ class CustomOpFactory {
41 return CustomOpCast<T>(custom_op);46 return CustomOpCast<T>(custom_op);
42 }47 }
43 48 
49+ template <typename T>
50+ static T *GetCustomOpCommonCapability(const AscendString &op_type, OpRegistrationPriority priority, OpEngine engine) {
51+ static_assert(std::is_same<T, ShapeInferOp>::value || std::is_same<T, CustomOpInferMetaProvider>::value ||
52+ std::is_same<T, PortableOp>::value,
53+ "GetCustomOpCommonCapability only supports ShapeInferOp, CustomOpInferMetaProvider and PortableOp");
54+ auto *const custom_op = GetGlobalRegistry().GetCustomOpCommonCapability(
55+ op_type, CustomOpCapabilityTrait<T>::kCapability, priority, engine);
56+ return CustomOpCast<T>(custom_op);
57+ }
58+ 
44 static void RemoveCustomOps(const std::vector<AscendString> &op_types);59 static void RemoveCustomOps(const std::vector<AscendString> &op_types);
45 60 
46 static CustomOpRegistryPtr GetGlobalRegistryPtr();61 static CustomOpRegistryPtr GetGlobalRegistryPtr();
@@ -49,6 +64,8 @@ class CustomOpFactory {
49 64 
50 static bool IsExistOp(const AscendString &op_type);65 static bool IsExistOp(const AscendString &op_type);
51 static bool IsExistOp(const AscendString &op_type, OpBackend backend);66 static bool IsExistOp(const AscendString &op_type, OpBackend backend);
67+ static bool IsExistOp(const AscendString &op_type, OpBackend backend, OpRegistrationPriority priority,
68+ OpEngine engine);
52 69 
53 static graphStatus LoadCustomOpsPartition(const uint8_t *data, size_t len);70 static graphStatus LoadCustomOpsPartition(const uint8_t *data, size_t len);
54 71 
@@ -28,6 +28,8 @@
28namespace ge {28namespace ge {
29class BaseCustomOp;29class BaseCustomOp;
30enum class OpBackend : uint32_t;30enum class OpBackend : uint32_t;
31+enum class OpRegistrationPriority : uint32_t;
32+enum class OpEngine : uint32_t;
31using BaseOpCreator = std::function<std::unique_ptr<BaseCustomOp>()>;33using BaseOpCreator = std::function<std::unique_ptr<BaseCustomOp>()>;
32 34 
33class CustomOpSoHandle;35class CustomOpSoHandle;
@@ -38,15 +40,23 @@ class CustomOpRegistry {
38 ~CustomOpRegistry();40 ~CustomOpRegistry();
39 41 
40 graphStatus RegisterCreator(const AscendString &op_type, OpBackend backend, const BaseOpCreator &creator);42 graphStatus RegisterCreator(const AscendString &op_type, OpBackend backend, const BaseOpCreator &creator);
43+ graphStatus RegisterCreator(const AscendString &op_type, OpBackend backend, OpRegistrationPriority priority,
44+ OpEngine engine, const BaseOpCreator &creator);
41 void AddSoHandles(const std::vector<CustomOpSoHandlePtr> &so_handles);45 void AddSoHandles(const std::vector<CustomOpSoHandlePtr> &so_handles);
42 46 
43 BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend);47 BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend);
48+ BaseCustomOp *CreateOrGetCustomOp(const AscendString &op_type, OpBackend backend, OpRegistrationPriority priority,
49+ OpEngine engine);
44 BaseCustomOp *GetCustomOpCommonCapability(const AscendString &op_type, CustomOpCapability capability);50 BaseCustomOp *GetCustomOpCommonCapability(const AscendString &op_type, CustomOpCapability capability);
51+ BaseCustomOp *GetCustomOpCommonCapability(const AscendString &op_type, CustomOpCapability capability,
52+ OpRegistrationPriority priority, OpEngine engine);
45 void RemoveCustomOps(const std::vector<AscendString> &op_types);53 void RemoveCustomOps(const std::vector<AscendString> &op_types);
46 ArgsRefreshStrategy GetArgsRefreshStrategy(const AscendString &op_type);54 ArgsRefreshStrategy GetArgsRefreshStrategy(const AscendString &op_type);
47 bool IsAddressRefreshable(const AscendString &op_type);55 bool IsAddressRefreshable(const AscendString &op_type);
48 bool HasCreator(const AscendString &op_type) const;56 bool HasCreator(const AscendString &op_type) const;
49 bool HasCreator(const AscendString &op_type, OpBackend backend) const;57 bool HasCreator(const AscendString &op_type, OpBackend backend) const;
58+ bool HasCreator(const AscendString &op_type, OpBackend backend, OpRegistrationPriority priority,
59+ OpEngine engine) const;
50 bool HasCustomOp(const AscendString &op_type) const;60 bool HasCustomOp(const AscendString &op_type) const;
51 graphStatus GetAllRegisteredOps(std::vector<AscendString> &all_registered_ops) const;61 graphStatus GetAllRegisteredOps(std::vector<AscendString> &all_registered_ops) const;
52 graphStatus LoadCustomOpsPartition(const uint8_t *data, size_t len);62 graphStatus LoadCustomOpsPartition(const uint8_t *data, size_t len);
@@ -54,14 +64,21 @@ class CustomOpRegistry {
54 64 
55 private:65 private:
56 struct Impl;66 struct Impl;
57- BaseCustomOp *CreateOrGetCustomOpLocked(const AscendString &op_type, OpBackend backend);67+ BaseCustomOp *CreateOrGetCustomOpLocked(const AscendString &op_type, OpBackend backend,
58- BaseCustomOp *CacheCustomOpLocked(const AscendString &op_type, OpBackend backend,68+ OpRegistrationPriority priority, OpEngine engine);
59- std::unique_ptr<BaseCustomOp> base_custom_op);69+ BaseCustomOp *CacheCustomOpLocked(const AscendString &op_type, OpBackend backend, OpRegistrationPriority priority,
70+ OpEngine engine, std::unique_ptr<BaseCustomOp> base_custom_op);
60 71 
61 mutable std::mutex mu_;72 mutable std::mutex mu_;
62 std::vector<CustomOpSoHandlePtr> so_handles_;73 std::vector<CustomOpSoHandlePtr> so_handles_;
63- std::map<AscendString, std::map<OpBackend, BaseOpCreator>> creators_;74+ using BackendCreatorMap = std::map<OpBackend, BaseOpCreator>;
64- std::map<AscendString, std::map<OpBackend, std::shared_ptr<BaseCustomOp>>> custom_ops_;75+ using PriorityCreatorMap = std::map<OpRegistrationPriority, BackendCreatorMap>;
76+ using EngineCreatorMap = std::map<OpEngine, PriorityCreatorMap>;
77+ using BackendInstanceMap = std::map<OpBackend, std::shared_ptr<BaseCustomOp>>;
78+ using PriorityInstanceMap = std::map<OpRegistrationPriority, BackendInstanceMap>;
79+ using EngineInstanceMap = std::map<OpEngine, PriorityInstanceMap>;
80+ std::map<AscendString, EngineCreatorMap> creators_;
81+ std::map<AscendString, EngineInstanceMap> custom_ops_;
65 std::shared_ptr<Impl> impl_;82 std::shared_ptr<Impl> impl_;
66};83};
67 84 
@@ -15,6 +15,7 @@
15#include "graph/debug/ge_attr_define.h"15#include "graph/debug/ge_attr_define.h"
16#include "register/hidden_inputs_func_registry.h"16#include "register/hidden_inputs_func_registry.h"
17#include "graph/args_format_desc.h"17#include "graph/args_format_desc.h"
18+#include "framework/common/debug/ge_log.h"
18#include "exe_graph/lowering/bg_kernel_context_extend.h"19#include "exe_graph/lowering/bg_kernel_context_extend.h"
19 20 
20namespace ge {21namespace ge {
@@ -23,6 +23,7 @@
23#include "common/op_tiling/tiling_memcheck.h"23#include "common/op_tiling/tiling_memcheck.h"
24#include "engine/aicore/fe_rt2_common.h"24#include "engine/aicore/fe_rt2_common.h"
25#include "graph/args_format_desc.h"25#include "graph/args_format_desc.h"
26+#include "framework/common/debug/ge_log.h"
26#include "graph/utils/op_desc_utils.h"27#include "graph/utils/op_desc_utils.h"
27#include "graph/load/model_manager/task_info/args_format/args_format_utils.h"28#include "graph/load/model_manager/task_info/args_format/args_format_utils.h"
28 29 
@@ -31,6 +31,7 @@
31#include "graph/utils/args_format_desc_utils.h"31#include "graph/utils/args_format_desc_utils.h"
32#include "graph/utils/math_util.h"32#include "graph/utils/math_util.h"
33#include "graph/args_format_desc.h"33#include "graph/args_format_desc.h"
34+#include "framework/common/debug/ge_log.h"
34#include "graph/utils/op_desc_utils.h"35#include "graph/utils/op_desc_utils.h"
35#include "graph/utils/node_utils.h"36#include "graph/utils/node_utils.h"
36#include "graph/custom_op.h"37#include "graph/custom_op.h"
@@ -14,6 +14,7 @@
14#include "graph/load/model_manager/davinci_model.h"14#include "graph/load/model_manager/davinci_model.h"
15#include "graph/load/model_manager/model_utils.h"15#include "graph/load/model_manager/model_utils.h"
16#include "graph/args_format_desc.h"16#include "graph/args_format_desc.h"
17+#include "framework/common/debug/ge_log.h"
17#include "rt_external_device.h"18#include "rt_external_device.h"
18 19 
19namespace ge {20namespace ge {
@@ -12,6 +12,7 @@
12 12 
13#include "rt_external_mem.h"13#include "rt_external_mem.h"
14#include "graph/args_format_desc.h"14#include "graph/args_format_desc.h"
15+#include "framework/common/debug/ge_log.h"
15#include "graph/load/model_manager/davinci_model.h"16#include "graph/load/model_manager/davinci_model.h"
16#include "graph/load/model_manager/model_utils.h"17#include "graph/load/model_manager/model_utils.h"
17#include "rt_external_device.h"18#include "rt_external_device.h"
@@ -30,6 +30,7 @@
30#include "common/utils/executor_utils.h"30#include "common/utils/executor_utils.h"
31#include "runtime/subscriber/global_profiler.h"31#include "runtime/subscriber/global_profiler.h"
32#include "graph/args_format_desc.h"32#include "graph/args_format_desc.h"
33+#include "framework/common/debug/ge_log.h"
33#include "acl/acl_rt.h"34#include "acl/acl_rt.h"
34 35 
35namespace ge {36namespace ge {
@@ -19,6 +19,7 @@
19#include "base/registry/op_impl_space_registry_v2.h"19#include "base/registry/op_impl_space_registry_v2.h"
20#include "graph/utils/op_desc_utils.h"20#include "graph/utils/op_desc_utils.h"
21#include "graph/args_format_desc.h"21#include "graph/args_format_desc.h"
22+#include "framework/common/debug/ge_log.h"
22#include "graph/utils/graph_utils.h"23#include "graph/utils/graph_utils.h"
23// compatible tiling need24// compatible tiling need
24#include "register/op_tiling_registry.h"25#include "register/op_tiling_registry.h"
@@ -16,6 +16,7 @@
16#include "base/registry/op_impl_space_registry_v2.h"16#include "base/registry/op_impl_space_registry_v2.h"
17#include "graph/utils/math_util.h"17#include "graph/utils/math_util.h"
18#include "graph/args_format_desc.h"18#include "graph/args_format_desc.h"
19+#include "framework/common/debug/ge_log.h"
19#include "common/checker.h"20#include "common/checker.h"
20#include "engine/node_converter_utils.h"21#include "engine/node_converter_utils.h"
21#include "exe_graph/lowering/shape_utils.h"22#include "exe_graph/lowering/shape_utils.h"
@@ -14,6 +14,7 @@
14#include "base/registry/op_impl_space_registry_v2.h"14#include "base/registry/op_impl_space_registry_v2.h"
15#include "graph/utils/math_util.h"15#include "graph/utils/math_util.h"
16#include "graph/args_format_desc.h"16#include "graph/args_format_desc.h"
17+#include "framework/common/debug/ge_log.h"
17#include "common/checker.h"18#include "common/checker.h"
18#include "engine/node_converter_utils.h"19#include "engine/node_converter_utils.h"
19#include "exe_graph/lowering/shape_utils.h"20#include "exe_graph/lowering/shape_utils.h"
@@ -25,6 +25,8 @@
25#include "graph_builder_utils.h"25#include "graph_builder_utils.h"
26#include "host_kernels/kernel.h"26#include "host_kernels/kernel.h"
27#include "host_kernels/kernel_factory.h"27#include "host_kernels/kernel_factory.h"
28+#include "register/graph_register.h"
29+#include "register/op_kernel_registry.h"
28#include "graph/utils/constant_utils.h"30#include "graph/utils/constant_utils.h"
29#include "api/gelib/gelib.h"31#include "api/gelib/gelib.h"
30#include "securec.h"32#include "securec.h"
@@ -75,6 +77,10 @@ class TestHostCustomFoldOp : public HostCpuExecuteOp {
75 77 
76int32_t TestHostCustomFoldOp::execute_count_ = 0;78int32_t TestHostCustomFoldOp::execute_count_ = 0;
77REG_OP_BACKEND(TestHostCustomFoldOp, "HostCustomFold", ge::OpBackend::kHostCPU);79REG_OP_BACKEND(TestHostCustomFoldOp, "HostCustomFold", ge::OpBackend::kHostCPU);
80+REG_OP_WITH_PRIORITY(TestHostCustomFoldOp, "BottomHostFold", ge::OpBackend::kHostCPU,
81+ ge::OpRegistrationPriority::kBottom, ge::OpEngine::kHostCpu);
82+REG_OP_WITH_PRIORITY(TestHostCustomFoldOp, "TopHostFold", ge::OpBackend::kHostCPU, ge::OpRegistrationPriority::kTop,
83+ ge::OpEngine::kHostCpu);
78 84 
79class TestHostCustomFoldFailureOp final : public HostCpuExecuteOp {85class TestHostCustomFoldFailureOp final : public HostCpuExecuteOp {
80 public:86 public:
@@ -82,9 +88,28 @@ class TestHostCustomFoldFailureOp final : public HostCpuExecuteOp {
82 return GRAPH_FAILED;88 return GRAPH_FAILED;
83 }89 }
84};90};
91+REG_OP_WITH_PRIORITY(TestHostCustomFoldFailureOp, "BottomHostFoldFailure", ge::OpBackend::kHostCPU,
92+ ge::OpRegistrationPriority::kBottom, ge::OpEngine::kHostCpu);
93+REG_OP_WITH_PRIORITY(TestHostCustomFoldOp, "PriorityMaskHostFold", ge::OpBackend::kHostCPU,
94+ ge::OpRegistrationPriority::kBottom, ge::OpEngine::kHostCpu);
95+REG_OP_WITH_PRIORITY(TestHostCustomFoldFailureOp, "PriorityMaskHostFold", ge::OpBackend::kHostCPU,
96+ ge::OpRegistrationPriority::kTop, ge::OpEngine::kHostCpu);
85 97 
86class TestNonHostCustomFoldOp final : public BaseCustomOp {};98class TestNonHostCustomFoldOp final : public BaseCustomOp {};
87 99 
100+class TestLegacyHostCpuOp final : public HostCpuOp {
101+ public:
102+ graphStatus Compute(Operator &, const std::map<std::string, const ge::Tensor> &,
103+ std::map<std::string, ge::Tensor> &) override {
104+ ++compute_count_;
105+ return GRAPH_SUCCESS;
106+ }
107+ 
108+ static int32_t compute_count_;
109+};
110+ 
111+int32_t TestLegacyHostCpuOp::compute_count_ = 0;
112+ 
88class TestAddNKernel : public Kernel {113class TestAddNKernel : public Kernel {
89 public:114 public:
90 Status Compute(const ge::OpDescPtr op_desc_ptr, const std::vector<ge::ConstGeTensorPtr> &input,115 Status Compute(const ge::OpDescPtr op_desc_ptr, const std::vector<ge::ConstGeTensorPtr> &input,
@@ -261,6 +286,31 @@ void SetWeightForConstNode(NodePtr &const_node) {
261 ConstantUtils::SetWeight(const_node->GetOpDesc(), 0, tensor);286 ConstantUtils::SetWeight(const_node->GetOpDesc(), 0, tensor);
262}287}
263 288 
289+GeTensorPtr MakeHostCpuInputTensor() {
290+ auto input_tensor = MakeShared<GeTensor>();
291+ if (input_tensor == nullptr) {
292+ return nullptr;
293+ }
294+ input_tensor->MutableTensorDesc().SetShape(GeShape({3}));
295+ input_tensor->MutableTensorDesc().SetOriginShape(GeShape({3}));
296+ input_tensor->MutableTensorDesc().SetFormat(FORMAT_NCHW);
297+ input_tensor->MutableTensorDesc().SetOriginFormat(FORMAT_NCHW);
298+ input_tensor->MutableTensorDesc().SetDataType(DT_UINT8);
299+ input_tensor->MutableTensorDesc().SetOriginDataType(DT_UINT8);
300+ if (input_tensor->SetData(std::vector<uint8_t>{1, 2, 3}) != SUCCESS) {
301+ return nullptr;
302+ }
303+ return input_tensor;
304+}
305+ 
306+NodePtr BuildHostCpuFoldNode(const std::string &op_type) {
307+ auto builder = ut::GraphBuilder("test");
308+ auto input = builder.AddNode("input", CONSTANT, 0, 1, FORMAT_NCHW, DT_UINT8, {3});
309+ auto output = builder.AddNode("output", op_type, 1, 1, FORMAT_NCHW, DT_UINT8, {3});
310+ builder.AddDataEdge(input, 0, output, 0);
311+ return output;
312+}
313+ 
264/**314/**
265 * netoutput1315 * netoutput1
266 * |316 * |
@@ -1042,6 +1092,111 @@ TEST_F(UtestGraphPassesConstantFoldingPass, testComputeWithHostCpuKernel) {
1042 EXPECT_EQ(ret, UNSUPPORTED);1092 EXPECT_EQ(ret, UNSUPPORTED);
1043}1093}
1044 1094 
1095+TEST_F(UtestGraphPassesConstantFoldingPass, bottom_priority_host_cpu_op_runs_before_legacy_kernel) {
1096+ TestHostCustomFoldOp::execute_count_ = 0;
1097+ TestLegacyHostCpuOp::compute_count_ = 0;
1098+ const std::string op_type = "BottomHostFold";
1099+ auto node = BuildHostCpuFoldNode(op_type);
1100+ ASSERT_NE(node, nullptr);
1101+ auto input_tensor = MakeHostCpuInputTensor();
1102+ ASSERT_NE(input_tensor, nullptr);
1103+ std::vector<ConstGeTensorPtr> inputs{input_tensor};
1104+ std::vector<GeTensorPtr> outputs;
1105+ 
1106+ OpKernelRegistry::GetInstance().RegisterHostCpuOp(op_type, []() -> HostCpuOp * { return new TestLegacyHostCpuOp(); });
1107+ EXPECT_EQ(ConstantFoldingPass::RunOpKernel(node, inputs, outputs), SUCCESS);
1108+ EXPECT_EQ(TestHostCustomFoldOp::execute_count_, 1);
1109+ EXPECT_EQ(TestLegacyHostCpuOp::compute_count_, 0);
1110+ ASSERT_EQ(outputs.size(), 1U);
1111+ EXPECT_EQ(outputs[0]->GetData().GetData()[0], 1U);
1112+}
1113+ 
1114+TEST_F(UtestGraphPassesConstantFoldingPass, bottom_priority_host_cpu_op_failure_does_not_retry_legacy_kernel) {
1115+ TestLegacyHostCpuOp::compute_count_ = 0;
1116+ const std::string op_type = "BottomHostFoldFailure";
1117+ auto node = BuildHostCpuFoldNode(op_type);
1118+ ASSERT_NE(node, nullptr);
1119+ auto input_tensor = MakeHostCpuInputTensor();
1120+ ASSERT_NE(input_tensor, nullptr);
1121+ std::vector<ConstGeTensorPtr> inputs{input_tensor};
1122+ std::vector<GeTensorPtr> outputs;
1123+ 
1124+ OpKernelRegistry::GetInstance().RegisterHostCpuOp(op_type, []() -> HostCpuOp * { return new TestLegacyHostCpuOp(); });
1125+ EXPECT_NE(ConstantFoldingPass::RunOpKernel(node, inputs, outputs), SUCCESS);
1126+ EXPECT_EQ(TestLegacyHostCpuOp::compute_count_, 0);
1127+}
1128+ 
1129+TEST_F(UtestGraphPassesConstantFoldingPass, run_op_kernel_ignores_top_host_cpu_registration) {
1130+ TestHostCustomFoldOp::execute_count_ = 0;
1131+ TestLegacyHostCpuOp::compute_count_ = 0;
1132+ const std::string op_type = "TopHostFold";
1133+ auto node = BuildHostCpuFoldNode(op_type);
1134+ ASSERT_NE(node, nullptr);
1135+ auto input_tensor = MakeHostCpuInputTensor();
1136+ ASSERT_NE(input_tensor, nullptr);
1137+ std::vector<ConstGeTensorPtr> inputs{input_tensor};
1138+ std::vector<GeTensorPtr> outputs;
1139+ 
1140+ OpKernelRegistry::GetInstance().RegisterHostCpuOp(op_type, []() -> HostCpuOp * { return new TestLegacyHostCpuOp(); });
1141+ EXPECT_EQ(ConstantFoldingPass::RunOpKernel(node, inputs, outputs), SUCCESS);
1142+ EXPECT_EQ(TestHostCustomFoldOp::execute_count_, 0);
1143+ EXPECT_EQ(TestLegacyHostCpuOp::compute_count_, 1);
1144+}
1145+ 
1146+TEST_F(UtestGraphPassesConstantFoldingPass, top_custom_op_runs_before_legacy_host_cpu_kernel) {
1147+ TestHostCustomFoldOp::execute_count_ = 0;
1148+ TestLegacyHostCpuOp::compute_count_ = 0;
1149+ auto builder = ut::GraphBuilder("test");
1150+ auto input = builder.AddNode("input", CONSTANT, 0, 1, FORMAT_NCHW, DT_UINT8, {3});
1151+ auto output = builder.AddNode("output", HostCustomFold, 1, 1, FORMAT_NCHW, DT_UINT8, {3});
1152+ builder.AddDataEdge(input, 0, output, 0);
1153+ ASSERT_NE(builder.GetGraph(), nullptr);
1154+ 
1155+ auto input_tensor = MakeHostCpuInputTensor();
1156+ ASSERT_NE(input_tensor, nullptr);
1157+ ConstantUtils::SetWeight(input->GetOpDesc(), 0, input_tensor);
1158+ 
1159+ OpKernelRegistry::GetInstance().RegisterHostCpuOp(HostCustomFold,
1160+ []() -> HostCpuOp * { return new TestLegacyHostCpuOp(); });
1161+ ConstantFoldingPass pass;
1162+ std::vector<GeTensorPtr> outputs;
1163+ EXPECT_EQ(pass.ComputePotentialWeight(output, outputs), SUCCESS);
1164+ EXPECT_EQ(TestHostCustomFoldOp::execute_count_, 1);
1165+ EXPECT_EQ(TestLegacyHostCpuOp::compute_count_, 0);
1166+}
1167+ 
1168+TEST_F(UtestGraphPassesConstantFoldingPass, bottom_priority_host_cpu_registration_runs_with_top_registration) {
1169+ TestHostCustomFoldOp::execute_count_ = 0;
1170+ TestLegacyHostCpuOp::compute_count_ = 0;
1171+ const std::string op_type = "PriorityMaskHostFold";
1172+ auto node = BuildHostCpuFoldNode(op_type);
1173+ ASSERT_NE(node, nullptr);
1174+ auto input_tensor = MakeHostCpuInputTensor();
1175+ ASSERT_NE(input_tensor, nullptr);
1176+ std::vector<ConstGeTensorPtr> inputs{input_tensor};
1177+ std::vector<GeTensorPtr> outputs;
1178+ 
1179+ OpKernelRegistry::GetInstance().RegisterHostCpuOp(op_type, []() -> HostCpuOp * { return new TestLegacyHostCpuOp(); });
1180+ EXPECT_EQ(ConstantFoldingPass::RunOpKernel(node, inputs, outputs), SUCCESS);
1181+ EXPECT_EQ(TestHostCustomFoldOp::execute_count_, 1);
1182+ EXPECT_EQ(TestLegacyHostCpuOp::compute_count_, 0);
1183+}
1184+ 
1185+TEST_F(UtestGraphPassesConstantFoldingPass, priority_registry_miss_runs_legacy_kernel) {
1186+ TestLegacyHostCpuOp::compute_count_ = 0;
1187+ const std::string op_type = "PriorityHostFoldLegacyOnly";
1188+ auto node = BuildHostCpuFoldNode(op_type);
1189+ ASSERT_NE(node, nullptr);
1190+ auto input_tensor = MakeHostCpuInputTensor();
1191+ ASSERT_NE(input_tensor, nullptr);
1192+ std::vector<ConstGeTensorPtr> inputs{input_tensor};
1193+ std::vector<GeTensorPtr> outputs;
1194+ 
1195+ OpKernelRegistry::GetInstance().RegisterHostCpuOp(op_type, []() -> HostCpuOp * { return new TestLegacyHostCpuOp(); });
1196+ EXPECT_EQ(ConstantFoldingPass::RunOpKernel(node, inputs, outputs), SUCCESS);
1197+ EXPECT_EQ(TestLegacyHostCpuOp::compute_count_, 1);
1198+}
1199+ 
1045TEST_F(UtestGraphPassesConstantFoldingPass, test_compute_with_host_cpu_custom_op) {1200TEST_F(UtestGraphPassesConstantFoldingPass, test_compute_with_host_cpu_custom_op) {
1046 TestHostCustomFoldOp::execute_count_ = 0;1201 TestHostCustomFoldOp::execute_count_ = 0;
1047 auto builder = ut::GraphBuilder("test");1202 auto builder = ut::GraphBuilder("test");
@@ -0,0 +1,169 @@
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 <gtest/gtest.h>
12+ 
13+#include <memory>
14+#include <stdexcept>
15+ 
16+#include "graph/custom_op.h"
17+#include "graph/custom_op_factory.h"
18+#include "graph/custom_op_registry.h"
19+ 
20+namespace ge {
21+namespace {
22+class TestPriorityOp final : public BaseCustomOp {};
23+ 
24+class TestPriorityOtherOp final : public BaseCustomOp {};
25+ 
26+class TestPriorityRegisteredOp final : public BaseCustomOp {};
27+REG_OP_WITH_PRIORITY(TestPriorityRegisteredOp, "CustomOpRegistryPriorityMacroOp", OpBackend::kDevice,
28+ OpRegistrationPriority::kBottom, OpEngine::kAiCore);
29+ 
30+class TestPriorityShapeInferOp final : public ShapeInferOp {
31+ public:
32+ graphStatus InferShape(gert::InferShapeContext *) override {
33+ return GRAPH_SUCCESS;
34+ }
35+ 
36+ graphStatus InferDataType(gert::InferDataTypeContext *) override {
37+ return GRAPH_SUCCESS;
38+ }
39+};
40+REG_OP_WITH_PRIORITY(TestPriorityShapeInferOp, "CustomOpRegistryCapabilityMacroOp", OpBackend::kDevice,
41+ OpRegistrationPriority::kBottom, OpEngine::kAiCore);
42+ 
43+class TestDefaultRegisteredOp final : public BaseCustomOp {};
44+REG_OP_BACKEND(TestDefaultRegisteredOp, "CustomOpRegistryDefaultMacroOp", OpBackend::kHostCPU);
45+} // namespace
46+ 
47+TEST(CustomOpRegistryPriorityTest, RegisterAndQueryByEngineBackendAndPriority) {
48+ CustomOpRegistry registry;
49+ const AscendString op_type("CustomOpRegistryPriorityRegisterOp");
50+ const OpEngine engine = OpEngine::kAiCore;
51+ const auto ret =
52+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine,
53+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOp>(); });
54+ ASSERT_EQ(ret, GRAPH_SUCCESS);
55+ 
56+ EXPECT_TRUE(registry.HasCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine));
57+ EXPECT_FALSE(registry.HasCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine));
58+ EXPECT_FALSE(registry.HasCreator(op_type, OpBackend::kDevice, OpRegistrationPriority::kBottom, engine));
59+ EXPECT_FALSE(
60+ registry.HasCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, OpEngine::kVectorCore));
61+ EXPECT_FALSE(registry.HasCreator(op_type, OpBackend::kHostCPU));
62+}
63+ 
64+TEST(CustomOpRegistryPriorityTest, TopAndBottomCanBeRegisteredIndependently) {
65+ CustomOpRegistry registry;
66+ const AscendString op_type("CustomOpRegistryPrioritySelectOp");
67+ const OpEngine engine = OpEngine::kAiCore;
68+ ASSERT_EQ(
69+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine,
70+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOp>(); }),
71+ GRAPH_SUCCESS);
72+ ASSERT_EQ(registry.RegisterCreator(
73+ op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine,
74+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOtherOp>(); }),
75+ GRAPH_SUCCESS);
76+ 
77+ EXPECT_TRUE(registry.HasCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine));
78+ EXPECT_TRUE(registry.HasCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine));
79+}
80+ 
81+TEST(CustomOpRegistryPriorityTest, CreateOrGetKeepsInstancesIndependentByPriority) {
82+ CustomOpRegistry registry;
83+ const AscendString op_type("CustomOpRegistryPriorityInstanceOp");
84+ const OpEngine engine = OpEngine::kAiCore;
85+ ASSERT_EQ(
86+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine,
87+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOp>(); }),
88+ GRAPH_SUCCESS);
89+ ASSERT_EQ(
90+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine,
91+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOp>(); }),
92+ GRAPH_SUCCESS);
93+ 
94+ auto *top_first = registry.CreateOrGetCustomOp(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine);
95+ auto *top_second = registry.CreateOrGetCustomOp(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine);
96+ auto *bottom = registry.CreateOrGetCustomOp(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine);
97+ ASSERT_NE(top_first, nullptr);
98+ EXPECT_EQ(top_first, top_second);
99+ ASSERT_NE(bottom, nullptr);
100+ EXPECT_NE(top_first, bottom);
101+}
102+ 
103+TEST(CustomOpRegistryPriorityTest, RejectsInvalidAndDuplicateRegistration) {
104+ CustomOpRegistry registry;
105+ const AscendString op_type("CustomOpRegistryPriorityInvalidOp");
106+ const OpEngine engine = OpEngine::kAiCore;
107+ EXPECT_EQ(
108+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine, BaseOpCreator()),
109+ GRAPH_PARAM_INVALID);
110+ EXPECT_EQ(
111+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, static_cast<OpEngine>(100U),
112+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOp>(); }),
113+ GRAPH_PARAM_INVALID);
114+ ASSERT_EQ(
115+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine,
116+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOp>(); }),
117+ GRAPH_SUCCESS);
118+ EXPECT_EQ(
119+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, engine,
120+ []() -> std::unique_ptr<BaseCustomOp> { return std::make_unique<TestPriorityOp>(); }),
121+ GRAPH_FAILED);
122+}
123+ 
124+TEST(CustomOpRegistryPriorityTest, CreatorExceptionPropagates) {
125+ CustomOpRegistry registry;
126+ const AscendString op_type("CustomOpRegistryPriorityExceptionOp");
127+ const OpEngine engine = OpEngine::kAiCore;
128+ ASSERT_EQ(
129+ registry.RegisterCreator(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine,
130+ []() -> std::unique_ptr<BaseCustomOp> { throw std::runtime_error("creator failed"); }),
131+ GRAPH_SUCCESS);
132+ 
133+ EXPECT_THROW(
134+ (void)registry.CreateOrGetCustomOp(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, engine),
135+ std::runtime_error);
136+}
137+ 
138+TEST(CustomOpRegistryPriorityTest, MacroRegistersExplicitPriorityCreator) {
139+ const AscendString op_type("CustomOpRegistryPriorityMacroOp");
140+ const OpEngine engine = OpEngine::kAiCore;
141+ EXPECT_TRUE(CustomOpFactory::IsExistOp(op_type, OpBackend::kDevice, OpRegistrationPriority::kBottom, engine));
142+ EXPECT_NE(CustomOpFactory::CreateOrGetCustomOp(op_type, OpBackend::kDevice, OpRegistrationPriority::kBottom, engine),
143+ nullptr);
144+}
145+ 
146+TEST(CustomOpRegistryPriorityTest, CommonCapabilityCanQueryExplicitPriorityAndEngine) {
147+ const AscendString op_type("CustomOpRegistryCapabilityMacroOp");
148+ const OpEngine engine = OpEngine::kAiCore;
149+ auto *custom_op =
150+ CustomOpFactory::GetCustomOpCommonCapability<ShapeInferOp>(op_type, OpRegistrationPriority::kBottom, engine);
151+ EXPECT_NE(custom_op, nullptr);
152+ EXPECT_EQ(CustomOpFactory::GetCustomOpCommonCapability<ShapeInferOp>(op_type, OpRegistrationPriority::kTop, engine),
153+ nullptr);
154+ EXPECT_EQ(CustomOpFactory::GetCustomOpCommonCapability<ShapeInferOp>(op_type, OpRegistrationPriority::kBottom,
155+ OpEngine::kVectorCore),
156+ nullptr);
157+}
158+ 
159+TEST(CustomOpRegistryPriorityTest, RegOpBackendUsesCustomEngineTopPriority) {
160+ const AscendString op_type("CustomOpRegistryDefaultMacroOp");
161+ EXPECT_TRUE(CustomOpFactory::IsExistOp(op_type, OpBackend::kHostCPU));
162+ EXPECT_TRUE(
163+ CustomOpFactory::IsExistOp(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, OpEngine::kCustom));
164+ EXPECT_FALSE(
165+ CustomOpFactory::IsExistOp(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kBottom, OpEngine::kCustom));
166+ EXPECT_FALSE(
167+ CustomOpFactory::IsExistOp(op_type, OpBackend::kHostCPU, OpRegistrationPriority::kTop, OpEngine::kAiCore));
168+}
169+} // namespace ge