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] Reduce memory consumption during import using WEIGHTS_PATH (#31474)
Alexey Smirnov2e923d8b9689
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex 32d86e68336..37898b24563 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -812,6 +812,7 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::import_model(if (is_weightless) {compiled->m_weights_bank = ov::npuw::weights::bank(bank_name, compiled->get_plugin()->get_core(), "");compiled->finalize_weights_bank();+ compiled->m_import_weights_ctx.reset();} else {compiled->m_weights_bank =ov::npuw::weights::Bank::deserialize(model_stream, compiled->get_plugin()->get_core(), bank_name);@@ -1053,17 +1054,20 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(}}- ov::npuw::s11n::Weights weights = nullptr;+ ov::npuw::s11n::WeightsPtr weights = nullptr;if (is_weightless) {if (!weights_path.empty()) {auto mapped_memory = ov::load_mmap_object(weights_path);- weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(mapped_memory->data(),- mapped_memory->size(),- mapped_memory);+ weights = std::make_shared<ov::npuw::s11n::Weights>(mapped_memory->data(),+ mapped_memory->size(),+ mapped_memory);}}- WeightsContext ctx(weights, consts_cache, compiled->m_bf16_consts);+ // FIXME: prolong lifetime of ov::Model for import with MODEL_PTR.+ // Unclear why it's needed, but without saving consts_cache until bank evaluation,+ // the memory is freed somewhere.+ compiled->m_import_weights_ctx = WeightsContext(weights, weights_path, consts_cache, compiled->m_bf16_consts);// Deserialize compiled submodelsstd::size_t subm_size = 0;@@ -1085,7 +1089,7 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(plugin->get_core()->import_model(buffer, compiled->m_dev_list[device_idx]);}compiled->m_compiled_submodels[i].device_it = compiled->m_dev_list.begin() + device_idx;- compiled->m_compiled_submodels[i].deserialize(stream, ctx);+ compiled->m_compiled_submodels[i].deserialize(stream, compiled->m_import_weights_ctx);}compiled->implement_properties();diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hppindex 1f57c050843..dffd58ccd14 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp@@ -66,6 +66,7 @@ private:friend class MemAccessSim;friend class FuncMemMgr;friend class LLMCompiledModel;+ friend class LLMInferRequest;bool compile_for_success(std::size_t id);bool compile_for_device(std::size_t id, const std::string& device_to_try);@@ -191,6 +192,7 @@ private:std::unordered_map<const void*, std::size_t> m_const_to_offset;ov::npuw::s11n::BF16Cache m_bf16_consts;+ ov::npuw::s11n::WeightsContext m_import_weights_ctx;};} // namespace npuw} // namespace ovdiff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cppindex 39ac63c62aa..05cf51b5bf9 100644--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp@@ -13,6 +13,8 @@#include "openvino/op/util/op_types.hpp"#include "openvino/reference/convert.hpp"#include "openvino/runtime/make_tensor.hpp"+#include "openvino/runtime/shared_buffer.hpp"+#include "openvino/util/mmap_object.hpp"#include "util.hpp"using ov::npuw::weights::LazyTensor;@@ -53,6 +55,14 @@ ov::Tensor Const::eval() const {return ov::npuw::util::copy_tensor_from_const(m_node);}+ // Weightless import case. Mmmap CPU weight on demand to avoid allocating all weights at once.+ if (!m_weights_path.empty()) {+ 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);+ return ov::Tensor(m_cached_type, m_cached_shape, m_mmaped_weights->get_ptr(m_offset));+ }+NPUW_ASSERT(m_read_from_bin && "Underlying data should have been read first! Or the tensor is already detached.");return m_read_from_bin;}@@ -62,6 +72,11 @@ LazyTensor::Meta Const::eval_meta() const {return {m_node->get_shape(), m_node->get_element_type()};}+ // Weightless import case+ if (!m_weights_path.empty()) {+ return {m_cached_shape, m_cached_type};+ }+NPUW_ASSERT(m_read_from_bin && "Underlying data should have been read first!");return {m_read_from_bin.get_shape(), m_read_from_bin.get_element_type()};}@@ -85,13 +100,18 @@ void Const::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {auto dst_data = m_read_from_bin.data<dst_type>();ov::reference::convert_from_bf16_to_f16_with_clamp(src_data, dst_data, m_read_from_bin.get_size());} else {- m_read_from_bin = ov::Tensor(m_cached_type, m_cached_shape);- std::memcpy(m_read_from_bin.data(), ctx.weights->get_ptr(m_offset), m_byte_size);+ // Each LazyTensor will mmap the whole weights file on demand (in eval()).+ // It doesn't introduce extra allocation, however it allows to gradually 1 by 1+ // read mmaped CPU weights and allocate them on device without loading all the weights first.+ // Thus the memory consumption during import is greatly reduced but at the slight cost of performance.+ NPUW_ASSERT(!ctx.weights_path.empty());+ // Just save weights_path for the eval() to call the actual mmap.+ m_weights_path = ctx.weights_path;}} else {auto it = ctx.consts_cache.find({m_offset, m_byte_size});NPUW_ASSERT(it != ctx.consts_cache.end() && "Couldn't find Constant in cache!");- m_read_from_bin = ov::npuw::util::copy_tensor_from_const(it->second);+ m_read_from_bin = ov::npuw::util::tensor_from_const(it->second);NPUW_ASSERT(m_read_from_bin.get_byte_size() == m_byte_size && m_read_from_bin.get_shape() == m_cached_shape &&m_read_from_bin.get_element_type() == m_cached_type);}@@ -100,6 +120,7 @@ void Const::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {void Const::detach() {m_node.reset();m_read_from_bin = ov::Tensor();+ m_mmaped_weights.reset();}void Const::serialize(std::ostream& stream) const {diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hppindex d89f7fdbc21..273a24c15ac 100644--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp@@ -101,6 +101,8 @@ private:std::size_t m_offset = 0;std::size_t m_byte_size = 0;ov::Tensor m_read_from_bin;+ std::string m_weights_path;+ mutable ov::npuw::s11n::WeightsPtr m_mmaped_weights = nullptr;};class Concat {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 bcb8a643cdb..9f8e0ed10d4 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@@ -1393,7 +1393,9 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::import_mcompiled->m_prefill_compiled->m_weights_bank = bank;compiled->m_kvcache_compiled->finalize_weights_bank();+ compiled->m_kvcache_compiled->m_import_weights_ctx.reset();compiled->m_prefill_compiled->finalize_weights_bank();+ compiled->m_prefill_compiled->m_import_weights_ctx.reset();} else {auto bank =ov::npuw::weights::Bank::deserialize(model_stream, compiled->get_plugin()->get_core(), bank_name);diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cppindex 2f000cfb6cf..2820fe6d16e 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_infer_request.cpp@@ -237,6 +237,34 @@ ov::npuw::LLMInferRequest::LLMInferRequest(const std::shared_ptr<ov::npuw::LLMCom_lm_head_request->get_tensor(lm_head_embed_port));}+ // FIXME: E-177589+ // FIXME: "fixes"/workarounds caching import on CPU (also might be related to bf16 weights).+ // Unclear how it's related. Previously fill_tensor()+ // was in copy_kvcache() call. When it was removed, it broke the import accuracy.+ bool enable_cpu_wa = false;+ const auto& kvcache_compiled = m_npuw_llm_compiled_model->m_kvcache_compiled;+ for (std::size_t idx = 0; idx < kvcache_compiled->m_compiled_submodels.size(); ++idx) {+ if (kvcache_compiled->submodel_device(idx) == "CPU") {+ enable_cpu_wa = true;+ break;+ }+ }++ if (enable_cpu_wa) {+ const auto& kvcache_compiled = m_kvcache_request->get_compiled_model();+ // FIXME: Find only matching by names outputs and copy them, having previously checked that such inputs exist+ for (std::size_t i = kStartOutputKVCacheLayers; i < kvcache_compiled->outputs().size(); ++i) {+ const auto& output_name = kvcache_compiled->outputs()[i].get_any_name();+ const auto& input_name =+ std::regex_replace(output_name, std::regex("present"), layer_names::past_key_values);+ if (m_kvcache_in_ports.find(input_name) == m_kvcache_in_ports.end()) {+ continue;+ }+ auto kvcache_in_tensor = m_kvcache_request->get_tensor(m_kvcache_in_ports.at(input_name));+ fill_tensor<ov::float16>(kvcache_in_tensor, 0);+ }+ }+m_generate_initialized = false;}diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cppindex d1b4a6d2090..a2f51314b14 100644--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp@@ -24,10 +24,12 @@ ov::npuw::s11n::WeightsContext::WeightsContext(bool _is_weightless,const_to_offset(_const_to_offset) {}// NOTE: This construtor can and should only be used when importing blobs-ov::npuw::s11n::WeightsContext::WeightsContext(const ov::npuw::s11n::Weights& _weights,+ov::npuw::s11n::WeightsContext::WeightsContext(const ov::npuw::s11n::WeightsPtr& _weights,+ const std::string& _weights_path,const s11n::WeightsContext::ConstsCache& _consts_cache,const BF16Cache& _bf16_consts): weights(_weights),+ weights_path(_weights_path),consts_cache(_consts_cache),bf16_consts(_bf16_consts) {is_weightless = _weights || !_consts_cache.empty();diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hppindex dbe5f69194f..6f4a0d81e1f 100644--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp@@ -89,7 +89,8 @@ public:};using BF16Cache = std::unordered_set<std::pair<std::size_t, std::size_t>, ov::npuw::s11n::PairHash>;-using Weights = std::shared_ptr<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>;+using Weights = ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>;+using WeightsPtr = std::shared_ptr<Weights>;struct CompiledContext {CompiledContext(bool _encrypted,@@ -115,17 +116,28 @@ struct WeightsContext {};using ConstsCache = std::unordered_map<std::pair<std::size_t, std::size_t>, std::shared_ptr<ov::Node>, CtxHash>;+ WeightsContext() = default;+// NOTE: This construtor should only be used when exporting blobs- explicit WeightsContext(bool _is_weightless, const std::unordered_map<const void*, std::size_t>& _const_to_offset);+ WeightsContext(bool _is_weightless, const std::unordered_map<const void*, std::size_t>& _const_to_offset);// NOTE: This construtor can and should only be used when importing weightless blobs- explicit WeightsContext(const ov::npuw::s11n::Weights& _weights,- const ConstsCache& _consts_cache,- const BF16Cache& _bf16_consts);+ WeightsContext(const ov::npuw::s11n::WeightsPtr& _weights,+ const std::string& _weights_path,+ const ConstsCache& _consts_cache,+ const BF16Cache& _bf16_consts);++ WeightsContext& operator=(const WeightsContext& other) = default;++ void reset() {+ weights = nullptr;+ consts_cache.clear();+ }bool is_weightless = true;std::unordered_map<const void*, std::size_t> const_to_offset;- ov::npuw::s11n::Weights weights = nullptr;+ ov::npuw::s11n::WeightsPtr weights = nullptr;+ std::string weights_path;ConstsCache consts_cache;BF16Cache bf16_consts;};diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cppindex 09c4937df11..24f67fb92fd 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.cpp@@ -110,7 +110,10 @@ void Bank::evaluate_cpu(Bank::DeviceBank& device_bank, const std::vector<LazyTenNPUW_ASSERT(iter_device_registered != device_bank.registered_tensors.end() &&"Tensor should be registered first!");auto uid = iter_device_registered->second;- device_bank.storage.at(uid).tensor = lt.eval();+ auto t = lt.eval();+ device_bank.storage.at(uid).tensor = ov::Tensor(t.get_element_type(), t.get_shape());+ // Get ownership of the weights, might be a mmaped object during import+ t.copy_to(device_bank.storage.at(uid).tensor);const_cast<LazyTensor&>(lt).detach();});}diff --git a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hppindex 994f3b0cec0..ade5ebbbc5e 100644--- a/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/weights_bank.hpp@@ -25,9 +25,7 @@ namespace weights {class Bank {public:- explicit Bank(const std::shared_ptr<const ov::ICore>& core,- const std::string& alloc_device,- const std::string& bank_name)+ 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) {}diff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cppindex badf8899584..0ad57418eb0 100644--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp@@ -271,7 +271,7 @@ TEST(SerializationTest, OVTypes_Tensor_with_weights) {WeightsContext::ConstsCache consts_cache;consts_cache[{0, 0}] = nullptr;- WeightsContext des_ctx(nullptr, consts_cache, {});+ WeightsContext des_ctx(nullptr, "", consts_cache, {});write_weightless(ss, {var}, ctx);read_weightless(ss, res, des_ctx);