diff --git a/include/onnxruntime/core/framework/customregistry.h b/include/onnxruntime/core/framework/customregistry.h index ffa7212aef..aafe5a5df8 100644 --- a/include/onnxruntime/core/framework/customregistry.h +++ b/include/onnxruntime/core/framework/customregistry.h @@ -15,10 +15,11 @@ namespace onnxruntime { /** Represents a registry that contains both custom kernels and custom schemas. */ -class CustomRegistry : public KernelRegistry, public onnxruntime::OnnxRuntimeOpSchemaRegistry { +class CustomRegistry final { public: - CustomRegistry() = default; - ~CustomRegistry() override = default; + CustomRegistry() : + kernel_registry_(std::make_shared()), + opschema_registry_(std::make_shared()) {} /** * Register a kernel definition together with kernel factory method to this session. @@ -31,8 +32,18 @@ class CustomRegistry : public KernelRegistry, public onnxruntime::OnnxRuntimeOpS common::Status RegisterCustomKernel(KernelCreateInfo&); + common::Status RegisterOpSet(std::vector& schemas, const std::string& domain, + int baseline_opset_version, int opset_version); + + const std::shared_ptr& GetKernelRegistry(); + + const std::shared_ptr& GetOpschemaRegistry(); + private: ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(CustomRegistry); + std::shared_ptr kernel_registry_; + std::shared_ptr opschema_registry_; + }; } // namespace onnxruntime diff --git a/onnxruntime/core/framework/customregistry.cc b/onnxruntime/core/framework/customregistry.cc index a7223a28aa..a70f8bbfd2 100644 --- a/onnxruntime/core/framework/customregistry.cc +++ b/onnxruntime/core/framework/customregistry.cc @@ -5,11 +5,28 @@ namespace onnxruntime { common::Status CustomRegistry::RegisterCustomKernel(KernelDefBuilder& kernel_def_builder, const KernelCreateFn& kernel_creator) { - return Register(kernel_def_builder, kernel_creator); + return kernel_registry_->Register(kernel_def_builder, kernel_creator); } common::Status CustomRegistry::RegisterCustomKernel(KernelCreateInfo& create_info) { - return Register(std::move(create_info)); + return kernel_registry_->Register(std::move(create_info)); +} + +common::Status CustomRegistry::RegisterOpSet( + std::vector& schemas, + const std::string& domain, + int baseline_opset_version, + int opset_version) { + + return opschema_registry_->RegisterOpSet(schemas, domain, baseline_opset_version, opset_version); +} + +const std::shared_ptr& CustomRegistry::GetKernelRegistry() { + return kernel_registry_; +} + +const std::shared_ptr& CustomRegistry::GetOpschemaRegistry() { + return opschema_registry_; } } // namespace onnxruntime diff --git a/onnxruntime/core/framework/kernel_registry_manager.h b/onnxruntime/core/framework/kernel_registry_manager.h index 9f573d4a4c..b776c5ac6d 100644 --- a/onnxruntime/core/framework/kernel_registry_manager.h +++ b/onnxruntime/core/framework/kernel_registry_manager.h @@ -9,6 +9,7 @@ #include "core/common/status.h" #include "core/platform/ort_mutex.h" #include "core/graph/graph_viewer.h" +#include "core/framework/customregistry.h" namespace onnxruntime { struct KernelCreateInfo; diff --git a/onnxruntime/core/session/inference_session.cc b/onnxruntime/core/session/inference_session.cc index db33083558..2a155eb9d4 100644 --- a/onnxruntime/core/session/inference_session.cc +++ b/onnxruntime/core/session/inference_session.cc @@ -294,11 +294,13 @@ common::Status InferenceSession::RegisterCustomRegistry(std::shared_ptrGetKernelRegistry()); // if (custom_schema_registries_.empty()) // custom_schema_registries_.push_back(); - custom_schema_registries_.push_back(custom_registry); + custom_schema_registries_.push_back(custom_registry->GetOpschemaRegistry()); return Status::OK(); } diff --git a/onnxruntime/core/session/inference_session.h b/onnxruntime/core/session/inference_session.h index d17960b030..ba4a3c8f85 100644 --- a/onnxruntime/core/session/inference_session.h +++ b/onnxruntime/core/session/inference_session.h @@ -417,5 +417,10 @@ class InferenceSession { bool is_inited_ = false; // GUARDED_BY(session_mutex_) InsertCastTransformer insert_cast_transformer_; + + //CustomRegistry objects own the corresponding KernelRegistry and OnnxRuntimeOpSchemaRegistry objects. + //So its lifetime should be same as its constituents. This vector is to extend the lifetime of the owner. + std::vector> custom_registries_; + }; } // namespace onnxruntime diff --git a/onnxruntime/test/framework/opaque_kernels_test.cc b/onnxruntime/test/framework/opaque_kernels_test.cc index d6c4f425ba..fe3367c86c 100644 --- a/onnxruntime/test/framework/opaque_kernels_test.cc +++ b/onnxruntime/test/framework/opaque_kernels_test.cc @@ -296,7 +296,7 @@ TEST_F(OpaqueTypeTests, RunModel) { auto shape_def = ConstructFetchSparseShape(); EXPECT_TRUE(registry->RegisterCustomKernel(shape_def, [](const OpKernelInfo& info) { return new FetchSparseTensorShape(info); }).IsOK()); - IOnnxRuntimeOpSchemaRegistryList custom_schema_registries_ = {registry}; + IOnnxRuntimeOpSchemaRegistryList custom_schema_registries_ = {registry->GetOpschemaRegistry()}; std::unordered_map domain_to_version = {{onnxruntime::kMLDomain, 8}}; Model model("SparseTensorTest", false, ModelMetaData(), custom_schema_registries_, domain_to_version); diff --git a/onnxruntime/test/providers/provider_test_utils.h b/onnxruntime/test/providers/provider_test_utils.h index 30203135bc..216a8bc872 100644 --- a/onnxruntime/test/providers/provider_test_utils.h +++ b/onnxruntime/test/providers/provider_test_utils.h @@ -221,7 +221,8 @@ class OpTester { void AddCustomOpRegistry(std::shared_ptr registry) { // need to do some static casting so we can easily use this later - custom_schema_registries_.push_back(std::static_pointer_cast(registry)); + //UTScustom_schema_registries_.push_back(std::static_pointer_cast(registry->GetOpschemaRegistry())); + custom_schema_registries_.push_back(registry->GetOpschemaRegistry()); custom_session_registries_.push_back(std::static_pointer_cast(registry)); }