diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index 66aef37825..8ac52f5ff3 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -91,6 +91,7 @@ file(GLOB onnxruntime_test_ir_src set(onnxruntime_test_framework_src_patterns "${TEST_SRC_DIR}/framework/*.cc" + "${TEST_SRC_DIR}/framework/*.h" "${TEST_SRC_DIR}/platform/*.cc" ) diff --git a/include/onnxruntime/core/framework/execution_provider.h b/include/onnxruntime/core/framework/execution_provider.h index 8726dcf035..2f311161b8 100644 --- a/include/onnxruntime/core/framework/execution_provider.h +++ b/include/onnxruntime/core/framework/execution_provider.h @@ -38,6 +38,9 @@ struct NodeComputeInfo { }; class IExecutionProvider { + protected: + IExecutionProvider(const std::string& type) : type_{type} {} + public: virtual ~IExecutionProvider() = default; @@ -105,7 +108,7 @@ class IExecutionProvider { through the SetExecutionProvider API. Example valid return values are: kCpuExecutionProvider, kCudaExecutionProvider */ - virtual std::string Type() const = 0; + const std::string& Type() const { return type_; } /** Blocks until the device has completed all preceding requested tasks. @@ -149,6 +152,7 @@ class IExecutionProvider { std::string& dll_path); private: + const std::string type_; AllocatorMap allocators_; // convenience list of the allocators so GetAllocatorList doesn't have to build a new vector each time diff --git a/onnxruntime/core/providers/cpu/cpu_execution_provider.h b/onnxruntime/core/providers/cpu/cpu_execution_provider.h index 43c2fe347b..48fde27bc9 100644 --- a/onnxruntime/core/providers/cpu/cpu_execution_provider.h +++ b/onnxruntime/core/providers/cpu/cpu_execution_provider.h @@ -15,16 +15,21 @@ struct CPUExecutionProviderInfo { explicit CPUExecutionProviderInfo(bool use_arena) : create_arena(use_arena) {} + CPUExecutionProviderInfo() = default; }; -using FuseRuleFn = std::function>&)>; +using FuseRuleFn = std::function>&)>; // Logical device representation. class CPUExecutionProvider : public IExecutionProvider { public: - explicit CPUExecutionProvider(const CPUExecutionProviderInfo& info) { - DeviceAllocatorRegistrationInfo device_info({OrtMemTypeDefault, [](int) { return std::make_unique(); }, std::numeric_limits::max()}); + explicit CPUExecutionProvider(const CPUExecutionProviderInfo& info) + : IExecutionProvider{onnxruntime::kCpuExecutionProvider} { + DeviceAllocatorRegistrationInfo device_info{OrtMemTypeDefault, + [](int) { return std::make_unique(); }, + std::numeric_limits::max()}; #ifdef USE_JEMALLOC ORT_UNUSED_PARAMETER(info); //JEMalloc already has memory pool, so just use device allocator. @@ -41,10 +46,6 @@ class CPUExecutionProvider : public IExecutionProvider { #endif } - std::string Type() const override { - return onnxruntime::kCpuExecutionProvider; - } - std::vector> GetCapability( const onnxruntime::GraphViewer& graph, const std::vector& kernel_registries) const override; @@ -63,7 +64,7 @@ class CPUExecutionProvider : public IExecutionProvider { void InsertFusedRules(FuseRuleFn rule); - protected: + private: std::vector fuse_rules_; }; } // namespace onnxruntime diff --git a/onnxruntime/core/providers/cuda/cuda_execution_provider.cc b/onnxruntime/core/providers/cuda/cuda_execution_provider.cc index 51ed64a9f8..c3b7aae053 100644 --- a/onnxruntime/core/providers/cuda/cuda_execution_provider.cc +++ b/onnxruntime/core/providers/cuda/cuda_execution_provider.cc @@ -54,7 +54,7 @@ CUDAExecutionProvider::PerThreadContext::~PerThreadContext() { } CUDAExecutionProvider::CUDAExecutionProvider(const CUDAExecutionProviderInfo& info) - : device_id_(info.device_id) { + : IExecutionProvider{onnxruntime::kCudaExecutionProvider}, device_id_(info.device_id) { CUDA_CALL_THROW(cudaSetDevice(device_id_)); // create streams, default is nullptr streams_[kCudaStreamDefault] = nullptr; diff --git a/onnxruntime/core/providers/cuda/cuda_execution_provider.h b/onnxruntime/core/providers/cuda/cuda_execution_provider.h index 60bd5bc9cd..e9f3848e8f 100644 --- a/onnxruntime/core/providers/cuda/cuda_execution_provider.h +++ b/onnxruntime/core/providers/cuda/cuda_execution_provider.h @@ -32,10 +32,6 @@ class CUDAExecutionProvider : public IExecutionProvider { AllocatorPtr GetAllocator(int id, OrtMemType mem_type = OrtMemTypeDefault) const override; - std::string Type() const override { - return onnxruntime::kCudaExecutionProvider; - } - Status Sync() const override; Status OnRunStart() override; diff --git a/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.cc b/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.cc index 1a6f1b8844..6163c80266 100644 --- a/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.cc +++ b/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.cc @@ -32,7 +32,8 @@ ONNX_OPERATOR_KERNEL_EX( } // namespace mkl_dnn -MKLDNNExecutionProvider::MKLDNNExecutionProvider(const MKLDNNExecutionProviderInfo& /*info*/) { +MKLDNNExecutionProvider::MKLDNNExecutionProvider(const MKLDNNExecutionProviderInfo& /*info*/) + : IExecutionProvider{onnxruntime::kMklDnnExecutionProvider} { DeviceAllocatorRegistrationInfo default_allocator_info({OrtMemTypeDefault, [](int) { return std::make_unique(std::make_unique(MKLDNN, OrtAllocatorType::OrtDeviceAllocator, 0, OrtMemTypeDefault)); }, std::numeric_limits::max()}); InsertAllocator(CreateAllocator(default_allocator_info)); diff --git a/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.h b/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.h index a85446ea1b..359f9b96be 100644 --- a/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.h +++ b/onnxruntime/core/providers/mkldnn/mkldnn_execution_provider.h @@ -34,10 +34,6 @@ class MKLDNNExecutionProvider : public IExecutionProvider { explicit MKLDNNExecutionProvider(const MKLDNNExecutionProviderInfo& info); virtual ~MKLDNNExecutionProvider(); - std::string Type() const override { - return onnxruntime::kMklDnnExecutionProvider; - } - Status CopyTensor(const Tensor& src, Tensor& dst) const override; const void* GetExecutionHandle() const noexcept override { diff --git a/onnxruntime/test/framework/dummy_provider.h b/onnxruntime/test/framework/dummy_provider.h index 818146712a..c434a3a6dd 100644 --- a/onnxruntime/test/framework/dummy_provider.h +++ b/onnxruntime/test/framework/dummy_provider.h @@ -14,14 +14,10 @@ class DummyExecutionProvider : public IExecutionProvider { static constexpr const char* kDummyExecutionProviderType = "DummyExecutionProvider"; public: - DummyExecutionProvider() { + DummyExecutionProvider() : IExecutionProvider{kDummyExecutionProviderType} { InsertAllocator(std::make_unique()); } - std::string Type() const override { - return kDummyExecutionProviderType; - } - Status CopyTensor(const Tensor& src, Tensor& dst) const override { // we can 'copy' from anything we allocated to/from CPU ORT_ENFORCE(strcmp(dst.Location().name, DummyAllocator::kDummyAllocator) == 0 || diff --git a/onnxruntime/test/framework/inference_session_test.cc b/onnxruntime/test/framework/inference_session_test.cc index 1a21a2a060..a84e449dc5 100644 --- a/onnxruntime/test/framework/inference_session_test.cc +++ b/onnxruntime/test/framework/inference_session_test.cc @@ -77,7 +77,7 @@ std::shared_ptr GetFusedKernelRegistry() { class FuseExecutionProvider : public IExecutionProvider { public: - explicit FuseExecutionProvider() { + explicit FuseExecutionProvider() : IExecutionProvider{kFuseExecutionProvider} { DeviceAllocatorRegistrationInfo device_info({OrtMemTypeDefault, [](int) { return std::make_unique(); }, std::numeric_limits::max()}); @@ -120,11 +120,8 @@ class FuseExecutionProvider : public IExecutionProvider { const void* GetExecutionHandle() const noexcept override { return nullptr; } - - std::string Type() const override { - return "FuseExecutionProvider"; - } }; + namespace test { static void VerifyOutputs(const std::vector& fetches, const std::vector& expected_dims, diff --git a/onnxruntime/test/framework/op_kernel_test.cc b/onnxruntime/test/framework/op_kernel_test.cc index 032b9a34c6..e2e26e44a6 100644 --- a/onnxruntime/test/framework/op_kernel_test.cc +++ b/onnxruntime/test/framework/op_kernel_test.cc @@ -12,18 +12,15 @@ #include "test_utils.h" using namespace std; using namespace ONNX_NAMESPACE; -using namespace ::onnxruntime::logging; namespace onnxruntime { +using namespace logging; + namespace test { class XPUExecutionProvider : public IExecutionProvider { public: - XPUExecutionProvider() = default; - - std::string Type() const override { - return onnxruntime::kCpuExecutionProvider; - } + XPUExecutionProvider() : IExecutionProvider{onnxruntime::kCpuExecutionProvider} {} Status CopyTensor(const Tensor& src, Tensor& dst) const override { ORT_UNUSED_PARAMETER(src);