Skip to content

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

[NPUW] Weightless serialization (#28469)

Alexey Smirnov91ba886d0f30
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 4fd02e61cb3..e90cf708b9d 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
@@ -198,6 +198,27 @@ struct NUM_STREAMS final : OptionBase<NUM_STREAMS, ov::streams::Num> {
}
};
+//
+// WEIGHTS_PATH
+//
+struct WEIGHTS_PATH final : OptionBase<WEIGHTS_PATH, std::string> {
+ static std::string_view key() {
+ return ov::weights_path.name();
+ }
+
+ static constexpr std::string_view getTypeName() {
+ return "std::string";
+ }
+
+ static std::string defaultValue() {
+ return "";
+ }
+
+ static OptionMode mode() {
+ return OptionMode::CompileTime;
+ }
+};
+
//
// ENABLE_CPU_PINNING
//
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 23938b7974d..ed629da651d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -21,7 +21,6 @@
#include "openvino/util/common_util.hpp"
#include "partitioning/patterns/opt.hpp"
#include "plugin.hpp"
-#include "serialization.hpp"
#include "unfold_sync_infer_request.hpp"
#include "util.hpp"
@@ -30,6 +29,7 @@
#include "intel_npu/config/npuw.hpp"
#include "intel_npu/npuw_private_properties.hpp"
#include "llm_compiled_model.hpp"
+#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
#include "openvino/runtime/device_id_parser.hpp"
#include "openvino/runtime/internal_properties.hpp"
#include "openvino/runtime/properties.hpp"
@@ -206,6 +206,9 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
}
}
+ // Store original constants' offset for serialization purposes
+ store_const_offsets(model);
+
auto partitioning = getPartitioning(model, m_cfg);
m_total_stat.gflops = partitioning.total_gflops;
m_total_stat.ops = partitioning.total_ops;
@@ -509,7 +512,8 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
LOG_DEBUG("CompiledModel is being deserialized, skipping the full constructor flow...");
}
-void ov::npuw::CompiledModel::CompiledModelDesc::serialize(std::ostream& stream) const {
+void ov::npuw::CompiledModel::CompiledModelDesc::serialize(std::ostream& stream,
+ const ov::npuw::s11n::Context& ctx) const {
using namespace ov::npuw::s11n;
LOG_DEBUG("Serializing CompiledModelDesc...");
@@ -526,37 +530,58 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(std::ostream& stream)
write(stream, spatial);
- write(stream, scales);
- write(stream, zerops);
write(stream, is_remote);
-
- // NOTE: for closure only serialize uids - full flow
write(stream, closure_uid);
- // Some tensors might be present in CPU closure already - need to serialize as is
- // FIXME: When weightless serialization is introduced, this should be handled differently
- write(stream, closure.size());
- std::vector<ov::Tensor> cpu_closures;
- std::vector<std::size_t> cpu_closure_ids;
- for (std::size_t cidx = 0; cidx < closure.size(); ++cidx) {
- if (closure_uid[cidx] == -1) { // CPU closure, not in the bank
- cpu_closure_ids.push_back(cidx);
- cpu_closures.push_back(closure[cidx]);
+ if (ctx.is_weightless) {
+ write_weightless(stream, scales, ctx);
+ write_weightless(stream, zerops, ctx);
+
+ write(stream, closure.size());
+ std::vector<ov::Tensor> cpu_closures;
+ std::vector<std::size_t> cpu_closure_ids;
+ std::vector<ov::npuw::weights::LazyTensor> non_cpu_tensors;
+ std::vector<std::size_t> non_cpu_tensors_ids;
+ for (std::size_t cidx = 0; cidx < closure.size(); ++cidx) {
+ if (closure_uid[cidx] == -1) { // CPU closure
+ cpu_closure_ids.push_back(cidx);
+ cpu_closures.push_back(closure[cidx]);
+ } else {
+ non_cpu_tensors_ids.push_back(cidx);
+ non_cpu_tensors.push_back(lazy_closure[cidx]); // must be there
+ }
}
- }
- write(stream, cpu_closure_ids);
+ write(stream, cpu_closure_ids);
+ write_weightless(stream, cpu_closures, ctx);
+ write(stream, non_cpu_tensors_ids);
+ write(stream, non_cpu_tensors);
+ } else {
+ write(stream, scales);
+ write(stream, zerops);
+
+ write(stream, closure.size());
+ std::vector<ov::Tensor> cpu_closures;
+ std::vector<std::size_t> cpu_closure_ids;
+ for (std::size_t cidx = 0; cidx < closure.size(); ++cidx) {
+ if (closure_uid[cidx] == -1) { // CPU closure, not in the bank
+ cpu_closure_ids.push_back(cidx);
+ cpu_closures.push_back(closure[cidx]);
+ }
+ }
- for (const auto& tensor : cpu_closures) {
- write(stream, tensor);
- }
+ write(stream, cpu_closure_ids);
- // FIXME: support weightless flow!
+ for (const auto& tensor : cpu_closures) {
+ write(stream, tensor);
+ }
+ }
LOG_DEBUG("DONE.");
}
-void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& stream) {
+void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& stream,
+ const ov::npuw::s11n::Weights& weights) {
using namespace ov::npuw::s11n;
LOG_DEBUG("Deserializing CompiledModelDesc...");
@@ -573,25 +598,57 @@ void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& strea
read(stream, spatial);
- read(stream, scales);
- read(stream, zerops);
read(stream, is_remote);
-
- // NOTE: for closure only deserialize uids - full flow
read(stream, closure_uid);
- // Some tensors might be present in CPU closure already - need to deserialize as is
- // FIXME: When weightless serialization is introduced, this should be handled differently
- std::size_t closure_size = 0;
- read(stream, closure_size);
- std::vector<std::size_t> cpu_closure_ids;
- read(stream, cpu_closure_ids);
- closure.resize(closure_size);
- for (const auto& cidx : cpu_closure_ids) {
- read(stream, closure[cidx]);
- }
+ if (weights) {
+ read_weightless(stream, scales, weights);
+ read_weightless(stream, zerops, weights);
+
+ std::size_t closure_size = 0;
+ read(stream, closure_size);
+ closure.resize(closure_size);
+ lazy_closure.resize(closure_size);
+
+ std::vector<std::size_t> cpu_closure_ids;
+ read(stream, cpu_closure_ids);
+
+ std::vector<ov::Tensor> cpu_closures;
+ read_weightless(stream, cpu_closures, weights);
+ std::size_t tidx = 0;
+ for (const auto& idx : cpu_closure_ids) {
+ closure[idx] = std::move(cpu_closures[tidx++]);
+ }
+
+ std::vector<std::size_t> non_cpu_tensors_ids;
+ read(stream, non_cpu_tensors_ids);
- // FIXME: support weightless flow!
+ std::vector<ov::npuw::weights::LazyTensor> non_cpu_tensors;
+ read(stream, non_cpu_tensors);
+ std::size_t ltidx = 0;
+ for (const auto& idx : non_cpu_tensors_ids) {
+ lazy_closure[idx] = std::move(non_cpu_tensors[ltidx++]);
+ }
+
+ // Also read weights into LazyTensors
+ for (std::size_t cidx = 0; cidx < closure.size(); ++cidx) {
+ if (closure_uid[cidx] != -1) { // previously registered before serialization
+ lazy_closure[cidx].read_weight(weights);
+ }
+ }
+ } else {
+ read(stream, scales);
+ read(stream, zerops);
+
+ std::size_t closure_size = 0;
+ read(stream, closure_size);
+ std::vector<std::size_t> cpu_closure_ids;
+ read(stream, cpu_closure_ids);
+ closure.resize(closure_size);
+ for (const auto& cidx : cpu_closure_ids) {
+ read(stream, closure[cidx]);
+ }
+ }
LOG_DEBUG("DONE.");
}
@@ -627,6 +684,17 @@ void ov::npuw::CompiledModel::serialize(std::ostream& stream) const {
write_any(stream, p.second);
}
+ // Write flow identifier
+ bool is_weightless = true;
+ if (m_non_npuw_props.count(ov::cache_mode.name()) &&
+ m_non_npuw_props.at(ov::cache_mode.name()).as<CacheMode>() == CacheMode::OPTIMIZE_SPEED) {
+ is_weightless = false;
+ }
+ write(stream, is_weightless);
+
+ // Create weightless context
+ Context ctx(is_weightless, m_const_to_offset);
+
// Serialize compiled submodels
write(stream, m_compiled_submodels.size());
for (const auto& subm : m_compiled_submodels) {
@@ -644,7 +712,7 @@ void ov::npuw::CompiledModel::serialize(std::ostream& stream) const {
write(stream, false);
}
// Write the rest of the submodel desc
- subm.serialize(stream);
+ subm.serialize(stream, ctx);
}
LOG_INFO("Done.");
@@ -652,7 +720,8 @@ void ov::npuw::CompiledModel::serialize(std::ostream& stream) const {
std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
std::istream& stream,
- const std::shared_ptr<const ov::IPlugin>& plugin) {
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties) {
LOG_INFO("Deserializing CompiledModel...");
LOG_BLOCK();
@@ -699,6 +768,26 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
}
compiled->implement_properties();
+ // Read flow identifier
+ bool is_weightless = false;
+ read(stream, is_weightless);
+
+ // Initialize weights stream if weightless flow
+ std::string weights_path;
+ 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>();
+ }
+
+ 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);
+ }
+
// Deserialize compiled submodels
std::size_t subm_size = 0;
read(stream, subm_size);
@@ -719,7 +808,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);
+ compiled->m_compiled_submodels[i].deserialize(stream, weights);
}
compiled->implement_properties();
@@ -730,6 +819,33 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
return compiled;
}
+void ov::npuw::CompiledModel::reconstruct_closure() {
+ for (size_t idx = 0; idx < m_compiled_submodels.size(); ++idx) {
+ auto& comp_model_desc = m_compiled_submodels[idx];
+
+ // Skip optimized out and non-functions
+ if (!comp_model_desc.compiled_model && !comp_model_desc.replaced_by) {
+ continue;
+ }
+
+ 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 nothing
+ NPUW_ASSERT(!comp_model_desc.is_remote[cidx]);
+ continue;
+ }
+ NPUW_ASSERT(comp_model_desc.closure_uid[cidx] != -1);
+ comp_model_desc.closure[cidx] =
+ m_weights_bank->get(comp_model_desc.closure_uid[cidx], *func_desc.device_it);
+ }
+ }
+}
+
void ov::npuw::CompiledModel::finalize_weights_bank() {
LOG_INFO("Finalizing weights bank...");
// Register lazy tensors
@@ -785,29 +901,22 @@ void ov::npuw::CompiledModel::finalize_weights_bank() {
LOG_INFO("Done.");
}
-void ov::npuw::CompiledModel::reconstruct_closure() {
- for (size_t idx = 0; idx < m_compiled_submodels.size(); ++idx) {
- auto& comp_model_desc = m_compiled_submodels[idx];
-
- // Skip optimized out and non-functions
- if (!comp_model_desc.compiled_model && !comp_model_desc.replaced_by) {
- continue;
- }
-
- 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 nothing
- NPUW_ASSERT(!comp_model_desc.is_remote[cidx]);
+void ov::npuw::CompiledModel::store_const_offsets(const std::shared_ptr<ov::Model>& model) {
+ for (auto&& node_ptr : model->get_ordered_ops()) {
+ if (ov::op::util::is_constant(node_ptr)) {
+ const auto& c = std::static_pointer_cast<ov::op::v0::Constant>(node_ptr);
+ 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;
}
- NPUW_ASSERT(comp_model_desc.closure_uid[cidx] != -1);
- comp_model_desc.closure[cidx] =
- m_weights_bank->get(comp_model_desc.closure_uid[cidx], *func_desc.device_it);
+ std::size_t offset = weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset;
+ auto data_ptr = c->get_data_ptr();
+ auto inserted = m_const_to_offset.insert({data_ptr, offset});
+ if (!inserted.second) {
+ NPUW_ASSERT(inserted.first->second == offset &&
+ "Model contains two constants with same pointer and different offset!");
+ }
}
}
}
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 b4faf9d417b..33af8fe5bb6 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -11,8 +11,11 @@
#include "intel_npu/config/npuw.hpp"
#include "openvino/openvino.hpp"
#include "openvino/runtime/icompiled_model.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/runtime/so_ptr.hpp"
+#include "openvino/util/mmap_object.hpp"
#include "partitioning/partitioning.hpp"
+#include "serialization.hpp"
#include "spatial.hpp"
#include "weights_bank.hpp"
@@ -72,7 +75,8 @@ private:
void serialize(std::ostream& stream) const;
static std::shared_ptr<CompiledModel> deserialize(std::istream& stream,
- const std::shared_ptr<const ov::IPlugin>& plugin);
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties);
// This is used for removing too long output tensor names to fix some compilation issues
// NB: These two methods has nothing to do with this particular class and should be
@@ -93,6 +97,8 @@ private:
// For full deserialization flow with weights
void reconstruct_closure();
+ // For weightless serialization flow
+ void store_const_offsets(const std::shared_ptr<ov::Model>& model);
void finalize_weights_bank();
void detach_memory();
@@ -166,8 +172,8 @@ private:
// Metrics
execution_stats stat;
- void serialize(std::ostream& stream) const;
- void deserialize(std::istream& stream);
+ void serialize(std::ostream& stream, const ov::npuw::s11n::Context& ctx) const;
+ void deserialize(std::istream& stream, const ov::npuw::s11n::Weights& weights);
};
std::vector<CompiledModelDesc> m_compiled_submodels;
@@ -177,6 +183,8 @@ private:
execution_stats m_total_stat;
std::shared_ptr<weights::Bank> m_weights_bank = nullptr;
+
+ std::unordered_map<const void*, std::size_t> m_const_to_offset;
};
} // namespace npuw
} // namespace ov
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 133101da8b7..937925b62ee 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
@@ -9,6 +9,7 @@
#include <variant>
#include "logging.hpp"
+#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "util.hpp"
@@ -19,15 +20,27 @@ namespace npuw {
namespace weights {
namespace op {
struct Const {
- std::shared_ptr<ov::op::v0::Constant> m_node;
+ std::shared_ptr<ov::op::v0::Constant> m_node = nullptr;
ov::element::Type m_cached_type;
ov::Shape m_cached_shape;
const void* m_cached_ptr = nullptr;
+ std::size_t m_offset = 0;
+ std::size_t m_byte_size = 0;
+ ov::Tensor m_read_from_bin;
+
+ Const() = default;
explicit Const(std::shared_ptr<ov::op::v0::Constant> n) : m_node(n) {
m_cached_type = m_node->get_element_type();
m_cached_shape = m_node->get_shape();
m_cached_ptr = m_node->get_data_ptr();
+ m_byte_size = m_node->get_byte_size();
+
+ auto rt_info = m_node->get_rt_info();
+ 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;
+ }
}
std::size_t hash() const {
std::size_t seed = std::hash<const void*>()(m_cached_ptr) + 0x9e3779b9;
@@ -42,17 +55,48 @@ struct Const {
m_cached_ptr == other.m_cached_ptr);
}
ov::Tensor eval() const {
- NPUW_ASSERT(m_node && "Const::eval() can only happen before detach");
- return ov::npuw::util::tensor_from_const(m_node);
+ if (m_node) {
+ return ov::npuw::util::tensor_from_const(m_node);
+ }
+
+ 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) {
+ 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);
}
void detach() {
m_node.reset();
+ m_read_from_bin = ov::Tensor();
+ }
+ void serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ write(stream, m_cached_type.to_string());
+ write(stream, m_cached_shape);
+ write(stream, m_offset);
+ write(stream, m_byte_size);
+ }
+ static Const deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ Const c;
+ std::string type_str;
+ read(stream, type_str);
+ c.m_cached_type = ov::element::Type(type_str);
+ read(stream, c.m_cached_shape);
+ read(stream, c.m_offset);
+ read(stream, c.m_byte_size);
+ return c;
}
};
struct Concat {
std::vector<LazyTensor> tensors;
std::size_t axis;
+ Concat() = default;
+
std::size_t hash() const {
std::size_t seed = std::hash<std::size_t>()(axis) + 0x9e3779b9;
for (auto& lt : tensors) {
@@ -70,18 +114,36 @@ struct Concat {
}
return ov::npuw::util::concat(to_concat, axis);
}
+ void read_weight(const ov::npuw::s11n::Weights& weights) {
+ for (auto& lt : tensors) {
+ lt.read_weight(weights);
+ }
+ }
void detach() {
for (auto&& lt : tensors) {
lt.detach();
}
}
+ void serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ write(stream, axis);
+ write(stream, tensors);
+ }
+ static Concat deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ Concat c;
+ read(stream, c.axis);
+ read(stream, c.tensors);
+ return c;
+ }
};
-
struct Unpack {
LazyTensor w, z, s;
ov::element::Type type;
ov::Shape shape;
+ Unpack() = default;
+
std::size_t hash() const {
std::size_t seed = w.get_hash() + 0x9e3779b9;
seed ^= z.get_hash() + 0x9e3779b9;
@@ -111,16 +173,43 @@ struct Unpack {
}
return dst;
}
+ void read_weight(const ov::npuw::s11n::Weights& weights) {
+ w.read_weight(weights);
+ z.read_weight(weights);
+ s.read_weight(weights);
+ }
void detach() {
w.detach();
z.detach();
s.detach();
}
+ void serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ write(stream, type.to_string());
+ write(stream, shape);
+ write(stream, w);
+ write(stream, z);
+ write(stream, s);
+ }
+ static Unpack deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ Unpack u;
+ std::string type_str;
+ read(stream, type_str);
+ u.type = ov::element::Type(type_str);
+ read(stream, u.shape);
+ read(stream, u.w);
+ read(stream, u.z);
+ read(stream, u.s);
+ return u;
+ }
};
struct Permute {
LazyTensor tensor;
std::vector<std::size_t> axes;
+ Permute() = default;
+
std::size_t hash() const {
std::size_t seed = tensor.get_hash() + 0x9e3779b9;
for (const auto& axis : axes) {
@@ -134,14 +223,31 @@ 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 detach() {
tensor.detach();
}
+ void serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ write(stream, axes);
+ write(stream, tensor);
+ }
+ static Permute deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ Permute p;
+ read(stream, p.axes);
+ read(stream, p.tensor);
+ return p;
+ }
};
struct Convert {
LazyTensor tensor;
ov::element::Type type;
+ Convert() = default;
+
std::size_t hash() const {
std::size_t seed = type.hash() + 0x9e3779b9;
seed ^= tensor.get_hash() + 0x9e3779b9;
@@ -154,16 +260,36 @@ 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 detach() {
tensor.detach();
}
+ void serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ write(stream, type.to_string());
+ write(stream, tensor);
+ }
+ static Convert deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ Convert c;
+ std::string type_str;
+ read(stream, type_str);
+ c.type = ov::element::Type(type_str);
+ read(stream, c.tensor);
+ return c;
+ }
};
} // namespace op
using Transform = std::variant<op::Const, op::Concat, op::Unpack, op::Permute, op::Convert>;
+enum class TransformType : int { CONST = 0, CONCAT, UNPACK, PERMUTE, CONVERT };
+
struct LazyTensorImpl {
public:
+ LazyTensorImpl() = default;
explicit LazyTensorImpl(Transform&& t);
bool operator==(const LazyTensorImpl& other) const;
@@ -172,8 +298,12 @@ public:
void detach();
+ void serialize(std::ostream& stream) const;
+ static std::shared_ptr<LazyTensorImpl> deserialize(std::istream& stream);
+ void read_weight(const ov::npuw::s11n::Weights& weights);
+
Transform m_transform;
- const std::size_t m_hash = 0;
+ std::size_t m_hash = 0;
};
} // namespace weights
@@ -219,6 +349,13 @@ 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);
+ }},
+ m_transform);
+}
+
std::size_t LazyTensorImpl::get_hash() const {
return m_hash;
}
@@ -230,6 +367,62 @@ void LazyTensorImpl::detach() {
m_transform);
}
+void LazyTensorImpl::serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ write(stream, m_hash);
+ // FIXME: create proper op identificators instead of int
+ std::visit(overloaded{[&stream](const op::Concat& op) {
+ write(stream, static_cast<int>(TransformType::CONCAT));
+ op.serialize(stream);
+ },
+ [&stream](const op::Const& op) {
+ write(stream, static_cast<int>(TransformType::CONST));
+ op.serialize(stream);
+ },
+ [&stream](const op::Convert& op) {
+ write(stream, static_cast<int>(TransformType::CONVERT));
+ op.serialize(stream);
+ },
+ [&stream](const op::Permute& op) {
+ write(stream, static_cast<int>(TransformType::PERMUTE));
+ op.serialize(stream);
+ },
+ [&stream](const op::Unpack& op) {
+ write(stream, static_cast<int>(TransformType::UNPACK));
+ op.serialize(stream);
+ }},
+ m_transform);
+}
+
+std::shared_ptr<LazyTensorImpl> LazyTensorImpl::deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ auto lt_impl = std::make_shared<LazyTensorImpl>();
+ read(stream, lt_impl->m_hash);
+ int op_type;
+ read(stream, op_type);
+ switch (TransformType(op_type)) {
+ case TransformType::CONCAT:
+ lt_impl->m_transform = op::Concat::deserialize(stream);
+ break;
+ case TransformType::CONST:
+ lt_impl->m_transform = op::Const::deserialize(stream);
+ break;
+ case TransformType::CONVERT:
+ lt_impl->m_transform = op::Convert::deserialize(stream);
+ break;
+ case TransformType::PERMUTE:
+ lt_impl->m_transform = op::Permute::deserialize(stream);
+ break;
+ case TransformType::UNPACK:
+ lt_impl->m_transform = op::Unpack::deserialize(stream);
+ break;
+ default:
+ NPUW_ASSERT(false && "Unsupported type");
+ break;
+ }
+ return lt_impl;
+}
+
LazyTensor::LazyTensor(const std::shared_ptr<ov::op::v0::Constant>& const_ptr)
: m_impl(std::make_shared<LazyTensorImpl>(op::Const(const_ptr))) {}
LazyTensor::LazyTensor(const std::vector<LazyTensor>& to_concat, const std::size_t axis)
@@ -274,6 +467,11 @@ ov::Tensor LazyTensor::eval() const {
return m_impl->eval();
}
+void LazyTensor::read_weight(const ov::npuw::s11n::Weights& weights) {
+ NPUW_ASSERT(m_impl && "Trying to read weights into uninitialized tensor!");
+ m_impl->read_weight(weights);
+}
+
std::size_t LazyTensor::get_hash() const {
if (!m_impl) {
return 0;
@@ -287,6 +485,28 @@ void LazyTensor::detach() {
}
}
+void LazyTensor::serialize(std::ostream& stream) const {
+ using namespace ov::npuw::s11n;
+ if (!m_impl) {
+ write(stream, false);
+ return;
+ }
+ write(stream, true);
+ m_impl->serialize(stream);
+}
+
+LazyTensor LazyTensor::deserialize(std::istream& stream) {
+ using namespace ov::npuw::s11n;
+ bool is_initialized;
+ read(stream, is_initialized);
+ LazyTensor lt;
+ if (!is_initialized) {
+ return lt;
+ }
+ lt.m_impl = LazyTensorImpl::deserialize(stream);
+ return lt;
+}
+
std::size_t LazyTensor::Hash::operator()(const LazyTensor& lt) const {
return lt.get_hash();
}
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 840e22dceda..d11b22a85a1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.hpp
@@ -7,7 +7,10 @@
#include <memory>
#include "openvino/op/constant.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/runtime/tensor.hpp"
+#include "openvino/util/mmap_object.hpp"
+#include "serialization.hpp"
namespace ov {
namespace npuw {
@@ -42,6 +45,10 @@ public:
std::size_t get_hash() const;
void detach();
+ void serialize(std::ostream& stream) const;
+ static LazyTensor deserialize(std::istream& stream);
+ void read_weight(const ov::npuw::s11n::Weights& weights);
+
private:
std::shared_ptr<LazyTensorImpl> m_impl = nullptr;
};
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 49c1d572122..d2137c3ee85 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
@@ -14,6 +14,7 @@
#include "openvino/pass/stateful_to_stateless.hpp"
#include "openvino/pass/validate.hpp"
#include "openvino/runtime/iasync_infer_request.hpp"
+#include "openvino/runtime/properties.hpp"
#include "serialization.hpp"
#include "transformations/convert_precision.hpp"
@@ -546,6 +547,8 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
ov::AnyMap npuw_llm_props;
ov::AnyMap other_props;
split_llm_properties(properties, npuw_llm_props, other_props);
+ // Solely used for serialization at the moment
+ m_non_llm_props = other_props;
// Remove "NPUW_LLM_PREFILL_CONFIG", "NPUW_LLM_GENERATE_CONFIG" from map,
// to not pass them into ::intel_npu::Config object, as we don't need to
@@ -676,28 +679,42 @@ void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {
write(stream, m_kvcache_desc.total_size);
write(stream, m_kvcache_desc.num_stored_tokens);
write(stream, m_kvcache_desc.dim);
+ write(stream, m_kvcache_desc.v_tensors_transposed);
// Write config
write(stream, m_cfg);
+ // Identify either full flow or weightless
+ bool is_weightless = true;
+ if (m_non_llm_props.count(ov::cache_mode.name()) &&
+ m_non_llm_props.at(ov::cache_mode.name()).as<CacheMode>() == CacheMode::OPTIMIZE_SPEED) {
+ LOG_INFO("Serialization will be done via flow with weights.");
+ is_weightless = false;
+ }
+ write(stream, is_weightless);
+
// Serialize CompiledModels
m_kvcache_compiled->serialize(stream);
m_prefill_compiled->serialize(stream);
- // Serialize weights bank (if required)
+ // Serialize bank name
const auto& kv_bank = m_kvcache_compiled->m_weights_bank;
const auto& p_bank = m_prefill_compiled->m_weights_bank;
NPUW_ASSERT(kv_bank && p_bank && kv_bank == p_bank && "Prefill and KVCache models' weight bank should be shared!");
- // FIXME: support weightless flow
write(stream, kv_bank->get_name());
- kv_bank->serialize(stream);
+
+ if (!is_weightless) {
+ // Serialize weights bank
+ kv_bank->serialize(stream);
+ }
LOG_INFO("Done.");
}
std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserialize(
std::istream& stream,
- const std::shared_ptr<const ov::IPlugin>& plugin) {
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties) {
LOG_INFO("Deserializing LLMCompiledModel...");
LOG_BLOCK();
@@ -758,27 +775,41 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserial
read(stream, compiled->m_kvcache_desc.total_size);
read(stream, compiled->m_kvcache_desc.num_stored_tokens);
read(stream, compiled->m_kvcache_desc.dim);
+ read(stream, compiled->m_kvcache_desc.v_tensors_transposed);
// Deserialize config
read(stream, compiled->m_cfg);
compiled->implement_properties();
+ // Deserialize flow indicator
+ bool is_weightless = false;
+ read(stream, is_weightless);
+
// Deserialize CompiledModels
- compiled->m_kvcache_compiled = ov::npuw::CompiledModel::deserialize(stream, plugin);
- compiled->m_prefill_compiled = ov::npuw::CompiledModel::deserialize(stream, plugin);
+ compiled->m_kvcache_compiled = ov::npuw::CompiledModel::deserialize(stream, plugin, properties);
+ compiled->m_prefill_compiled = ov::npuw::CompiledModel::deserialize(stream, plugin, properties);
- // Deserialize weights bank (if required)
+ // Deserialize weights bank name
std::string bank_name;
read(stream, bank_name);
- auto bank = ov::npuw::weights::Bank::deserialize(stream, compiled->get_plugin()->get_core(), bank_name);
- // FIXME: support weightless option
- compiled->m_kvcache_compiled->m_weights_bank = bank;
- compiled->m_prefill_compiled->m_weights_bank = bank;
+ if (is_weightless) {
+ auto bank = ov::npuw::weights::bank(bank_name, compiled->get_plugin()->get_core(), "");
- // After bank deserialization - reconstruct NPU closures from the bank
- compiled->m_kvcache_compiled->reconstruct_closure();
- compiled->m_prefill_compiled->reconstruct_closure();
+ compiled->m_kvcache_compiled->m_weights_bank = bank;
+ compiled->m_prefill_compiled->m_weights_bank = bank;
+
+ compiled->m_kvcache_compiled->finalize_weights_bank();
+ compiled->m_prefill_compiled->finalize_weights_bank();
+ } else {
+ auto bank = ov::npuw::weights::Bank::deserialize(stream, compiled->get_plugin()->get_core(), bank_name);
+
+ compiled->m_kvcache_compiled->m_weights_bank = bank;
+ compiled->m_prefill_compiled->m_weights_bank = bank;
+
+ compiled->m_kvcache_compiled->reconstruct_closure();
+ compiled->m_prefill_compiled->reconstruct_closure();
+ }
LOG_INFO("Done.");
return compiled;
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 5003ccce40b..a15215bba57 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
@@ -35,7 +35,8 @@ public:
void export_model(std::ostream& model) const override;
static std::shared_ptr<LLMCompiledModel> deserialize(std::istream& stream,
- const std::shared_ptr<const ov::IPlugin>& plugin);
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties);
std::shared_ptr<const ov::Model> get_runtime_model() const override;
@@ -53,6 +54,7 @@ private:
std::shared_ptr<::intel_npu::OptionsDesc> m_options_desc;
::intel_npu::Config m_cfg;
GetPropertiesMap m_prop_to_opt;
+ ov::AnyMap m_non_llm_props;
KVCacheDesc m_kvcache_desc;
std::shared_ptr<ov::npuw::CompiledModel> m_kvcache_compiled;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
index 80b10efd57a..e6729203ea6 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -1551,6 +1551,7 @@ void Partitioner::createFunction(FunctionPipeline& func_ggg) {
} // for(inputs)
} // for(nodes)
funcall._closure.resize(funcall._lazy_closure.size());
+ funcall._is_lazy_unpack.resize(funcall._lazy_closure.size(), false);
function._num_params_total = new_param_idx;
function._model->validate_nodes_and_infer_types();
P.functions.insert({func_name, std::move(function)});
@@ -1678,6 +1679,7 @@ void Partitioner::matchRepeatedSubgraphs(const std::string& func_name) {
const auto& function = func_iter->second;
funcall._closure.resize(function._num_params_total - function._param_offset);
funcall._lazy_closure.resize(function._num_params_total - function._param_offset);
+ funcall._is_lazy_unpack.resize(function._num_params_total - function._param_offset, false);
auto tmp_model = *mod_iter;
for (auto&& node_ptr : tmp_model->get_ordered_ops()) {
@@ -1766,7 +1768,7 @@ void Partitioner::optimize(const std::string& func_name) {
for (auto&& p : ctx.closures_to_permute) {
auto param_idx = f._model->get_parameter_index(p.first);
auto closure_idx = param_idx - f._param_offset;
- ov::parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
+ ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
auto& funcall = func_group.refs[f_idx].get();
funcall._lazy_closure[closure_idx] = funcall._lazy_closure[closure_idx].permute(p.second);
});
@@ -1776,7 +1778,7 @@ void Partitioner::optimize(const std::string& func_name) {
for (auto&& p : ctx.closures_to_f16) {
auto param_idx = f._model->get_parameter_index(p);
auto closure_idx = param_idx - f._param_offset;
- ov::parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
+ ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
auto& funcall = func_group.refs[f_idx].get();
funcall._lazy_closure[closure_idx] = funcall._lazy_closure[closure_idx].convert(ov::element::f16);
});
@@ -1830,7 +1832,7 @@ void Partitioner::optimize(const std::string& func_name) {
to_concat_idx.push_back(p_to_concat_idx - f._param_offset);
to_remove_idx.insert(p_to_concat_idx);
}
- ov::parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
+ ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
auto& funcall = func_group.refs[f_idx].get();
std::vector<LazyTensor> to_concat;
// Fill tensor vector
@@ -1841,6 +1843,7 @@ void Partitioner::optimize(const std::string& func_name) {
// and the new one added into the vector
if (!to_concat.empty()) {
funcall._lazy_closure.push_back(LazyTensor(to_concat, axis));
+ funcall._is_lazy_unpack.push_back(false);
// Some of the tensors might be in closure - preserve it's 1:1 idx mapping with _lazy_closure
funcall._closure.push_back(ov::Tensor());
}
@@ -1865,7 +1868,7 @@ void Partitioner::optimize(const std::string& func_name) {
to_remove_idx.insert(z_idx);
}
- ov::parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
+ ov::npuw::util::non_parallel_for(func_group.refs.size(), [&](std::size_t f_idx) {
auto& funcall = func_group.refs[f_idx].get();
LazyTensor cw = funcall._lazy_closure[w_idx - f._param_offset];
LazyTensor cz = z_idx != -1 ? funcall._lazy_closure[z_idx - f._param_offset] : LazyTensor();
@@ -1874,6 +1877,11 @@ void Partitioner::optimize(const std::string& func_name) {
LazyTensor(cw, cz, cs, p.first->get_element_type(), p.first->get_shape()));
// Some of the tensors might be in closure - preserve it's 1:1 idx mapping with _lazy_closure
funcall._closure.push_back(ov::Tensor());
+ // FIXME: in some cases we might have DCOFF and DQ enabled together. This
+ // might lead to DQ passes finding patterns and running remap on closures unconditionally.
+ // It assigns some closures to be calculated instead of keeping the lazy ones.
+ // Here we remember lazy closure not to be unpacked in compile time in DCOFF.
+ funcall._is_lazy_unpack.push_back(true);
});
}
@@ -1896,6 +1904,7 @@ void Partitioner::optimize(const std::string& func_name) {
// to the closure), this tensor should be non-empty to avoid this process.
funcall.get()._closure.push_back(ov::Tensor(new_elem_type, new_shape));
funcall.get()._lazy_closure.push_back(LazyTensor());
+ funcall.get()._is_lazy_unpack.push_back(false);
}
}
@@ -1906,16 +1915,19 @@ void Partitioner::optimize(const std::string& func_name) {
for (auto&& fref : func_group.refs) {
auto& funcall = fref.get();
std::vector<LazyTensor> new_transforms;
+ std::vector<bool> new_is_lazy_unpack;
// Some of the tensors might be in closure (e.g. host-gather), thus need to preserve the _closure as well
std::vector<ov::Tensor> new_closure;
for (std::size_t tidx = 0; tidx < funcall._lazy_closure.size(); tidx++) {
if (to_remove_idx.count(f._param_offset + tidx) == 0) {
new_transforms.push_back(funcall._lazy_closure[tidx]);
new_closure.push_back(funcall._closure[tidx]);
+ new_is_lazy_unpack.push_back(funcall._is_lazy_unpack[tidx]);
}
}
funcall._lazy_closure = std::move(new_transforms);
funcall._closure = std::move(new_closure);
+ funcall._is_lazy_unpack = std::move(new_is_lazy_unpack);
}
// Remove parameters that were concatenated
for (auto&& now_remove : to_remove) {
@@ -1934,6 +1946,17 @@ void Partitioner::optimize(const std::string& func_name) {
funcall.get()._host_gather = ov::npuw::Subgraph::Gather{gather_dst_id, gather_src_id, gather_idx_id};
}
}
+
+ // FIXME: workaround
+ // Set lazy unpack indexes not to be unpacked in DCOFF
+ for (auto&& fref : func_group.refs) {
+ auto& funcall = fref.get();
+ for (std::size_t idx = 0; idx < funcall._is_lazy_unpack.size(); ++idx) {
+ if (funcall._is_lazy_unpack[idx]) {
+ f._idx_lazy_unpack.insert(idx);
+ }
+ }
+ }
}
if (!cfg.get<::intel_npu::NPUW_DQ>()) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
index d613e9dcc4a..b4b97df1a96 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
@@ -46,6 +46,8 @@ struct Subgraph {
// Stores transformation history for weights which will be applied before inference
std::vector<weights::LazyTensor> _lazy_closure;
+ // FIXME: shouldn't be here. Needed to not unpack some lazy closures in DCOFF
+ std::vector<bool> _is_lazy_unpack;
bool _forced_to_fcall = false;
@@ -72,6 +74,9 @@ struct Function {
std::map<std::pair<std::string, std::size_t>, std::size_t> _param_mapping;
std::optional<ov::npuw::function::Spatial> _spatial;
+
+ // FIXME: shouldn't be here. Needed to not unpack some lazy closures in DCOFF
+ std::set<std::size_t> _idx_lazy_unpack;
};
struct Group {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp
index a4a03dea982..4f1f91948b4 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/dcoff.cpp
@@ -75,6 +75,8 @@ ClosureRemap build_remap(const Function& fbody, const DCOFFParams& params_to) {
LOG_DEBUG("Checking the function parameter " << param);
LOG_BLOCK();
+ auto cindex = i - fbody._param_offset;
+
// First find among scale factors...
auto pscale_iter = params_to.scales.find(param);
auto pzerop_iter = params_to.zerops_asymm.find(param);
@@ -83,24 +85,27 @@ ClosureRemap build_remap(const Function& fbody, const DCOFFParams& params_to) {
auto& pscale_weight_param = pscale_iter->second;
auto pscale_weight_pindex = fbody._model->get_parameter_index(pscale_weight_param);
auto pscale_weight_cindex = pscale_weight_pindex - fbody._param_offset;
- m.scale_remap[pscale_weight_cindex] = i - fbody._param_offset;
+ m.scale_remap[pscale_weight_cindex] = cindex;
m.params_to_remove.push_back(param);
} else if (pzerop_iter != params_to.zerops_asymm.end()) {
LOG_DEBUG("There is an Asymmetric zero point corresponding to this parameter, it will be removed");
auto zerop_pindex = fbody._model->get_parameter_index(pzerop_iter->second);
auto zerop_cindex = zerop_pindex - fbody._param_offset;
- m.zerop_remap[i - fbody._param_offset] = zerop_cindex;
+ m.zerop_remap[cindex] = zerop_cindex;
m.params_to_remove.push_back(pzerop_iter->second);
- m.closure_remap.push_back(i - fbody._param_offset);
+ m.closure_remap.push_back(cindex);
} else if (ban_list.find(param) == ban_list.end()) {
// If it's not in the ban list, it's an OK parameter and should be kept
LOG_DEBUG("This is an OK parameter, will be kept");
- m.closure_remap.push_back(i - fbody._param_offset);
+ m.closure_remap.push_back(cindex);
// FIXME: type should be queried from a lazy tensor
// and compared against param->get_element_type()
// to decide 100%
- m.weights_to_unpack.insert(i - fbody._param_offset);
+ // FIXME: workaround, in case of lazy unpack do not do unpack here
+ if (fbody._idx_lazy_unpack.find(cindex) == fbody._idx_lazy_unpack.end()) {
+ m.weights_to_unpack.insert(cindex);
+ }
}
// Process zero points for parameters
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
index 60417f9f241..54286071a12 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
@@ -5,8 +5,11 @@
#include "serialization.hpp"
#include "intel_npu/config/config.hpp"
+#include "lazy_tensor.hpp"
#include "logging.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
+#include "openvino/util/mmap_object.hpp"
#include "spatial.hpp"
void ov::npuw::s11n::write(std::ostream& stream, const std::streampos& var) {
@@ -115,6 +118,10 @@ void ov::npuw::s11n::write_any(std::ostream& stream, const ov::Any& var) {
}
}
+void ov::npuw::s11n::write(std::ostream& stream, const ov::npuw::weights::LazyTensor& var) {
+ var.serialize(stream);
+}
+
void ov::npuw::s11n::read(std::istream& stream, std::streampos& var) {
stream.read(reinterpret_cast<char*>(&var), sizeof var);
}
@@ -261,3 +268,68 @@ void ov::npuw::s11n::read_any(std::istream& stream, ov::Any& var) {
NPUW_ASSERT(false && "Unsupported type");
}
}
+
+void ov::npuw::s11n::read(std::istream& stream, ov::npuw::weights::LazyTensor& var) {
+ var = ov::npuw::weights::LazyTensor::deserialize(stream);
+}
+
+// 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) {
+ write(stream, var.size());
+ for (const auto& t : var) {
+ if (!t) {
+ write(stream, false);
+ continue;
+ }
+ write(stream, true);
+ auto data = t.data();
+ auto iter = ctx.const_to_offset.find(data);
+ if (iter == ctx.const_to_offset.end()) {
+ write(stream, false);
+ write(stream, t);
+ } else {
+ write(stream, true);
+ write(stream, t.get_element_type().to_string());
+ write(stream, t.get_shape());
+ write(stream, t.get_byte_size());
+ write(stream, iter->second); // offset in weights file
+ }
+ }
+}
+
+void ov::npuw::s11n::read_weightless(std::istream& stream,
+ std::vector<ov::Tensor>& var,
+ const ov::npuw::s11n::Weights& weights) {
+ var.clear();
+ std::size_t size;
+ read(stream, size);
+ for (std::size_t i = 0; i < size; ++i) {
+ bool is_initialized = false;
+ read(stream, is_initialized);
+ if (!is_initialized) {
+ var.push_back(ov::Tensor());
+ continue;
+ }
+ bool is_weightless = false;
+ read(stream, is_weightless);
+ if (is_weightless) {
+ std::string type_str;
+ read(stream, type_str);
+ ov::element::Type type(type_str);
+ ov::Shape shape;
+ read(stream, shape);
+ std::size_t byte_size = 0;
+ read(stream, byte_size);
+ std::size_t offset = 0;
+ read(stream, offset);
+ ov::Tensor t(type, shape);
+ std::memcpy(t.data(), weights->get_ptr(offset), byte_size);
+ var.push_back(t);
+ } else {
+ ov::Tensor t;
+ read(stream, t);
+ var.push_back(t);
+ }
+ }
+}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index bdfb7cfa10d..ab11c853f4e 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -11,13 +11,14 @@
#include <optional>
#include <string>
#include <tuple>
+#include <unordered_map>
#include <unordered_set>
#include <vector>
const constexpr std::array<uint8_t, 6> NPUW_SERIALIZATION_INDICATOR =
{char{0x13}, char{0x37}, char{0x6e}, char{0x70}, char{0x75}, char{0x77}};
-const constexpr char* NPUW_SERIALIZATION_VERSION = "0.0";
+const constexpr char* NPUW_SERIALIZATION_VERSION = "0.1";
// Forward declaration
namespace intel_npu {
@@ -32,6 +33,9 @@ class Node;
class Tensor;
template <class>
class Output;
+template <class>
+class SharedBuffer;
+class MappedMemory;
// Forward declaration
namespace op {
@@ -46,9 +50,22 @@ namespace npuw {
namespace compiled {
struct Spatial;
} // namespace compiled
+namespace weights {
+class LazyTensor;
+} // namespace weights
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;
+};
+
+using Weights = std::shared_ptr<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>;
+
// Specific type overloads
void write(std::ostream& stream, const std::streampos& var);
void write(std::ostream& stream, const std::string& var);
@@ -59,6 +76,7 @@ void write(std::ostream& stream, const ov::Tensor& var);
void write(std::ostream& stream, const ::intel_npu::Config& var);
void write(std::ostream& stream, const ov::Output<const ov::Node>& var);
void write_any(std::ostream& stream, const ov::Any& var);
+void write(std::ostream& stream, const ov::npuw::weights::LazyTensor& var);
void read(std::istream& stream, std::streampos& var);
void read(std::istream& stream, std::string& var);
@@ -70,6 +88,12 @@ void read(std::istream& stream, ::intel_npu::Config& var);
void read(std::istream& stream, std::shared_ptr<ov::op::v0::Parameter>& var);
void read(std::istream& stream, std::shared_ptr<ov::Node>& var);
void read_any(std::istream& stream, ov::Any& var);
+void read(std::istream& stream, ov::npuw::weights::LazyTensor& var);
+
+// Weightless utils
+void write_weightless(std::ostream& stream, const std::vector<ov::Tensor>& var, const Context& ctx);
+// No allocation needed
+void read_weightless(std::istream& stream, std::vector<ov::Tensor>& var, const Weights& weights);
// 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 fce64c069be..3bfbd7aead3 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -330,6 +330,12 @@ Plugin::Plugin()
[](const Config& config) {
return config.get<NUM_STREAMS>();
}}},
+ {ov::weights_path.name(),
+ {true,
+ ov::PropertyMutability::RW,
+ [](const Config& config) {
+ return config.get<WEIGHTS_PATH>();
+ }}},
{ov::device::uuid.name(),
{true,
ov::PropertyMutability::RO,
@@ -797,8 +803,9 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, c
std::array<uint8_t, 6> serialization_indicator;
ov::npuw::s11n::read(stream, serialization_indicator);
if (serialization_indicator == NPUW_SERIALIZATION_INDICATOR) {
- stream.seekg(stream_start_pos);
- return ov::npuw::LLMCompiledModel::deserialize(stream, shared_from_this());
+ stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);
+ // Properties are required for ov::weights_path
+ return ov::npuw::LLMCompiledModel::deserialize(stream, shared_from_this(), properties);
}
stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);