Skip to content

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

[`NPUW`] [`GQA`] [`ORC`] Migrate `GQACompiledModel` to `ORC` (#38479)

Mircea-Aurelian Dan3d1882acf321
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 12a8ef01a39..12a086a01da 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -373,11 +373,6 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::ICompiledModel::create(
}
namespace {
-bool is_partitioned_orc(std::istream& stream) {
- const auto header = ov::npuw::orc::is_orc(stream);
- return header.has_value() && header->schema_uuid == ov::npuw::orc::schema_npuw::NPUW_ORC_PARTITIONED_SCHEMA;
-}
-
bool has_npuw_indicator(std::istream& stream) {
const auto start = stream.tellg();
ov::npuw::s11n::IndicatorType indicator;
@@ -390,7 +385,7 @@ bool has_npuw_indicator(std::istream& stream) {
} // anonymous namespace
bool ov::npuw::ICompiledModel::is_npuw_blob(std::istream& stream) {
- return is_partitioned_orc(stream) || has_npuw_indicator(stream);
+ return ov::npuw::orc::is_orc(stream).has_value() || has_npuw_indicator(stream);
}
std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::ICompiledModel::import_model(
@@ -400,10 +395,15 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::ICompiledModel::import_model
LOG_INFO("Choosing which NPUW CompiledModel to import");
LOG_BLOCK();
- // The partitioned CompiledModel is a plain ORC container with no indicator
- // header of its own.
- if (is_partitioned_orc(stream)) {
- return ov::npuw::CompiledModel::import_model(stream, plugin, properties);
+ const auto orc_header = orc::is_orc(stream);
+ if (orc_header.has_value()) {
+ if (orc_header->schema_uuid == ov::npuw::orc::schema_npuw::NPUW_ORC_GQA_SCHEMA) {
+ return ov::npuw::GQACompiledModel::import_model(stream, plugin, properties);
+ }
+ if (orc_header->schema_uuid == ov::npuw::orc::schema_npuw::NPUW_ORC_PARTITIONED_SCHEMA) {
+ return ov::npuw::CompiledModel::import_model(stream, plugin, properties);
+ }
+ NPUW_ASSERT(false && "Couldn't determine blob type from ORC root section");
}
const auto stream_start_pos = stream.tellg();
@@ -419,19 +419,25 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::ICompiledModel::import_model
stream.clear();
stream.seekg(stream_start_pos);
+ std::string legacy_kind_cmod;
if (compiled_model_indicator == NPUW_FLUX2_COMPILED_MODEL_INDICATOR) {
return ov::npuw::Flux2CompiledModel::import_model(stream, plugin, properties);
- } else if (compiled_model_indicator == NPUW_GQA_COMPILED_MODEL_INDICATOR) {
- return ov::npuw::GQACompiledModel::import_model(stream, plugin, properties);
} else if (compiled_model_indicator == NPUW_LLM_COMPILED_MODEL_INDICATOR) {
// Properties are required for ov::weights_path
return ov::npuw::LLMCompiledModel::import_model(stream, plugin, properties);
} else if (compiled_model_indicator == NPUW_BATCHED_COMPILED_MODEL_INDICATOR) {
return ov::npuw::batched::CompiledModel::import_model(stream, plugin, properties);
} else if (compiled_model_indicator == NPUW_COMPILED_MODEL_INDICATOR) {
- // The flat CompiledModel moved to the ORC container above; its old
+ // The flat CompiledModel and GQACompiledModel moved to the ORC container above; its old
// indicator-headed layout has no reader anymore.
- OPENVINO_THROW("Legacy flat NPUW CompiledModel blobs are no longer supported. Re-export the model with "
+ legacy_kind_cmod = "CompiledModel";
+ } else if (compiled_model_indicator == NPUW_GQA_COMPILED_MODEL_INDICATOR) {
+ legacy_kind_cmod = "GQACompiledModel";
+ }
+ if (!legacy_kind_cmod.empty()) {
+ OPENVINO_THROW("Legacy flat NPUW ",
+ legacy_kind_cmod,
+ " blobs are no longer supported. Re-export the model with "
"the current ORC serializer.");
}
OPENVINO_THROW("Couldn't deserialize NPUW blob - fatal error!");
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 cf84784df42..bad09121af7 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -156,6 +156,7 @@ private:
friend class LLMInferRequest;
friend class moe::MoEExecutor;
friend class CompiledModelDescSerializationAccess;
+ friend class GQACompiledModel;
bool compile_for_success(std::size_t id, const std::vector<std::string>& devices);
ov::SoPtr<ov::ICompiledModel> compile_submodel(const std::shared_ptr<ov::Model>& submodel,
diff --git a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp
index 66d57a01813..19a31c7b4e2 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.cpp
@@ -37,6 +37,8 @@
#include "openvino/op/util/op_types.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/runtime/properties.hpp"
+#include "orc.hpp"
+#include "orc/schema_npuw.hpp"
#include "serialization.hpp"
#include "util.hpp"
@@ -309,6 +311,13 @@ std::string_view gqa_case_name(ov::npuw::GQACompiledModel::Case gqa_case) {
return "Unknown";
}
+std::function<std::string(const std::string&)> get_encrypt_callback(const ov::AnyMap& properties) {
+ if (auto it = properties.find(ov::cache_encryption_callbacks.name()); it != properties.end()) {
+ return it->second.as<ov::EncryptionCallbacks>().encrypt;
+ }
+ return {};
+}
+
} // namespace
// Prints a "[GQA-TRACE]" line when OPENVINO_NPUW_GQA_LOG=1 is set; no-op otherwise.
@@ -657,23 +666,24 @@ ov::npuw::GQACompiledModel::GQACompiledModel(const std::shared_ptr<ov::Model>& o
// Hand-rolled port serialization instead of ov::pass::Serialize: the IR writer recomputes
// a Result's shape/type from its input node, which would collapse our dummy-placeholder
// carcass Results down to the placeholder's shape.
-void ov::npuw::GQACompiledModel::write_port_list(std::ostream& stream,
+void ov::npuw::GQACompiledModel::write_port_list(ov::npuw::orc::Stream& stream,
const std::vector<ov::Output<const ov::Node>>& ports) {
- using namespace ov::npuw::s11n;
- write(stream, ports.size());
+ NPUW_ASSERT(stream.output());
+ uint64_t ports_size = static_cast<uint64_t>(ports.size());
+ stream & ports_size;
for (const auto& port : ports) {
- write(stream, port.get_node()->get_friendly_name());
- write(stream, port.get_element_type().to_string());
- write(stream, port.get_partial_shape().to_string());
+ stream & port.get_node()->get_friendly_name();
+ stream & port.get_element_type().to_string();
+ stream & port.get_partial_shape().to_string();
// Preserve tensor names too: ORT/OVEP match ports by these, not friendly names.
- write(stream, port.get_names());
+ stream & port.get_names();
}
}
-ov::ParameterVector ov::npuw::GQACompiledModel::read_input_port_list(std::istream& stream) {
- using namespace ov::npuw::s11n;
- size_t count = 0;
- read(stream, count);
+ov::ParameterVector ov::npuw::GQACompiledModel::read_input_port_list(ov::npuw::orc::Stream& stream) {
+ NPUW_ASSERT(stream.input());
+ uint64_t count = 0;
+ stream & count;
// Guard against a corrupted blob surfacing as an opaque bad_alloc instead of a clear error.
OPENVINO_ASSERT(count <= 1024, "GQACompiledModel: implausible outer input port count (", count, ") on import");
ov::ParameterVector params;
@@ -681,10 +691,10 @@ ov::ParameterVector ov::npuw::GQACompiledModel::read_input_port_list(std::istrea
for (size_t i = 0; i < count; ++i) {
std::string name, elem_type_str, shape_str;
std::unordered_set<std::string> tensor_names;
- read(stream, name);
- read(stream, elem_type_str);
- read(stream, shape_str);
- read(stream, tensor_names);
+ stream & name;
+ stream & elem_type_str;
+ stream & shape_str;
+ stream & tensor_names;
auto param =
std::make_shared<ov::op::v0::Parameter>(ov::element::Type(elem_type_str), ov::PartialShape(shape_str));
param->set_friendly_name(name);
@@ -694,20 +704,20 @@ ov::ParameterVector ov::npuw::GQACompiledModel::read_input_port_list(std::istrea
return params;
}
-ov::NodeVector ov::npuw::GQACompiledModel::read_output_port_list(std::istream& stream) {
- using namespace ov::npuw::s11n;
- size_t count = 0;
- read(stream, count);
+ov::NodeVector ov::npuw::GQACompiledModel::read_output_port_list(ov::npuw::orc::Stream& stream) {
+ NPUW_ASSERT(stream.input());
+ uint64_t count = 0;
+ stream & count;
OPENVINO_ASSERT(count <= 1024, "GQACompiledModel: implausible outer output port count (", count, ") on import");
ov::NodeVector results;
results.reserve(count);
for (size_t i = 0; i < count; ++i) {
std::string name, elem_type_str, shape_str;
std::unordered_set<std::string> tensor_names;
- read(stream, name);
- read(stream, elem_type_str);
- read(stream, shape_str);
- read(stream, tensor_names);
+ stream & name;
+ stream & elem_type_str;
+ stream & shape_str;
+ stream & tensor_names;
const auto elem_type = ov::element::Type(elem_type_str);
// A dummy Constant source, purely so Result has something to wrap -- its own
// reported type/shape is overridden right below via a fresh descriptor::Tensor,
@@ -725,6 +735,11 @@ ov::NodeVector ov::npuw::GQACompiledModel::read_output_port_list(std::istream& s
void ov::npuw::GQACompiledModel::export_model(std::ostream& stream) const {
LOG_INFO("Exporting GQACompiledModel...");
LOG_BLOCK();
+
+ auto npu_compiled_model = std::dynamic_pointer_cast<ov::npuw::CompiledModel>(m_inner_compiled_model);
+ NPUW_ASSERT(npu_compiled_model &&
+ "GQACompiledModel: Currently, ov::npuw::CompiledModel is the only allowed inner compiled model type");
+
GQA_TRACE("GQACompiledModel::export_model() begin, m_dynamic_kv_cache_axes has " << m_dynamic_kv_cache_axes.size()
<< " entries:");
#ifdef NPU_PLUGIN_DEVELOPER_BUILD
@@ -732,34 +747,40 @@ void ov::npuw::GQACompiledModel::export_model(std::ostream& stream) const {
GQA_TRACE(" '" << name << "' -> axis " << axis);
}
#endif
- using namespace ov::npuw::s11n;
- write_header(stream, NPUW_GQA_COMPILED_MODEL_INDICATOR);
// Preserve the outer-facing ports and axis map so import_model() can rebuild an
// equivalent GQACompiledModel, not just the bare inner CompiledModel.
auto outer_inputs = inputs();
auto outer_outputs = outputs();
- GQACompiledModel::write_port_list(stream, outer_inputs);
- GQACompiledModel::write_port_list(stream, outer_outputs);
- write(stream, m_dynamic_kv_cache_axes);
- write(stream, m_effective_max_context_len);
- write(stream, m_effective_sliding_window_length);
- write(stream, m_effective_sliding_window_headroom);
- // Written here (from the real model, available at this point) rather than
- // re-derived by import_model() from its dummy carcass outer_model, which has no
- // actual GroupQueryAttention ops for the graph-walking find_*() helpers to find.
- write(stream, m_total_seq_len_param_name);
- write(stream, m_present_to_past_param_names);
- GQA_TRACE("GQACompiledModel::export_model() -> wrote "
- << outer_inputs.size() << " outer inputs, " << outer_outputs.size() << " outer outputs, "
- << m_dynamic_kv_cache_axes.size()
- << " dynamic-axis entries, effective_max_context_len=" << m_effective_max_context_len
- << ", effective_sliding_window_length=" << m_effective_sliding_window_length
- << ", effective_sliding_window_headroom=" << m_effective_sliding_window_headroom
- << ", total_seq_len_param_name=" << m_total_seq_len_param_name.value_or("<none>")
- << ", present_to_past_param_names has " << m_present_to_past_param_names.size() << " entries"
- << "; delegating to inner m_inner_compiled_model->export_model()");
- m_inner_compiled_model->export_model(stream);
+ ov::npuw::orc::write_file_header(stream, ov::npuw::orc::schema_npuw::NPUW_ORC_GQA_SCHEMA);
+ ov::npuw::orc::with_section(stream, kOrcType, kOrcVersion, 0u, [&] {
+ ov::npuw::orc::with_leaf_section(stream, ov::npuw::orc::META_SECTION_TYPE, 0u, [&] {
+ auto meta_stream = ov::npuw::orc::Stream::writer(stream);
+ GQACompiledModel::write_port_list(meta_stream, outer_inputs);
+ GQACompiledModel::write_port_list(meta_stream, outer_outputs);
+ meta_stream & m_dynamic_kv_cache_axes;
+ meta_stream & m_effective_max_context_len;
+ meta_stream & m_effective_sliding_window_length;
+ meta_stream & m_effective_sliding_window_headroom;
+ // Written here (from the real model, available at this point) rather than
+ // re-derived by import_model() from its dummy carcass outer_model, which has no
+ // actual GroupQueryAttention ops for the graph-walking find_*() helpers to find.
+ meta_stream & m_total_seq_len_param_name;
+ meta_stream & m_present_to_past_param_names;
+ GQA_TRACE("GQACompiledModel::export_model() -> wrote "
+ << outer_inputs.size() << " outer inputs, " << outer_outputs.size() << " outer outputs, "
+ << m_dynamic_kv_cache_axes.size()
+ << " dynamic-axis entries, effective_max_context_len=" << m_effective_max_context_len
+ << ", effective_sliding_window_length=" << m_effective_sliding_window_length
+ << ", effective_sliding_window_headroom=" << m_effective_sliding_window_headroom
+ << ", total_seq_len_param_name=" << m_total_seq_len_param_name.value_or("<none>")
+ << ", present_to_past_param_names has " << m_present_to_past_param_names.size() << " entries"
+ << "; delegating to inner m_inner_compiled_model->serialize_orc_container()");
+ });
+ npu_compiled_model->serialize_orc_container(stream,
+ /* include_weights_bank = */ true,
+ get_encrypt_callback(npu_compiled_model->non_npuw_props()));
+ });
LOG_INFO("Done");
GQA_TRACE("GQACompiledModel::export_model() done");
}
@@ -772,9 +793,25 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::GQACompiledModel::import_mod
LOG_BLOCK();
GQA_TRACE("GQACompiledModel::import_model() begin");
- using namespace ov::npuw::s11n;
-
- read_and_check_header(stream, NPUW_GQA_COMPILED_MODEL_INDICATOR, "GQACompiledModel");
+ const auto header = orc::read_file_header(stream);
+ OPENVINO_ASSERT(header.schema_uuid == orc::schema_npuw::NPUW_ORC_GQA_SCHEMA,
+ "Unsupported ORC schema for NPUW GQACompiledModel");
+
+ orc::ScopedReadSection root(stream);
+ OPENVINO_ASSERT(root.remaining() <= ov::npuw::orc::Stream::reader(stream).remaining(),
+ "GQA root section claims ",
+ root.remaining(),
+ " which exceeds the stream size");
+ OPENVINO_ASSERT(root.header().type == kOrcType, "Not a GQA ORC blob");
+ OPENVINO_ASSERT(root.header().version <= kOrcVersion,
+ "GQA blob was produced by a newer NPUW (section version ",
+ root.header().version,
+ "; this build supports up to ",
+ kOrcVersion,
+ ")");
+ OPENVINO_ASSERT(!orc::has_flag(root.header().flags, orc::SectionFlag::LEAF),
+ "Unsupported ORC NPUW GQA root section");
+ OPENVINO_ASSERT(!root.done(), "Unexpected end of GQA section: empty GQA root section (missing META)");
// Rebuild the outer-facing model (dynamic axes restored) exactly as before export.
ov::ParameterVector outer_parameters;
@@ -785,14 +822,29 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::GQACompiledModel::import_mod
uint32_t effective_sliding_window_headroom = 0;
std::optional<std::string> total_seq_len_param_name;
std::unordered_map<std::string, std::string> present_to_past_param_names;
- outer_parameters = GQACompiledModel::read_input_port_list(stream);
- outer_results = GQACompiledModel::read_output_port_list(stream);
- read(stream, dynamic_kv_cache_axes);
- read(stream, effective_max_context_len);
- read(stream, effective_sliding_window_length);
- read(stream, effective_sliding_window_headroom);
- read(stream, total_seq_len_param_name);
- read(stream, present_to_past_param_names);
+
+ auto read_meta_fields = [&](const std::string& meta_blob) {
+ auto meta_stream = ov::npuw::orc::Stream::memory_reader(meta_blob.data(), meta_blob.size());
+ outer_parameters = GQACompiledModel::read_input_port_list(meta_stream);
+ outer_results = GQACompiledModel::read_output_port_list(meta_stream);
+ meta_stream & dynamic_kv_cache_axes;
+ meta_stream & effective_max_context_len;
+ meta_stream & effective_sliding_window_length;
+ meta_stream & effective_sliding_window_headroom;
+ meta_stream & total_seq_len_param_name;
+ meta_stream & present_to_past_param_names;
+ OPENVINO_ASSERT(meta_stream.remaining() == 0, "Unexpected remaining bytes of GQACompiledModel's META section");
+ };
+ ov::npuw::orc::ScopedReadSection meta(stream);
+ OPENVINO_ASSERT(meta.remaining() <= root.remaining(), "GQA META section exceeds GQA root section");
+ if (meta.header().type != ov::npuw::orc::META_SECTION_TYPE ||
+ !ov::npuw::orc::has_flag(meta.header().flags, ov::npuw::orc::SectionFlag::LEAF)) {
+ OPENVINO_THROW("Expected ORC NPUW metadata section, got type ", meta.header().type);
+ }
+ auto meta_blob = meta.read_remaining_blob();
+ read_meta_fields(meta_blob);
+ meta.expect_end();
+ OPENVINO_ASSERT(!root.done(), "Unexpected end of GQA section: missing inner CompiledModel section after META");
GQA_TRACE(" read " << outer_parameters.size() << " outer parameters, " << outer_results.size()
<< " outer results, " << dynamic_kv_cache_axes.size() << " dynamic-axis entries:");
@@ -805,9 +857,14 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::GQACompiledModel::import_mod
auto outer_model =
std::make_shared<ov::Model>(ov::as_output_vector(outer_results), outer_parameters, "gqa_outer_model");
- // The rest of the stream is the self-contained inner CompiledModel ORC blob.
- auto inner_compiled_model = ov::npuw::CompiledModel::import_model(stream, plugin, properties);
- NPUW_ASSERT(inner_compiled_model != nullptr);
+ // Next child of the GQA root: the inner CompiledModel ORC container section (no file header).
+ auto inner_compiled_model = ov::npuw::CompiledModel::deserialize_orc_container(stream,
+ plugin,
+ properties,
+ /* require_weights_bank = */ true,
+ /* decrypt = */ {});
+ root.expect_end();
+
GQA_TRACE(" inner CompiledModel imported @ "
<< inner_compiled_model.get()
<< "; wrapping it back into a GQACompiledModel so dynamic KV-cache/attention-bias reshape "
@@ -937,7 +994,7 @@ void ov::npuw::GQAInferRequest::trace_attention_mask_stats_locked() const {
ov::npuw::util::contains_ignore_case(name, "past_value")) {
continue; // KV-cache tensor, not the mask/bias -- already traced/handled elsewhere
}
- auto it = std::find_if(outer_inputs.begin(), outer_inputs.end(), [&name](const auto& input) {
+ auto it = std::find_if(outer_inputs.begin(), outer_inputs.end(), [&](const auto& input) {
return input.get_node()->get_friendly_name() == name;
});
if (it == outer_inputs.end()) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.hpp
index 39fcb1827ec..fbed251a31d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/gqa_compiled_model.hpp
@@ -89,6 +89,10 @@ public:
V2,
};
+ static constexpr ov::npuw::orc::TypeId kOrcType =
+ static_cast<ov::npuw::orc::TypeId>(ov::npuw::orc::schema_npuw::GQAModel::ID);
+ static constexpr ov::npuw::orc::Version kOrcVersion = 0u;
+
static std::shared_ptr<ov::npuw::ICompiledModel> make_compiled_model(
const std::shared_ptr<ov::Model>& model,
const std::shared_ptr<const ov::IPlugin>& plugin,
@@ -117,9 +121,9 @@ public:
// Writes/reads outer-facing port metadata for export_model()/import_model(), keyed by
// friendly name (GQAInferRequest's dynamic-axis lookups key off it too).
- static void write_port_list(std::ostream& stream, const std::vector<ov::Output<const ov::Node>>& ports);
- static ov::ParameterVector read_input_port_list(std::istream& stream);
- static ov::NodeVector read_output_port_list(std::istream& stream);
+ static void write_port_list(ov::npuw::orc::Stream& stream, const std::vector<ov::Output<const ov::Node>>& ports);
+ static ov::ParameterVector read_input_port_list(ov::npuw::orc::Stream& stream);
+ static ov::NodeVector read_output_port_list(ov::npuw::orc::Stream& stream);
GQACompiledModel(const std::shared_ptr<ov::Model>& model,
const std::shared_ptr<const ov::IPlugin>& plugin,
diff --git a/src/plugins/intel_npu/src/plugin/npuw/orc/schema_npuw.hpp b/src/plugins/intel_npu/src/plugin/npuw/orc/schema_npuw.hpp
index 756b2785ed6..06338bf58fd 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/orc/schema_npuw.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/orc/schema_npuw.hpp
@@ -11,6 +11,9 @@ namespace ov::npuw::orc::schema_npuw {
inline constexpr SchemaUUID NPUW_ORC_PARTITIONED_SCHEMA =
{0x4E, 0x50, 0x55, 0x57, 0x43, 0x4D, 0x4F, 0x44, 0x50, 0x48, 0x41, 0x53, 0x45, 0x30, 0x30, 0x31};
+inline constexpr SchemaUUID NPUW_ORC_GQA_SCHEMA =
+ {0x4E, 0x50, 0x55, 0x57, 0x43, 0x4D, 0x4F, 0x44, 0x47, 0x51, 0x41, 0x30, 0x30, 0x30, 0x30, 0x31};
+
// Keep the top-level on-wire type IDs centralized so future schemas can reuse
// the same registry without accidental collisions.
enum class PartitionedModel : TypeId {
@@ -25,4 +28,9 @@ enum class WeightsBank : TypeId {
ID = 300,
};
+// High-level front-end containers start at ~16K
+enum class GQAModel : TypeId {
+ ID = 17000,
+};
+
} // namespace ov::npuw::orc::schema_npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index c585ecc91f3..c996eaab7d1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -170,8 +170,9 @@ struct WeightsContext {
// NOTE: This constructor should only be used when exporting blobs.
WeightsContext(bool _is_weightless, const std::unordered_map<const void*, std::size_t>& _const_to_offset);
- // NOTE: This constructor is used on blob import to carry the resolved weight source
- // (embedded weights, mmap'ed weights file, or model-backed constants cache).
+ // NOTE: This constructor is used on blob import to carry the resolved weight
+ // source (embedded weights, mmap'ed weights file, or model-backed constants
+ // cache).
WeightsContext(const ov::npuw::s11n::WeightsPtr& _weights,
const std::string& _weights_path,
const ConstsCache& _consts_cache,
@@ -196,7 +197,8 @@ struct WeightsContext {
// Context for deserializing submodels with dynamic attention mechanisms
// (Pyramid Attention, Host Flash Attention, etc.)
-// Provides plugin, device, and compiled model reference for proper deserialization
+// Provides plugin, device, and compiled model reference for proper
+// deserialization
struct SubmodelDeserializeCtx {
SubmodelDeserializeCtx(const std::shared_ptr<const ov::IPlugin>& _plugin,
const std::string& _device,
diff --git a/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp
index 40bfa2fd3ba..d276eb65d81 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/gqa_compiled_model_test.cpp
@@ -37,7 +37,10 @@
#include "openvino/op/unsqueeze.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/runtime/properties.hpp"
+#include "openvino/util/codec_xor.hpp"
#include "serialization.hpp"
+#include "unit_test_utils/mocks/openvino/runtime/mock_icompiled_model.hpp"
+#include "unit_test_utils/mocks/openvino/runtime/mock_icore.hpp"
namespace {
@@ -45,18 +48,6 @@ using ov::test::npuw::build_llm_test_model;
using ov::test::npuw::MockSubCompiledModel;
using ov::test::npuw::NullPlugin;
-std::string make_gqa_header(const std::size_t claimed_version_size, const std::string& version) {
- std::ostringstream stream;
- ov::npuw::s11n::write(stream, NPUW_SERIALIZATION_INDICATOR);
- ov::npuw::s11n::write(stream, NPUW_GQA_COMPILED_MODEL_INDICATOR);
- ov::npuw::s11n::write(stream, OPENVINO_VERSION_MAJOR);
- ov::npuw::s11n::write(stream, OPENVINO_VERSION_MINOR);
- ov::npuw::s11n::write(stream, OPENVINO_VERSION_PATCH);
- ov::npuw::s11n::write(stream, claimed_version_size);
- stream.write(version.data(), static_cast<std::streamsize>(version.size()));
- return stream.str();
-}
-
template <class Op>
std::size_t count_ops(const std::shared_ptr<ov::Model>& model) {
const auto ops = model->get_ops();
@@ -857,6 +848,80 @@ protected:
}
}
+ void setup_mocks(const std::vector<ov::Output<const ov::Node>>& model_inputs,
+ const std::vector<ov::Output<const ov::Node>>& model_outputs,
+ const std::shared_ptr<testing::NiceMock<ov::MockICore>>& mock_core,
+ const std::shared_ptr<testing::NiceMock<ov::MockICompiledModel>>& mock_compiled_model) {
+ ON_CALL(*mock_core, get_property(testing::_, testing::_, testing::_))
+ .WillByDefault(testing::Return(ov::Any("")));
+ ON_CALL(*mock_core, get_property(testing::_, testing::StrEq(ov::available_devices.name()), testing::_))
+ .WillByDefault(testing::Return(ov::Any(std::vector<std::string>{})));
+ ON_CALL(*mock_core,
+ compile_model(testing::An<const std::shared_ptr<const ov::Model>&>(),
+ testing::An<const std::string&>(),
+ testing::An<const ov::AnyMap&>()))
+ .WillByDefault(testing::Return(mock_compiled_model));
+
+ ON_CALL(*mock_compiled_model, inputs())
+ .WillByDefault([&model_inputs]() -> const std::vector<ov::Output<const ov::Node>>& {
+ return model_inputs;
+ });
+ ON_CALL(*mock_compiled_model, outputs())
+ .WillByDefault([&model_outputs]() -> const std::vector<ov::Output<const ov::Node>>& {
+ return model_outputs;
+ });
+ ON_CALL(*mock_compiled_model, get_property(testing::_)).WillByDefault(testing::Return(ov::Any("")));
+ ON_CALL(*mock_compiled_model, get_property(testing::StrEq(ov::execution_devices.name())))
+ .WillByDefault(testing::Return(ov::Any(std::vector<std::string>{"NPU.0"})));
+ }
+
+ void make_port_asserts(const std::vector<ov::Output<const ov::Node>>& model_inputs,
+ const std::vector<ov::Output<const ov::Node>>& model_outputs,
+ const std::vector<ov::Output<const ov::Node>>& inputs,
+ const std::vector<ov::Output<const ov::Node>>& outputs) {
+ ASSERT_EQ(inputs.size(), model_inputs.size());
+ ASSERT_EQ(outputs.size(), model_outputs.size());
+ for (size_t i = 0; i < model_inputs.size(); ++i) {
+ ASSERT_EQ(model_inputs.at(i).get_partial_shape(), inputs.at(i).get_partial_shape());
+ ASSERT_EQ(model_inputs.at(i).get_element_type(), inputs.at(i).get_element_type());
+ ASSERT_EQ(model_inputs.at(i).get_node()->get_friendly_name(), inputs.at(i).get_node()->get_friendly_name());
+ ASSERT_EQ(model_inputs.at(i).get_names(), inputs.at(i).get_names());
+ }
+ for (size_t j = 0; j < model_outputs.size(); ++j) {
+ ASSERT_EQ(model_outputs.at(j).get_partial_shape(), outputs.at(j).get_partial_shape());
+ ASSERT_EQ(model_outputs.at(j).get_element_type(), outputs.at(j).get_element_type());
+ ASSERT_EQ(model_outputs.at(j).get_node()->get_friendly_name(),
+ outputs.at(j).get_node()->get_friendly_name());
+ ASSERT_EQ(model_outputs.at(j).get_names(), outputs.at(j).get_names());
+ }
+ }
+
+ void validate_gqa_roundtrip(const size_t axis,
+ const std::vector<ov::Output<const ov::Node>>& model_inputs,
+ const std::vector<ov::Output<const ov::Node>>& model_outputs,
+ const uint32_t max_context_len,
+ const uint32_t sliding_window_length,
+ const uint32_t sliding_window_headroom,
+ const std::shared_ptr<const ov::npuw::GQACompiledModel>& gqa_compiled_model) {
+ make_port_asserts(model_inputs, model_outputs, gqa_compiled_model->inputs(), gqa_compiled_model->outputs());
+
+ size_t matched_ports = 0;
+ for (const auto& input : gqa_compiled_model->inputs()) {
+ if (input.get_node()->get_friendly_name() != "past_keys_0" &&
+ input.get_node()->get_friendly_name() != "past_values_0") {
+ continue;
+ }
+ ASSERT_TRUE(input.get_partial_shape()[axis].is_dynamic());
+ ++matched_ports;
+ }
+ ASSERT_EQ(matched_ports, 2u);
+
+ ASSERT_EQ(gqa_compiled_model->get_property(ov::intel_npu::npuw::llm::max_context_len.name()), max_context_len);
+ ASSERT_EQ(gqa_compiled_model->get_property(ov::intel_npu::npuw::llm::swa_len.name()), sliding_window_length);
+ ASSERT_EQ(gqa_compiled_model->get_property(ov::intel_npu::npuw::llm::swa_headroom.name()),
+ sliding_window_headroom);
+ }
+
std::unique_ptr<ov::npuw::GQACompiledModel> create_compiled_model(const std::shared_ptr<ov::Model>& model,
const ov::AnyMap& extra_props,
RecordingFactory& recorder) const {
@@ -1576,11 +1641,11 @@ private:
class FakeInnerCompiledModel final : public ov::npuw::ICompiledModel {
public:
- FakeInnerCompiledModel(const std::shared_ptr<ov::Model>& model,
- const std::shared_ptr<const ov::IPlugin>& plugin,
- std::shared_ptr<FakeInnerRequestState> state)
- : ov::npuw::ICompiledModel(model, plugin),
- m_state(std::move(state)) {}
+ FakeInnerCompiledModel(const std::shared_ptr<ov::Model>& model,
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ std::shared_ptr<FakeInnerRequestState> state)
+ : ov::npuw::ICompiledModel(model, plugin),
+ m_state(std::move(state)) {}
void export_model(std::ostream&) const override {}
std::shared_ptr<const ov::Model> get_runtime_model() const override {
return {};
@@ -1659,9 +1724,10 @@ TEST_F(GQACompiledModelTest, SharedCallerKvBuffersDoNotAllocateInnerKvTensors) {
auto tensor = make_f16_kv_cache_tensor(shape, 1.f);
request->set_tensor(input, tensor);
const auto output_name = name == "past_keys_0" ? "present_keys_0" : "present_values_0";
- const auto output = std::find_if(compiled->outputs().begin(), compiled->outputs().end(), [&](const auto& port) {
- return port.get_node()->get_friendly_name() == output_name;
- });
+ const auto output =
+ std::find_if(compiled->outputs().begin(), compiled->outputs().end(), [&](const auto& port) {
+ return port.get_node()->get_friendly_name() == output_name;
+ });
ASSERT_NE(output, compiled->outputs().end());
request->set_tensor(*output, tensor);
EXPECT_EQ(state->tensors.at(name)._ptr, tensor._ptr);
@@ -1705,7 +1771,7 @@ TEST_F(GQACompiledModelTest, PaddingBufferIsLazyZeroedAndReleasedWhenReplaced) {
EXPECT_FLOAT_EQ(static_cast<float>(data[index]), 0.f);
} else {
const auto source_index = axis == 2 ? (index / (16 * row_size)) * 4 * row_size + index % (16 * row_size)
- : (index / 16) * 4 + index % 16;
+ : (index / 16) * 4 + index % 16;
EXPECT_FLOAT_EQ(static_cast<float>(data[index]), static_cast<float>(source_data[source_index]));
}
}
@@ -1950,20 +2016,16 @@ TEST(GQACompiledModelSerializationTest, RoundTripsOuterModelPortsAndDynamicAxisM
const std::unordered_map<std::string, size_t> axis_map{{"past_keys_0", 2u}, {"past_values_0", 2u}};
std::stringstream buffer(std::ios::in | std::ios::out | std::ios::binary);
- ov::npuw::GQACompiledModel::write_port_list(buffer, outer_inputs);
- ov::npuw::GQACompiledModel::write_port_list(buffer, outer_outputs);
- {
- auto writer = ov::npuw::orc::Stream::writer(buffer);
- writer & axis_map;
- }
-
- ov::ParameterVector read_parameters = ov::npuw::GQACompiledModel::read_input_port_list(buffer);
- ov::NodeVector read_results = ov::npuw::GQACompiledModel::read_output_port_list(buffer);
+ auto writer = ov::npuw::orc::Stream::writer(buffer);
+ ov::npuw::GQACompiledModel::write_port_list(writer, outer_inputs);
+ ov::npuw::GQACompiledModel::write_port_list(writer, outer_outputs);
+ writer & axis_map;
+
+ auto reader = ov::npuw::orc::Stream::reader(buffer);
+ ov::ParameterVector read_parameters = ov::npuw::GQACompiledModel::read_input_port_list(reader);
+ ov::NodeVector read_results = ov::npuw::GQACompiledModel::read_output_port_list(reader);
std::unordered_map<std::string, size_t> read_axis_map;
- {
- auto reader = ov::npuw::orc::Stream::reader(buffer);
- reader & read_axis_map;
- }
+ reader & read_axis_map;
ASSERT_EQ(read_parameters.size(), outer_inputs.size());
ASSERT_EQ(read_results.size(), outer_outputs.size());
@@ -2013,8 +2075,10 @@ TEST(GQACompiledModelSerializationTest, RoundTripsTensorNamesSeparatelyFromFrien
std::vector<ov::Output<const ov::Node>> ports{param->output(0)};
std::stringstream buffer(std::ios::in | std::ios::out | std::ios::binary);
- ov::npuw::GQACompiledModel::write_port_list(buffer, ports);
- auto read_back = ov::npuw::GQACompiledModel::read_input_port_list(buffer);
+ auto writer = ov::npuw::orc::Stream::writer(buffer);
+ ov::npuw::GQACompiledModel::write_port_list(writer, ports);
+ auto reader = ov::npuw::orc::Stream::reader(buffer);
+ auto read_back = ov::npuw::GQACompiledModel::read_input_port_list(reader);
ASSERT_EQ(read_back.size(), 1u);
EXPECT_EQ(read_back[0]->get_friendly_name(), "past_keys_0");
@@ -2067,8 +2131,10 @@ TEST(GQACompiledModelSerializationTest, RoundTripsBoundedDynamicDimensionAndNonF
std::vector<ov::Output<const ov::Node>> ports{param->output(0)};
std::stringstream buffer(std::ios::in | std::ios::out | std::ios::binary);
- ov::npuw::GQACompiledModel::write_port_list(buffer, ports);
- auto read_back = ov::npuw::GQACompiledModel::read_input_port_list(buffer);
+ auto writer = ov::npuw::orc::Stream::writer(buffer);
+ ov::npuw::GQACompiledModel::write_port_list(writer, ports);
+ auto reader = ov::npuw::orc::Stream::reader(buffer);
+ auto read_back = ov::npuw::GQACompiledModel::read_input_port_list(reader);
ASSERT_EQ(read_back.size(), 1u);
EXPECT_EQ(read_back[0]->get_friendly_name(), "attention_bias");
@@ -2408,42 +2474,220 @@ TEST(GQACompiledModelPresentToPastParamNamesTest, MapsDotStyleNamesNotMatchingSu
EXPECT_EQ(map.at("present.0.value"), "past_key_values.0.value");
}
-TEST_F(GQACompiledModelTest, ImportRejectsExcessiveSerializationVersionLengthBeforeAllocation) {
- const auto header = make_gqa_header(std::numeric_limits<std::size_t>::max(), NPUW_SERIALIZATION_VERSION);
- std::istringstream stream(header);
+TEST_F(GQACompiledModelTest, ImportRejectsNonORCGQACompiledModelWithPrettyMessage) {
+ std::stringstream stream;
+ ov::npuw::s11n::write(stream, NPUW_SERIALIZATION_INDICATOR);
+ ov::npuw::s11n::write(stream, NPUW_GQA_COMPILED_MODEL_INDICATOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MAJOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_MINOR);
+ ov::npuw::s11n::write(stream, OPENVINO_VERSION_PATCH);
+ ov::npuw::s11n::write(stream, std::string(NPUW_SERIALIZATION_VERSION));
- OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
- ov::Exception,
- "the maximum is 64");
+ OV_EXPECT_THROW_HAS_SUBSTRING(
+ ov::npuw::ICompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "Legacy flat NPUW GQACompiledModel blobs are no longer supported. Re-export the model with "
+ "the current ORC serializer.");
}
-TEST_F(GQACompiledModelTest, ImportRejectsSerializationVersionLongerThanCap) {
- const std::string version(65u, 'x');
- const auto header = make_gqa_header(version.size(), version);
- std::istringstream stream(header);
+TEST_F(GQACompiledModelTest, ImportRejectsNonGQAORCSchema) {
+ std::stringstream stream;
+ ov::npuw::orc::write_file_header(stream, ov::npuw::orc::schema_npuw::NPUW_ORC_PARTITIONED_SCHEMA);
+ ov::npuw::orc::with_section(stream,
+ ov::npuw::GQACompiledModel::kOrcType,
+ ov::npuw::GQACompiledModel::kOrcVersion,
+ 0u,
+ [&] {});
OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
ov::Exception,
- "serialization version string is 65 bytes, the maximum is 64");
+ "Unsupported ORC schema for NPUW GQACompiledModel");
}
-TEST_F(GQACompiledModelTest, ImportReportsVersionMismatchForVersionAtCap) {
- const std::string version(64u, 'x');
- const auto header = make_gqa_header(version.size(), version);
- std::istringstream stream(header);
+TEST_F(GQACompiledModelTest, ImportRejectsNewerORCGQAVersion) {
+ constexpr ov::npuw::orc::Version newer_gqa_version = ov::npuw::GQACompiledModel::kOrcVersion + 1;
+
+ std::stringstream stream;
+ ov::npuw::orc::write_file_header(stream, ov::npuw::orc::schema_npuw::NPUW_ORC_GQA_SCHEMA);
+ ov::npuw::orc::with_section(stream, ov::npuw::GQACompiledModel::kOrcType, newer_gqa_version, 0u, [&] {});
OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
ov::Exception,
- "NPUW serialized by version " + version);
-}
+ "GQA blob was produced by a newer NPUW");
+}
+
+TEST_F(GQACompiledModelTest, ImportRejectsORCGQAMetaMissingOrNonLeafSection) {
+ const std::vector<std::function<void(std::ostream&)>> test_section_lambdas = {
+ [](std::ostream& stream) {
+ // It is expected for GQACompiledModel to have an inner ov::npuw::CompiledModel
+ // section, but not before META section
+ ov::npuw::orc::with_section(stream,
+ ov::npuw::CompiledModel::kOrcType,
+ ov::npuw::CompiledModel::kOrcVersion,
+ 0u,
+ [] {});
+ },
+ [](std::ostream& stream) {
+ // Even if it is a nested section of META type, import still rejects it because it is not a LEAF section
+ ov::npuw::orc::with_section(stream, ov::npuw::orc::META_SECTION_TYPE, 0u, 0u, [] {});
+ }};
+ for (const auto& test_lambda_case : test_section_lambdas) {
+ std::stringstream stream;
+ ov::npuw::orc::write_file_header(stream, ov::npuw::orc::schema_npuw::NPUW_ORC_GQA_SCHEMA);
+ ov::npuw::orc::with_section(stream,
+ ov::npuw::GQACompiledModel::kOrcType,
+ ov::npuw::GQACompiledModel::kOrcVersion,
+ 0u,
+ [&] {
+ test_lambda_case(stream);
+ });
+
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "Expected ORC NPUW metadata section, got type");
+ }
+}
+
+TEST_F(GQACompiledModelTest, ImportRejectsORCGQARootOverrunningStream) {
+ std::stringstream stream;
+ std::streampos pos;
+ ov::npuw::orc::write_file_header(stream, ov::npuw::orc::schema_npuw::NPUW_ORC_GQA_SCHEMA);
+ pos = stream.tellp();
+ ov::npuw::orc::with_section(stream,
+ ov::npuw::GQACompiledModel::kOrcType,
+ ov::npuw::GQACompiledModel::kOrcVersion,
+ 0u,
+ [] {});
+ uint64_t total_wrote_bytes = stream.tellp();
+ stream.seekp(pos + static_cast<std::stringstream::pos_type>(sizeof(ov::npuw::orc::TypeId)) +
+ static_cast<std::stringstream::pos_type>(sizeof(ov::npuw::orc::Version)) +
+ static_cast<std::stringstream::pos_type>(sizeof(ov::npuw::orc::SectionFlags)),
+ std::ios::beg);
+ uint64_t altered_meta_size = total_wrote_bytes + static_cast<std::stringstream::pos_type>(1);
+ stream.write(reinterpret_cast<const char*>(&altered_meta_size), sizeof(altered_meta_size));
-TEST_F(GQACompiledModelTest, ImportReportsVersionMismatchForDifferentVersionLength) {
- const auto header = make_gqa_header(3u, "0.3");
- std::istringstream stream(header);
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "GQA root section claims");
+}
+
+TEST_F(GQACompiledModelTest, ImportRejectsORCGQAMetaOverrunningRoot) {
+ std::stringstream stream;
+ std::streampos pos;
+ ov::npuw::orc::write_file_header(stream, ov::npuw::orc::schema_npuw::NPUW_ORC_GQA_SCHEMA);
+ ov::npuw::orc::with_section(stream,
+ ov::npuw::GQACompiledModel::kOrcType,
+ ov::npuw::GQACompiledModel::kOrcVersion,
+ 0u,
+ [&] {
+ pos = stream.tellp();
+ ov::npuw::orc::with_leaf_section(stream, ov::npuw::orc::META_SECTION_TYPE, 0u, [&] {
+ std::fill_n(std::ostreambuf_iterator<char>(stream), 10, 0);
+ });
+ });
+ stream.seekp(pos + static_cast<std::stringstream::pos_type>(sizeof(ov::npuw::orc::TypeId)) +
+ static_cast<std::stringstream::pos_type>(sizeof(ov::npuw::orc::Version)) +
+ static_cast<std::stringstream::pos_type>(sizeof(ov::npuw::orc::SectionFlags)),
+ std::ios::beg);
+ uint64_t altered_meta_size = stream.tellp() + static_cast<std::stringstream::pos_type>(1);
+ stream.write(reinterpret_cast<const char*>(&altered_meta_size), sizeof(altered_meta_size));
OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
ov::Exception,
- "NPUW serialized by version 0.3");
+ "GQA META section exceeds GQA root section");
+}
+
+TEST_F(GQACompiledModelTest, GQAExportImportRoundTrip) {
+ const size_t axis = 2;
+ auto model = build_gqa_model_with_dynamic_kv_cache(axis);
+ const auto& model_inputs = std::const_pointer_cast<const ov::Model>(model)->inputs();
+ const auto& model_outputs = std::const_pointer_cast<const ov::Model>(model)->outputs();
+
+ const uint32_t max_context_len = 4096u;
+ const uint32_t sliding_window_length = 8u;
+ const uint32_t sliding_window_headroom = 512u;
+ auto test_props = base_props();
+ merge_props(test_props,
+ ov::AnyMap{ov::intel_npu::npuw::llm::max_context_len(max_context_len),
+ ov::intel_npu::npuw::llm::swa_len(sliding_window_length),
+ ov::intel_npu::npuw::llm::swa_headroom(sliding_window_headroom)});
+
+ auto mock_core = std::make_shared<testing::NiceMock<ov::MockICore>>();
+ auto mock_compiled_model = std::make_shared<testing::NiceMock<ov::MockICompiledModel>>(model, m_plugin);
+ m_plugin->set_core(mock_core);
+ setup_mocks(model_inputs, model_outputs, mock_core, mock_compiled_model);
+
+ auto compiled = std::make_unique<ov::npuw::GQACompiledModel>(model, m_plugin, test_props);
+ std::stringstream stream;
+ ASSERT_NO_THROW(compiled->export_model(stream));
+
+ std::shared_ptr<ov::npuw::GQACompiledModel> imported;
+ EXPECT_CALL(
+ *mock_core,
+ import_model(testing::An<std::istream&>(), testing::An<const std::string&>(), testing::An<const ov::AnyMap&>()))
+ .Times(testing::AtLeast(1))
+ .WillRepeatedly(testing::Return(mock_compiled_model));
+ ASSERT_NO_THROW(imported = std::dynamic_pointer_cast<ov::npuw::GQACompiledModel>(
+ ov::npuw::ICompiledModel::import_model(stream, m_plugin, {})));
+ ASSERT_NE(imported, nullptr);
+ ASSERT_EQ(stream.peek(), EOF);
+ ASSERT_NO_FATAL_FAILURE(validate_gqa_roundtrip(axis,
+ model_inputs,
+ model_outputs,
+ max_context_len,
+ sliding_window_length,
+ sliding_window_headroom,
+ imported));
+}
+
+TEST_F(GQACompiledModelTest, GQAExportImportRoundTripWithEncryption) {
+ const size_t axis = 2;
+ auto model = build_gqa_model_with_dynamic_kv_cache(axis);
+ const auto& model_inputs = std::const_pointer_cast<const ov::Model>(model)->inputs();
+ const auto& model_outputs = std::const_pointer_cast<const ov::Model>(model)->outputs();
+
+ const uint32_t max_context_len = 4096u;
+ const uint32_t sliding_window_length = 8u;
+ const uint32_t sliding_window_headroom = 512u;
+ auto test_props = base_props();
+ merge_props(
+ test_props,
+ ov::AnyMap{ov::intel_npu::npuw::llm::max_context_len(max_context_len),
+ ov::intel_npu::npuw::llm::swa_len(sliding_window_length),
+ ov::intel_npu::npuw::llm::swa_headroom(sliding_window_headroom),
+ ov::cache_encryption_callbacks(ov::EncryptionCallbacks{ov::util::codec_xor, ov::util::codec_xor})});
+
+ auto mock_core = std::make_shared<testing::NiceMock<ov::MockICore>>();
+ auto mock_compiled_model = std::make_shared<testing::NiceMock<ov::MockICompiledModel>>(model, m_plugin);
+ m_plugin->set_core(mock_core);
+ setup_mocks(model_inputs, model_outputs, mock_core, mock_compiled_model);
+
+ auto compiled = std::make_unique<ov::npuw::GQACompiledModel>(model, m_plugin, test_props);
+ std::stringstream stream;
+ ASSERT_NO_THROW(compiled->export_model(stream));
+
+ std::shared_ptr<ov::npuw::GQACompiledModel> imported;
+ OV_EXPECT_THROW_HAS_SUBSTRING(ov::npuw::GQACompiledModel::import_model(stream, m_plugin, {}),
+ ov::Exception,
+ "Blob is encrypted, but no decryption callback was provided");
+ stream.clear();
+ stream.seekg(0);
+ EXPECT_CALL(
+ *mock_core,
+ import_model(testing::An<std::istream&>(), testing::An<const std::string&>(), testing::An<const ov::AnyMap&>()))
+ .Times(testing::AtLeast(1))
+ .WillRepeatedly(testing::Return(mock_compiled_model));
+ ASSERT_NO_THROW(imported = std::dynamic_pointer_cast<ov::npuw::GQACompiledModel>(
+ ov::npuw::ICompiledModel::import_model(stream, m_plugin, test_props)));
+ ASSERT_NE(imported, nullptr);
+ ASSERT_EQ(stream.peek(), EOF);
+ ASSERT_NO_FATAL_FAILURE(validate_gqa_roundtrip(axis,
+ model_inputs,
+ model_outputs,
+ max_context_len,
+ sliding_window_length,
+ sliding_window_headroom,
+ imported));
}
} // namespace