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
[NPUW] Make weights bank name unique by default (#32321)
Alexey Smirnovae48bc24494d
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cppindex e75542f62ad..25c0bb4574a 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp@@ -712,6 +712,18 @@ std::optional<ov::Any> pop_option(ov::AnyMap& config, const std::string& option_return std::nullopt;}+void apply_weights_bank_name(ov::AnyMap& config, const std::string& bank_name) {+ auto it = config.find("NPUW_WEIGHTS_BANK");+ if (it != config.end()) {+ if (it->second.as<std::string>().empty()) {+ NPUW_ASSERT(false && "NPUW_WEIGHTS_BANK is empty in the provided config! Please use non-empty name to "+ "share the model weights.");+ }+ } else {+ config["NPUW_WEIGHTS_BANK"] = bank_name;+ }+}+ov::AnyMap get_baseline_common_config(const std::optional<NPUDesc>& npudesc) {ov::AnyMap config = {{"NPU_COMPILATION_MODE_PARAMS", "compute-layers-with-higher-precision=Sqrt,Power,ReduceMean,Add_RMSNorm"},@@ -720,7 +732,6 @@ ov::AnyMap get_baseline_common_config(const std::optional<NPUDesc>& npudesc) {{"NPUW_FOLD", "YES"},{"NPUW_DCOFF_TYPE", "f16"},{"NPUW_DCOFF_SCALE", "YES"},- {"NPUW_WEIGHTS_BANK", "shared"},{"NPUW_SLICE_OUT", "YES"},{"NPUW_FUNCALL_ASYNC", "YES"}};// FIXME: this config logic is getting more and more complex@@ -1109,6 +1120,13 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mmerge_config_with(prefill_config, prefill_config_addition_value);merge_config_with(generate_config, generate_config_addition_value);+ // Generate a random weights bank name unique to this LLMCompiledModel object+ auto weights_bank_name = ov::npuw::util::generate_random_string();+ LOG_VERB("Generated a unique weights bank name: " << weights_bank_name);++ apply_weights_bank_name(prefill_config, weights_bank_name);+ apply_weights_bank_name(generate_config, weights_bank_name);+// Handle attention hints. FIXME: Maybe it makes sense to make those// mutually exclusive with the precise configuration sections as wellconst ov::AnyMap dyn_attn_opts = {@@ -1163,8 +1181,10 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& mauto lm_head_config = get_default_lm_head_config(npudesc);merge_config_with(lm_head_config, other_props);auto lm_head_config_addition_value = lm_head_config_addition.value_or(ov::AnyMap{}).as<ov::AnyMap>();-merge_config_with(lm_head_config, lm_head_config_addition_value);++ apply_weights_bank_name(lm_head_config, weights_bank_name);+m_lm_head_compiled = std::dynamic_pointer_cast<ov::npuw::CompiledModel>(ov::npuw::ICompiledModel::create(lm_head_model, plugin, lm_head_config));NPUW_ASSERT(m_lm_head_compiled);diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.cpp b/src/plugins/intel_npu/src/plugin/npuw/util.cppindex 147780706c3..e99f5988409 100644--- a/src/plugins/intel_npu/src/plugin/npuw/util.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/util.cpp@@ -916,6 +916,20 @@ ov::npuw::util::TensorPtr ov::npuw::util::allocMem(const ov::element::Type type,return ov::get_tensor_impl(ov::make_tensor(remote_tensor));}+std::string ov::npuw::util::generate_random_string(std::size_t size) {+ static constexpr auto chars = "0123456789"+ "abcdefghijklmnopqrstuvwxyz"+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ";+ std::random_device rd;+ std::mt19937 gen(rd());+ std::uniform_int_distribution dist({}, std::strlen(chars) - 1);+ std::string result(size, '\0');+ std::generate_n(result.begin(), size, [&]() {+ return chars[dist(gen)];+ });+ return result;+}+bool ov::npuw::util::matchStringWithLoRAPattern(const std::string& input, const std::string& pattern_suffix) {std::string pattern = "^lora_state.*" + pattern_suffix + "$";std::regex regex_pattern(pattern);diff --git a/src/plugins/intel_npu/src/plugin/npuw/util.hpp b/src/plugins/intel_npu/src/plugin/npuw/util.hppindex 6af9fe9cdfa..f1b5f914ab6 100644--- a/src/plugins/intel_npu/src/plugin/npuw/util.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/util.hpp@@ -4,6 +4,7 @@#pragma once+#include <random>#include <string>#include "llm_compiled_model_utils.hpp"@@ -171,6 +172,8 @@ struct Unique {}};+std::string generate_random_string(std::size_t size = 32);+using TensorPtr = ov::SoPtr<ov::ITensor>;TensorPtr allocMem(const ov::element::Type type,const ov::Shape& shape,diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cppindex 24f67fb92fd..8dcb09c7a70 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp@@ -36,6 +36,17 @@ private:std::mutex m_mutex;};+Bank::Bank(const std::shared_ptr<const ov::ICore>& core, const std::string& alloc_device, const std::string& bank_name)+ : m_core(core),+ m_alloc_device(alloc_device),+ m_bank_name(bank_name) {+ if (m_bank_name.empty()) {+ auto unique_name = ov::npuw::util::generate_random_string();+ LOG_WARN("Got an empty name for weights bank! Using a uniquely generated instead: " << unique_name);+ m_bank_name = unique_name;+ }+}+int64_t Bank::registerLT(const LazyTensor& tensor, const std::string& device) {const std::string& device_for_alloc = m_alloc_device.empty() ? device : m_alloc_device;diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hppindex ade5ebbbc5e..0b95b8abb52 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp@@ -25,10 +25,7 @@ namespace weights {class Bank {public:- Bank(const std::shared_ptr<const ov::ICore>& core, const std::string& alloc_device, const std::string& bank_name)- : m_core(core),- m_alloc_device(alloc_device),- m_bank_name(bank_name) {}+ Bank(const std::shared_ptr<const ov::ICore>& core, const std::string& alloc_device, const std::string& bank_name);// Register LazyTensor in a bank if it's not there. Returns LazyTensor's unique idint64_t registerLT(const LazyTensor& tensor, const std::string& device);