Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPUW] Support MODEL_PTR for weightless serialization (#29274)

Alexey Smirnov47f24fb33b65
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/runtime.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/runtime.hpp
index c74db6f8253..0987b27f85d 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/runtime.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/runtime.hpp
@@ -219,6 +219,34 @@ struct WEIGHTS_PATH final : OptionBase<WEIGHTS_PATH, std::string> {
}
};
+//
+// MODEL_PTR
+//
+struct MODEL_PTR final : OptionBase<MODEL_PTR, std::shared_ptr<const ov::Model>> {
+ static std::string_view key() {
+ return ov::hint::model.name();
+ }
+
+ static constexpr std::string_view getTypeName() {
+ return "std::shared_ptr<ov::Model>";
+ }
+
+ static std::shared_ptr<const ov::Model> defaultValue() {
+ return nullptr;
+ }
+
+ static std::shared_ptr<const ov::Model> parse(std::string_view) {
+ return nullptr;
+ }
+ static std::string toString(const std::shared_ptr<const ov::Model>& m) {
+ return "";
+ }
+
+ static OptionMode mode() {
+ return OptionMode::RunTime;
+ }
+};
+
//
// ENABLE_CPU_PINNING
//
diff --git a/src/plugins/intel_npu/src/al/src/config/runtime.cpp b/src/plugins/intel_npu/src/al/src/config/runtime.cpp
index 0db9bb89d97..63913142503 100644
--- a/src/plugins/intel_npu/src/al/src/config/runtime.cpp
+++ b/src/plugins/intel_npu/src/al/src/config/runtime.cpp
@@ -27,6 +27,7 @@ void intel_npu::registerRunTimeOptions(OptionsDesc& desc) {
desc.add<WORKLOAD_TYPE>();
desc.add<TURBO>();
desc.add<WEIGHTS_PATH>();
+ desc.add<MODEL_PTR>();
desc.add<BYPASS_UMD_CACHING>();
desc.add<RUN_INFERENCES_SEQUENTIALLY>();
desc.add<DISABLE_VERSION_CHECK>();
diff --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.cpp
index 045e1adc5a2..56e4a4d639b 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
@@ -572,46 +572,24 @@ std::string DriverCompilerAdapter::serializeConfig(const Config& config,
content = std::regex_replace(content, std::regex(dqstr.str()), "");
}
- // NPU_DEFER_WEIGHTS_LOAD is needed at runtime only
- {
- std::ostringstream batchstr;
- batchstr << ov::intel_npu::defer_weights_load.name() << KEY_VALUE_SEPARATOR << VALUE_DELIMITER << "\\S+"
- << VALUE_DELIMITER;
- logger.info("NPU_DEFER_WEIGHTS_LOAD property is needed at runtime only. Removing from parameters");
- content = std::regex_replace(content, std::regex(batchstr.str()), "");
- }
-
- // NPU_RUN_INFERENCES_SEQUENTIALLY is needed at runtime only
- {
- std::ostringstream batchstr;
- batchstr << ov::intel_npu::run_inferences_sequentially.name() << KEY_VALUE_SEPARATOR << VALUE_DELIMITER
- << "\\S+" << VALUE_DELIMITER;
- logger.info("NPU_RUN_INFERENCES_SEQUENTIALLY property is needed at runtime only. Removing from parameters");
- content = std::regex_replace(content, std::regex(batchstr.str()), "");
- }
-
- // Remove the properties that are not used by the compiler WorkloadType is used only by compiled model
- std::ostringstream workloadtypestr;
- workloadtypestr << ov::workload_type.name() << KEY_VALUE_SEPARATOR << VALUE_DELIMITER << "\\S+" << VALUE_DELIMITER;
- content = std::regex_replace(content, std::regex(workloadtypestr.str()), "");
- // Remove turbo property as it is not used by compiler
- std::ostringstream turbostring;
- turbostring << ov::intel_npu::turbo.name() << KEY_VALUE_SEPARATOR << VALUE_DELIMITER << "\\S+" << VALUE_DELIMITER;
- content = std::regex_replace(content, std::regex(turbostring.str()), "");
- // Remove weights path property as it is not used by compiler
- std::ostringstream weightspathstream;
- weightspathstream << ov::weights_path.name() << KEY_VALUE_SEPARATOR << VALUE_DELIMITER << "\\S+" << VALUE_DELIMITER;
- content = std::regex_replace(content, std::regex(weightspathstream.str()), "");
- // Remove Bypass UMD Caching propery
- std::ostringstream umdcachestring;
- umdcachestring << ov::intel_npu::bypass_umd_caching.name() << KEY_VALUE_SEPARATOR << VALUE_DELIMITER << "\\S+"
- << VALUE_DELIMITER;
- content = std::regex_replace(content, std::regex(umdcachestring.str()), "");
+ // Remove properties that are not used by the compiler
+ auto remove_unused_property = [&](const char* property_name) {
+ std::ostringstream propertystream;
+ propertystream << property_name << KEY_VALUE_SEPARATOR << VALUE_DELIMITER << "\\S+" << VALUE_DELIMITER;
+ std::string log(property_name);
+ log.append(" property is needed at runtime only. Removing from parameters");
+ logger.info(log.c_str());
+ content = std::regex_replace(content, std::regex(propertystream.str()), "");
+ };
- std::ostringstream skipversioncheck;
- skipversioncheck << ov::intel_npu::disable_version_check.name() << KEY_VALUE_SEPARATOR << VALUE_DELIMITER << "\\S+"
- << VALUE_DELIMITER;
- content = std::regex_replace(content, std::regex(skipversioncheck.str()), "");
+ remove_unused_property(ov::intel_npu::defer_weights_load.name());
+ remove_unused_property(ov::intel_npu::run_inferences_sequentially.name());
+ remove_unused_property(ov::workload_type.name());
+ remove_unused_property(ov::intel_npu::turbo.name());
+ remove_unused_property(ov::weights_path.name());
+ remove_unused_property(ov::hint::model.name());
+ remove_unused_property(ov::intel_npu::bypass_umd_caching.name());
+ remove_unused_property(ov::intel_npu::disable_version_check.name());
// FINAL step to convert prefixes of remaining params, to ensure backwards compatibility
// From 5.0.0, driver compiler start to use NPU_ prefix, the old version uses VPU_ prefix
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
index 7536b3bcc42..43d230ca649 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -513,7 +513,7 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
}
void ov::npuw::CompiledModel::CompiledModelDesc::serialize(std::ostream& stream,
- const ov::npuw::s11n::Context& ctx) const {
+ const ov::npuw::s11n::WeightsContext& ctx) const {
using namespace ov::npuw::s11n;
LOG_DEBUG("Serializing CompiledModelDesc...");
@@ -581,7 +581,7 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(std::ostream& stream,
}
void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& stream,
- const ov::npuw::s11n::Weights& weights) {
+ const ov::npuw::s11n::WeightsContext& ctx) {
using namespace ov::npuw::s11n;
LOG_DEBUG("Deserializing CompiledModelDesc...");
@@ -601,9 +601,9 @@ void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& strea
read(stream, is_remote);
read(stream, closure_uid);
- if (weights) {
- read_weightless(stream, scales, weights);
- read_weightless(stream, zerops, weights);
+ if (ctx.weights || !ctx.consts_cache.empty()) {
+ read_weightless(stream, scales, ctx);
+ read_weightless(stream, zerops, ctx);
std::size_t closure_size = 0;
read(stream, closure_size);
@@ -614,7 +614,7 @@ void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& strea
read(stream, cpu_closure_ids);
std::vector<ov::Tensor> cpu_closures;
- read_weightless(stream, cpu_closures, weights);
+ read_weightless(stream, cpu_closures, ctx);
std::size_t tidx = 0;
for (const auto& idx : cpu_closure_ids) {
closure[idx] = std::move(cpu_closures[tidx++]);
@@ -633,7 +633,7 @@ void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& strea
// Also read weights into LazyTensors
for (std::size_t cidx = 0; cidx < closure.size(); ++cidx) {
if (closure_uid[cidx] != -1 && lazy_closure[cidx]) { // previously registered before serialization
- lazy_closure[cidx].read_weight(weights);
+ lazy_closure[cidx].read_weight(ctx);
}
}
} else {
@@ -700,7 +700,7 @@ void ov::npuw::CompiledModel::serialize(std::ostream& stream) const {
write(stream, is_weightless);
// Create weightless context
- Context ctx(is_weightless, m_const_to_offset);
+ WeightsContext ctx(is_weightless, m_const_to_offset);
// Serialize compiled submodels
write(stream, m_compiled_submodels.size());
@@ -788,20 +788,50 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
// Initialize weights stream if weightless flow
std::string weights_path;
+ std::shared_ptr<const ov::Model> model_ptr;
+ // Cache model's constants
+ WeightsContext::ConstsCache consts_cache;
if (is_weightless) {
- NPUW_ASSERT(properties.find(ov::weights_path.name()) != properties.end() &&
- "There is no WEIGHTS_PATH set in properties but the blob was exported as weightless!");
- weights_path = properties.at(ov::weights_path.name()).as<std::string>();
+ if (properties.find(ov::weights_path.name()) != properties.end()) {
+ weights_path = properties.at(ov::weights_path.name()).as<std::string>();
+ NPUW_ASSERT(!weights_path.empty() &&
+ "Empty weights_path. Please provide WEIGHTS_PATH or MODEL_PTR in the configuration.");
+ } else if (properties.find(ov::hint::model.name()) != properties.end()) {
+ model_ptr = properties.at(ov::hint::model.name()).as<std::shared_ptr<const ov::Model>>();
+ NPUW_ASSERT(
+ model_ptr &&
+ "Empty model passed in MODEL_PTR. Please provide WEIGHTS_PATH or MODEL_PTR in the configuration.");
+ // Fill the cache
+ for (const auto& node : model_ptr->get_ordered_ops()) {
+ if (ov::op::util::is_constant(node)) {
+ const auto& c = std::static_pointer_cast<ov::op::v0::Constant>(node);
+ auto rt_info = c->get_rt_info();
+ auto weightless_cache_attr = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());
+ if (weightless_cache_attr == rt_info.end()) {
+ continue;
+ }
+ std::size_t offset = weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset;
+ std::size_t size = c->get_byte_size();
+ consts_cache[{offset, size}] = node;
+ }
+ }
+ } else {
+ NPUW_ASSERT(false && "Blob is weightless but no WEIGHTS_PATH nor MODEL_PTR property is provided!");
+ }
}
ov::npuw::s11n::Weights weights = nullptr;
if (is_weightless) {
- 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);
+ 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);
+ }
}
+ WeightsContext ctx(weights, consts_cache);
+
// Deserialize compiled submodels
std::size_t subm_size = 0;
read(stream, subm_size);
@@ -822,7 +852,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, weights);
+ compiled->m_compiled_submodels[i].deserialize(stream, 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.hpp
index 33af8fe5bb6..0b9392351f0 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -172,8 +172,8 @@ private:
// Metrics
execution_stats stat;
- void serialize(std::ostream& stream, const ov::npuw::s11n::Context& ctx) const;
- void deserialize(std::istream& stream, const ov::npuw::s11n::Weights& weights);
+ void serialize(std::ostream& stream, const ov::npuw::s11n::WeightsContext& ctx) const;
+ void deserialize(std::istream& stream, const ov::npuw::s11n::WeightsContext& ctx);
};
std::vector<CompiledModelDesc> m_compiled_submodels;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
index dd59c4ba894..85fcda56236 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
@@ -10,6 +10,7 @@
#include "logging.hpp"
#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
+#include "openvino/op/util/op_types.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "util.hpp"
@@ -62,11 +63,21 @@ struct Const {
NPUW_ASSERT(m_read_from_bin && "Underlying data should have been read first!");
return m_read_from_bin;
}
- void read_weight(const ov::npuw::s11n::Weights& weights) {
+ void 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!");
m_read_from_bin = ov::Tensor(m_cached_type, m_cached_shape);
- std::memcpy(m_read_from_bin.data(), weights->get_ptr(m_offset), m_byte_size);
+
+ if (ctx.weights) {
+ std::memcpy(m_read_from_bin.data(), ctx.weights->get_ptr(m_offset), m_byte_size);
+ } 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!");
+ auto tensor = ov::npuw::util::tensor_from_const(it->second);
+ NPUW_ASSERT(tensor.get_byte_size() == m_byte_size && tensor.get_shape() == m_cached_shape &&
+ tensor.get_element_type() == m_cached_type);
+ tensor.copy_to(m_read_from_bin);
+ }
}
void detach() {
m_node.reset();
@@ -114,9 +125,9 @@ struct Concat {
}
return ov::npuw::util::concat(to_concat, axis);
}
- void read_weight(const ov::npuw::s11n::Weights& weights) {
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
for (auto& lt : tensors) {
- lt.read_weight(weights);
+ lt.read_weight(ctx);
}
}
void detach() {
@@ -173,12 +184,12 @@ struct Unpack {
}
return dst;
}
- void read_weight(const ov::npuw::s11n::Weights& weights) {
- w.read_weight(weights);
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
+ w.read_weight(ctx);
if (z) { // could be empty
- z.read_weight(weights);
+ z.read_weight(ctx);
}
- s.read_weight(weights);
+ s.read_weight(ctx);
}
void detach() {
w.detach();
@@ -225,8 +236,8 @@ struct Permute {
ov::Tensor eval() const {
return ov::npuw::util::permute(tensor.eval(), axes);
}
- void read_weight(const ov::npuw::s11n::Weights& weights) {
- tensor.read_weight(weights);
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
+ tensor.read_weight(ctx);
}
void detach() {
tensor.detach();
@@ -262,8 +273,8 @@ struct Convert {
NPUW_ASSERT(ov::element::f16 == type);
return ov::npuw::util::to_f16(tensor.eval());
}
- void read_weight(const ov::npuw::s11n::Weights& weights) {
- tensor.read_weight(weights);
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
+ tensor.read_weight(ctx);
}
void detach() {
tensor.detach();
@@ -302,7 +313,7 @@ public:
void serialize(std::ostream& stream) const;
static std::shared_ptr<LazyTensorImpl> deserialize(std::istream& stream);
- void read_weight(const ov::npuw::s11n::Weights& weights);
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);
Transform m_transform;
std::size_t m_hash = 0;
@@ -351,9 +362,9 @@ ov::Tensor LazyTensorImpl::eval() const {
m_transform);
}
-void LazyTensorImpl::read_weight(const ov::npuw::s11n::Weights& weights) {
- std::visit(overloaded{[&weights](auto& op) {
- return op.read_weight(weights);
+void LazyTensorImpl::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
+ std::visit(overloaded{[&ctx](auto& op) {
+ return op.read_weight(ctx);
}},
m_transform);
}
@@ -469,9 +480,9 @@ ov::Tensor LazyTensor::eval() const {
return m_impl->eval();
}
-void LazyTensor::read_weight(const ov::npuw::s11n::Weights& weights) {
+void LazyTensor::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
NPUW_ASSERT(m_impl && "Trying to read weights into uninitialized tensor!");
- m_impl->read_weight(weights);
+ m_impl->read_weight(ctx);
}
LazyTensor::operator bool() const {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
index 9436d625768..4850c9c85e5 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
@@ -47,7 +47,7 @@ public:
void serialize(std::ostream& stream) const;
static LazyTensor deserialize(std::istream& stream);
- void read_weight(const ov::npuw::s11n::Weights& weights);
+ void read_weight(const ov::npuw::s11n::WeightsContext& ctx);
operator bool() const;
private:
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.cpp
index b12449a02ee..11c1e094d8f 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
@@ -742,7 +742,7 @@ void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {
write(stream, is_weightless);
if (!encryption_required) {
- LLMSerializeContext ctx(false, nullptr);
+ EncryptContext ctx(false, nullptr, nullptr);
return serialize(stream, ctx);
}
@@ -750,18 +750,18 @@ void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {
std::stringstream non_encrypted_stream;
if (is_weightless) {
non_encrypted_stream.copyfmt(stream);
- LLMSerializeContext ctx(false, nullptr);
+ EncryptContext ctx(false, nullptr, nullptr);
serialize(non_encrypted_stream, ctx);
std::string encrypted = enc_callbacks.encrypt(non_encrypted_stream.str());
write(stream, encrypted);
} else {
// In case of blob with weights only encrypt XML part of the model
- LLMSerializeContext ctx(true, enc_callbacks.encrypt);
+ EncryptContext ctx(true, enc_callbacks.encrypt, nullptr);
serialize(stream, ctx);
}
}
-void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::LLMSerializeContext& ctx) const {
+void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::EncryptContext& ctx) const {
LOG_INFO("Serializing LLMCompiledModel...");
LOG_BLOCK();
@@ -899,7 +899,7 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::import_m
};
if (!encrypted) {
- LLMDeserializeContext ctx(false, nullptr);
+ EncryptContext ctx(false, nullptr, nullptr);
auto compiled_model = ov::npuw::LLMCompiledModel::deserialize(stream, plugin, properties, ctx);
NPUW_ASSERT(compiled_model && "Couldn't import NPUW compiled model!");
read_and_finalize_banks(stream, compiled_model);
@@ -922,10 +922,10 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::import_m
std::string encrypted_str;
read(stream, encrypted_str);
std::istringstream decrypted_stream(std::move(enc_callbacks.decrypt(encrypted_str)));
- LLMDeserializeContext ctx(false, nullptr);
+ EncryptContext ctx(false, nullptr, nullptr);
compiled_model = ov::npuw::LLMCompiledModel::deserialize(decrypted_stream, plugin, properties, ctx);
} else {
- LLMDeserializeContext ctx(true, enc_callbacks.decrypt);
+ EncryptContext ctx(true, nullptr, enc_callbacks.decrypt);
compiled_model = ov::npuw::LLMCompiledModel::deserialize(stream, plugin, properties, ctx);
}
@@ -941,7 +941,7 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserial
std::istream& stream,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties,
- const ov::npuw::s11n::LLMDeserializeContext& ctx) {
+ const ov::npuw::s11n::EncryptContext& ctx) {
using namespace ov::npuw::s11n;
auto read_model_meta = [&](std::istream& model_stream) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
index aa9a2e8c616..a888c7b1328 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp
@@ -50,11 +50,11 @@ private:
std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override;
void implement_properties();
- void serialize(std::ostream& stream, const ov::npuw::s11n::LLMSerializeContext& ctx) const;
+ void serialize(std::ostream& stream, const ov::npuw::s11n::EncryptContext& ctx) const;
static std::shared_ptr<LLMCompiledModel> deserialize(std::istream& stream,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties,
- const ov::npuw::s11n::LLMDeserializeContext& ctx);
+ const ov::npuw::s11n::EncryptContext& ctx);
std::string m_name;
std::shared_ptr<::intel_npu::OptionsDesc> m_options_desc;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
index 374d0c4aa78..efb3f867b2b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
@@ -8,9 +8,11 @@
#include "lazy_tensor.hpp"
#include "logging.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/util/op_types.hpp"
#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/util/mmap_object.hpp"
#include "spatial.hpp"
+#include "util.hpp"
void ov::npuw::s11n::write(std::ostream& stream, const std::streampos& var) {
stream.write(reinterpret_cast<const char*>(&var), sizeof var);
@@ -316,7 +318,9 @@ void ov::npuw::s11n::read(std::istream& stream, ov::element::Type& var) {
// Weightless
// FIXME: all serialization needs a good rewriting
-void ov::npuw::s11n::write_weightless(std::ostream& stream, const std::vector<ov::Tensor>& var, const Context& ctx) {
+void ov::npuw::s11n::write_weightless(std::ostream& stream,
+ const std::vector<ov::Tensor>& var,
+ const ov::npuw::s11n::WeightsContext& ctx) {
write(stream, var.size());
for (const auto& t : var) {
if (!t) {
@@ -341,7 +345,7 @@ void ov::npuw::s11n::write_weightless(std::ostream& stream, const std::vector<ov
void ov::npuw::s11n::read_weightless(std::istream& stream,
std::vector<ov::Tensor>& var,
- const ov::npuw::s11n::Weights& weights) {
+ const ov::npuw::s11n::WeightsContext& ctx) {
var.clear();
std::size_t size;
read(stream, size);
@@ -365,7 +369,18 @@ void ov::npuw::s11n::read_weightless(std::istream& stream,
std::size_t offset = 0;
read(stream, offset);
ov::Tensor t(type, shape);
- std::memcpy(t.data(), weights->get_ptr(offset), byte_size);
+
+ if (ctx.weights) {
+ std::memcpy(t.data(), ctx.weights->get_ptr(offset), byte_size);
+ } else {
+ auto it = ctx.consts_cache.find({offset, byte_size});
+ NPUW_ASSERT(it != ctx.consts_cache.end() && "Couldn't find Constant in cache!");
+ auto tensor = ov::npuw::util::tensor_from_const(it->second);
+ NPUW_ASSERT(tensor.get_byte_size() == byte_size && tensor.get_shape() == shape &&
+ tensor.get_element_type() == type);
+ tensor.copy_to(t);
+ }
+
var.push_back(t);
} else {
ov::Tensor t;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index 8514457bb5d..88684dc33fe 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -45,6 +45,7 @@ class Output;
template <class>
class SharedBuffer;
class MappedMemory;
+class Model;
enum class CacheMode;
namespace element {
class Type;
@@ -69,31 +70,41 @@ class LazyTensor;
namespace s11n {
-struct Context {
- explicit Context(bool _is_weightless, const std::unordered_map<const void*, std::size_t>& _const_to_offset)
- : is_weightless(_is_weightless),
- const_to_offset(_const_to_offset) {}
- bool is_weightless;
- const std::unordered_map<const void*, std::size_t>& const_to_offset;
-};
-
-struct LLMSerializeContext {
- explicit LLMSerializeContext(bool _encrypted, std::function<std::string(const std::string&)> _encrypt)
- : encrypted(_encrypted),
- encrypt(_encrypt) {}
- bool encrypted = false;
- std::function<std::string(const std::string&)> encrypt = nullptr;
-};
+using Weights = std::shared_ptr<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>;
-struct LLMDeserializeContext {
- explicit LLMDeserializeContext(bool _encrypted, std::function<std::string(const std::string&)> _decrypt)
+struct EncryptContext {
+ explicit EncryptContext(bool _encrypted,
+ std::function<std::string(const std::string&)> _encrypt,
+ std::function<std::string(const std::string&)> _decrypt)
: encrypted(_encrypted),
+ encrypt(_encrypt),
decrypt(_decrypt) {}
bool encrypted = false;
+ std::function<std::string(const std::string&)> encrypt = nullptr;
std::function<std::string(const std::string&)> decrypt = nullptr;
};
-using Weights = std::shared_ptr<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>;
+struct WeightsContext {
+ struct CtxHash {
+ inline size_t operator()(const std::pair<std::size_t, std::size_t>& p) const {
+ return (std::hash<std::size_t>()(p.first) + 0x9e3779b9) ^ (std::hash<std::size_t>()(p.second) + 0x9e3779b9);
+ }
+ };
+ using ConstsCache = std::unordered_map<std::pair<std::size_t, std::size_t>, std::shared_ptr<ov::Node>, CtxHash>;
+
+ explicit WeightsContext(bool _is_weightless, const std::unordered_map<const void*, std::size_t>& _const_to_offset)
+ : is_weightless(_is_weightless),
+ const_to_offset(_const_to_offset) {}
+
+ explicit WeightsContext(const ov::npuw::s11n::Weights& _weights, const ConstsCache& _consts_cache)
+ : weights(_weights),
+ consts_cache(_consts_cache) {}
+
+ bool is_weightless;
+ std::unordered_map<const void*, std::size_t> const_to_offset;
+ ov::npuw::s11n::Weights weights = nullptr;
+ ConstsCache consts_cache;
+};
// Specific type overloads
void write(std::ostream& stream, const std::streampos& var);
@@ -124,9 +135,9 @@ void read(std::istream& stream, ov::CacheMode& var);
void read(std::istream& stream, ov::element::Type& var);
// Weightless utils
-void write_weightless(std::ostream& stream, const std::vector<ov::Tensor>& var, const Context& ctx);
+void write_weightless(std::ostream& stream, const std::vector<ov::Tensor>& var, const WeightsContext& ctx);
// No allocation needed
-void read_weightless(std::istream& stream, std::vector<ov::Tensor>& var, const Weights& weights);
+void read_weightless(std::istream& stream, std::vector<ov::Tensor>& var, const WeightsContext& ctx);
// Forward declaration
template <typename T1, typename T2>
diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
index dbd7f9d2a19..d8d9e65e6b5 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -326,6 +326,12 @@ Plugin::Plugin()
[](const Config& config) {
return config.get<WEIGHTS_PATH>();
}}},
+ {ov::hint::model.name(),
+ {true,
+ ov::PropertyMutability::RW,
+ [](const Config& config) {
+ return config.get<MODEL_PTR>();
+ }}},
{ov::device::uuid.name(),
{true,
ov::PropertyMutability::RO,
diff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
index 24c7087bdea..a15d2c45463 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
@@ -266,10 +266,13 @@ TEST(SerializationTest, OVTypes_Tensor_with_weights) {
std::stringstream ss;
std::unordered_map<const void*, std::size_t> const_offset;
- Context ctx(false, const_offset);
+ WeightsContext ctx(false, const_offset);
+
+ WeightsContext::ConstsCache consts_cache;
+ WeightsContext des_ctx(nullptr, consts_cache);
write_weightless(ss, {var}, ctx);
- read_weightless(ss, res, nullptr);
+ read_weightless(ss, res, des_ctx);
EXPECT_EQ(res[0].get_element_type(), ov::element::u8);
EXPECT_EQ(res[0].get_shape(), ov::Shape({2, 2}));