Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPU] Extend NPU_COMPILER_TYPE with PREFER_PLUGIN option (#33684)

Bogdan Pereanud25bfebcbb73
diff --git a/src/bindings/python/src/pyopenvino/core/properties/properties.cpp b/src/bindings/python/src/pyopenvino/core/properties/properties.cpp
index 15d41edc314..847f4e40c77 100644
--- a/src/bindings/python/src/pyopenvino/core/properties/properties.cpp
+++ b/src/bindings/python/src/pyopenvino/core/properties/properties.cpp
@@ -356,7 +356,8 @@ void regmodule_properties(py::module m) {
py::enum_<ov::intel_npu::CompilerType>(m_intel_npu, "CompilerType", py::arithmetic())
.value("DRIVER", ov::intel_npu::CompilerType::DRIVER)
- .value("PLUGIN", ov::intel_npu::CompilerType::PLUGIN);
+ .value("PLUGIN", ov::intel_npu::CompilerType::PLUGIN)
+ .value("PREFER_PLUGIN", ov::intel_npu::CompilerType::PREFER_PLUGIN);
wrap_property_RW(m_intel_npu, ov::intel_npu::compiler_type, "compiler_type");
}
diff --git a/src/bindings/python/tests/test_runtime/test_properties.py b/src/bindings/python/tests/test_runtime/test_properties.py
index 9142f70e94d..f7c53d843c0 100644
--- a/src/bindings/python/tests/test_runtime/test_properties.py
+++ b/src/bindings/python/tests/test_runtime/test_properties.py
@@ -512,7 +512,8 @@ def test_properties_ro(ov_property_ro, expected_value):
intel_npu.compiler_type,
"NPU_COMPILER_TYPE",
((intel_npu.CompilerType.DRIVER, intel_npu.CompilerType.DRIVER),
- (intel_npu.CompilerType.PLUGIN, intel_npu.CompilerType.PLUGIN),),
+ (intel_npu.CompilerType.PLUGIN, intel_npu.CompilerType.PLUGIN),
+ (intel_npu.CompilerType.PREFER_PLUGIN, intel_npu.CompilerType.PREFER_PLUGIN),),
),
(props.enable_weightless, "ENABLE_WEIGHTLESS", ((True, True), (False, False))),
],
diff --git a/src/inference/include/openvino/runtime/intel_npu/properties.hpp b/src/inference/include/openvino/runtime/intel_npu/properties.hpp
index 94201ff32aa..afb106d148e 100644
--- a/src/inference/include/openvino/runtime/intel_npu/properties.hpp
+++ b/src/inference/include/openvino/runtime/intel_npu/properties.hpp
@@ -31,7 +31,7 @@ namespace intel_npu {
* Type of NPU compiler to be used for compilation of a network
* @note Configuration API v 2.0
*/
-enum class CompilerType { PLUGIN, DRIVER };
+enum class CompilerType { PLUGIN, DRIVER, PREFER_PLUGIN };
/**
* @brief Prints a string representation of ov::intel_npu::CompilerType to a stream
@@ -48,6 +48,9 @@ inline std::ostream& operator<<(std::ostream& out, const CompilerType& fmt) {
case CompilerType::DRIVER: {
out << "DRIVER";
} break;
+ case CompilerType::PREFER_PLUGIN: {
+ out << "PREFER_PLUGIN";
+ } break;
default:
out << static_cast<uint32_t>(fmt);
break;
diff --git a/src/plugins/intel_npu/README.md b/src/plugins/intel_npu/README.md
index f58a0a91913..c8d36ac2dc5 100644
--- a/src/plugins/intel_npu/README.md
+++ b/src/plugins/intel_npu/README.md
@@ -196,7 +196,7 @@ The following properties are supported (may differ based on current system confi
| `ov::intel_npu::device_alloc_mem_size`/</br>`NPU_DEVICE_ALLOC_MEM_SIZE` | RO | Size of already allocated NPU DDR memory | `N/A` | `N/A` |
| `ov::intel_npu::device_total_mem_size`/</br>`NPU_DEVICE_TOTAL_MEM_SIZE` | RO | Size of available NPU DDR memory | `N/A` | `N/A` |
| `ov::intel_npu::driver_version`/</br>`NPU_DRIVER_VERSION` | RO | NPU driver version. | `N/A` | `N/A` |
-| `ov::intel_npu::compiler_type`/</br>`NPU_COMPILER_TYPE` | RW | Selects the compiler type to be used | `PLUGIN`</br>`DRIVER` | Depends on the platform. `PLUGIN` for 4000 and 5010, `DRIVER` otherwise |
+| `ov::intel_npu::compiler_type`/</br>`NPU_COMPILER_TYPE` | RW | Selects the compiler type to be used | `PLUGIN`</br>`DRIVER`</br>`PREFER_PLUGIN` | Depends on the platform. `PREFER_PLUGIN` for 4000 and 5010, `DRIVER` otherwise |
| `ov::intel_npu::compiler_version`/</br>`NPU_COMPILER_VERSION` | RO | NPU compiler version. MSB 16 bits are Major version, LSB 16 bits are Minor version | `N/A` | `N/A` |
| `ov::intel_npu::compilation_mode_params`/</br>`NPU_COMPILATION_MODE_PARAMS` | RW | Set various parameters supported by the NPU compiler. (See bellow) | `<std::string>`| `N/A` |
| `ov::intel_npu::compiler_dynamic_quantization`/</br>`NPU_COMPILER_DYNAMIC_QUANTIZATION` | RW | Enable/Disable dynamic quantization by NPU compiler | `YES` / `NO` | `N/A` |
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp
index 30aeb9abfb2..feb98b35a5a 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp
@@ -820,7 +820,7 @@ struct COMPILER_TYPE final : OptionBase<COMPILER_TYPE, ov::intel_npu::CompilerTy
}
static ov::intel_npu::CompilerType defaultValue() {
- return ov::intel_npu::CompilerType::DRIVER;
+ return ov::intel_npu::CompilerType::PREFER_PLUGIN;
}
static ov::intel_npu::CompilerType parse(std::string_view val) {
@@ -828,6 +828,8 @@ struct COMPILER_TYPE final : OptionBase<COMPILER_TYPE, ov::intel_npu::CompilerTy
return ov::intel_npu::CompilerType::PLUGIN;
} else if (val == "DRIVER") {
return ov::intel_npu::CompilerType::DRIVER;
+ } else if (val == "PREFER_PLUGIN") {
+ return ov::intel_npu::CompilerType::PREFER_PLUGIN;
}
OPENVINO_THROW("Value '", val, "' is not a valid COMPILER_TYPE option");
@@ -839,6 +841,8 @@ struct COMPILER_TYPE final : OptionBase<COMPILER_TYPE, ov::intel_npu::CompilerTy
strStream << "PLUGIN";
} else if (val == ov::intel_npu::CompilerType::DRIVER) {
strStream << "DRIVER";
+ } else if (val == ov::intel_npu::CompilerType::PREFER_PLUGIN) {
+ strStream << "PREFER_PLUGIN";
} else {
OPENVINO_THROW("No valid string for current COMPILER_TYPE option");
}
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
index 9cc095e53b7..eac9caa0c1c 100644
--- 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
@@ -6,6 +6,7 @@
#include "driver_compiler_adapter.hpp"
#include "intel_npu/common/icompiler_adapter.hpp"
+#include "intel_npu/common/npu.hpp"
#include "intel_npu/config/config.hpp"
#include "intel_npu/config/options.hpp"
#include "plugin_compiler_adapter.hpp"
@@ -14,26 +15,54 @@ namespace intel_npu {
class CompilerAdapterFactory final {
public:
+ static CompilerAdapterFactory& getInstance() {
+ static CompilerAdapterFactory instance;
+ return instance;
+ }
+
+ CompilerAdapterFactory(const CompilerAdapterFactory&) = delete;
+ CompilerAdapterFactory(CompilerAdapterFactory&&) = delete;
+ CompilerAdapterFactory& operator=(const CompilerAdapterFactory&) = delete;
+ CompilerAdapterFactory& operator=(CompilerAdapterFactory&&) = delete;
+
std::unique_ptr<ICompilerAdapter> getCompiler(const ov::SoPtr<IEngineBackend>& engineBackend,
- const ov::intel_npu::CompilerType type) const {
- switch (type) {
- case ov::intel_npu::CompilerType::PLUGIN: {
+ ov::intel_npu::CompilerType& compilerType) const {
+ if (_pluginCompilerIsPresent) {
+ if (compilerType == ov::intel_npu::CompilerType::PREFER_PLUGIN) {
+ try {
+ compilerType = ov::intel_npu::CompilerType::PLUGIN;
+ return std::make_unique<PluginCompilerAdapter>(engineBackend->getInitStructs());
+ } catch (...) {
+ _pluginCompilerIsPresent = false;
+ compilerType = ov::intel_npu::CompilerType::DRIVER;
+ }
+ }
+ } else {
+ compilerType = ov::intel_npu::CompilerType::DRIVER;
+ }
+
+ if (compilerType == ov::intel_npu::CompilerType::PLUGIN) {
if (engineBackend == nullptr || engineBackend->getName() != "LEVEL0") {
return std::make_unique<PluginCompilerAdapter>(nullptr);
}
+
return std::make_unique<PluginCompilerAdapter>(engineBackend->getInitStructs());
- }
- case ov::intel_npu::CompilerType::DRIVER: {
+ } else if (compilerType == ov::intel_npu::CompilerType::DRIVER) {
if (engineBackend == nullptr || engineBackend->getName() != "LEVEL0") {
OPENVINO_THROW("NPU Compiler Adapter must be used with LEVEL0 backend");
}
return std::make_unique<DriverCompilerAdapter>(engineBackend->getInitStructs());
- }
- default:
+ } else {
OPENVINO_THROW("Invalid NPU_COMPILER_TYPE");
}
}
+
+private:
+ CompilerAdapterFactory() = default;
+ ~CompilerAdapterFactory() = default;
+
+ mutable std::atomic<bool> _pluginCompilerIsPresent = true;
};
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
index 8dd1ed207b1..b1f55273518 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -333,34 +333,6 @@ void Plugin::init_options() {
_backend->registerOptions(*_options);
}
- // Compiler library is present, need to check if the current platform is supported
- if (_backend) {
- // Try to set PLUGIN as default compiler type if:
- // 1. Compiler library is present
- // 2. The current platform is supported by the compiler (plugin)
- auto platformName = _backend->getDevice()->getName();
- if (platformName == ov::intel_npu::Platform::NPU4000 || platformName == ov::intel_npu::Platform::NPU5010) {
- try {
- CompilerAdapterFactory compilerAdapterFactory;
- // This is expected to throw in case the compiler library is not available
- (void)compilerAdapterFactory.getCompiler(_backend, ov::intel_npu::CompilerType::PLUGIN);
-
- _globalConfig.enable("NPU_COMPILER_TYPE", true);
- _globalConfig.update({{ov::intel_npu::compiler_type.name(), "PLUGIN"}});
- _logger.info("Use PLUGIN as default compiler");
- } catch (...) {
- _logger.warning("Failed to set PLUGIN as default compiler type. Compiler library is not available");
- }
- } else {
- _logger.info("Use DRIVER as default compiler type for the %s platform", platformName.c_str());
- }
- } else {
- // No device is available, only PLUGIN compiler type can be used for offline compilation
- _globalConfig.enable("NPU_COMPILER_TYPE", true);
- _globalConfig.update({{ov::intel_npu::compiler_type.name(), "PLUGIN"}});
- _logger.info("Use PLUGIN as default compiler. Offline compilation");
- }
-
// parse again env_variables to update registered configs which have env vars set
_globalConfig.parseEnvVars();
@@ -432,6 +404,18 @@ void Plugin::init_options() {
// Special cases
_globalConfig.enable(ov::log::level.name(), true); // needed also by runtime options
+
+ if (_globalConfig.get<COMPILER_TYPE>() == ov::intel_npu::CompilerType::PREFER_PLUGIN) {
+ if (_backend) {
+ auto platformName = _backend->getDevice()->getName();
+ if (platformName != ov::intel_npu::Platform::NPU4000 && platformName != ov::intel_npu::Platform::NPU5010) {
+ std::ostringstream oss;
+ oss << ov::intel_npu::CompilerType::DRIVER;
+ _globalConfig.update({{ov::intel_npu::compiler_type.name(), oss.str()}});
+ _logger.info("Use %s as default compiler", oss.str().c_str());
+ }
+ }
+ }
}
void Plugin::filter_config_by_compiler_support(FilteredConfig& cfg) const {
@@ -440,11 +424,11 @@ void Plugin::filter_config_by_compiler_support(FilteredConfig& cfg) const {
std::unique_ptr<ICompilerAdapter> compiler = nullptr;
std::vector<std::string> compiler_support_list{};
uint32_t compiler_version = 0;
- // create a dummy compiler to fetch version and supported options
+ // create a dummy compiler to fetch version and supported options
try {
- CompilerAdapterFactory compilerAdapterFactory;
- compiler = compilerAdapterFactory.getCompiler(_backend, cfg.get<COMPILER_TYPE>());
+ auto compiler_type = cfg.get<COMPILER_TYPE>();
+ compiler = CompilerAdapterFactory::getInstance().getCompiler(_backend, compiler_type);
} catch (...) {
// assuming getCompiler failed, meaning we are offline
_logger.warning("No available compiler. Enabling only runtime options ");
@@ -706,8 +690,12 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<
update_log_level(localProperties);
// create compiler
- CompilerAdapterFactory compilerAdapterFactory;
- auto compiler = compilerAdapterFactory.getCompiler(_backend, resolveCompilerType(_globalConfig, properties));
+ ov::intel_npu::CompilerType compilerType = resolveCompilerType(_globalConfig, localProperties);
+ const bool wasPreferPlugin = (compilerType == ov::intel_npu::CompilerType::PREFER_PLUGIN);
+ auto compiler = CompilerAdapterFactory::getInstance().getCompiler(_backend, compilerType);
+ if (wasPreferPlugin) {
+ localProperties[ov::intel_npu::compiler_type.name()] = compilerType;
+ }
OV_ITT_TASK_CHAIN(PLUGIN_COMPILE_MODEL, itt::domains::NPUPlugin, "Plugin::compile_model", "fork_local_config");
auto localConfig = fork_local_config(localProperties, compiler);
@@ -1039,31 +1027,36 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& compi
ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>& model,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::query_model");
- CompilerAdapterFactory compilerAdapterFactory;
- auto npu_plugin_properties = properties;
+ auto localProperties = properties;
// There is an on-going migration from "USE_BASE_MODEL_SERIALIZER" to "MODEL_SERIALIZER_VERSION". Until done, make
// sure only the option supported by the compiler is registered in the config.
bool useBaseModelSerializer = true;
bool modelSerializerChosenExplicitly = false;
const std::string useBaseModelSerializerKey = ov::intel_npu::use_base_model_serializer.name();
const std::string modelSerializerVersionKey = ov::intel_npu::model_serializer_version.name();
- if (npu_plugin_properties.count(useBaseModelSerializerKey)) {
+ if (localProperties.count(useBaseModelSerializerKey)) {
modelSerializerChosenExplicitly = true;
- useBaseModelSerializer = npu_plugin_properties.at(useBaseModelSerializerKey).as<bool>();
- npu_plugin_properties.erase(useBaseModelSerializerKey);
- npu_plugin_properties.erase(modelSerializerVersionKey);
- } else if (npu_plugin_properties.count(modelSerializerVersionKey)) {
+ useBaseModelSerializer = localProperties.at(useBaseModelSerializerKey).as<bool>();
+ localProperties.erase(useBaseModelSerializerKey);
+ localProperties.erase(modelSerializerVersionKey);
+ } else if (localProperties.count(modelSerializerVersionKey)) {
modelSerializerChosenExplicitly = true;
const auto modelSerializerVersion =
- npu_plugin_properties.at(modelSerializerVersionKey).as<ov::intel_npu::ModelSerializerVersion>();
+ localProperties.at(modelSerializerVersionKey).as<ov::intel_npu::ModelSerializerVersion>();
useBaseModelSerializer = !(modelSerializerVersion == ov::intel_npu::ModelSerializerVersion::NO_WEIGHTS_COPY);
- npu_plugin_properties.erase(modelSerializerVersionKey);
+ localProperties.erase(modelSerializerVersionKey);
}
- exclude_model_ptr_from_map(npu_plugin_properties);
- update_log_level(npu_plugin_properties);
- auto compiler =
- compilerAdapterFactory.getCompiler(_backend, resolveCompilerType(_globalConfig, npu_plugin_properties));
- auto localConfig = fork_local_config(npu_plugin_properties, compiler, OptionMode::CompileTime);
+ exclude_model_ptr_from_map(localProperties);
+ update_log_level(localProperties);
+
+ ov::intel_npu::CompilerType compilerType = resolveCompilerType(_globalConfig, localProperties);
+ const bool wasPreferPlugin = (compilerType == ov::intel_npu::CompilerType::PREFER_PLUGIN);
+ auto compiler = CompilerAdapterFactory::getInstance().getCompiler(_backend, compilerType);
+ if (wasPreferPlugin) {
+ localProperties[ov::intel_npu::compiler_type.name()] = compilerType;
+ }
+
+ auto localConfig = fork_local_config(localProperties, compiler, OptionMode::CompileTime);
_logger.setLevel(localConfig.get<LOG_LEVEL>());
const auto platform =
utils::getCompilationPlatform(localConfig.get<PLATFORM>(),
@@ -1113,19 +1106,23 @@ std::shared_ptr<ov::ICompiledModel> Plugin::parse(const ov::Tensor& tensorBig,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::parse");
- auto npu_plugin_properties = properties;
+ auto localProperties = properties;
// ov::hint::model has no corresponding "Config" implementation thus we need to remove it from the
// list of properties
- auto originalModel = exclude_model_ptr_from_map(npu_plugin_properties);
+ auto originalModel = exclude_model_ptr_from_map(localProperties);
+
+ update_log_level(localProperties);
- CompilerAdapterFactory compilerAdapterFactory;
- update_log_level(npu_plugin_properties);
- auto compiler =
- compilerAdapterFactory.getCompiler(_backend, resolveCompilerType(_globalConfig, npu_plugin_properties));
+ ov::intel_npu::CompilerType compilerType = resolveCompilerType(_globalConfig, localProperties);
+ const bool wasPreferPlugin = (compilerType == ov::intel_npu::CompilerType::PREFER_PLUGIN);
+ auto compiler = CompilerAdapterFactory::getInstance().getCompiler(_backend, compilerType);
+ if (wasPreferPlugin) {
+ localProperties[ov::intel_npu::compiler_type.name()] = compilerType;
+ }
OV_ITT_TASK_CHAIN(PLUGIN_PARSE_MODEL, itt::domains::NPUPlugin, "Plugin::parse", "fork_local_config");
- auto localConfig = fork_local_config(npu_plugin_properties, compiler, OptionMode::RunTime);
+ auto localConfig = fork_local_config(localProperties, compiler, OptionMode::RunTime);
_logger.setLevel(localConfig.get<LOG_LEVEL>());
const auto platform =
utils::getCompilationPlatform(localConfig.get<PLATFORM>(),
diff --git a/src/plugins/intel_npu/src/plugin/src/properties.cpp b/src/plugins/intel_npu/src/plugin/src/properties.cpp
index 3df1201063b..89da3db7b37 100644
--- a/src/plugins/intel_npu/src/plugin/src/properties.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/properties.cpp
@@ -559,8 +559,8 @@ void Properties::registerPluginProperties() {
});
REGISTER_CUSTOM_METRIC(ov::intel_npu::compiler_version, true, [&](const Config& config) {
/// create dummy compiler
- CompilerAdapterFactory compilerAdapterFactory;
- auto dummyCompiler = compilerAdapterFactory.getCompiler(_backend, config.get<COMPILER_TYPE>());
+ auto compilerType = config.get<COMPILER_TYPE>();
+ auto dummyCompiler = CompilerAdapterFactory::getInstance().getCompiler(_backend, compilerType);
return dummyCompiler->get_version();
});
REGISTER_CUSTOM_METRIC(ov::internal::caching_properties, false, [&](const Config& config) {
@@ -605,6 +605,7 @@ void Properties::registerCompiledModelProperties() {
TRY_REGISTER_SIMPLE_PROPERTY(ov::compilation_num_threads, COMPILATION_NUM_THREADS);
TRY_REGISTER_SIMPLE_PROPERTY(ov::hint::inference_precision, INFERENCE_PRECISION_HINT);
TRY_REGISTER_SIMPLE_PROPERTY(ov::cache_mode, CACHE_MODE);
+ TRY_REGISTER_SIMPLE_PROPERTY(ov::intel_npu::compiler_type, COMPILER_TYPE);
// Properties we shall only enable if they were set prior-to-compilation
TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::weights_path, WEIGHTS_PATH);
@@ -616,7 +617,6 @@ void Properties::registerCompiledModelProperties() {
TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::intel_npu::dma_engines, DMA_ENGINES);
TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::intel_npu::tiles, TILES);
TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::intel_npu::compilation_mode, COMPILATION_MODE);
- TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::intel_npu::compiler_type, COMPILER_TYPE);
TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::intel_npu::platform, PLATFORM);
TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::intel_npu::dynamic_shape_to_static, DYNAMIC_SHAPE_TO_STATIC);
TRY_REGISTER_COMPILEDMODEL_PROPERTY_IFSET(ov::intel_npu::backend_compilation_params, BACKEND_COMPILATION_PARAMS);
@@ -712,8 +712,8 @@ void Properties::set_property(const ov::AnyMap& properties) {
if (_pType == PropertiesType::PLUGIN) {
try {
// Only accepting unknown config keys in plugin
- CompilerAdapterFactory compilerAdapterFactory;
- compiler = compilerAdapterFactory.getCompiler(_backend, _config.get<COMPILER_TYPE>());
+ auto compilerType = _config.get<COMPILER_TYPE>();
+ compiler = CompilerAdapterFactory::getInstance().getCompiler(_backend, compilerType);
} catch (...) {
// just throw the exception below in case unknown property check is called
}