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] Handle new Constants to prevent deserialization issues (#32181)
Alexey Smirnov2ad60ea19c3d
diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cppindex 3e026135e7c..f30ac6c42c4 100644--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp@@ -33,6 +33,11 @@ Const::Const(const std::shared_ptr<ov::op::v0::Constant>& n) : m_node(n) {auto weightless_cache_attr = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());if (weightless_cache_attr != rt_info.end()) {m_offset = weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset;+ } else {+ // See the comment in serialize() for more details+ LOG_WARN("Some pattern introduced a new Constant node not present in the original weights file. We need to "+ "keep it in case export occurs. This will increase memory consumption.");+ m_copied_if_not_in_model = ov::npuw::util::copy_tensor_from_const(m_node);}}@@ -57,6 +62,8 @@ ov::Tensor Const::eval() const {// Weightless import case. Mmmap CPU weight on demand to avoid allocating all weights at once.if (!m_weights_path.empty()) {+ NPUW_ASSERT(!m_read_from_bin &&+ "Trying to read weight from weights file, but the weight has been already deserialized!");auto mapped_memory = ov::load_mmap_object(m_weights_path);m_mmaped_weights =std::make_shared<ov::npuw::s11n::Weights>(mapped_memory->data(), mapped_memory->size(), mapped_memory);@@ -84,6 +91,10 @@ LazyTensor::Meta Const::eval_meta() const {void Const::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {NPUW_ASSERT(!m_node &&"LazyTensor can only read weight when it's being deserialized and not created from a Constant!");+ if (m_read_from_bin) {+ // already deserialized, see the comment in serialize() for more details+ return;+ }if (ctx.weights) {if (ctx.bf16_consts.find({m_offset, m_byte_size}) != ctx.bf16_consts.end()) {NPUW_ASSERT(m_cached_type == ov::element::f16);@@ -129,6 +140,24 @@ void Const::serialize(std::ostream& stream) const {write(stream, m_cached_shape);write(stream, m_offset);write(stream, m_byte_size);++ // FIXME: handle a special case:+ // 1) We added a Constant to the model before compilation (e.g. int RoPE patterns)+ // 2) This Constant became a parameter during folding+ // 3) Thus it became a LazyTensor, but there is no original data in weights file,+ // so it will fail in read_weight() during weightless deserialization.+ // In this case we need to include Constant's data into the blob.+ if (m_copied_if_not_in_model) {+ LOG_WARN("Some pattern introduced a new Constant node not present in the original weights file. This will "+ "increase the blob size by "+ << m_byte_size << " bytes.");+ write(stream, true);+ write(stream, m_copied_if_not_in_model);+ // detach the tensor+ m_copied_if_not_in_model = ov::Tensor();+ } else {+ write(stream, false);+ }}Const Const::deserialize(std::istream& stream) {@@ -141,6 +170,12 @@ Const Const::deserialize(std::istream& stream) {read(stream, c.m_offset);read(stream, c.m_byte_size);+ bool contains_weight = false;+ read(stream, contains_weight);+ if (contains_weight) {+ read(stream, c.m_read_from_bin);+ }+return c;}diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hppindex 78a482f5f32..a7e27e02536 100644--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp@@ -109,6 +109,10 @@ private:ov::Tensor m_read_from_bin;std::string m_weights_path;mutable ov::npuw::s11n::WeightsPtr m_mmaped_weights = nullptr;+ // FIXME: special case when a new Constant was added into the model,+ // then made into LazyTensor during folding. We need to keep a copy of it,+ // so during weightless deserialization we can access it.+ mutable ov::Tensor m_copied_if_not_in_model;};class Concat {diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cppindex 2a282e6ed02..7fc9e26c9b6 100644--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp@@ -12,6 +12,7 @@#include "online/compiler.hpp"#include "online/utils/utils.hpp" // getMetaDesc#include "openvino/core/parallel.hpp"+#include "openvino/core/rt_info/weightless_caching_attributes.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/slice.hpp"#include "openvino/op/util/op_types.hpp"@@ -21,7 +22,6 @@#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 {@@ -326,7 +326,6 @@ 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);@@ -1472,39 +1471,6 @@ 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();@@ -2506,7 +2472,6 @@ 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 8330f68d5a1..224b21d8ec6 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,29 +161,3 @@ 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 7163e2a9106..480d7bb54f0 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,6 @@#pragma once-#include "openvino/pass/graph_rewrite.hpp"#include "openvino/pass/pattern/multi_matcher.hpp"namespace ov ::npuw ::patterns ::pre_compute {@@ -67,15 +66,5 @@ 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