OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[NPU] Optimize backend flow (#29373)
Bogdan Pereanu24c40be45ff7
diff --git a/src/plugins/intel_npu/src/backend/include/zero_backend.hpp b/src/plugins/intel_npu/src/backend/include/zero_backend.hppindex 097d7eb4510..d4cb931a293 100644--- a/src/plugins/intel_npu/src/backend/include/zero_backend.hpp+++ b/src/plugins/intel_npu/src/backend/include/zero_backend.hpp@@ -14,7 +14,7 @@namespace intel_npu {class ZeroEngineBackend final : public IEngineBackend {public:- ZeroEngineBackend(const Config& config);+ ZeroEngineBackend();virtual ~ZeroEngineBackend();const std::shared_ptr<IDevice> getDevice() const override;const std::shared_ptr<IDevice> getDevice(const std::string&) const override;diff --git a/src/plugins/intel_npu/src/backend/src/zero_backend.cpp b/src/plugins/intel_npu/src/backend/src/zero_backend.cppindex 3d99525bb18..7b8c7957a61 100644--- a/src/plugins/intel_npu/src/backend/src/zero_backend.cpp+++ b/src/plugins/intel_npu/src/backend/src/zero_backend.cpp@@ -11,7 +11,7 @@namespace intel_npu {-ZeroEngineBackend::ZeroEngineBackend(const Config& config) : _logger("ZeroEngineBackend", Logger::global().level()) {+ZeroEngineBackend::ZeroEngineBackend() : _logger("ZeroEngineBackend", Logger::global().level()) {_logger.debug("ZeroEngineBackend - initialize started");_initStruct = std::make_shared<ZeroInitStructsHolder>();diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/device_helpers.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/device_helpers.hppindex 0172437b99a..ef60d387cd0 100644--- a/src/plugins/intel_npu/src/common/include/intel_npu/common/device_helpers.hpp+++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/device_helpers.hpp@@ -13,4 +13,7 @@ namespace utils {bool isNPUDevice(const uint32_t deviceId);uint32_t getSliceIdBySwDeviceId(const uint32_t swDevId);std::string getPlatformByDeviceName(const std::string& deviceName);+std::string getCompilationPlatform(const std::string_view platform,+ const std::string& deviceId,+ std::vector<std::string> availableDevicesNames);} // namespace utilsdiff --git a/src/plugins/intel_npu/src/common/src/device_helpers.cpp b/src/plugins/intel_npu/src/common/src/device_helpers.cppindex 00ed6bdd146..3981e590691 100644--- a/src/plugins/intel_npu/src/common/src/device_helpers.cpp+++ b/src/plugins/intel_npu/src/common/src/device_helpers.cpp@@ -31,3 +31,24 @@ std::string utils::getPlatformByDeviceName(const std::string& deviceName) {return platformName;}++std::string utils::getCompilationPlatform(const std::string_view platform,+ const std::string& deviceId,+ std::vector<std::string> availableDevicesNames) {+ // Platform parameter has a higher priority than deviceID+ if (platform != ov::intel_npu::Platform::AUTO_DETECT) {+ return std::string(platform);+ }++ // Get compilation platform from deviceID+ if (!deviceId.empty()) {+ return utils::getPlatformByDeviceName(deviceId);+ }++ // Automatic detection of compilation platform+ if (availableDevicesNames.empty()) {+ OPENVINO_THROW("No NPU devices were found.");+ }++ return utils::getPlatformByDeviceName(availableDevicesNames.at(0));+}diff --git a/src/plugins/intel_npu/src/common/src/npu.cpp b/src/plugins/intel_npu/src/common/src/npu.cppindex d8273e4e0fc..43978396273 100644--- a/src/plugins/intel_npu/src/common/src/npu.cpp+++ b/src/plugins/intel_npu/src/common/src/npu.cpp@@ -39,14 +39,14 @@ void* IEngineBackend::getContext() const {void IEngineBackend::registerOptions(OptionsDesc&) const {}-IDevice::Uuid IDevice::getUuid() const {- OPENVINO_THROW("Get UUID not supported");-}-const std::shared_ptr<ZeroInitStructsHolder> IEngineBackend::getInitStructs() const {return nullptr;}+IDevice::Uuid IDevice::getUuid() const {+ OPENVINO_THROW("Get UUID not supported");+}+ov::device::LUID IDevice::getLUID() const {OPENVINO_THROW("Get LUID not supported");}diff --git a/src/plugins/intel_npu/src/plugin/include/backends.hpp b/src/plugins/intel_npu/src/plugin/include/backends.hppdeleted file mode 100644index 3c9b5ebb15f..00000000000--- a/src/plugins/intel_npu/src/plugin/include/backends.hpp+++ /dev/null@@ -1,49 +0,0 @@-// Copyright (C) 2018-2025 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#pragma once--// System-#include <memory>-#include <set>-#include <vector>--#include "openvino/runtime/so_ptr.hpp"--// Plugin-#include "intel_npu/common/npu.hpp"-#include "intel_npu/npu_private_properties.hpp"-#include "intel_npu/utils/logger/logger.hpp"--namespace intel_npu {--enum class AvailableBackends { LEVEL_ZERO, IMD };--/** @brief Represent container for all backends and hide all related searching logic */-class NPUBackends final {-public:- explicit NPUBackends(const std::vector<AvailableBackends>& backendRegistry, const Config& config);-- std::shared_ptr<IDevice> getDevice(const std::string& specificName = "") const;- std::shared_ptr<IDevice> getDevice(const ov::AnyMap& paramMap) const;- std::vector<std::string> getAvailableDevicesNames() const;- ov::SoPtr<IEngineBackend> getIEngineBackend();- std::string getBackendName() const;- uint32_t getDriverVersion() const;- uint32_t getGraphExtVersion() const;- bool isBatchingSupported() const;- bool isCommandQueueExtSupported() const;- bool isLUIDExtSupported() const;- void registerOptions(OptionsDesc& options) const;- void* getContext() const;- std::string getCompilationPlatform(const std::string_view platform, const std::string& deviceId) const;-- void setup(const Config& config);--private:- Logger _logger;- ov::SoPtr<IEngineBackend> _backend;-};--} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/include/backends_registry.hpp b/src/plugins/intel_npu/src/plugin/include/backends_registry.hppnew file mode 100644index 00000000000..deea6772568--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/include/backends_registry.hpp@@ -0,0 +1,42 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++// System+#include <memory>+#include <set>+#include <vector>++#include "openvino/runtime/so_ptr.hpp"++// Plugin+#include "intel_npu/common/npu.hpp"+#include "intel_npu/utils/logger/logger.hpp"++namespace intel_npu {++enum class AvailableBackends { LEVEL_ZERO, IMD };++class BackendsRegistry final {+public:+ BackendsRegistry();++ BackendsRegistry(const BackendsRegistry& other) = delete;+ BackendsRegistry(BackendsRegistry&& other) = delete;+ void operator=(const BackendsRegistry&) = delete;+ void operator=(BackendsRegistry&&) = delete;++ ov::SoPtr<IEngineBackend> getEngineBackend();++private:+ ov::SoPtr<IEngineBackend> initializeBackend(const AvailableBackends& backendName);+ void registerBackend(const AvailableBackends& backendName);++ std::unordered_map<AvailableBackends, ov::SoPtr<IEngineBackend>> _registeredBackends;++ Logger _logger;+};++} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/include/metrics.hpp b/src/plugins/intel_npu/src/plugin/include/metrics.hppindex 357d8b51da4..8837f9021d7 100644--- a/src/plugins/intel_npu/src/plugin/include/metrics.hpp+++ b/src/plugins/intel_npu/src/plugin/include/metrics.hpp@@ -8,7 +8,6 @@#include <unordered_set>#include <vector>-#include "backends.hpp"#include "intel_npu/common/npu.hpp"#include "intel_npu/npu_private_properties.hpp"#include "openvino/runtime/intel_npu/properties.hpp"@@ -19,7 +18,7 @@ namespace intel_npu {class Metrics final {public:- Metrics(const std::shared_ptr<const NPUBackends>& backends);+ Metrics(const ov::SoPtr<IEngineBackend>& backend);std::vector<std::string> GetAvailableDevicesNames() const;const std::vector<std::string>& SupportedMetrics() const;@@ -48,7 +47,7 @@ public:~Metrics() = default;private:- const std::shared_ptr<const NPUBackends> _backends;+ const ov::SoPtr<IEngineBackend> _backend;std::vector<std::string> _supportedMetrics;std::vector<std::string> _supportedConfigKeys;const std::vector<std::string> _optimizationCapabilities = {@@ -78,6 +77,7 @@ private:const std::tuple<uint32_t, uint32_t> _rangeForStreams{1u, 4u};std::string getDeviceName(const std::string& specifiedDeviceName) const;+ std::shared_ptr<intel_npu::IDevice> getDevice(const std::string& specifiedDeviceName) const;};} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/include/plugin.hpp b/src/plugins/intel_npu/src/plugin/include/plugin.hppindex b13be500051..2c9dfbf1a76 100644--- a/src/plugins/intel_npu/src/plugin/include/plugin.hpp+++ b/src/plugins/intel_npu/src/plugin/include/plugin.hpp@@ -8,7 +8,8 @@#include <memory>#include <string>-#include "backends.hpp"+#include "backends_registry.hpp"+#include "intel_npu/common/npu.hpp"#include "intel_npu/config/config.hpp"#include "intel_npu/utils/logger/logger.hpp"#include "metrics.hpp"@@ -52,7 +53,11 @@ public:const ov::AnyMap& properties) const override;private:- std::shared_ptr<NPUBackends> _backends;+ std::unique_ptr<BackendsRegistry> _backendsRegistry;++ // _backend might not be set by the plugin; certain actions, such as offline compilation, might be supported.+ // Appropriate checks are needed in plugin/metrics/properties when actions depend on a backend.+ ov::SoPtr<IEngineBackend> _backend;std::map<std::string, std::string> _config;std::shared_ptr<OptionsDesc> _options;diff --git a/src/plugins/intel_npu/src/plugin/include/remote_context.hpp b/src/plugins/intel_npu/src/plugin/include/remote_context.hppindex 3485229e6b2..759e2c76a61 100644--- a/src/plugins/intel_npu/src/plugin/include/remote_context.hpp+++ b/src/plugins/intel_npu/src/plugin/include/remote_context.hpp@@ -8,7 +8,7 @@#include <memory>#include <string>-#include "backends.hpp"+#include "intel_npu/common/npu.hpp"#include "intel_npu/config/config.hpp"#include "openvino/runtime/intel_npu/remote_properties.hpp"#include "openvino/runtime/iremote_context.hpp"@@ -17,7 +17,7 @@ namespace intel_npu {class RemoteContextImpl : public ov::IRemoteContext {public:- RemoteContextImpl(const std::shared_ptr<const NPUBackends>& backends,+ RemoteContextImpl(const ov::SoPtr<IEngineBackend>& engineBackend,const Config& config,const ov::AnyMap& remote_properties = {});diff --git a/src/plugins/intel_npu/src/plugin/src/backends.cpp b/src/plugins/intel_npu/src/plugin/src/backends.cppdeleted file mode 100644index bfd6184b36b..00000000000--- a/src/plugins/intel_npu/src/plugin/src/backends.cpp+++ /dev/null@@ -1,253 +0,0 @@-// Copyright (C) 2018-2025 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#include "backends.hpp"--#include <fstream>-#include <memory>--#include "intel_npu/common/device_helpers.hpp"-#include "intel_npu/config/common.hpp"-#include "zero_backend.hpp"--#if !defined(OPENVINO_STATIC_LIBRARY) && defined(ENABLE_IMD_BACKEND)-# include "openvino/util/file_util.hpp"-# include "openvino/util/shared_object.hpp"-#endif--using namespace intel_npu;--namespace {--std::string backendToString(const AvailableBackends& backend) {- switch (backend) {- case AvailableBackends::LEVEL_ZERO:- return "npu_level_zero_backend";- case AvailableBackends::IMD:- return "npu_imd_backend";- default:- return "unsupported backend";- }-}--#if !defined(OPENVINO_STATIC_LIBRARY) && defined(ENABLE_IMD_BACKEND)-std::shared_ptr<void> loadBackendLibrary(const std::string& libpath) {-# if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)- return ov::util::load_shared_object(ov::util::string_to_wstring(libpath).c_str());-# else- return ov::util::load_shared_object(libpath.c_str());-# endif-}--std::shared_ptr<IEngineBackend> getBackend(std::shared_ptr<void> so, const Config& config) {- static constexpr auto CreateFuncName = "CreateNPUEngineBackend";- auto symbol = ov::util::get_symbol(so, CreateFuncName);-- using CreateFuncT = void (*)(std::shared_ptr<IEngineBackend>&, const Config&);- const auto createFunc = reinterpret_cast<CreateFuncT>(symbol);-- std::shared_ptr<IEngineBackend> backendPtr;- createFunc(backendPtr, config);- return backendPtr;-}--ov::SoPtr<IEngineBackend> loadBackend(const std::string& libpath, const Config& config) {- auto backendSO = loadBackendLibrary(libpath);- auto backend = getBackend(backendSO, config);-- return ov::SoPtr<IEngineBackend>(backend, backendSO);-}-#endif--} // namespace--namespace intel_npu {--// TODO Config will be useless here, since only default values will be used-NPUBackends::NPUBackends(const std::vector<AvailableBackends>& backendRegistry, [[maybe_unused]] const Config& config)- : _logger("NPUBackends", Logger::global().level()) {- std::vector<ov::SoPtr<IEngineBackend>> registeredBackends;- [[maybe_unused]] const auto registerBackend = [&](ov::SoPtr<IEngineBackend> backend, const std::string& name) {- const auto backendDevices = backend->getDeviceNames();- if (!backendDevices.empty()) {- std::stringstream deviceNames;- for (const auto& device : backendDevices) {- deviceNames << device << " ";- }- _logger.debug("Register '%s' with devices '%s'", name.c_str(), deviceNames.str().c_str());- registeredBackends.emplace_back(backend);- }- };-- for (const auto& name : backendRegistry) {- std::string backendName = backendToString(name);- _logger.debug("Try '%s' backend", backendName.c_str());-- try {-#if !defined(OPENVINO_STATIC_LIBRARY) && defined(ENABLE_IMD_BACKEND)- if (name == AvailableBackends::IMD) {- const auto path =- ov::util::make_plugin_library_name(ov::util::get_ov_lib_path(), backendName + OV_BUILD_POSTFIX);- const auto exists = std::ifstream(path).good();- if (!exists) {- _logger.debug("Backend '%s' at '%s' doesn't exist", backendName.c_str(), path.c_str());- continue;- }- const auto backend = loadBackend(path, config);- registerBackend(backend, backendName);- }-#endif-- if (name == AvailableBackends::LEVEL_ZERO) {- const auto backend = ov::SoPtr<IEngineBackend>(std::make_shared<ZeroEngineBackend>(config));- registerBackend(backend, backendName);- }- } catch (const std::exception& ex) {- _logger.warning("Got an error during backend '%s' loading : %s", backendName.c_str(), ex.what());- } catch (...) {- _logger.warning("Got an unknown error during backend '%s' loading", backendName.c_str());- }- }-- if (registeredBackends.empty()) {- registeredBackends.emplace_back(nullptr);- }-- // TODO: implementation of getDevice methods needs to be updated to go over all- // registered backends to search a device.- // A single backend is chosen for now to keep existing behavior- _backend = *registeredBackends.begin();-- if (_backend != nullptr) {- _logger.info("Use '%s' backend for inference", _backend->getName().c_str());- } else {- _logger.warning("None of the backends were initialized successfully."- "Only offline compilation can be done!");- }-}--ov::SoPtr<IEngineBackend> NPUBackends::getIEngineBackend() {- return _backend;-}--std::string NPUBackends::getBackendName() const {- if (_backend != nullptr) {- return _backend->getName();- }-- return "";-}--uint32_t NPUBackends::getDriverVersion() const {- if (_backend != nullptr) {- return _backend->getDriverVersion();- }-- OPENVINO_THROW("No available backend");-}--uint32_t NPUBackends::getGraphExtVersion() const {- if (_backend != nullptr) {- return _backend->getGraphExtVersion();- }-- OPENVINO_THROW("No available backend");-}--bool NPUBackends::isBatchingSupported() const {- if (_backend != nullptr) {- return _backend->isBatchingSupported();- }-- return false;-}--bool NPUBackends::isCommandQueueExtSupported() const {- if (_backend != nullptr) {- return _backend->isCommandQueueExtSupported();- }-- return false;-}--bool NPUBackends::isLUIDExtSupported() const {- if (_backend != nullptr) {- return _backend->isLUIDExtSupported();- }-- return false;-}--std::shared_ptr<IDevice> NPUBackends::getDevice(const std::string& specificName) const {- _logger.debug("Searching for device %s to use started...", specificName.c_str());- // TODO iterate over all available backends- std::shared_ptr<IDevice> deviceToUse;-- if (_backend != nullptr) {- if (specificName.empty()) {- deviceToUse = _backend->getDevice();- } else {- deviceToUse = _backend->getDevice(specificName);- }- }-- if (deviceToUse == nullptr) {- _logger.warning("Device not found!");- } else {- _logger.debug("Device found: %s", deviceToUse->getName().c_str());- }- return deviceToUse;-}--std::shared_ptr<IDevice> NPUBackends::getDevice(const ov::AnyMap& paramMap) const {- return _backend->getDevice(paramMap);-}--std::vector<std::string> NPUBackends::getAvailableDevicesNames() const {- return _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames();-}--void NPUBackends::registerOptions(OptionsDesc& options) const {- if (_backend != nullptr) {- _backend->registerOptions(options);- }-}--void* NPUBackends::getContext() const {- if (_backend != nullptr) {- return _backend->getContext();- }-- OPENVINO_THROW("No available backend");-}--// TODO config should be also specified to backends, to allow use logging in devices and all levels below-void NPUBackends::setup(const Config& config) {- _logger.setLevel(config.get<LOG_LEVEL>());- if (_backend != nullptr) {- _backend->updateInfo(config);- }-}--std::string NPUBackends::getCompilationPlatform(const std::string_view platform, const std::string& deviceId) const {- // Platform parameter has a higher priority than deviceID- if (platform != ov::intel_npu::Platform::AUTO_DETECT) {- return std::string(platform);- }-- // Get compilation platform from deviceID- if (!deviceId.empty()) {- return utils::getPlatformByDeviceName(deviceId);- }-- // Automatic detection of compilation platform- const auto devNames = getAvailableDevicesNames();- if (devNames.empty()) {- OPENVINO_THROW("No NPU devices were found.");- }-- return utils::getPlatformByDeviceName(devNames.at(0));-}--} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/src/backends_registry.cpp b/src/plugins/intel_npu/src/plugin/src/backends_registry.cppnew file mode 100644index 00000000000..4b0de7bd296--- /dev/null+++ b/src/plugins/intel_npu/src/plugin/src/backends_registry.cpp@@ -0,0 +1,169 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "backends_registry.hpp"++#include <fstream>+#include <memory>++#include "intel_npu/common/device_helpers.hpp"+#include "zero_backend.hpp"++#if !defined(OPENVINO_STATIC_LIBRARY) && defined(ENABLE_IMD_BACKEND)+# include "openvino/util/file_util.hpp"+# include "openvino/util/shared_object.hpp"+#endif++using namespace intel_npu;++namespace {++std::string backendToString(const AvailableBackends& backend) {+ switch (backend) {+ case AvailableBackends::LEVEL_ZERO:+ return "npu_level_zero_backend";+ case AvailableBackends::IMD:+ return "npu_imd_backend";+ default:+ return "unsupported backend";+ }+}++#if !defined(OPENVINO_STATIC_LIBRARY) && defined(ENABLE_IMD_BACKEND)+std::shared_ptr<void> loadBackendLibrary(const std::string& libpath) {+# if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)+ return ov::util::load_shared_object(ov::util::string_to_wstring(libpath).c_str());+# else+ return ov::util::load_shared_object(libpath.c_str());+# endif+}++std::shared_ptr<IEngineBackend> getBackend(std::shared_ptr<void> so) {+ static constexpr auto CreateFuncName = "CreateNPUEngineBackend";+ auto symbol = ov::util::get_symbol(so, CreateFuncName);++ using CreateFuncT = void (*)(std::shared_ptr<IEngineBackend>&);+ const auto createFunc = reinterpret_cast<CreateFuncT>(symbol);++ std::shared_ptr<IEngineBackend> backendPtr;+ createFunc(backendPtr);+ return backendPtr;+}++ov::SoPtr<IEngineBackend> loadBackend(const std::string& libpath) {+ auto backendSO = loadBackendLibrary(libpath);+ auto backend = getBackend(backendS);++ return ov::SoPtr<IEngineBackend>(backend, backendSO);+}+#endif++} // namespace++namespace intel_npu {++BackendsRegistry::BackendsRegistry() : _logger("BackendsRegistry", Logger::global().level()) {+#if defined(OPENVINO_STATIC_LIBRARY)+ registerBackend(AvailableBackends::LEVEL_ZERO);+#else+# if defined(ENABLE_IMD_BACKEND)+ if (const auto* envVar = std::getenv("IE_NPU_USE_IMD_BACKEND")) {+ if (envVarStrToBool("IE_NPU_USE_IMD_BACKEND", envVar)) {+ registerBackend(AvailableBackends::IMD);+ }+ }+# endif++# if defined(_WIN32) || defined(_WIN64) || (defined(__linux__) && defined(__x86_64__))+ registerBackend(AvailableBackends::LEVEL_ZERO);+# endif+#endif+}++ov::SoPtr<IEngineBackend> BackendsRegistry::initializeBackend(const AvailableBackends& backendName) {+ std::string backendNameToString = backendToString(backendName);+ try {+ switch (backendName) {+ case AvailableBackends::IMD: {+#if !defined(OPENVINO_STATIC_LIBRARY) && defined(ENABLE_IMD_BACKEND)+ const auto path =+ ov::util::make_plugin_library_name(ov::util::get_ov_lib_path(), backendNameToString + OV_BUILD_POSTFIX);+ if (!std::ifstream(path).good()) {+ _logger.debug("Backend '%s' at '%s' doesn't exist", backendNameToString.c_str(), path.c_str());+ break;+ }+ return loadBackend(path);+#endif+ }+ case AvailableBackends::LEVEL_ZERO: {+ return ov::SoPtr<IEngineBackend>(std::make_shared<ZeroEngineBackend>());+ }+ default:+ _logger.warning("Invalid backend '%s'", backendNameToString.c_str());+ }+ } catch (const std::exception& ex) {+ _logger.warning("Got an error during backend '%s' loading : %s", backendNameToString.c_str(), ex.what());+ } catch (...) {+ _logger.warning("Got an unknown error during backend '%s' loading", backendNameToString.c_str());+ }++ return {nullptr};+}++void BackendsRegistry::registerBackend(const AvailableBackends& backendName) {+ if (_registeredBackends.find(backendName) != _registeredBackends.end()) {+ return;+ }++ const auto& backend = initializeBackend(backendName);++ if (backend != nullptr) {+ const auto backendDevices = backend->getDeviceNames();+ if (!backendDevices.empty()) {+ std::stringstream deviceNames;+ for (const auto& device : backendDevices) {+ deviceNames << device << " ";+ }+ _logger.debug("Register '%s' with devices '%s'", backend->getName().c_str(), deviceNames.str().c_str());+ _registeredBackends.emplace(backendName, backend);+ }+ }+}++ov::SoPtr<IEngineBackend> BackendsRegistry::getEngineBackend() {+#if defined(OPENVINO_STATIC_LIBRARY)+ if (_registeredBackends.find(AvailableBackends::LEVEL_ZERO) != _registeredBackends.end()) {+ _logger.info("Use '%s' backend for inference",+ _registeredBackends.at(AvailableBackends::LEVEL_ZERO)->getName().c_str());+ return _registeredBackends.at(AvailableBackends::LEVEL_ZERO);+ }+#else+# if defined(ENABLE_IMD_BACKEND)+ if (const auto* envVar = std::getenv("IE_NPU_USE_IMD_BACKEND")) {+ if (envVarStrToBool("IE_NPU_USE_IMD_BACKEND", envVar)) {+ if (_registeredBackends.find(AvailableBackends::IMD) != _registeredBackends.end()) {+ _logger.info("Use '%s' backend for inference",+ _registeredBackends.at(AvailableBackends::IMD)->getName().c_str());+ return _registeredBackends.at(AvailableBackends::IMD);+ }+ }+ }+# endif++# if defined(_WIN32) || defined(_WIN64) || (defined(__linux__) && defined(__x86_64__))+ if (_registeredBackends.find(AvailableBackends::LEVEL_ZERO) != _registeredBackends.end()) {+ _logger.info("Use '%s' backend for inference",+ _registeredBackends.at(AvailableBackends::LEVEL_ZERO)->getName().c_str());+ return _registeredBackends.at(AvailableBackends::LEVEL_ZERO);+ }+# endif+#endif++ _logger.warning("None of the backends were initialized successfully."+ "Only offline compilation can be done!");++ return {nullptr};+}++} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/src/metrics.cpp b/src/plugins/intel_npu/src/plugin/src/metrics.cppindex 5361540e775..a042a506708 100644--- a/src/plugins/intel_npu/src/plugin/src/metrics.cpp+++ b/src/plugins/intel_npu/src/plugin/src/metrics.cpp@@ -11,7 +11,7 @@namespace intel_npu {-Metrics::Metrics(const std::shared_ptr<const NPUBackends>& backends) : _backends(backends) {+Metrics::Metrics(const ov::SoPtr<IEngineBackend>& backend) : _backend(backend) {_supportedMetrics = {ov::supported_properties.name(),ov::available_devices.name(),ov::device::full_name.name(),@@ -42,29 +42,27 @@ Metrics::Metrics(const std::shared_ptr<const NPUBackends>& backends) : _backends}std::vector<std::string> Metrics::GetAvailableDevicesNames() const {- return _backends == nullptr ? std::vector<std::string>() : _backends->getAvailableDevicesNames();+ return _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames();}-// TODO each backend may support different metricsconst std::vector<std::string>& Metrics::SupportedMetrics() const {return _supportedMetrics;}std::string Metrics::GetFullDeviceName(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = _backend->getDevice(devName);if (device) {return device->getFullDeviceName();}+OPENVINO_THROW("No device with name '", specifiedDeviceName, "' is available");}-// TODO each backend may support different configsconst std::vector<std::string>& Metrics::GetSupportedConfigKeys() const {return _supportedConfigKeys;}-// TODO each backend may support different optimization capabilitiesconst std::vector<std::string> Metrics::GetOptimizationCapabilities() const {return _optimizationCapabilities;}@@ -83,17 +81,17 @@ std::string Metrics::GetDeviceArchitecture(const std::string& specifiedDeviceNam}IDevice::Uuid Metrics::GetDeviceUuid(const std::string& specifiedDeviceName) const {- const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);- if (device) {- return device->getUuid();+ const auto& devName = getDeviceName(specifiedDeviceName);+ const auto& deviceToUse = getDevice(devName);+ if (deviceToUse) {+ return deviceToUse->getUuid();}return IDevice::Uuid{};}ov::device::LUID Metrics::GetDeviceLUID(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device) {return device->getLUID();}@@ -111,32 +109,32 @@ std::vector<ov::PropertyName> Metrics::GetInternalSupportedProperties() const {}std::string Metrics::GetBackendName() const {- if (_backends == nullptr) {+ if (_backend == nullptr) {OPENVINO_THROW("No available backends");}- return _backends->getBackendName();+ return _backend->getName();}uint32_t Metrics::GetDriverVersion() const {- if (_backends == nullptr) {+ if (_backend == nullptr) {OPENVINO_THROW("No available backends");}- return _backends->getDriverVersion();+ return _backend->getDriverVersion();}uint32_t Metrics::GetGraphExtVersion() const {- if (_backends == nullptr) {+ if (_backend == nullptr) {OPENVINO_THROW("No available backends");}- return _backends->getGraphExtVersion();+ return _backend->getGraphExtVersion();}uint32_t Metrics::GetSteppingNumber(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device) {return device->getSubDevId();}@@ -145,7 +143,7 @@ uint32_t Metrics::GetSteppingNumber(const std::string& specifiedDeviceName) consuint32_t Metrics::GetMaxTiles(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device) {return device->getMaxNumSlices();}@@ -154,7 +152,7 @@ uint32_t Metrics::GetMaxTiles(const std::string& specifiedDeviceName) const {uint64_t Metrics::GetDeviceAllocMemSize(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device) {return device->getAllocMemSize();}@@ -163,7 +161,7 @@ uint64_t Metrics::GetDeviceAllocMemSize(const std::string& specifiedDeviceName)uint64_t Metrics::GetDeviceTotalMemSize(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device) {return device->getTotalMemSize();}@@ -174,10 +172,10 @@ std::string Metrics::getDeviceName(const std::string& specifiedDeviceName) const// In case of single device and empty input from user we should use the first element from the device listif (specifiedDeviceName.empty()) {std::vector<std::string> devNames;- if (_backends == nullptr || (devNames = _backends->getAvailableDevicesNames()).empty()) {+ if (_backend == nullptr || (devNames = _backend->getDeviceNames()).empty()) {OPENVINO_THROW("No available devices");}- if (devNames.size() == 1) {+ if (devNames.size() >= 1) {return devNames[0];} else {OPENVINO_THROW("The device name was not specified. Please specify device name by providing DEVICE_ID");@@ -187,9 +185,23 @@ std::string Metrics::getDeviceName(const std::string& specifiedDeviceName) constreturn specifiedDeviceName;}+std::shared_ptr<intel_npu::IDevice> Metrics::getDevice(const std::string& specifiedDeviceName) const {+ std::shared_ptr<IDevice> deviceToUse;++ if (_backend != nullptr) {+ if (specifiedDeviceName.empty()) {+ return _backend->getDevice();+ } else {+ return _backend->getDevice(specifiedDeviceName);+ }+ }++ return nullptr;+}+ov::device::PCIInfo Metrics::GetPciInfo(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device != nullptr) {return device->getPciInfo();}@@ -198,7 +210,7 @@ ov::device::PCIInfo Metrics::GetPciInfo(const std::string& specifiedDeviceName)std::map<ov::element::Type, float> Metrics::GetGops(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device != nullptr) {return device->getGops();}@@ -207,7 +219,7 @@ std::map<ov::element::Type, float> Metrics::GetGops(const std::string& specifiedov::device::Type Metrics::GetDeviceType(const std::string& specifiedDeviceName) const {const auto devName = getDeviceName(specifiedDeviceName);- auto device = _backends->getDevice(devName);+ auto device = getDevice(devName);if (device != nullptr) {return device->getDeviceType();}diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex 330b528e66d..5bcb091a309 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -186,32 +186,18 @@ Plugin::Plugin()_globalConfig.parseEnvVars();Logger::global().setLevel(_globalConfig.get<LOG_LEVEL>());- // TODO: generation of available backends list can be done during execution of CMake scripts- std::vector<AvailableBackends> backendRegistry;--#if defined(OPENVINO_STATIC_LIBRARY)- backendRegistry.push_back(AvailableBackends::LEVEL_ZERO);-#else-# if defined(ENABLE_IMD_BACKEND)- if (const auto* envVar = std::getenv("IE_NPU_USE_IMD_BACKEND")) {- if (envVarStrToBool("IE_NPU_USE_IMD_BACKEND", envVar)) {- backendRegistry.push_back(AvailableBackends::IMD);- }- }-# endif+ OV_ITT_TASK_CHAIN(PLUGIN, itt::domains::NPUPlugin, "Plugin::Plugin", "GetBackend");+ // backend registry shall be created after configs are updated+ _backendsRegistry = std::make_unique<BackendsRegistry>();+ _backend = _backendsRegistry->getEngineBackend();-# if defined(_WIN32) || defined(_WIN64) || (defined(__linux__) && defined(__x86_64__))- backendRegistry.push_back(AvailableBackends::LEVEL_ZERO);-# endif-#endif-- OV_ITT_TASK_CHAIN(PLUGIN, itt::domains::NPUPlugin, "Plugin::Plugin", "NPUBackends");- _backends = std::make_shared<NPUBackends>(backendRegistry, _globalConfig);- OV_ITT_TASK_NEXT(PLUGIN, "registerOptions");- _backends->registerOptions(*_options);+ if (_backend) {+ OV_ITT_TASK_NEXT(PLUGIN, "registerBackendOptions");+ _backend->registerOptions(*_options);+ }- OV_ITT_TASK_NEXT(PLUGIN, "Metrics");- _metrics = std::make_unique<Metrics>(_backends);+ OV_ITT_TASK_NEXT(PLUGIN, "createMetrics");+ _metrics = std::make_unique<Metrics>(_backend);// parse again env_variables after backend is initialized to get backend proprieties_globalConfig.parseEnvVars();@@ -294,7 +280,7 @@ Plugin::Plugin()return _metrics->GetAvailableDevicesNames();}}},{ov::workload_type.name(),- {_backends->isCommandQueueExtSupported(),+ {_backend == nullptr ? false : _backend->isCommandQueueExtSupported(),ov::PropertyMutability::RW,[](const Config& config) {return config.get<WORKLOAD_TYPE>();@@ -311,7 +297,10 @@ Plugin::Plugin()[&](const Config& config) {return static_cast<uint32_t>(getOptimalNumberOfInferRequestsInParallel(add_platform_to_the_config(config,- _backends->getCompilationPlatform(config.get<PLATFORM>(), config.get<DEVICE_ID>()))));+ utils::getCompilationPlatform(+ config.get<PLATFORM>(),+ config.get<DEVICE_ID>(),+ _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames()))));}}},{ov::range_for_async_infer_requests.name(),{true,@@ -346,7 +335,7 @@ Plugin::Plugin()return decltype(ov::device::uuid)::value_type{devUuid};}}},{ov::device::luid.name(),- {_backends->isLUIDExtSupported(),+ {_backend == nullptr ? false : _backend->isLUIDExtSupported(),ov::PropertyMutability::RO,[&](const Config& config) {const auto specifiedDeviceName = get_specified_device_name(config);@@ -448,7 +437,7 @@ Plugin::Plugin()[&](const Config& config) {/// create dummy compilerCompilerAdapterFactory compilerAdapterFactory;- auto dummyCompiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), config);+ auto dummyCompiler = compilerAdapterFactory.getCompiler(_backend, config);return dummyCompiler->get_version();}}},{ov::intel_npu::compilation_mode_params.name(),@@ -470,7 +459,7 @@ Plugin::Plugin()return config.get<QDQ_OPTIMIZATION>();}}},{ov::intel_npu::turbo.name(),- {_backends->isCommandQueueExtSupported(),+ {_backend == nullptr ? false : _backend->isCommandQueueExtSupported(),ov::PropertyMutability::RW,[](const Config& config) {return config.get<TURBO>();@@ -609,7 +598,7 @@ void Plugin::reset_compiler_dependent_properties() const {// get active compiler versiontry {CompilerAdapterFactory compilerAdapterFactory;- auto dummyCompiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), _globalConfig);+ auto dummyCompiler = compilerAdapterFactory.getCompiler(_backend, _globalConfig);active_compiler_version = dummyCompiler->get_version();} catch (...) {_logger.warning(@@ -656,8 +645,8 @@ void Plugin::set_property(const ov::AnyMap& properties) {}_globalConfig.update(config);- if (_backends != nullptr) {- _backends->setup(_globalConfig);+ if (_backend != nullptr) {+ _backend->updateInfo(_globalConfig);}for (const auto& entry : config) {@@ -723,11 +712,14 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<}}- const auto platform = _backends->getCompilationPlatform(localConfig.get<PLATFORM>(), localConfig.get<DEVICE_ID>());- auto device = _backends->getDevice(localConfig.get<DEVICE_ID>());+ const auto platform =+ utils::getCompilationPlatform(localConfig.get<PLATFORM>(),+ localConfig.get<DEVICE_ID>(),+ _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames());+ auto device = _backend == nullptr ? nullptr : _backend->getDevice(localConfig.get<DEVICE_ID>());localConfig.update({{ov::intel_npu::platform.name(), platform}});- set_batch_config(_backends->isBatchingSupported(), localConfig);+ set_batch_config(_backend == nullptr ? false : _backend->isBatchingSupported(), localConfig);if (!model->get_variables().empty()) {if (localConfig.get<BATCH_MODE>() == ov::intel_npu::BatchMode::PLUGIN) {@@ -768,7 +760,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<auto originalModel = model->clone();CompilerAdapterFactory compilerAdapterFactory;- auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig);+ auto compiler = compilerAdapterFactory.getCompiler(_backend, localConfig);OV_ITT_TASK_NEXT(PLUGIN_COMPILE_MODEL, "compile");std::shared_ptr<intel_npu::IGraph> graph;@@ -809,11 +801,11 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<}ov::SoPtr<ov::IRemoteContext> Plugin::create_context(const ov::AnyMap& remoteProperties) const {- return std::make_shared<RemoteContextImpl>(_backends, _globalConfig, remoteProperties);+ return std::make_shared<RemoteContextImpl>(_backend, _globalConfig, remoteProperties);}ov::SoPtr<ov::IRemoteContext> Plugin::get_default_context(const ov::AnyMap&) const {- return std::make_shared<RemoteContextImpl>(_backends, _globalConfig);+ return std::make_shared<RemoteContextImpl>(_backend, _globalConfig);}std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, const ov::AnyMap& properties) const {@@ -855,11 +847,14 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, cauto localConfig = merge_configs(_globalConfig, propertiesMap, OptionMode::RunTime);_logger.setLevel(localConfig.get<LOG_LEVEL>());- const auto platform = _backends->getCompilationPlatform(localConfig.get<PLATFORM>(), localConfig.get<DEVICE_ID>());+ const auto platform =+ utils::getCompilationPlatform(localConfig.get<PLATFORM>(),+ localConfig.get<DEVICE_ID>(),+ _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames());localConfig.update({{ov::intel_npu::platform.name(), platform}});- auto device = _backends->getDevice(localConfig.get<DEVICE_ID>());+ auto device = _backend == nullptr ? nullptr : _backend->getDevice(localConfig.get<DEVICE_ID>());- set_batch_config(_backends->isBatchingSupported(), localConfig);+ set_batch_config(_backend == nullptr ? false : _backend->isBatchingSupported(), localConfig);const auto loadedFromCache = localConfig.get<LOADED_FROM_CACHE>();if (!loadedFromCache) {@@ -873,7 +868,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, ctry {CompilerAdapterFactory compilerAdapterFactory;- auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig);+ auto compiler = compilerAdapterFactory.getCompiler(_backend, localConfig);uint64_t graphSize;const bool skipCompatibility = localConfig.get<DISABLE_VERSION_CHECK>();@@ -938,11 +933,14 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>&const std::map<std::string, std::string> propertiesMap = any_copy(properties);auto localConfig = merge_configs(_globalConfig, propertiesMap, OptionMode::CompileTime);_logger.setLevel(localConfig.get<LOG_LEVEL>());- const auto platform = _backends->getCompilationPlatform(localConfig.get<PLATFORM>(), localConfig.get<DEVICE_ID>());+ const auto platform =+ utils::getCompilationPlatform(localConfig.get<PLATFORM>(),+ localConfig.get<DEVICE_ID>(),+ _backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames());localConfig.update({{ov::intel_npu::platform.name(), platform}});CompilerAdapterFactory compilerAdapterFactory;- auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig);+ auto compiler = compilerAdapterFactory.getCompiler(_backend, localConfig);ov::SupportedOpsMap supportedOpsMap;try {supportedOpsMap = compiler->query(model, localConfig);diff --git a/src/plugins/intel_npu/src/plugin/src/remote_context.cpp b/src/plugins/intel_npu/src/plugin/src/remote_context.cppindex d6f40ff388c..5823e0796f1 100644--- a/src/plugins/intel_npu/src/plugin/src/remote_context.cpp+++ b/src/plugins/intel_npu/src/plugin/src/remote_context.cpp@@ -24,17 +24,22 @@ std::optional<Type> extract_object(const ov::AnyMap& params, const ov::Property<namespace intel_npu {-RemoteContextImpl::RemoteContextImpl(const std::shared_ptr<const NPUBackends>& backends,+RemoteContextImpl::RemoteContextImpl(const ov::SoPtr<IEngineBackend>& engineBackend,const Config& config,const ov::AnyMap& remote_properties): _config(config),- _device(backends->getDevice(_config.get<DEVICE_ID>())),- _properties({l0_context(backends->getContext())}),_device_name("NPU") {+ if (engineBackend == nullptr || engineBackend->getName() != "LEVEL0") {+ OPENVINO_THROW("Level zero backend is not found!");+ }++ _device = engineBackend->getDevice(config.get<DEVICE_ID>());if (_device == nullptr) {- OPENVINO_THROW("Device is not available");+ OPENVINO_THROW("Device is not available!");}+ _properties = {l0_context(engineBackend->getContext())};+if (!remote_properties.empty()) {_mem_type_object = extract_object(remote_properties, mem_type);_tensor_type_object = extract_object(remote_properties, tensor_type);