diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index 16edbbf5af..74ed0416b2 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -3495,6 +3495,10 @@ struct OrtApi { * \since Version 1.13 */ const OrtTrainingApi*(ORT_API_CALL* GetTrainingApi)(uint32_t version)NO_EXCEPTION ORT_ALL_ARGS_NONNULL; + +#ifdef __cplusplus + OrtApi(const OrtApi&)=delete; // Prevent users from accidentally copying the API structure, it should always be passed as a pointer +#endif }; /* @@ -3503,7 +3507,6 @@ struct OrtApi { * 2 Create an OrtCustomOp structure for each op and add them to the domain * 3 Call OrtAddCustomOpDomain to add the custom domain of ops to the session options */ -#define OrtCustomOpApi OrtApi // Specifies some characteristics of inputs/outputs of custom ops: // Specify if the inputs/outputs are one of: diff --git a/onnxruntime/core/providers/coreml/coreml_execution_provider.cc b/onnxruntime/core/providers/coreml/coreml_execution_provider.cc index 28f2d4a23d..dbaab52d11 100644 --- a/onnxruntime/core/providers/coreml/coreml_execution_provider.cc +++ b/onnxruntime/core/providers/coreml/coreml_execution_provider.cc @@ -139,7 +139,7 @@ common::Status CoreMLExecutionProvider::Compile(const std::vector(state); const size_t num_inputs = ort.KernelContext_GetInputCount(context); @@ -243,7 +243,7 @@ common::Status CoreMLExecutionProvider::Compile(const std::vector& fuse ORT_UNUSED_PARAMETER(state); }; - compute_info.compute_func = [](FunctionState state, const OrtCustomOpApi* api, OrtKernelContext* context) { + compute_info.compute_func = [](FunctionState state, const OrtApi* api, OrtKernelContext* context) { Ort::CustomOpApi ort{*api}; ort_dnnl::DnnlSubgraphPrimitive* subgraph_primitive = reinterpret_cast(state); diff --git a/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc b/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc index ece24007e4..1f1ee2239f 100644 --- a/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc +++ b/onnxruntime/core/providers/migraphx/migraphx_execution_provider.cc @@ -1038,7 +1038,7 @@ Status MIGraphXExecutionProvider::Compile(const std::vector& delete static_cast(state); }; - compute_info.compute_func = [](FunctionState state, const OrtCustomOpApi* api, OrtKernelContext* context) { + compute_info.compute_func = [](FunctionState state, const OrtApi* api, OrtKernelContext* context) { Ort::CustomOpApi ort{*api}; MIGraphXFuncState* mgx_state = reinterpret_cast(state); std::unordered_map& map_input_name_index = mgx_state->input_name_indexes; diff --git a/onnxruntime/core/providers/nnapi/nnapi_builtin/nnapi_execution_provider.cc b/onnxruntime/core/providers/nnapi/nnapi_builtin/nnapi_execution_provider.cc index f31bd9307f..52c4cb5732 100644 --- a/onnxruntime/core/providers/nnapi/nnapi_builtin/nnapi_execution_provider.cc +++ b/onnxruntime/core/providers/nnapi/nnapi_builtin/nnapi_execution_provider.cc @@ -297,7 +297,7 @@ common::Status NnapiExecutionProvider::Compile(const std::vector(state); const size_t num_inputs = ort.KernelContext_GetInputCount(context); diff --git a/onnxruntime/core/providers/nuphar/nuphar_execution_provider.cc b/onnxruntime/core/providers/nuphar/nuphar_execution_provider.cc index 806a593e9d..8e90a5ef76 100644 --- a/onnxruntime/core/providers/nuphar/nuphar_execution_provider.cc +++ b/onnxruntime/core/providers/nuphar/nuphar_execution_provider.cc @@ -453,7 +453,7 @@ Status NupharExecutionProvider::Compile( // Compute function // This is similar to the original OpKernel's Compute() info.compute_func = - [](FunctionState state, const OrtCustomOpApi*, OrtKernelContext* op_kernel_context) { + [](FunctionState state, const OrtApi*, OrtKernelContext* op_kernel_context) { NupharKernelState* s = reinterpret_cast(state); return s->Compute(reinterpret_cast(op_kernel_context)); }; diff --git a/onnxruntime/core/providers/rknpu/rknpu_execution_provider.cc b/onnxruntime/core/providers/rknpu/rknpu_execution_provider.cc index 6afedaaf96..cfe2527bef 100644 --- a/onnxruntime/core/providers/rknpu/rknpu_execution_provider.cc +++ b/onnxruntime/core/providers/rknpu/rknpu_execution_provider.cc @@ -315,7 +315,7 @@ common::Status RknpuExecutionProvider::Compile(const std::vector(state); diff --git a/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc b/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc index 2d5e98b17b..72601345c9 100644 --- a/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc +++ b/onnxruntime/core/providers/tensorrt/tensorrt_execution_provider.cc @@ -1368,7 +1368,7 @@ common::Status TensorrtExecutionProvider::Compile(const std::vector(state); std::lock_guard lock(*(trt_state->tensorrt_mu_ptr)); diff --git a/onnxruntime/core/providers/tvm/tvm_runner.cc b/onnxruntime/core/providers/tvm/tvm_runner.cc index 117ecea680..8d2797668d 100644 --- a/onnxruntime/core/providers/tvm/tvm_runner.cc +++ b/onnxruntime/core/providers/tvm/tvm_runner.cc @@ -18,7 +18,7 @@ TVMRunner::TVMRunner(const TvmEPOptions& options, runner_ = getTVMRunnerImpl(mod, options, inputs_info, output_tensors); } -common::Status TVMRunner::operator()(FunctionState state, const OrtCustomOpApi* api, OrtKernelContext* context) { +common::Status TVMRunner::operator()(FunctionState state, const OrtApi* api, OrtKernelContext* context) { return runner_->run(api, context); } diff --git a/onnxruntime/core/providers/tvm/tvm_runner.h b/onnxruntime/core/providers/tvm/tvm_runner.h index 85d37ccec1..7eeae766f3 100644 --- a/onnxruntime/core/providers/tvm/tvm_runner.h +++ b/onnxruntime/core/providers/tvm/tvm_runner.h @@ -23,7 +23,7 @@ public: const InputsInfoMap& inputs_info, const std::vector& output_tensor); - common::Status operator()(FunctionState state, const OrtCustomOpApi* api, OrtKernelContext* context); + common::Status operator()(FunctionState state, const OrtApi* api, OrtKernelContext* context); private: std::shared_ptr runner_; diff --git a/onnxruntime/core/providers/tvm/tvm_runner_impl.h b/onnxruntime/core/providers/tvm/tvm_runner_impl.h index e9104859c7..80813f1e4d 100644 --- a/onnxruntime/core/providers/tvm/tvm_runner_impl.h +++ b/onnxruntime/core/providers/tvm/tvm_runner_impl.h @@ -27,7 +27,7 @@ public: const std::vector tensors_outputs); virtual ~RunnerImpl() = default; - virtual common::Status run(const OrtCustomOpApi* api, OrtKernelContext* context) { + virtual common::Status run(const OrtApi* api, OrtKernelContext* context) { Ort::CustomOpApi ort{*api}; set_input(ort, context); diff --git a/onnxruntime/test/providers/internal_testing/internal_testing_execution_provider.cc b/onnxruntime/test/providers/internal_testing/internal_testing_execution_provider.cc index 68e15bf73b..1ec8d72af1 100644 --- a/onnxruntime/test/providers/internal_testing/internal_testing_execution_provider.cc +++ b/onnxruntime/test/providers/internal_testing/internal_testing_execution_provider.cc @@ -215,7 +215,7 @@ common::Status InternalTestingExecutionProvider::Compile(const std::vector Status { Ort::CustomOpApi api{*c_api}; // use C++ API for convenience diff --git a/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc b/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc index 2165596178..a67284c5bc 100644 --- a/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc +++ b/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc @@ -46,9 +46,8 @@ struct OrtTensorDimensions : std::vector { }; struct KernelOne { - KernelOne(OrtApi api) - : api_(api), - ort_(api_) { + KernelOne(const OrtApi& api) + : ort_(api) { } void Compute(OrtKernelContext* context) { @@ -80,14 +79,12 @@ struct KernelOne { } private: - OrtApi api_; // keep a copy of the struct, whose ref is used in the ort_ Ort::CustomOpApi ort_; }; struct KernelTwo { - KernelTwo(OrtApi api) - : api_(api), - ort_(api_) { + KernelTwo(const OrtApi& api) + : ort_(api) { } void Compute(OrtKernelContext* context) { @@ -112,12 +109,11 @@ struct KernelTwo { } private: - OrtApi api_; // keep a copy of the struct, whose ref is used in the ort_ Ort::CustomOpApi ort_; }; struct CustomOpOne : Ort::CustomOpBase { - void* CreateKernel(OrtApi api, const OrtKernelInfo* /* info */) const { + void* CreateKernel(const OrtApi& api, const OrtKernelInfo* /* info */) const { return new KernelOne(api); }; @@ -136,7 +132,7 @@ struct CustomOpOne : Ort::CustomOpBase { } c_CustomOpOne; struct CustomOpTwo : Ort::CustomOpBase { - void* CreateKernel(OrtApi api, const OrtKernelInfo* /* info */) const { + void* CreateKernel(const OrtApi& api, const OrtKernelInfo* /* info */) const { return new KernelTwo(api); };