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] Moving options cache from plugin compiler adapter to compiler_impl (#38184)
Bogdan Pereanu5e9f324ea26e
diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/option_support_cache.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/option_support_cache.hppindex 4e693934a9a..661fdd0f5fb 100644--- a/src/plugins/intel_npu/src/common/include/intel_npu/common/option_support_cache.hpp+++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/option_support_cache.hpp@@ -5,9 +5,11 @@#pragma once#include <cstdint>+#include <memory>#include <mutex>#include <optional>#include <string>+#include <utility>#include <vector>namespace intel_npu {@@ -37,4 +39,45 @@ private:std::vector<KeyOptionsState> _optionSupportStates;};+/**+ * @brief An option support cache bound to the cache key of a single compiler.+ * @note Copyable and default-constructible. A default-constructed (or null cache) instance is disabled: all+ * queries answer "not cached" and all stores are dropped, so callers do not need to check for a cache.+ */+class ScopedOptionSupportCache final {+public:+ ScopedOptionSupportCache() = default;+ ScopedOptionSupportCache(std::shared_ptr<OptionSupportCache> cache, const OptionSupportCache::CacheKey key)+ : _cache(std::move(cache)),+ _key(key) {}++ /**+ * @brief Tells whether a cache is attached. Only needed by callers that want to skip work which is+ * pointless without a cache; the accessors below are safe to call either way.+ */+ bool enabled() const {+ return _cache != nullptr;+ }++ std::optional<bool> isOptionSupported(const std::string& optionName) const {+ return _cache ? _cache->isOptionSupported(_key, optionName) : std::nullopt;+ }++ void addSupportedOption(const std::string& optionName, const bool supported = true) const {+ if (_cache) {+ _cache->addSupportedOption(_key, optionName, supported);+ }+ }++ void setSupportedOptions(const std::vector<std::string>& supportedOptions) const {+ if (_cache) {+ _cache->setSupportedOptions(_key, supportedOptions);+ }+ }++private:+ std::shared_ptr<OptionSupportCache> _cache;+ OptionSupportCache::CacheKey _key = 0;+};+} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hppindex 2b8528fd623..23089a3640f 100644--- a/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp+++ b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp@@ -13,6 +13,7 @@#include "intel_npu/common/filtered_config.hpp"#include "intel_npu/common/npu.hpp"+#include "intel_npu/common/option_support_cache.hpp"#include "intel_npu/utils/vcl/vcl_api.hpp"#include "openvino/core/except.hpp"#include "openvino/core/model.hpp"@@ -24,8 +25,15 @@ namespace intel_npu {class VCLCompilerImpl final : public std::enable_shared_from_this<VCLCompilerImpl> {public:+ /**+ * @param functions A shared pointer to the VCL function table+ * @param deviceProperties The properties of the device the compilation targets, if known+ * @param optionSupportCache The cache used for storing the compiler's option support answers, already bound to+ * this compiler's cache key. May be disabled, in which case the compiler is queried every time.+ */VCLCompilerImpl(std::shared_ptr<const VCLFunctionTable> functions,- const std::optional<IDevice::DeviceProperties>& deviceProperties = std::nullopt);+ const std::optional<IDevice::DeviceProperties>& deviceProperties = std::nullopt,+ ScopedOptionSupportCache optionSupportCache = {});~VCLCompilerImpl();/**@@ -88,6 +96,8 @@ public:/*** @brief Returns the compiler supported options list+ * @note The result is stored in the option support cache, if one was provided, so that subsequent+ * "is_option_supported" calls for these options can be answered without querying the compiler.*/std::vector<std::string> get_supported_options() const;@@ -96,6 +106,9 @@ public:* @param option The option name to check* @param optValue The option value to validate* @return true if the option and value are supported, false otherwise+ * @note Queries without a value are served from and recorded in the option support cache, if one was+ * provided. Queries carrying a value always reach the compiler, since the cache is keyed by option+ * name alone and cannot tell whether a specific value is accepted.*/bool is_option_supported(const std::string& option,const std::optional<std::string>& optValue = std::nullopt) const;@@ -116,6 +129,9 @@ private:vcl_compiler_properties_t _compilerProperties;vcl_version_info_t _vclVersion;vcl_version_info_t _vclProfilingVersion;++ ScopedOptionSupportCache _optionSupportCache;+Logger _logger;};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.hppindex 5d90c27ab55..3df51802804 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@@ -45,7 +45,7 @@ private:std::optional<std::string> get_compatibility_descriptor(ze_graph_handle_t graphHandle) const;std::shared_ptr<ZeroInitStructsHolder> _zeroInitStruct;- std::shared_ptr<OptionSupportCache> _optionSupportCache;+ ScopedOptionSupportCache _optionSupportCache;std::shared_ptr<ZeGraphExtWrappers> _zeGraphExt;ze_device_graph_properties_t _compilerProperties = {};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.hppindex a91a3a05552..c7119fcdb89 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@@ -42,7 +42,6 @@ public:private:std::shared_ptr<ZeroInitStructsHolder> _zeroInitStruct;- std::shared_ptr<OptionSupportCache> _optionSupportCache;std::shared_ptr<ZeGraphExtWrappers> _zeGraphExt;ov::SoPtr<VCLCompilerImpl> _compiler;diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cppindex ac967a34c18..087376b4a82 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp@@ -5,11 +5,14 @@#include "compiler_impl.hpp"#include <algorithm>+#include <cinttypes>#include <cstring>#include <limits>#include <mutex>#include <sstream>+#include <string>#include <utility>+#include <vector>#include "intel_npu/config/options.hpp"#include "intel_npu/npu_private_properties.hpp"@@ -128,9 +131,11 @@ static std::optional<std::string> getVCLCompatibilityString(const VCLFunctionTab} while (0)VCLCompilerImpl::VCLCompilerImpl(std::shared_ptr<const VCLFunctionTable> functions,- const std::optional<IDevice::DeviceProperties>& deviceProperties)+ const std::optional<IDevice::DeviceProperties>& deviceProperties,+ ScopedOptionSupportCache optionSupportCache): _functions(std::move(functions)),_logHandle(nullptr),+ _optionSupportCache(std::move(optionSupportCache)),_logger("VCLCompilerImpl", Logger::global().level()) {_logger.debug("VCLCompilerImpl constructor start");@@ -254,15 +259,15 @@ std::pair<ov::Tensor, std::optional<std::string>> VCLCompilerImpl::compile(compilerVersion.major = _compilerProperties.version.major;compilerVersion.minor = _compilerProperties.version.minor;- const auto isOptionValueSupportedByCompiler = [this](const std::string& optionName,- const std::optional<std::string>& optionValue) {+ const auto isOptionSupportedByCompiler = [this](const std::string& optionName,+ const std::optional<std::string>& optionValue = std::nullopt) {return is_option_supported(optionName, optionValue);};auto serializedIR = compiler_utils::serializeIR(model,compilerVersion,maxOpsetVersion,config.get<MODEL_SERIALIZER_VERSION>(),- isOptionValueSupportedByCompiler,+ isOptionSupportedByCompiler,false,storeWeightlessCacheAttributeFlag);FilteredConfig updatedConfig = config;@@ -272,9 +277,6 @@ std::pair<ov::Tensor, std::optional<std::string>> VCLCompilerImpl::compile(}std::string buildFlags;- const auto isOptionSupportedByCompiler = [this](const std::string& optionName) {- return is_option_supported(optionName);- };_logger.debug("create build flags");buildFlags += compiler_utils::serializeIOInfo(model, true);@@ -387,15 +389,15 @@ std::pair<std::vector<ov::Tensor>, std::optional<std::string>> VCLCompilerImpl::compilerVersion.major = _compilerProperties.version.major;compilerVersion.minor = _compilerProperties.version.minor;- const auto isOptionValueSupportedByCompiler = [this](const std::string& optionName,- const std::optional<std::string>& optionValue) {+ const auto isOptionSupportedByCompiler = [this](const std::string& optionName,+ const std::optional<std::string>& optionValue = std::nullopt) {return is_option_supported(optionName, optionValue);};auto serializedIR = compiler_utils::serializeIR(model,compilerVersion,maxOpsetVersion,config.get<MODEL_SERIALIZER_VERSION>(),- isOptionValueSupportedByCompiler,+ isOptionSupportedByCompiler,false,true);FilteredConfig updatedConfig = config;@@ -405,9 +407,6 @@ std::pair<std::vector<ov::Tensor>, std::optional<std::string>> VCLCompilerImpl::}std::string buildFlags;- const auto isOptionSupportedByCompiler = [this](const std::string& optionName) {- return is_option_supported(optionName);- };_logger.debug("create build flags");buildFlags += compiler_utils::serializeIOInfo(model, true);@@ -567,24 +566,21 @@ ov::SupportedOpsMap VCLCompilerImpl::query(const std::shared_ptr<const ov::ModelcompilerVersion.major = _compilerProperties.version.major;compilerVersion.minor = _compilerProperties.version.minor;FilteredConfig updatedConfig = config;- const auto isOptionValueSupportedByCompiler = [this](const std::string& optionName,- const std::optional<std::string>& optionValue) {+ const auto isOptionSupportedByCompiler = [this](const std::string& optionName,+ const std::optional<std::string>& optionValue = std::nullopt) {return is_option_supported(optionName, optionValue);};auto serializedIR = compiler_utils::serializeIR(model,compilerVersion,maxOpsetVersion,config.get<MODEL_SERIALIZER_VERSION>(),- isOptionValueSupportedByCompiler);+ isOptionSupportedByCompiler);if (is_option_supported(ov::intel_npu::model_serializer_version.name())) {updatedConfig.update(ov::intel_npu::model_serializer_version.name(),MODEL_SERIALIZER_VERSION::toString(serializedIR.serializerVersion));}std::string buildFlags;- const auto isOptionSupportedByCompiler = [this](const std::string& optionName) {- return is_option_supported(optionName);- };buildFlags += compiler_utils::serializeConfig(updatedConfig, compilerVersion, isOptionSupportedByCompiler);_logger.debug("queryImpl build flags : %s", buildFlags.c_str());@@ -626,7 +622,7 @@ ov::SupportedOpsMap VCLCompilerImpl::query(const std::shared_ptr<const ov::Modelstd::vector<std::string> VCLCompilerImpl::get_supported_options() const {_logger.debug("get_supported_options start");- size_t str_size = 0;+ uint64_t str_size = 0;THROW_ON_FAIL_FOR_VCL(*_functions,"vclGetCompilerSupportedOptions",_functions->vclGetCompilerSupportedOptions(_compilerHandle, nullptr, &str_size),@@ -644,27 +640,42 @@ std::vector<std::string> VCLCompilerImpl::get_supported_options() const {_functions->vclGetCompilerSupportedOptions(_compilerHandle, options.data(), &str_size),_logHandle);- _logger.debug("Option list size %d, got option list", str_size);-- // VCL hands back a char buffer that may carry trailing NULs. Trimming and tokenising here keeps that- // calling convention out of IVCLCompiler.- auto trailingNul = std::find(options.begin(), options.end(), '\0');- std::string optionsStr(options.begin(), trailingNul);+ _logger.debug("Option list size %" PRIu64 ", got option list", str_size);- // Marker grepped by tests/functional/internal/plugin/test_compiler_option_support_helper.cpp to- // assert the bulk retrieval runs exactly once per compiler-type key. Keep the text in sync.- _logger.debug("VCLCompilerImpl return supported_options: %s", optionsStr.c_str());+ size_t optionsSize = options.size();+ while (optionsSize > 0 && options[optionsSize - 1] == '\0') {+ --optionsSize;+ }+ if (optionsSize == 0) {+ return {};+ }- std::vector<std::string> result;- std::istringstream iss(optionsStr);- std::string token;- while (iss >> token) {- result.push_back(token);+ std::string compilerOptionsStr(options.data(), optionsSize);+ _logger.debug("VCLCompilerImpl return supported_options: %s", compilerOptionsStr.c_str());+ // vectorize string+ std::istringstream suppstream(compilerOptionsStr);+ std::vector<std::string> compilerOpts;+ std::string option;+ while (suppstream >> option) {+ compilerOpts.push_back(option);}- return result;++ _optionSupportCache.setSupportedOptions(compilerOpts);++ return compilerOpts;}bool VCLCompilerImpl::is_option_supported(const std::string& option, const std::optional<std::string>& optValue) const {+ // The cache is keyed by option name alone, so it can only answer queries that do not carry a value.+ const bool useCache = !optValue.has_value();+ if (useCache) {+ const auto cachedSupport = _optionSupportCache.isOptionSupported(option);+ if (cachedSupport.has_value()) {+ return cachedSupport.value();+ }+ }++ bool supported = false;try {const char* optname_ch = option.c_str();const char* optvalue_ch = optValue.has_value() ? optValue.value().c_str() : nullptr;@@ -672,12 +683,17 @@ bool VCLCompilerImpl::is_option_supported(const std::string& option, const std::"vclGetCompilerIsOptionSupported",_functions->vclGetCompilerIsOptionSupported(_compilerHandle, optname_ch, optvalue_ch),_logHandle);- return true;+ supported = true;} catch (const std::exception& e) {// The API is only supported in new version, just add log here_logger.debug("Exception in is_option_supported: %s", e.what());}- return false;++ if (useCache) {+ _optionSupportCache.addSupportedOption(option, supported);+ }++ return supported;}} // namespace intel_npudiff --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.cppindex 61fca88f9ba..c3d27f028b7 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@@ -70,7 +70,7 @@ const std::vector<PropertySupportInfo> _supportedPropertiesWithVersions = {DriverCompilerAdapter::DriverCompilerAdapter(const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,const std::shared_ptr<OptionSupportCache>& optionSupportCache): _zeroInitStruct(zeroInitStruct),- _optionSupportCache(optionSupportCache),+ _optionSupportCache(optionSupportCache, driverOptionSupportKey),_logger("DriverCompilerAdapter", Logger::global().level()) {_logger.info("initialize DriverCompilerAdapter start");@@ -335,9 +335,7 @@ std::vector<std::string> DriverCompilerAdapter::get_supported_options() const {compilerOpts.push_back(option);}- if (_optionSupportCache) {- _optionSupportCache->setSupportedOptions(driverOptionSupportKey, compilerOpts);- }+ _optionSupportCache.setSupportedOptions(compilerOpts);return compilerOpts;}@@ -353,17 +351,15 @@ std::vector<std::string> DriverCompilerAdapter::get_supported_options() const {return {};}- if (_optionSupportCache) {- _optionSupportCache->setSupportedOptions(driverOptionSupportKey, compilerOpts);- }+ _optionSupportCache.setSupportedOptions(compilerOpts);return compilerOpts;}bool DriverCompilerAdapter::is_option_supported(const std::string& optName,const std::optional<std::string>& optValue) const {- bool optionSupportCache = _optionSupportCache && !optValue.has_value();- if (optionSupportCache) {- const auto cachedSupport = _optionSupportCache->isOptionSupported(driverOptionSupportKey, optName);+ const bool useCache = !optValue.has_value();+ if (useCache) {+ const auto cachedSupport = _optionSupportCache.isOptionSupported(optName);if (cachedSupport.has_value()) {_logger.debug("Option %s %s by DriverCompilerAdapter",optName.c_str(),@@ -375,8 +371,8 @@ bool DriverCompilerAdapter::is_option_supported(const std::string& optName,auto isOptionSupported = _zeGraphExt->isOptionSupported(optName, optValue);if (isOptionSupported.has_value()) {const bool supported = isOptionSupported.value();- if (optionSupportCache) {- _optionSupportCache->addSupportedOption(driverOptionSupportKey, optName, supported);+ if (useCache) {+ _optionSupportCache.addSupportedOption(optName, supported);}_logger.debug("Option %s with value '%s' %s by DriverCompilerAdapter",@@ -391,9 +387,9 @@ bool DriverCompilerAdapter::is_option_supported(const std::string& optName,for (const auto& prop : _supportedPropertiesWithVersions) {if (prop.name == optName) {const bool supported = isVersionSupportedByCompiler(prop.version, compilerVersion);- if (_optionSupportCache) {- _optionSupportCache->addSupportedOption(driverOptionSupportKey, optName, supported);- }+ // The legacy path resolves support from the option name alone, so the answer is cacheable+ // even when the query carried a value.+ _optionSupportCache.addSupportedOption(optName, supported);_logger.debug("Option %s %s by DriverCompilerAdapter",optName.c_str(),supported ? "is supported" : "is not supported");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.cppindex e74c47f838f..69ee9c1c6e0 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@@ -37,7 +37,6 @@ PluginCompilerAdapter::PluginCompilerAdapter(const std::shared_ptr<ZeroInitStrucconst std::shared_ptr<OptionSupportCache>& optionSupportCache,const std::optional<IDevice::DeviceProperties>& deviceProperties): _zeroInitStruct(zeroInitStruct),- _optionSupportCache(optionSupportCache),_logger("PluginCompilerAdapter", Logger::global().level()) {_logger.info("initialize PluginCompilerAdapter start");@@ -46,7 +45,10 @@ PluginCompilerAdapter::PluginCompilerAdapter(const std::shared_ptr<ZeroInitStrucauto ovLibPath = ov::util::path_to_string(ov::util::get_ov_lib_path());auto vclLoader = VCLLoader::getInstance(ovLibPath);OPENVINO_ASSERT(vclLoader != nullptr, "VCL loader is nullptr");- auto vclCompilerPtr = std::make_shared<VCLCompilerImpl>(vclLoader->sharedFunctions(), deviceProperties);+ auto vclCompilerPtr =+ std::make_shared<VCLCompilerImpl>(vclLoader->sharedFunctions(),+ deviceProperties,+ ScopedOptionSupportCache{optionSupportCache, pluginOptionSupportKey});OPENVINO_ASSERT(vclCompilerPtr != nullptr, "VCL compiler is nullptr");// Pair the compiler with the library so the .so cannot be unloaded while the compiler// dispatches into it. The compiler itself no longer knows a library is involved.@@ -291,31 +293,12 @@ uint32_t PluginCompilerAdapter::get_version() const {}std::vector<std::string> PluginCompilerAdapter::get_supported_options() const {- const std::vector<std::string> compilerOpts = _compiler->get_supported_options();-- if (_optionSupportCache) {- _optionSupportCache->setSupportedOptions(pluginOptionSupportKey, compilerOpts);- }- return compilerOpts;+ return _compiler->get_supported_options();}bool PluginCompilerAdapter::is_option_supported(const std::string& optname,const std::optional<std::string>& optValue) const {- bool optionSupportCache = _optionSupportCache && !optValue.has_value();- if (optionSupportCache) {- const auto cachedSupport = _optionSupportCache->isOptionSupported(pluginOptionSupportKey, optname);- if (cachedSupport.has_value()) {- _logger.debug("Option %s %s by PluginCompilerAdapter",- optname.c_str(),- cachedSupport.value() ? "is supported" : "is not supported");- return cachedSupport.value();- }- }-const bool supported = _compiler->is_option_supported(optname, optValue);- if (optionSupportCache) {- _optionSupportCache->addSupportedOption(pluginOptionSupportKey, optname, supported);- }_logger.debug("Option %s with value '%s' %s by PluginCompilerAdapter",optname.c_str(),diff --git a/src/plugins/intel_npu/tests/unit/npu/compiler_adapter/compiler_impl_test.cpp b/src/plugins/intel_npu/tests/unit/npu/compiler_adapter/compiler_impl_test.cppindex 300991c0f36..fb79f5421bd 100644--- a/src/plugins/intel_npu/tests/unit/npu/compiler_adapter/compiler_impl_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npu/compiler_adapter/compiler_impl_test.cpp@@ -6,6 +6,7 @@#include <gtest/gtest.h>+#include <algorithm>#include <cstdint>#include <limits>#include <memory>@@ -15,6 +16,7 @@#include "fake_vcl.hpp"#include "intel_npu/common/filtered_config.hpp"+#include "intel_npu/common/option_support_cache.hpp"#include "intel_npu/config/options.hpp"#include "intel_npu/utils/utils.hpp"#include "model_serializer.hpp"@@ -28,10 +30,16 @@ using ::fake_vcl::FakeVcl;using ::intel_npu::FilteredConfig;using ::intel_npu::IDevice;using ::intel_npu::OptionsDesc;+using ::intel_npu::OptionSupportCache;+using ::intel_npu::ScopedOptionSupportCache;using ::intel_npu::VCLCompilerImpl;namespace {+/// Two distinct cache keys, standing in for the plugin and driver adapters sharing one cache.+constexpr OptionSupportCache::CacheKey kFirstKey = 1u;+constexpr OptionSupportCache::CacheKey kSecondKey = 2u;+/// Registers just the options the compiler-in-plugin path reads, so `config.get<>` resolves.std::shared_ptr<OptionsDesc> makeOptionsDesc() {auto desc = std::make_shared<OptionsDesc>();@@ -63,6 +71,18 @@ struct VCLCompilerImplTest : public ::testing::Test {const std::optional<IDevice::DeviceProperties>& props = std::nullopt) {return std::make_shared<VCLCompilerImpl>(fake.functions(), props);}++ /// A compiler that routes its option-support answers through `cache` under `key`. Without a+ /// cache the compiler is queried on every call, which is the makeCompiler() behaviour above.+ std::shared_ptr<VCLCompilerImpl> makeCachingCompiler(const std::shared_ptr<OptionSupportCache>& cache,+ const OptionSupportCache::CacheKey key = kFirstKey) {+ return std::make_shared<VCLCompilerImpl>(fake.functions(), std::nullopt, ScopedOptionSupportCache{cache, key});+ }++ /// Number of times the compiler library was actually asked about an option.+ size_t optionQueryCount() const {+ return fake.callCount("vclGetCompilerIsOptionSupported");+ }};//@@ -331,6 +351,245 @@ TEST_F(VCLCompilerImplTest, GetSupportedOptionsThrowsWhenTheSizingCallFails) {EXPECT_THROW(compiler->get_supported_options(), ov::Exception);}+//+// --- option support cache ---+//+// The cache used to live in PluginCompilerAdapter; it now sits behind VCLCompilerImpl, so every+// caller (adapter, serializeConfig, serializeIR) shares one set of answers. The cache is keyed by+// (cache key, option name) only - it carries no value - which is what the tests below pin down.+//++TEST_F(VCLCompilerImplTest, WithoutACacheEveryIsOptionSupportedCallReachesTheCompiler) {+ // The baseline the cache is measured against: a null cache means no memoisation at all.+ auto compiler = makeCompiler();++ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));+ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));++ EXPECT_EQ(optionQueryCount(), 2u);+}++TEST_F(VCLCompilerImplTest, IsOptionSupportedIsAnsweredFromTheCacheOnRepeatedQueries) {+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);++ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));+ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));+ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));++ EXPECT_EQ(optionQueryCount(), 1u);+ EXPECT_EQ(cache->isOptionSupported(kFirstKey, "SOME_OPTION"), std::make_optional(true));+}++TEST_F(VCLCompilerImplTest, IsOptionSupportedCachesNegativeAnswersToo) {+ // "Not supported" is just as expensive to re-derive as "supported", and the compiler's answer+ // cannot change for a given compiler instance.+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);+ fake.unsupportedOptions.insert("MISSING_OPTION");++ EXPECT_FALSE(compiler->is_option_supported("MISSING_OPTION"));+ EXPECT_EQ(optionQueryCount(), 1u);++ // Make the fake start advertising the option: the cached "false" must still win, proving the+ // second call never reached the library.+ fake.unsupportedOptions.clear();+ EXPECT_FALSE(compiler->is_option_supported("MISSING_OPTION"));+ EXPECT_EQ(optionQueryCount(), 1u);+ EXPECT_EQ(cache->isOptionSupported(kFirstKey, "MISSING_OPTION"), std::make_optional(false));+}++TEST_F(VCLCompilerImplTest, QueriesCarryingAValueAlwaysReachTheCompiler) {+ // The cache key is the option name alone, so it cannot tell whether a specific value is+ // accepted. Serving a valued query from it would answer a different question.+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);++ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION", std::string("VALUE_A")));+ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION", std::string("VALUE_A")));++ EXPECT_EQ(optionQueryCount(), 2u);+}++TEST_F(VCLCompilerImplTest, AValuedQueryNeitherReadsNorWritesTheCache) {+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);+ fake.unsupportedOptions.insert("SOME_OPTION");++ // A rejected value must not be recorded as "the option is unsupported": the name-only answer+ // is a different fact, and caching the value verdict under the name would corrupt it.+ EXPECT_FALSE(compiler->is_option_supported("SOME_OPTION", std::string("BAD_VALUE")));+ EXPECT_FALSE(cache->isOptionSupported(kFirstKey, "SOME_OPTION").has_value());++ fake.unsupportedOptions.clear();+ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));+ EXPECT_EQ(cache->isOptionSupported(kFirstKey, "SOME_OPTION"), std::make_optional(true));+}++TEST_F(VCLCompilerImplTest, ACachedNameOnlyAnswerDoesNotShortCircuitAValuedQuery) {+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);++ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));+ ASSERT_EQ(optionQueryCount(), 1u);++ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION", std::string("VALUE_A")));+ EXPECT_EQ(optionQueryCount(), 2u);+ ASSERT_EQ(fake.optionSupportQueries.size(), 2u);+ ASSERT_TRUE(fake.optionSupportQueries[1].second.has_value());+ EXPECT_EQ(*fake.optionSupportQueries[1].second, "VALUE_A");+}++TEST_F(VCLCompilerImplTest, GetSupportedOptionsPopulatesTheCacheForLaterQueries) {+ // The bulk list is the cheap way to fill the cache: one library call, then every listed option+ // is answered locally. This is why get_supported_options writes through to the cache.+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);++ ASSERT_EQ(compiler->get_supported_options(), std::vector<std::string>({"OPT_A", "OPT_B", "OPT_C"}));++ EXPECT_TRUE(compiler->is_option_supported("OPT_A"));+ EXPECT_TRUE(compiler->is_option_supported("OPT_B"));+ EXPECT_TRUE(compiler->is_option_supported("OPT_C"));+ EXPECT_EQ(optionQueryCount(), 0u);+}++TEST_F(VCLCompilerImplTest, OptionsAbsentFromTheBulkListStillReachTheCompiler) {+ // setSupportedOptions only records positives, so an unlisted option is "unknown", not "false".+ // Treating it as false would deny options a newer library accepts but does not enumerate.+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);+ (void)compiler->get_supported_options();++ EXPECT_TRUE(compiler->is_option_supported("OPT_UNLISTED"));+ EXPECT_EQ(optionQueryCount(), 1u);+}++TEST_F(VCLCompilerImplTest, GetSupportedOptionsWithoutACacheDoesNotThrow) {+ auto compiler = makeCompiler();+ EXPECT_NO_THROW((void)compiler->get_supported_options());+}++TEST_F(VCLCompilerImplTest, EntriesAreScopedToTheCacheKey) {+ // One cache is shared by the plugin and driver adapters under different keys. Their compilers+ // accept different options, so an answer must never leak across keys.+ auto cache = std::make_shared<OptionSupportCache>();+ auto first = makeCachingCompiler(cache, kFirstKey);+ auto second = makeCachingCompiler(cache, kSecondKey);++ EXPECT_TRUE(first->is_option_supported("SOME_OPTION"));+ ASSERT_EQ(optionQueryCount(), 1u);++ EXPECT_TRUE(second->is_option_supported("SOME_OPTION"));+ EXPECT_EQ(optionQueryCount(), 2u);+}++TEST_F(VCLCompilerImplTest, CompilersSharingAKeyShareTheirAnswers) {+ auto cache = std::make_shared<OptionSupportCache>();+ auto first = makeCachingCompiler(cache, kFirstKey);+ auto second = makeCachingCompiler(cache, kFirstKey);++ EXPECT_TRUE(first->is_option_supported("SOME_OPTION"));+ ASSERT_EQ(optionQueryCount(), 1u);++ EXPECT_TRUE(second->is_option_supported("SOME_OPTION"));+ EXPECT_EQ(optionQueryCount(), 1u);+}++TEST_F(VCLCompilerImplTest, CachedAnswersOutliveTheCompilerThatProducedThem) {+ // The cache belongs to the plugin, not to a compiler instance: a compiler recreated for a+ // second compile must not have to re-query.+ auto cache = std::make_shared<OptionSupportCache>();+ {+ auto compiler = makeCachingCompiler(cache);+ EXPECT_TRUE(compiler->is_option_supported("SOME_OPTION"));+ }+ ASSERT_EQ(optionQueryCount(), 1u);++ auto recreated = makeCachingCompiler(cache);+ EXPECT_TRUE(recreated->is_option_supported("SOME_OPTION"));+ EXPECT_EQ(optionQueryCount(), 1u);+}++TEST_F(VCLCompilerImplTest, CompileHonoursACachedNegativeWithoutQueryingTheCompiler) {+ // The whole point of moving the cache behind the compiler: the build-flag construction inside+ // compile() goes through the same memoised answers, not just the adapter's public API.+ auto cache = std::make_shared<OptionSupportCache>();+ const std::string serializerVersion{ov::intel_npu::model_serializer_version.name()};+ cache->addSupportedOption(kFirstKey, serializerVersion, false);+ auto compiler = makeCachingCompiler(cache);++ const auto [tensor, compatibility] = compiler->compile(makeModel(), makeConfig());+ (void)tensor;+ (void)compatibility;++ ASSERT_EQ(fake.buildFlags.size(), 1u);+ EXPECT_EQ(fake.buildFlags[0].find(serializerVersion), std::string::npos);+ // The cached "false" is authoritative: the library was never asked whether it knows the option.+ // Valued probes for a concrete serializer version still go through, by design.+ const bool askedByNameOnly = std::any_of(fake.optionSupportQueries.begin(),+ fake.optionSupportQueries.end(),+ [&serializerVersion](const auto& query) {+ return query.first == serializerVersion && !query.second.has_value();+ });+ EXPECT_FALSE(askedByNameOnly);+}++TEST_F(VCLCompilerImplTest, CompileAsksAboutEachOptionNameOnlyOnceWhenCaching) {+ // compile() consults option support from several places - serializeIR, the serializer-version+ // write-back and serializeConfig - so without the cache the same name is queried repeatedly.+ // Valued queries (serializeIR probes concrete serializer versions) are exempt by design.+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);++ const auto result = compiler->compile(makeModel(), makeConfig());+ (void)result;++ ASSERT_FALSE(fake.optionSupportQueries.empty());+ std::vector<std::string> names;+ for (const auto& query : fake.optionSupportQueries) {+ if (!query.second.has_value()) {+ names.push_back(query.first);+ }+ }+ ASSERT_FALSE(names.empty()) << "expected at least one name-only option query during compile()";+ std::sort(names.begin(), names.end());+ EXPECT_EQ(std::adjacent_find(names.begin(), names.end()), names.end())+ << "an option name was queried more than once despite the cache";+}++TEST_F(VCLCompilerImplTest, WithoutACacheCompileRepeatsTheSameOptionQueries) {+ // The counterpart of the test above: it is only the cache that collapses the duplicates, so a+ // regression that stops wiring it would show up here as an unchanged query count.+ auto compiler = makeCompiler();++ const auto result = compiler->compile(makeModel(), makeConfig());+ (void)result;++ std::vector<std::string> names;+ for (const auto& query : fake.optionSupportQueries) {+ if (!query.second.has_value()) {+ names.push_back(query.first);+ }+ }+ std::sort(names.begin(), names.end());+ EXPECT_NE(std::adjacent_find(names.begin(), names.end()), names.end());+}++TEST_F(VCLCompilerImplTest, TheBulkListContradictingACachedNegativeIsRejected) {+ // Documents the cache's conflict guard as seen through the compiler: once an option is recorded+ // as unsupported, a bulk list that advertises it is a genuine inconsistency rather than an+ // update, and setSupportedOptions refuses it instead of silently flipping the answer.+ auto cache = std::make_shared<OptionSupportCache>();+ auto compiler = makeCachingCompiler(cache);+ fake.unsupportedOptions.insert("OPT_A");++ ASSERT_FALSE(compiler->is_option_supported("OPT_A"));++ fake.unsupportedOptions.clear();+ EXPECT_THROW((void)compiler->get_supported_options(), ov::Exception);+}+//// --- process_profiling_output ---//