Skip to content

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

[NPUW] Fix importing weightless blob with bf16 constants (#30913)

Alexey Smirnovdd3832222e19
diff --git a/src/plugins/intel_npu/src/plugin/CMakeLists.txt b/src/plugins/intel_npu/src/plugin/CMakeLists.txt
index 94526d1a20f..78ca20ead18 100644
--- a/src/plugins/intel_npu/src/plugin/CMakeLists.txt
+++ b/src/plugins/intel_npu/src/plugin/CMakeLists.txt
@@ -40,6 +40,7 @@ target_link_libraries(${TARGET_NAME}
PRIVATE
openvino::npu_al
openvino::npu_common
+ openvino::reference
)
target_include_directories(${TARGET_NAME}
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 5dfb4ac91f8..de1a7a75023 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -154,7 +154,6 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::ICompiledModel::create(
// and not the underlying models and submodels
auto config = properties;
config.erase(ov::cache_dir.name());
- pre_load_transform(model, properties);
compiled_model = std::make_shared<ov::npuw::CompiledModel>(model, plugin, config);
}
LOG_INFO("Done");
@@ -173,6 +172,11 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
m_cfg(m_options_desc),
m_name(model->get_friendly_name()),
m_loaded_from_cache(false) {
+ // Note: we need to identify original bf16 constants for potential weightless deserialization later
+ // And only then do bf16 to f16 transformation
+ m_bf16_consts = ov::npuw::s11n::get_bf16_consts(model);
+ pre_load_transform(model, properties);
+
::intel_npu::registerNPUWOptions(*m_options_desc);
std::map<std::string, ov::Any> npuw_props;
@@ -699,7 +703,7 @@ void ov::npuw::CompiledModel::export_model(std::ostream& stream) const {
write(stream, is_weightless);
if (!encryption_required) {
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext ctx(false, nullptr, nullptr, m_bf16_consts);
serialize(stream, ctx);
write(stream, m_weights_bank->get_name());
@@ -715,13 +719,13 @@ void ov::npuw::CompiledModel::export_model(std::ostream& stream) const {
std::stringstream non_encrypted_stream;
if (is_weightless) {
non_encrypted_stream.copyfmt(stream);
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext ctx(false, nullptr, nullptr, m_bf16_consts);
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
- EncryptContext ctx(true, enc_callbacks.encrypt, nullptr);
+ CompiledContext ctx(true, enc_callbacks.encrypt, nullptr, m_bf16_consts);
serialize(stream, ctx);
}
@@ -805,7 +809,7 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::import_model(
};
if (!encrypted) {
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext ctx(false, nullptr, nullptr);
auto compiled_model = ov::npuw::CompiledModel::deserialize(stream, plugin, properties, ctx);
NPUW_ASSERT(compiled_model && "Couldn't import NPUW compiled model!");
read_and_finalize_bank(stream, compiled_model);
@@ -828,10 +832,10 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::import_model(
std::string encrypted_str;
read(stream, encrypted_str);
std::istringstream decrypted_stream(std::move(enc_callbacks.decrypt(encrypted_str)));
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext ctx(false, nullptr, nullptr);
compiled_model = ov::npuw::CompiledModel::deserialize(decrypted_stream, plugin, properties, ctx);
} else {
- EncryptContext ctx(true, nullptr, enc_callbacks.decrypt);
+ CompiledContext ctx(true, nullptr, enc_callbacks.decrypt);
compiled_model = ov::npuw::CompiledModel::deserialize(stream, plugin, properties, ctx);
}
@@ -842,7 +846,7 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::import_model(
return compiled_model;
}
-void ov::npuw::CompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::EncryptContext& enc_ctx) const {
+void ov::npuw::CompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::CompiledContext& enc_ctx) const {
LOG_INFO("Serializing CompiledModel...");
LOG_BLOCK();
@@ -889,6 +893,9 @@ void ov::npuw::CompiledModel::serialize(std::ostream& stream, const ov::npuw::s1
}
write(model_stream, is_weightless);
+ // Write bf16 consts cache
+ write(model_stream, enc_ctx.bf16_consts);
+
// Create weightless context
WeightsContext ctx(is_weightless, m_const_to_offset);
@@ -931,7 +938,7 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
std::istream& stream,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties,
- const ov::npuw::s11n::EncryptContext& enc_ctx) {
+ const ov::npuw::s11n::CompiledContext& enc_ctx) {
LOG_INFO("Deserializing CompiledModel...");
LOG_BLOCK();
@@ -990,6 +997,9 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
bool is_weightless = false;
read(stream, is_weightless);
+ // Read bf16 consts cache
+ read(stream, compiled->m_bf16_consts);
+
// Initialize weights stream if weightless flow
std::string weights_path;
std::shared_ptr<ov::Model> model_ptr;
@@ -1042,7 +1052,7 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
}
}
- WeightsContext ctx(weights, consts_cache);
+ WeightsContext ctx(weights, consts_cache, compiled->m_bf16_consts);
// Deserialize compiled submodels
std::size_t subm_size = 0;
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 4e3ab9e363b..79524f38180 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -76,11 +76,11 @@ private:
void report_io() const;
- void serialize(std::ostream& stream, const ov::npuw::s11n::EncryptContext& ctx) const;
+ void serialize(std::ostream& stream, const ov::npuw::s11n::CompiledContext& ctx) const;
static std::shared_ptr<CompiledModel> deserialize(std::istream& stream,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties,
- const ov::npuw::s11n::EncryptContext& ctx);
+ const ov::npuw::s11n::CompiledContext& ctx);
// 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
@@ -189,6 +189,7 @@ private:
std::shared_ptr<weights::Bank> m_weights_bank = nullptr;
std::unordered_map<const void*, std::size_t> m_const_to_offset;
+ ov::npuw::s11n::BF16Cache m_bf16_consts;
};
} // 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 1aee369a9a0..f0a7727cbb3 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/lazy_tensor.cpp
@@ -11,6 +11,7 @@
#include "logging.hpp"
#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
#include "openvino/op/util/op_types.hpp"
+#include "openvino/reference/convert.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "util.hpp"
@@ -61,8 +62,24 @@ void Const::read_weight(const ov::npuw::s11n::WeightsContext& ctx) {
"LazyTensor can only read weight when it's being deserialized and not created from a Constant!");
if (ctx.weights) {
- 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);
+ if (ctx.bf16_consts.find({m_offset, m_byte_size}) != ctx.bf16_consts.end()) {
+ NPUW_ASSERT(m_cached_type == ov::element::f16);
+ // Read original bf16 weight
+ auto bf16_tensor = ov::Tensor(ov::element::bf16, m_cached_shape);
+ NPUW_ASSERT(bf16_tensor.get_byte_size() == m_byte_size);
+ std::memcpy(bf16_tensor.data(), ctx.weights->get_ptr(m_offset), m_byte_size);
+
+ m_read_from_bin = ov::Tensor(m_cached_type, m_cached_shape);
+ NPUW_ASSERT(bf16_tensor.get_size() == m_read_from_bin.get_size());
+ // Transform bf16 to f16 tensor
+ using dst_type = typename element_type_traits<ov::element::Type_t::f16>::value_type;
+ auto src_data = bf16_tensor.data<ov::bfloat16>();
+ 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);
+ }
} 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!");
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 cb5ff455fd8..8022408366e 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
@@ -628,6 +628,9 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
LOG_DEBUG("2. Transform kvcache model from stateful to stateless.");
ov::pass::StatefulToStateless().run_on_model(kvcache_model);
LOG_DEBUG(" ...also convert BF16 to FP16");
+ // Note: we need to identify original bf16 constants for potential weightless deserialization later
+ // And only then do bf16 to f16 transformation
+ m_bf16_consts = ov::npuw::s11n::get_bf16_consts(model);
ov::pass::ConvertPrecision(ov::element::bf16, ov::element::f16).run_on_model(kvcache_model);
LOG_DEBUG("3. Creating prefill model as clone of transformed kvcache one.");
auto prefill_model = kvcache_model->clone();
@@ -757,7 +760,7 @@ void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {
write(stream, is_weightless);
if (!encryption_required) {
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext ctx(false, nullptr, nullptr);
return serialize(stream, ctx);
}
@@ -765,18 +768,18 @@ void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {
std::stringstream non_encrypted_stream;
if (is_weightless) {
non_encrypted_stream.copyfmt(stream);
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext 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
- EncryptContext ctx(true, enc_callbacks.encrypt, nullptr);
+ CompiledContext ctx(true, enc_callbacks.encrypt, nullptr);
serialize(stream, ctx);
}
}
-void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::EncryptContext& ctx) const {
+void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::CompiledContext& ctx) const {
LOG_INFO("Serializing LLMCompiledModel...");
LOG_BLOCK();
@@ -810,7 +813,7 @@ void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw:
// Serialize CompiledModels
// Note: no need to pass any encryption here as it's done in export_model()
- EncryptContext enc_ctx(false, nullptr, nullptr);
+ CompiledContext enc_ctx(false, nullptr, nullptr, m_bf16_consts);
m_kvcache_compiled->serialize(model_stream, enc_ctx);
m_prefill_compiled->serialize(model_stream, enc_ctx);
};
@@ -921,7 +924,7 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::import_m
};
if (!encrypted) {
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext 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);
@@ -944,10 +947,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)));
- EncryptContext ctx(false, nullptr, nullptr);
+ CompiledContext ctx(false, nullptr, nullptr);
compiled_model = ov::npuw::LLMCompiledModel::deserialize(decrypted_stream, plugin, properties, ctx);
} else {
- EncryptContext ctx(true, nullptr, enc_callbacks.decrypt);
+ CompiledContext ctx(true, nullptr, enc_callbacks.decrypt);
compiled_model = ov::npuw::LLMCompiledModel::deserialize(stream, plugin, properties, ctx);
}
@@ -963,7 +966,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::EncryptContext& ctx) {
+ const ov::npuw::s11n::CompiledContext& ctx) {
using namespace ov::npuw::s11n;
auto read_model_meta = [&](std::istream& model_stream) {
@@ -996,7 +999,7 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserial
// Deserialize CompiledModels
// Note: no need to pass any encryption here as it's done in import_model()
- EncryptContext enc_ctx(false, nullptr, nullptr);
+ CompiledContext enc_ctx(false, nullptr, nullptr);
compiled->m_kvcache_compiled = ov::npuw::CompiledModel::deserialize(model_stream, plugin, properties, enc_ctx);
compiled->m_prefill_compiled = ov::npuw::CompiledModel::deserialize(model_stream, plugin, properties, enc_ctx);
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 a888c7b1328..fc2258acb4b 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::EncryptContext& ctx) const;
+ void serialize(std::ostream& stream, const ov::npuw::s11n::CompiledContext& 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::EncryptContext& ctx);
+ const ov::npuw::s11n::CompiledContext& ctx);
std::string m_name;
std::shared_ptr<::intel_npu::OptionsDesc> m_options_desc;
@@ -62,6 +62,9 @@ private:
GetPropertiesMap m_prop_to_opt;
ov::AnyMap m_non_llm_props;
+ // Cache bf16 constants for weightless deserialization
+ ov::npuw::s11n::BF16Cache m_bf16_consts;
+
KVCacheDesc m_kvcache_desc;
std::shared_ptr<ov::npuw::CompiledModel> m_kvcache_compiled;
std::shared_ptr<ov::npuw::CompiledModel> m_prefill_compiled;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
index c30e6368186..693685b4fc1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
@@ -7,8 +7,10 @@
#include "intel_npu/config/config.hpp"
#include "lazy_tensor.hpp"
#include "logging.hpp"
+#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/util/op_types.hpp"
+#include "openvino/reference/convert.hpp"
#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/util/mmap_object.hpp"
#include "spatial.hpp"
@@ -20,13 +22,33 @@ ov::npuw::s11n::WeightsContext::WeightsContext(bool _is_weightless,
: is_weightless(_is_weightless),
const_to_offset(_const_to_offset) {}
-// NOTE: This construtor can and should only be used when importing weightless blobs
+// NOTE: This construtor can and should only be used when importing blobs
ov::npuw::s11n::WeightsContext::WeightsContext(const ov::npuw::s11n::Weights& _weights,
- const s11n::WeightsContext::ConstsCache& _consts_cache)
- : is_weightless(true),
- weights(_weights),
- consts_cache(_consts_cache) {
- NPUW_ASSERT(_weights || !_consts_cache.empty());
+ const s11n::WeightsContext::ConstsCache& _consts_cache,
+ const BF16Cache& _bf16_consts)
+ : weights(_weights),
+ consts_cache(_consts_cache),
+ bf16_consts(_bf16_consts) {
+ is_weightless = _weights || !_consts_cache.empty();
+}
+
+ov::npuw::s11n::BF16Cache ov::npuw::s11n::get_bf16_consts(const std::shared_ptr<ov::Model>& model) {
+ ov::npuw::s11n::BF16Cache bf16_cache;
+ for (auto&& node_ptr : model->get_ordered_ops()) {
+ if (const auto c = ov::as_type_ptr<ov::op::v0::Constant>(node_ptr)) {
+ if (c->get_element_type() != ov::element::bf16) {
+ continue;
+ }
+ 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;
+ bf16_cache.insert({offset, c->get_byte_size()});
+ }
+ }
+ return bf16_cache;
}
void ov::npuw::s11n::write(std::ostream& stream, const std::streampos& var) {
@@ -434,7 +456,23 @@ void ov::npuw::s11n::read_weightless(std::istream& stream,
ov::Tensor t(type, shape);
if (ctx.weights) {
- std::memcpy(t.data(), ctx.weights->get_ptr(offset), byte_size);
+ if (ctx.bf16_consts.find({offset, byte_size}) != ctx.bf16_consts.end()) {
+ NPUW_ASSERT(type == ov::element::f16);
+ // Read original bf16 weight
+ auto bf16_tensor = ov::Tensor(ov::element::bf16, shape);
+ NPUW_ASSERT(bf16_tensor.get_byte_size() == byte_size);
+ std::memcpy(bf16_tensor.data(), ctx.weights->get_ptr(offset), byte_size);
+
+ NPUW_ASSERT(bf16_tensor.get_size() == t.get_size());
+
+ // Transform bf16 to f16 tensor
+ using dst_type = typename element_type_traits<ov::element::Type_t::f16>::value_type;
+ auto src_data = bf16_tensor.data<ov::bfloat16>();
+ auto dst_data = t.data<dst_type>();
+ ov::reference::convert_from_bf16_to_f16_with_clamp(src_data, dst_data, t.get_size());
+ } else {
+ 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!");
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index 48dae1eafe5..3c3cd9491ad 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -80,18 +80,31 @@ class LazyTensor;
namespace s11n {
+class PairHash {
+public:
+ template <typename T, typename U>
+ std::size_t operator()(const std::pair<T, U>& p) const {
+ return std::hash<T>()(p.first) ^ std::hash<U>()(p.second);
+ }
+};
+
+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>>>;
-struct EncryptContext {
- explicit EncryptContext(bool _encrypted,
- std::function<std::string(const std::string&)> _encrypt,
- std::function<std::string(const std::string&)> _decrypt)
+struct CompiledContext {
+ CompiledContext(bool _encrypted,
+ std::function<std::string(const std::string&)> _encrypt,
+ std::function<std::string(const std::string&)> _decrypt,
+ const BF16Cache& _bf16_consts = {})
: encrypted(_encrypted),
encrypt(_encrypt),
- decrypt(_decrypt) {}
+ decrypt(_decrypt),
+ bf16_consts(_bf16_consts) {}
bool encrypted = false;
std::function<std::string(const std::string&)> encrypt = nullptr;
std::function<std::string(const std::string&)> decrypt = nullptr;
+ // FIXME: needed to pass original bf16 consts meta to CompiledModel
+ BF16Cache bf16_consts;
};
struct WeightsContext {
@@ -106,14 +119,19 @@ struct WeightsContext {
explicit 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);
+ explicit WeightsContext(const ov::npuw::s11n::Weights& _weights,
+ const ConstsCache& _consts_cache,
+ const BF16Cache& _bf16_consts);
bool is_weightless = true;
std::unordered_map<const void*, std::size_t> const_to_offset;
ov::npuw::s11n::Weights weights = nullptr;
ConstsCache consts_cache;
+ BF16Cache bf16_consts;
};
+BF16Cache get_bf16_consts(const std::shared_ptr<ov::Model>& model);
+
// Specific type overloads
void write(std::ostream& stream, const std::streampos& var);
void write(std::ostream& stream, const std::string& var);
@@ -200,6 +218,14 @@ void write(std::ostream& stream, const std::unordered_set<T>& var) {
}
}
+template <typename T, typename H>
+void write(std::ostream& stream, const std::unordered_set<T, H>& var) {
+ write(stream, var.size());
+ for (const auto& el : var) {
+ write(stream, el);
+ }
+}
+
template <typename K, typename V>
void write(std::ostream& stream, const std::map<K, V>& var) {
write(stream, var.size());
@@ -264,6 +290,18 @@ void read(std::istream& stream, std::unordered_set<T>& var) {
}
}
+template <typename T, typename H>
+void read(std::istream& stream, std::unordered_set<T, H>& var) {
+ var.clear();
+ std::size_t var_size = 0;
+ stream.read(reinterpret_cast<char*>(&var_size), sizeof var_size);
+ for (std::size_t i = 0; i < var_size; ++i) {
+ T elem;
+ read(stream, elem);
+ var.insert(std::move(elem));
+ }
+}
+
template <typename K, typename V>
void read(std::istream& stream, std::map<K, V>& var) {
var.clear();
diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
index 4e3876e7af7..4eb7bd29212 100644
--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt
+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
@@ -10,6 +10,7 @@ set(MANDATORY_UNIT_TESTS_LIBS
"openvino::gtest"
"openvino::gtest_main"
"openvino::runtime"
+ "openvino::reference"
"openvino::npu_common"
"openvino::npu_al"
"openvino::npu_logger_utils"
diff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
index c3d3ff40f40..badf8899584 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);