From 773c21412e09457775fde9c86c85dcd00d824efb Mon Sep 17 00:00:00 2001 From: Bogdan Pereanu Date: Tue, 10 Dec 2024 15:43:18 +0200 Subject: [PATCH] [NPU] Create compiler adapter factory (#27962) ### Details: Clean-up code - create compiler adapter factory class to create the proper compiler adapter. ### Tickets: - *CVS-158848* Signed-off-by: Bogdan Pereanu --- .../src/backend/include/zero_backend.hpp | 4 +- .../src/backend/src/zero_backend.cpp | 8 +-- .../intel_npu/common/icompiler_adapter.hpp | 21 ++++++++ .../common/include/intel_npu/common/npu.hpp | 13 ++--- src/plugins/intel_npu/src/common/src/npu.cpp | 4 ++ .../include/compiler_adapter_factory.hpp | 41 ++++++++++++++ .../include/driver_compiler_adapter.hpp | 5 +- .../include/plugin_compiler_adapter.hpp | 2 +- .../src/driver_compiler_adapter.cpp | 3 -- .../src/plugin_compiler_adapter.cpp | 3 -- .../intel_npu/src/plugin/include/plugin.hpp | 2 - .../intel_npu/src/plugin/src/plugin.cpp | 54 ++++--------------- 12 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hpp create mode 100644 src/plugins/intel_npu/src/compiler_adapter/include/compiler_adapter_factory.hpp diff --git a/src/plugins/intel_npu/src/backend/include/zero_backend.hpp b/src/plugins/intel_npu/src/backend/include/zero_backend.hpp index 038c7c1d2d9bf9..358bdc93ce7a71 100644 --- a/src/plugins/intel_npu/src/backend/include/zero_backend.hpp +++ b/src/plugins/intel_npu/src/backend/include/zero_backend.hpp @@ -29,12 +29,12 @@ class ZeroEngineBackend final : public IEngineBackend { bool isCommandQueueExtSupported() const override; bool isLUIDExtSupported() const override; - const std::shared_ptr& getInitStruct() const; - void* getContext() const override; void updateInfo(const Config& config) override; + const std::shared_ptr getInitStructs() const override; + private: std::shared_ptr _initStruct; diff --git a/src/plugins/intel_npu/src/backend/src/zero_backend.cpp b/src/plugins/intel_npu/src/backend/src/zero_backend.cpp index 55aaad102e8b8f..afcf77d55616f0 100644 --- a/src/plugins/intel_npu/src/backend/src/zero_backend.cpp +++ b/src/plugins/intel_npu/src/backend/src/zero_backend.cpp @@ -72,10 +72,6 @@ void* ZeroEngineBackend::getContext() const { return _initStruct->getContext(); } -const std::shared_ptr& ZeroEngineBackend::getInitStruct() const { - return _initStruct; -} - void ZeroEngineBackend::updateInfo(const Config& config) { _logger.setLevel(config.get()); if (_devices.size() > 0) { @@ -85,4 +81,8 @@ void ZeroEngineBackend::updateInfo(const Config& config) { } } +const std::shared_ptr ZeroEngineBackend::getInitStructs() const { + return _initStruct; +} + } // namespace intel_npu diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hpp new file mode 100644 index 00000000000000..6e585299d68a1d --- /dev/null +++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/icompiler_adapter.hpp @@ -0,0 +1,21 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "intel_npu/common/igraph.hpp" + +namespace intel_npu { + +class ICompilerAdapter { +public: + virtual std::shared_ptr compile(const std::shared_ptr& model, + const Config& config) const = 0; + virtual std::shared_ptr parse(std::vector network, const Config& config) const = 0; + virtual ov::SupportedOpsMap query(const std::shared_ptr& model, const Config& config) const = 0; + + virtual ~ICompilerAdapter() = default; +}; + +} // namespace intel_npu diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/npu.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/npu.hpp index b34f2deee6c61e..9e4c59852151ce 100644 --- a/src/plugins/intel_npu/src/common/include/intel_npu/common/npu.hpp +++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/npu.hpp @@ -10,6 +10,7 @@ #include "intel_npu/common/igraph.hpp" #include "intel_npu/common/sync_infer_request.hpp" #include "intel_npu/config/config.hpp" +#include "intel_npu/utils/zero/zero_init.hpp" #include "openvino/runtime/intel_npu/remote_properties.hpp" #include "openvino/runtime/iremote_context.hpp" #include "openvino/runtime/properties.hpp" @@ -47,6 +48,8 @@ class IEngineBackend : public std::enable_shared_from_this { virtual void* getContext() const; /** @brief Update backend and device info */ virtual void updateInfo(const Config& config) = 0; + /** @brief Get LevelZero structures */ + virtual const std::shared_ptr getInitStructs() const; protected: virtual ~IEngineBackend() = default; @@ -54,16 +57,6 @@ class IEngineBackend : public std::enable_shared_from_this { //------------------------------------------------------------------------------ -class ICompilerAdapter { -public: - virtual std::shared_ptr compile(const std::shared_ptr& model, - const Config& config) const = 0; - virtual std::shared_ptr parse(std::vector network, const Config& config) const = 0; - virtual ov::SupportedOpsMap query(const std::shared_ptr& model, const Config& config) const = 0; - - virtual ~ICompilerAdapter() = default; -}; - //------------------------------------------------------------------------------ class IDevice : public std::enable_shared_from_this { diff --git a/src/plugins/intel_npu/src/common/src/npu.cpp b/src/plugins/intel_npu/src/common/src/npu.cpp index 0969b200ea09a5..afcfa5b0bba271 100644 --- a/src/plugins/intel_npu/src/common/src/npu.cpp +++ b/src/plugins/intel_npu/src/common/src/npu.cpp @@ -43,6 +43,10 @@ IDevice::Uuid IDevice::getUuid() const { OPENVINO_THROW("Get UUID not supported"); } +const std::shared_ptr IEngineBackend::getInitStructs() const { + return nullptr; +} + ov::device::LUID IDevice::getLUID() const { OPENVINO_THROW("Get LUID not supported"); } diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/compiler_adapter_factory.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_adapter_factory.hpp new file mode 100644 index 00000000000000..31c6ca348a234f --- /dev/null +++ b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_adapter_factory.hpp @@ -0,0 +1,41 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include "driver_compiler_adapter.hpp" +#include "intel_npu/common/icompiler_adapter.hpp" +#include "intel_npu/config/compiler.hpp" +#include "intel_npu/config/config.hpp" +#include "plugin_compiler_adapter.hpp" + +namespace intel_npu { + +class CompilerAdapterFactory final { +public: + const std::unique_ptr getCompiler(const ov::SoPtr& engineBackend, + const Config& config) const { + auto compilerType = config.get(); + switch (compilerType) { + case ov::intel_npu::CompilerType::MLIR: { + if (engineBackend->getName() != "LEVEL0") { + return std::make_unique(nullptr); + } + + return std::make_unique(engineBackend->getInitStructs()); + } + case ov::intel_npu::CompilerType::DRIVER: { + if (engineBackend->getName() != "LEVEL0") { + OPENVINO_THROW("NPU Compiler Adapter must be used with LEVEL0 backend"); + } + + return std::make_unique(engineBackend->getInitStructs()); + } + default: + OPENVINO_THROW("Invalid NPU_COMPILER_TYPE"); + } + } +}; + +} // namespace intel_npu diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hpp index 82ababf21c147a..3fb0ea8937da60 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hpp +++ b/src/plugins/intel_npu/src/compiler_adapter/include/driver_compiler_adapter.hpp @@ -6,13 +6,10 @@ #pragma once -#include -#include - #include #include -#include "intel_npu/common/npu.hpp" +#include "intel_npu/common/icompiler_adapter.hpp" #include "intel_npu/config/config.hpp" #include "intel_npu/utils/logger/logger.hpp" #include "intel_npu/utils/zero/zero_init.hpp" diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hpp index 8d2616884e7d5f..96c71d9f80c668 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hpp +++ b/src/plugins/intel_npu/src/compiler_adapter/include/plugin_compiler_adapter.hpp @@ -6,7 +6,7 @@ #pragma once -#include "intel_npu/common/npu.hpp" +#include "intel_npu/common/icompiler_adapter.hpp" #include "intel_npu/icompiler.hpp" #include "intel_npu/utils/logger/logger.hpp" #include "intel_npu/utils/zero/zero_init.hpp" diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp index 9d634656db109a..b17148c6411936 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp +++ b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp @@ -4,8 +4,6 @@ #include "driver_compiler_adapter.hpp" -#include - #include #include @@ -21,7 +19,6 @@ #include "intel_npu/utils/zero/zero_utils.hpp" #include "ir_serializer.hpp" #include "openvino/core/model.hpp" -#include "ze_graph_ext_wrappers.hpp" namespace { diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp index 06d71fd1126c17..6d67f544db2c17 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp +++ b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp @@ -4,8 +4,6 @@ #include "plugin_compiler_adapter.hpp" -#include - #include #include @@ -19,7 +17,6 @@ #include "openvino/util/file_util.hpp" #include "openvino/util/shared_object.hpp" #include "plugin_graph.hpp" -#include "ze_graph_ext_wrappers.hpp" namespace { std::shared_ptr loadLibrary(const std::string& libpath) { diff --git a/src/plugins/intel_npu/src/plugin/include/plugin.hpp b/src/plugins/intel_npu/src/plugin/include/plugin.hpp index c3c2daa525aaa1..6b1b46872788e3 100644 --- a/src/plugins/intel_npu/src/plugin/include/plugin.hpp +++ b/src/plugins/intel_npu/src/plugin/include/plugin.hpp @@ -52,8 +52,6 @@ class Plugin : public ov::IPlugin { const ov::AnyMap& properties) const override; private: - std::unique_ptr getCompiler(const Config& config) const; - std::shared_ptr _backends; std::map _config; diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp index da425d5d01a5c3..fa641dfdcd9641 100644 --- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp +++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp @@ -7,9 +7,9 @@ #include #include "compiled_model.hpp" -#include "npuw/compiled_model.hpp" -#include "driver_compiler_adapter.hpp" +#include "compiler_adapter_factory.hpp" #include "intel_npu/common/device_helpers.hpp" +#include "intel_npu/common/icompiler_adapter.hpp" #include "intel_npu/common/igraph.hpp" #include "intel_npu/common/itt.hpp" #include "intel_npu/config/common.hpp" @@ -17,13 +17,12 @@ #include "intel_npu/config/npuw.hpp" #include "intel_npu/config/runtime.hpp" #include "intel_npu/utils/zero/zero_init.hpp" +#include "npuw/compiled_model.hpp" #include "openvino/op/constant.hpp" #include "openvino/op/parameter.hpp" #include "openvino/runtime/intel_npu/properties.hpp" #include "openvino/runtime/properties.hpp" -#include "plugin_compiler_adapter.hpp" #include "remote_context.hpp" -#include "zero_backend.hpp" using namespace intel_npu; @@ -699,8 +698,9 @@ std::shared_ptr Plugin::compile_model(const std::shared_ptr< } } - auto original_model = model->clone(); - auto compiler = getCompiler(localConfig); + auto originalModel = model->clone(); + CompilerAdapterFactory compilerAdapterFactory; + auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig); OV_ITT_TASK_NEXT(PLUGIN_COMPILE_MODEL, "compile"); std::shared_ptr graph; @@ -716,7 +716,7 @@ std::shared_ptr Plugin::compile_model(const std::shared_ptr< std::shared_ptr compiledModel; try { - compiledModel = std::make_shared(original_model, shared_from_this(), device, graph, localConfig); + compiledModel = std::make_shared(originalModel, shared_from_this(), device, graph, localConfig); } catch (const std::exception& ex) { OPENVINO_THROW(ex.what()); } catch (...) { @@ -772,7 +772,8 @@ std::shared_ptr Plugin::import_model(std::istream& stream, c std::shared_ptr compiledModel; try { - auto compiler = getCompiler(localConfig); + CompilerAdapterFactory compilerAdapterFactory; + auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig); auto graphSize = getFileSize(stream); @@ -821,7 +822,8 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr& const auto platform = _backends->getCompilationPlatform(localConfig.get(), localConfig.get()); localConfig.update({{ov::intel_npu::platform.name(), platform}}); - auto compiler = getCompiler(localConfig); + CompilerAdapterFactory compilerAdapterFactory; + auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig); ov::SupportedOpsMap supportedOpsMap; try { supportedOpsMap = compiler->query(model, localConfig); @@ -834,40 +836,6 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr& return supportedOpsMap; } -std::unique_ptr Plugin::getCompiler(const Config& config) const { - auto compilerType = config.get(); - _logger.debug("performing createCompiler"); - - switch (compilerType) { - case ov::intel_npu::CompilerType::MLIR: { - if (_backends->getBackendName() != "LEVEL0") { - return std::make_unique(nullptr); - } - - auto zeroBackend = std::dynamic_pointer_cast(_backends->getIEngineBackend()._ptr); - if (zeroBackend == nullptr) { - return std::make_unique(nullptr); - } - - return std::make_unique(zeroBackend->getInitStruct()); - } - case ov::intel_npu::CompilerType::DRIVER: { - if (_backends->getBackendName() != "LEVEL0") { - OPENVINO_THROW("NPU Compiler Adapter must be used with LEVEL0 backend"); - } - - auto zeroBackend = std::dynamic_pointer_cast(_backends->getIEngineBackend()._ptr); - if (!zeroBackend) { - OPENVINO_THROW("Failed to cast zeroBackend, zeroBackend is a nullptr"); - } - - return std::make_unique(zeroBackend->getInitStruct()); - } - default: - OPENVINO_THROW("Invalid NPU_COMPILER_TYPE"); - } -} - std::atomic Plugin::_compiledModelLoadCounter{1}; static const ov::Version version = {CI_BUILD_NUMBER, NPU_PLUGIN_LIB_NAME};