diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index 45a3f8c2ba..c4354cac8d 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -580,8 +580,7 @@ struct OrtCustomOp { // Returns the name of the op const char*(ORT_API_CALL* GetName)(_In_ struct OrtCustomOp* op); - // Returns the type of the execution provider - // If the function pointer is null, use CPU execution provider by default + // Returns the type of the execution provider, return nullptr to use CPU execution provider const char*(ORT_API_CALL* GetExecutionProviderType)(_In_ struct OrtCustomOp* op); // Returns the count and types of the input & output tensors diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index 0bae3e83ce..df15d2d2ec 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -278,33 +278,6 @@ struct CustomOpApi { const OrtCustomOpApi& api_; }; -namespace CustomOpImpl { -// SFINAE definition to determine whether class T as GetExecutionProviderType interface -template -class HasProvider { - template ().GetExecutionProviderType(std::declval()...))> - static std::true_type test(int); - template - static std::false_type test(...); - - public: - static constexpr bool value = decltype(test(0))::value; -}; - -// SFINAE definitions to get the execution provider of op. -// If type T has GetExecutionProviderType, use the result of op->GetExecutionProviderType(). -// Otherwise, use kCpuExecutionProvider by default. -template -std::enable_if_t::value, const char*> GetProvider(T* op) { - return op->GetExecutionProviderType(); -} -template -std::enable_if_t::value, const char*> GetProvider(T*) { - return "CPUExecutionProvider"; -} -} // namespace CustomOpImpl - template struct CustomOpBase : OrtCustomOp { CustomOpBase() { @@ -312,8 +285,7 @@ struct CustomOpBase : OrtCustomOp { OrtCustomOp::CreateKernel = [](OrtCustomOp* this_, const OrtCustomOpApi* api, const OrtKernelInfo* info) { return static_cast(this_)->CreateKernel(*api, info); }; OrtCustomOp::GetName = [](OrtCustomOp* this_) { return static_cast(this_)->GetName(); }; - // If OrtCustomOp does not have a definition of GetExecutionProviderType, use CPUExecutorProvider by default - OrtCustomOp::GetExecutionProviderType = [](OrtCustomOp* this_) { return CustomOpImpl::GetProvider(static_cast(this_)); }; + OrtCustomOp::GetExecutionProviderType = [](OrtCustomOp* this_) { return static_cast(this_)->GetExecutionProviderType(); }; OrtCustomOp::GetInputTypeCount = [](OrtCustomOp* this_) { return static_cast(this_)->GetInputTypeCount(); }; OrtCustomOp::GetInputType = [](OrtCustomOp* this_, size_t index) { return static_cast(this_)->GetInputType(index); }; @@ -324,6 +296,9 @@ struct CustomOpBase : OrtCustomOp { OrtCustomOp::KernelCompute = [](void* op_kernel, OrtKernelContext* context) { static_cast(op_kernel)->Compute(context); }; OrtCustomOp::KernelDestroy = [](void* op_kernel) { delete static_cast(op_kernel); }; } + + // Default implementation of GetExecutionProviderType that returns nullptr to default to the CPU provider + const char* GetExecutionProviderType() const { return nullptr; } }; } // namespace Ort diff --git a/onnxruntime/core/session/custom_ops.cc b/onnxruntime/core/session/custom_ops.cc index 52ee238f4f..7d4218022a 100644 --- a/onnxruntime/core/session/custom_ops.cc +++ b/onnxruntime/core/session/custom_ops.cc @@ -135,11 +135,10 @@ common::Status CreateCustomRegistry(const std::vector& op_do def_builder.SetName(op->GetName(op)) .SetDomain(domain->domain_) .SinceVersion(1); - if (op->GetExecutionProviderType) { - def_builder.Provider(op->GetExecutionProviderType(op)); - } else { + if (const char* provider_type = op->GetExecutionProviderType(op)) + def_builder.Provider(provider_type); + else def_builder.Provider(onnxruntime::kCpuExecutionProvider); - } KernelCreateFn kernel_create_fn = [&op](const OpKernelInfo& info) -> OpKernel* { return new CustomOpKernel(info, *op); }; KernelCreateInfo create_info(def_builder.Build(), kernel_create_fn); diff --git a/onnxruntime/test/shared_lib/test_inference.cc b/onnxruntime/test/shared_lib/test_inference.cc index 940200de6b..ec834ce01e 100644 --- a/onnxruntime/test/shared_lib/test_inference.cc +++ b/onnxruntime/test/shared_lib/test_inference.cc @@ -223,10 +223,14 @@ TEST_F(CApiTest, test_pyop) { std::cout << "Test model with pyop" << std::endl; std::ofstream module("mymodule.py"); module << "class MyKernel:" << std::endl; - module << "\t" << "def __init__(self,A,B,C):" << std::endl; - module << "\t\t" << "self.a,self.b,self.c = A,B,C" << std::endl; - module << "\t" << "def compute(self,x):" << std::endl; - module << "\t\t" << "return x*2" << std::endl; + module << "\t" + << "def __init__(self,A,B,C):" << std::endl; + module << "\t\t" + << "self.a,self.b,self.c = A,B,C" << std::endl; + module << "\t" + << "def compute(self,x):" << std::endl; + module << "\t\t" + << "return x*2" << std::endl; module.close(); std::vector inputs(1); Input& input = inputs[0];