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] Fix deserialization in case new Constants were introduced into the model (#32054)
Alexey Smirnov9eea1fff2f4a
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex 36968be077e..32bfeb4a32c 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -1203,8 +1203,6 @@ void ov::npuw::CompiledModel::reconstruct_closure() {const auto real_idx = comp_model_desc.replaced_by.value_or(idx);auto& func_desc = m_compiled_submodels[real_idx];- // At this point closure size should have already been deserialized- NPUW_ASSERT(!comp_model_desc.closure.empty() && "Closure shouldn't be empty at this point!");for (std::size_t cidx = 0; cidx < comp_model_desc.closure.size(); ++cidx) {if (comp_model_desc.closure[cidx]) {// host-side closure - already set, do nothingdiff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cppindex 90fcb382fcf..3e026135e7c 100644--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp@@ -140,6 +140,7 @@ Const Const::deserialize(std::istream& stream) {read(stream, c.m_cached_shape);read(stream, c.m_offset);read(stream, c.m_byte_size);+return c;}diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cppindex 4c682b77d29..2a282e6ed02 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp@@ -21,6 +21,7 @@#include "openvino/util/xml_parse_utils.hpp"#include "patterns/dcoff.hpp"#include "patterns/opt.hpp"+#include "patterns/pre_compute.hpp"#include "traits.hpp"namespace ov {@@ -325,6 +326,7 @@ public:void saveScaleFactors(const std::string& func_name);void saveRepeatedConstants(const std::string& func_name);void saveTailDictConstants(const std::string& func_name);+ void saveRoPEConstants(const std::string& func_name);void matchParameters(const std::string& func_name);void matchResults(const std::string& func_name);void createFunction(const std::string& func_name);@@ -1470,6 +1472,39 @@ void Partitioner::saveTailDictConstants(const std::string& func_name) {LOG_VERB("Done");}+void Partitioner::saveRoPEConstants(const std::string& func_name) {+ // RoPE patterns introduce new Constants to the model when applied. When we import the model+ // as weightless blob, new Constants have no reference to the original weights.+ // After they get folded into Parameters and handled as LazyTensors, deserialization will fail+ // during read_weight() function. Thus, we need to keep newly introduced constants as Constants.+ auto& func_group = all_functions.at(func_name);+ auto& subgr_group = func_group.refs;++ if (subgr_group.size() > 1) {+ // RoPE should only be applied to the head+ return;+ }++ LOG_VERB("Trying to preserve some (head) constants for " << func_name << " in model " << model->get_friendly_name()+ << "...");+ LOG_BLOCK();++ auto& model_group = func_group.mdls;++ using CPtr = std::shared_ptr<ov::op::v0::Constant>;+ std::vector<CPtr> to_keep;++ ov::pass::GraphRewrite rewr;+ rewr.add_matcher<ov::npuw::patterns::pre_compute::PreserveConstsRope>(std::ref(to_keep));+ rewr.run_on_model(model_group.front());++ for (auto&& const_to_keep : to_keep) {+ LOG_DEBUG("[KEEP] " << const_to_keep);+ func_group.consts_to_keep.insert(const_to_keep);+ }+ LOG_VERB("Done");+}+void Partitioner::matchParameters(const std::string& func_name) {LOG_VERB("Matching parameters for function " << func_name << " in model " << model->get_friendly_name() << "...");LOG_BLOCK();@@ -2471,6 +2506,7 @@ ov::npuw::Partitioning ov::npuw::getPartitioning(const std::shared_ptr<ov::Modelp.sanityCheck(func_group);p.saveRepeatedConstants(func_group);p.saveTailDictConstants(func_group);+ p.saveRoPEConstants(func_group);p.matchParameters(func_group);p.matchResults(func_group);p.matchRepeatedSubgraphs(func_group);diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cppindex 224b21d8ec6..8330f68d5a1 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cpp@@ -161,3 +161,29 @@ bool ov::npuw::patterns::pre_compute::RopeCache::run_on_model(const std::shared_ov::npuw::patterns::pre_compute::RopeCacheMatcher ropeCache(m_max_prompt_len, model);return true;}++// Const -------> Gather ---> Convert ---> Squeeze ---> Unsqeeze+// Parameter --->+ov::npuw::patterns::pre_compute::PreserveConstsRope::PreserveConstsRope(+ ov::npuw::patterns::pre_compute::PreserveConstsRope::Results to_keep) {+ auto pids = opp::wrap_type<ov::op::v0::Parameter>();++ auto cnst = opp::wrap_type<ov::op::v0::Constant>();+ auto gather = opp::wrap_type<ov::op::v8::Gather>({cnst, pids, opp::any_input()});++ auto cvtg = opp::wrap_type<ov::op::v0::Convert>({gather});+ auto squeeze = opp::wrap_type<ov::op::v0::Squeeze>({cvtg, opp::any_input()});+ auto unsqueeze = opp::wrap_type<ov::op::v0::Unsqueeze>({squeeze, opp::any_input()});++ // Note: Use [=] to make sure the above objects stay alive in the callback+ auto callback = [=](ov::pass::pattern::Matcher& m) {+ auto& node_to_output = m.get_pattern_value_map();++ auto matched_node_cnst = node_to_output.at(cnst).get_node_shared_ptr();+ auto matched_cnst = std::static_pointer_cast<ov::op::v0::Constant>(matched_node_cnst);++ to_keep.get().push_back(matched_cnst);+ return false; // root hasn't changed+ };+ register_matcher(std::make_shared<opp::Matcher>(unsqueeze, "PrecomputePreserveConstsRope"), std::move(callback));+}diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hppindex 23882181f81..7163e2a9106 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hpp@@ -4,7 +4,9 @@#pragma once+#include "openvino/pass/graph_rewrite.hpp"#include "openvino/pass/pattern/multi_matcher.hpp"+namespace ov ::npuw ::patterns ::pre_compute {class RopePatternDesc {@@ -65,5 +67,15 @@ public:explicit RopeCache(const uint32_t max_prompt_len) : m_max_prompt_len(max_prompt_len) {}bool run_on_model(const std::shared_ptr<ov::Model>& m) override;};++class PreserveConstsRope : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::opt::PreserveConstsRope");++ using CPtr = std::shared_ptr<ov::op::v0::Constant>;+ using Results = std::reference_wrapper<std::vector<CPtr>>;++ PreserveConstsRope(Results to_keep);+};// NOLINTNEXTLINE(readability/namespace)} // namespace ov::npuw::patterns::pre_compute