Skip to content

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

[`NPU`] [`icompiler_adapter`] Extend `icompiler_adapter` interface to accept optional `value` (#32823)

Mircea-Aurelian Dan9d1f078e72a5
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hpp
index 221cd22f135..dd8b214077f 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/config.hpp
@@ -298,6 +298,16 @@ struct OptionBase {
return ONEAPI_MAKE_VERSION(7, 23);
}
+ static bool isValueSupported(std::string_view val) {
+ try {
+ (void)ActualOpt::parse(val);
+ return true;
+ } catch (...) {
+ // failed to parse, return false below
+ }
+ return false;
+ }
+
static std::string toString(const ValueType& val) {
return OptionPrinter<ValueType>::toString(val);
}
@@ -362,7 +372,17 @@ struct OptionConcept final {
bool (*isPublic)() = nullptr;
ov::PropertyMutability (*mutability)() = nullptr;
uint32_t (*compilerSupportVersion)() = nullptr;
+ bool (*isValueSupportedImpl)(std::string_view val) =
+ nullptr; // better make this private, but won't be able to use aggregate initialization anymore in
+ // "makeOptionModel"
std::shared_ptr<OptionValue> (*validateAndParse)(std::string_view val) = nullptr;
+ std::optional<std::function<bool(std::string_view)>> customValueCheckerOpt = std::nullopt;
+ bool isValueSupported(std::string_view val) {
+ if (customValueCheckerOpt.has_value()) {
+ return customValueCheckerOpt.value()(val);
+ }
+ return isValueSupportedImpl(val);
+ }
};
template <class Opt>
@@ -379,14 +399,17 @@ std::shared_ptr<OptionValue> validateAndParse(std::string_view val) {
}
template <class Opt>
-OptionConcept makeOptionModel() {
+OptionConcept makeOptionModel(
+ std::optional<std::function<bool(std::string_view)>> customValueCheckerOpt = std::nullopt) {
return {&Opt::key,
&Opt::envVar,
&Opt::mode,
&Opt::isPublic,
&Opt::mutability,
&Opt::compilerSupportVersion,
- &validateAndParse<Opt>};
+ &Opt::isValueSupported,
+ &validateAndParse<Opt>,
+ std::move(customValueCheckerOpt)};
}
} // namespace details
@@ -398,7 +421,7 @@ OptionConcept makeOptionModel() {
class OptionsDesc final {
public:
template <class Opt>
- void add();
+ void add(std::optional<std::function<bool(std::string_view)>> customValueCheckerOpt = std::nullopt);
bool has(std::string_view key) const;
@@ -417,9 +440,9 @@ private:
};
template <class Opt>
-void OptionsDesc::add() {
+void OptionsDesc::add(std::optional<std::function<bool(std::string_view)>> customValueCheckerOpt) {
OPENVINO_ASSERT(_impl.count(Opt::key().data()) == 0, "Option '", Opt::key().data(), "' was already registered");
- _impl.insert({Opt::key().data(), details::makeOptionModel<Opt>()});
+ _impl.insert({Opt::key().data(), details::makeOptionModel<Opt>(std::move(customValueCheckerOpt))});
for (const auto& deprecatedKey : Opt::deprecatedKeys()) {
OPENVINO_ASSERT(_deprecated.count(deprecatedKey.data()) == 0,
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
index f41ceef203a..97da4218f3e 100644
--- 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
@@ -54,7 +54,7 @@ public:
const FilteredConfig& config) const = 0;
virtual uint32_t get_version() const = 0;
virtual std::vector<std::string> get_supported_options() const = 0;
- virtual bool is_option_supported(std::string optname) const = 0;
+ virtual bool is_option_supported(std::string optName, std::optional<std::string> optValue = std::nullopt) const = 0;
virtual ~ICompilerAdapter() = default;
};
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 89f0d6feb8b..6938daf98b7 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
@@ -36,7 +36,7 @@ public:
std::vector<std::string> get_supported_options() const override;
- bool is_option_supported(std::string optname) const override;
+ bool is_option_supported(std::string optName, std::optional<std::string> optValue = std::nullopt) const override;
uint32_t get_version() const override;
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 0675d964565..91e0415c84b 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
@@ -36,7 +36,7 @@ public:
std::vector<std::string> get_supported_options() const override;
- bool is_option_supported(std::string optname) const override;
+ bool is_option_supported(std::string optName, std::optional<std::string> optValue = std::nullopt) const override;
uint32_t get_version() const override;
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
index eb529a34efe..6a6c2a57534 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
@@ -45,7 +45,7 @@ public:
std::string getCompilerSupportedOptions() const;
- bool isOptionSupported(std::string optname) const;
+ bool isOptionSupported(std::string optName, std::optional<std::string> optValue = std::nullopt) const;
bool isTurboOptionSupported(const ze_graph_compiler_version_info_t& compilerVersion) const;
void getGraphBinary(const GraphDescriptor& graphDescriptor,
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 15d0091e73b..19c2df2c9af 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
@@ -347,8 +347,8 @@ std::vector<std::string> DriverCompilerAdapter::get_supported_options() const {
return compilerOpts;
}
-bool DriverCompilerAdapter::is_option_supported(std::string optname) const {
- return _zeGraphExt->isOptionSupported(std::move(optname));
+bool DriverCompilerAdapter::is_option_supported(std::string optName, std::optional<std::string> optValue) const {
+ return _zeGraphExt->isOptionSupported(std::move(optName), std::move(optValue));
}
} // namespace intel_npu
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 c0c0b3ddff3..3e81442f264 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
@@ -343,9 +343,9 @@ std::vector<std::string> PluginCompilerAdapter::get_supported_options() const {
return {};
}
-bool PluginCompilerAdapter::is_option_supported(std::string optname) const {
+bool PluginCompilerAdapter::is_option_supported(std::string optName, std::optional<std::string> optValue) const {
// This functions has no utility in PluginCompiler
- // returning false for any request to avoid the option of spaming the plugin
+ // returning false for any request to avoid the option of spamming the plugin
return false;
}
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
index 52e8e4a3455..c1379a98c9c 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
@@ -554,7 +554,7 @@ std::string ZeGraphExtWrappers::getCompilerSupportedOptions() const {
return {};
}
-bool ZeGraphExtWrappers::isOptionSupported(std::string optname) const {
+bool ZeGraphExtWrappers::isOptionSupported(std::string optName, std::optional<std::string> optValue) const {
// Early exit if api is not supported
if (_graphExtVersion < ZE_MAKE_VERSION(1, 11)) {
return false;
@@ -570,11 +570,12 @@ bool ZeGraphExtWrappers::isOptionSupported(std::string optname) const {
}
#endif
- const char* optname_ch = optname.c_str();
+ const char* optname_ch = optName.c_str();
+ const char* optvalue_ch = optValue.has_value() ? optValue.value().c_str() : nullptr;
auto result = _zeroInitStruct->getGraphDdiTable().pfnCompilerIsOptionSupported(_zeroInitStruct->getDevice(),
ZE_NPU_COMPILER_OPTIONS,
optname_ch,
- nullptr);
+ optvalue_ch);
if (result == ZE_RESULT_SUCCESS) {
return true;
} else if ((result == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) || (result == ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE) ||
diff --git a/src/plugins/intel_npu/tests/unit/npu/config.cpp b/src/plugins/intel_npu/tests/unit/npu/config.cpp
new file mode 100644
index 00000000000..335d9d25cbf
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npu/config.cpp
@@ -0,0 +1,165 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "intel_npu/config/config.hpp"
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/test_assertions.hpp"
+
+namespace ov {
+
+namespace unit_test {
+
+namespace intel_npu {
+
+enum class DummyTestOption {
+ TESTVALUE_0 = 0,
+ TESTVALUE_1 = 1,
+ TESTVALUE_2 = 2,
+ TESTVALUE_3 = 3 // not parsed
+};
+
+std::string_view stringifyEnum(DummyTestOption dummyTestOption);
+
+std::string_view stringifyEnum(DummyTestOption dummyTestOption) {
+ switch (dummyTestOption) {
+ case DummyTestOption::TESTVALUE_0:
+ return "TESTVALUE_0";
+ case DummyTestOption::TESTVALUE_1:
+ return "TESTVALUE_1";
+ case DummyTestOption::TESTVALUE_2:
+ return "TESTVALUE_2";
+ default:
+ OPENVINO_THROW("Cannot stringify DummyTestOption!");
+ }
+}
+
+static constexpr ov::Property<DummyTestOption, PropertyMutability::RO> dummy_test_option{"DUMMY_TEST_OPTION"};
+
+struct DUMMY_TEST_OPTION final : ::intel_npu::OptionBase<DUMMY_TEST_OPTION, DummyTestOption> {
+ static std::string_view key() {
+ return dummy_test_option.name();
+ }
+
+ static constexpr std::string_view getTypeName() {
+ return "ov::unit_test_intel_npu::DummyTestOption";
+ }
+
+ static DummyTestOption defaultValue() {
+ return DummyTestOption::TESTVALUE_0;
+ }
+};
+
+static constexpr ov::Property<DummyTestOption, PropertyMutability::RO> dummy_test_option_custom_class_checker{
+ "DUMMY_TEST_OPTION_CUSTOM_CLASS_CHECKER"};
+
+struct DUMMY_TEST_OPTION_CUSTOM_CLASS_CHECKER final
+ : ::intel_npu::OptionBase<DUMMY_TEST_OPTION_CUSTOM_CLASS_CHECKER, DummyTestOption> {
+ static std::string_view key() {
+ return dummy_test_option_custom_class_checker.name();
+ }
+
+ static constexpr std::string_view getTypeName() {
+ return "ov::unit_test_intel_npu::DummyTestOption";
+ }
+
+ static DummyTestOption defaultValue() {
+ return DummyTestOption::TESTVALUE_0;
+ }
+
+ static bool isValueSupported(std::string_view /* unusedVal */) { // custom class checker is here!
+ return true;
+ }
+};
+
+} // namespace intel_npu
+
+} // namespace unit_test
+
+} // namespace ov
+
+template <>
+struct intel_npu::OptionParser<ov::unit_test::intel_npu::DummyTestOption> final {
+ static ov::unit_test::intel_npu::DummyTestOption parse(std::string_view val);
+};
+
+ov::unit_test::intel_npu::DummyTestOption intel_npu::OptionParser<ov::unit_test::intel_npu::DummyTestOption>::parse(
+ std::string_view val) {
+ if (val == "TESTVALUE_0") {
+ return ov::unit_test::intel_npu::DummyTestOption::TESTVALUE_0;
+ } else if (val == "TESTVALUE_1") {
+ return ov::unit_test::intel_npu::DummyTestOption::TESTVALUE_1;
+ } else if (val == "TESTVALUE_2") {
+ return ov::unit_test::intel_npu::DummyTestOption::TESTVALUE_2;
+ }
+ OPENVINO_THROW("Cannot parse DummyTestOption: ", val);
+}
+
+namespace {
+
+using ConfigUnitTests = ::testing::Test;
+
+using namespace ov::unit_test::intel_npu;
+
+static constexpr std::string_view hardcodedTestValue3 = "TESTVALUE_3";
+static constexpr std::string_view expectedParseErrorMessage = "Cannot parse DummyTestOption: TESTVALUE_3";
+
+TEST_F(ConfigUnitTests, DefaultOptionValueCheckerWorks) {
+ intel_npu::OptionsDesc options;
+ options.add<DUMMY_TEST_OPTION>();
+ options.add<DUMMY_TEST_OPTION_CUSTOM_CLASS_CHECKER>();
+
+ ASSERT_EQ(true,
+ options.get(dummy_test_option.name())
+ .isValueSupported(DUMMY_TEST_OPTION::toString(
+ DummyTestOption::TESTVALUE_1))); // parsable values will be returned as supported
+}
+
+TEST_F(ConfigUnitTests, OptionImplementationValueCheckerWorks) {
+ intel_npu::OptionsDesc options;
+ options.add<DUMMY_TEST_OPTION>();
+ options.add<DUMMY_TEST_OPTION_CUSTOM_CLASS_CHECKER>();
+
+ // make sure TESTVALUE_3 cannot be parsed by DUMMY_TEST_OPTION
+ OV_EXPECT_THROW_HAS_SUBSTRING(DUMMY_TEST_OPTION::parse(hardcodedTestValue3),
+ ov::Exception,
+ expectedParseErrorMessage.data());
+
+ ASSERT_EQ(false,
+ options.get(dummy_test_option.name())
+ .isValueSupported(hardcodedTestValue3)); // unparsable values will not be returned as supported
+
+ // logic changes for a class that implements its own `isValueSupported` method
+ ASSERT_EQ(true, options.get(dummy_test_option_custom_class_checker.name()).isValueSupported(hardcodedTestValue3));
+}
+
+TEST_F(ConfigUnitTests, CustomValueCheckerWorks) {
+ intel_npu::OptionsDesc options;
+ options.add<DUMMY_TEST_OPTION>(
+ [](std::string_view /* unusedVal */) { // custom value checker is given on option registration
+ return true;
+ });
+ options.add<DUMMY_TEST_OPTION_CUSTOM_CLASS_CHECKER>([](std::string_view /* unusedVal */) {
+ return false;
+ });
+
+ // make sure TESTVALUE_3 cannot be parsed by DUMMY_TEST_OPTION
+ OV_EXPECT_THROW_HAS_SUBSTRING(DUMMY_TEST_OPTION::parse(hardcodedTestValue3),
+ ov::Exception,
+ expectedParseErrorMessage.data());
+
+ ASSERT_EQ(true,
+ options.get(dummy_test_option.name())
+ .isValueSupported(hardcodedTestValue3)); // even if TESTVALUE_3 cannot be parsed,
+ // custom checker should be prioritized
+
+ // same prioritization expectation for custom value checker even if class implements self `isValueSupported` method
+ // which returns `true`
+ ASSERT_EQ(false,
+ options.get(dummy_test_option_custom_class_checker.name())
+ .isValueSupported(DUMMY_TEST_OPTION_CUSTOM_CLASS_CHECKER::toString(DummyTestOption::TESTVALUE_2)));
+}
+
+} // namespace