Skip to content

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

[NPUW] Validate imported NPUW ORC routing metadata (#37535)

Alexis Menard786821778aea
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 7f5086026c1..a93e4d0934d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -904,6 +904,186 @@ bool ov::npuw::CompiledModel::should_use_quantized_host_gather(const std::shared
return false;
}
+namespace {
+// Routing checks that need no compiled model, so they can also run for a function call desc
+// (whose compiled model lives in the function body and is not resolvable at codec time).
+void validate_routing_self_consistency(const ov::npuw::Subgraph::Gather& host_gather,
+ const ov::npuw::Subgraph::QuantUnpackGather& quant_unpack_gather,
+ std::size_t param_base,
+ std::size_t closure_size) {
+ auto require_sentinel_or_non_negative = [](int64_t idx, const char* field_name) {
+ OPENVINO_ASSERT(idx >= -1,
+ "NPUW routing: \"",
+ field_name,
+ "\" value ",
+ idx,
+ " is negative and not the -1 sentinel");
+ };
+ require_sentinel_or_non_negative(host_gather.dst_idx, "host_gather.dst_idx");
+ require_sentinel_or_non_negative(host_gather.src_idx, "host_gather.src_idx");
+ require_sentinel_or_non_negative(host_gather.idx_idx, "host_gather.idx_idx");
+ require_sentinel_or_non_negative(quant_unpack_gather.dst_idx, "quant_unpack_gather.dst_idx");
+ require_sentinel_or_non_negative(quant_unpack_gather.src_w_idx, "quant_unpack_gather.src_w_idx");
+ require_sentinel_or_non_negative(quant_unpack_gather.src_z_idx, "quant_unpack_gather.src_z_idx");
+ require_sentinel_or_non_negative(quant_unpack_gather.src_s_idx, "quant_unpack_gather.src_s_idx");
+ require_sentinel_or_non_negative(quant_unpack_gather.idx_idx, "quant_unpack_gather.idx_idx");
+
+ // Validate consistency of host_gather indices.
+ if (host_gather.dst_idx != -1) {
+ OPENVINO_ASSERT(host_gather.src_idx != -1,
+ "NPUW routing: \"host_gather.src_idx\" cannot be -1 when \"host_gather.dst_idx\" is active");
+ OPENVINO_ASSERT(host_gather.idx_idx != -1,
+ "NPUW routing: \"host_gather.idx_idx\" cannot be -1 when \"host_gather.dst_idx\" is active");
+ } else {
+ OPENVINO_ASSERT(host_gather.src_idx == -1,
+ "NPUW routing: \"host_gather.src_idx\" must be -1 when \"host_gather.dst_idx\" is -1");
+ OPENVINO_ASSERT(host_gather.idx_idx == -1,
+ "NPUW routing: \"host_gather.idx_idx\" must be -1 when \"host_gather.dst_idx\" is -1");
+ }
+
+ // Validate consistency of quant_unpack_gather indices.
+ if (quant_unpack_gather.dst_idx != -1) {
+ OPENVINO_ASSERT(quant_unpack_gather.idx_idx != -1,
+ "NPUW routing: \"quant_unpack_gather.idx_idx\" cannot be -1 when "
+ "\"quant_unpack_gather.dst_idx\" is active");
+ OPENVINO_ASSERT(quant_unpack_gather.src_w_idx != -1,
+ "NPUW routing: \"quant_unpack_gather.src_w_idx\" cannot be -1 when "
+ "\"quant_unpack_gather.dst_idx\" is active");
+ OPENVINO_ASSERT(quant_unpack_gather.src_s_idx != -1,
+ "NPUW routing: \"quant_unpack_gather.src_s_idx\" cannot be -1 when "
+ "\"quant_unpack_gather.dst_idx\" is active");
+ } else {
+ OPENVINO_ASSERT(
+ quant_unpack_gather.idx_idx == -1,
+ "NPUW routing: \"quant_unpack_gather.idx_idx\" must be -1 when \"quant_unpack_gather.dst_idx\" is -1");
+ OPENVINO_ASSERT(
+ quant_unpack_gather.src_w_idx == -1,
+ "NPUW routing: \"quant_unpack_gather.src_w_idx\" must be -1 when \"quant_unpack_gather.dst_idx\" is -1");
+ OPENVINO_ASSERT(
+ quant_unpack_gather.src_s_idx == -1,
+ "NPUW routing: \"quant_unpack_gather.src_s_idx\" must be -1 when \"quant_unpack_gather.dst_idx\" is -1");
+ OPENVINO_ASSERT(
+ quant_unpack_gather.src_z_idx == -1,
+ "NPUW routing: \"quant_unpack_gather.src_z_idx\" must be -1 when \"quant_unpack_gather.dst_idx\" is -1");
+ }
+
+ // host_gather.src_idx is an index into closure (not inputs): validate against closure_size.
+ if (host_gather.src_idx != -1) {
+ OPENVINO_ASSERT(static_cast<std::size_t>(host_gather.src_idx) >= param_base,
+ "NPUW routing: \"host_gather.src_idx\" (",
+ host_gather.src_idx,
+ ") is less than param_base (",
+ param_base,
+ ")");
+ const std::size_t closure_idx = static_cast<std::size_t>(host_gather.src_idx) - param_base;
+ OPENVINO_ASSERT(closure_idx < closure_size,
+ "NPUW routing: host_gather.src_idx - param_base (",
+ closure_idx,
+ ") is out of closure bounds [0, ",
+ closure_size,
+ ")");
+ }
+}
+} // anonymous namespace
+
+void ov::npuw::validate_submodel_indices(const Subgraph::Gather& host_gather,
+ const Subgraph::QuantUnpackGather& quant_unpack_gather,
+ std::size_t param_base,
+ std::size_t closure_size,
+ bool has_compiled_model,
+ std::size_t n_model_inputs) {
+ if (!has_compiled_model) {
+ // No compiled model was loaded: all routing indices must be the disabled sentinel (-1).
+ auto require_disabled = [](int64_t idx, const char* field_name) {
+ OPENVINO_ASSERT(idx == -1,
+ "NPUW routing: \"",
+ field_name,
+ "\" = ",
+ idx,
+ " but no compiled model was loaded for this subgraph");
+ };
+ require_disabled(host_gather.dst_idx, "host_gather.dst_idx");
+ require_disabled(host_gather.src_idx, "host_gather.src_idx");
+ require_disabled(host_gather.idx_idx, "host_gather.idx_idx");
+ require_disabled(quant_unpack_gather.dst_idx, "quant_unpack_gather.dst_idx");
+ require_disabled(quant_unpack_gather.src_w_idx, "quant_unpack_gather.src_w_idx");
+ require_disabled(quant_unpack_gather.src_z_idx, "quant_unpack_gather.src_z_idx");
+ require_disabled(quant_unpack_gather.src_s_idx, "quant_unpack_gather.src_s_idx");
+ require_disabled(quant_unpack_gather.idx_idx, "quant_unpack_gather.idx_idx");
+ return;
+ }
+
+ validate_routing_self_consistency(host_gather, quant_unpack_gather, param_base, closure_size);
+
+ // Validate indices that directly address compiled_model->inputs().
+ auto check_input_idx = [&](int64_t idx, const char* field_name) {
+ OPENVINO_ASSERT(idx == -1 || (idx >= 0 && static_cast<std::size_t>(idx) < n_model_inputs),
+ "NPUW routing: \"",
+ field_name,
+ "\" value ",
+ idx,
+ " is out of range [0, ",
+ n_model_inputs,
+ ")");
+ };
+ check_input_idx(host_gather.dst_idx, "host_gather.dst_idx");
+ check_input_idx(host_gather.idx_idx, "host_gather.idx_idx");
+ check_input_idx(quant_unpack_gather.dst_idx, "quant_unpack_gather.dst_idx");
+ check_input_idx(quant_unpack_gather.src_w_idx, "quant_unpack_gather.src_w_idx");
+ check_input_idx(quant_unpack_gather.src_z_idx, "quant_unpack_gather.src_z_idx");
+ check_input_idx(quant_unpack_gather.src_s_idx, "quant_unpack_gather.src_s_idx");
+ check_input_idx(quant_unpack_gather.idx_idx, "quant_unpack_gather.idx_idx");
+
+ // param_base + closure_size must not overflow compiled_model->inputs() (used in unpack_closure and funcall
+ // prologue).
+ OPENVINO_ASSERT(param_base <= n_model_inputs && closure_size <= n_model_inputs - param_base,
+ "NPUW routing: param_base (",
+ param_base,
+ ") + closure_size (",
+ closure_size,
+ ") exceeds n_model_inputs (",
+ n_model_inputs,
+ ")");
+}
+
+void ov::npuw::CompiledModel::validate_submodels(const std::vector<CompiledModelDesc>& submodels) {
+ for (std::size_t idx = 0; idx < submodels.size(); ++idx) {
+ const auto& subm = submodels[idx];
+ ov::SoPtr<ov::ICompiledModel> effective_compiled_model = subm.compiled_model;
+
+ if (subm.replaced_by.has_value()) {
+ const std::size_t target_idx = subm.replaced_by.value();
+ OPENVINO_ASSERT(target_idx < submodels.size(),
+ "NPUW routing: submodel ",
+ idx,
+ " replaced_by index ",
+ target_idx,
+ " is out of range [0, ",
+ submodels.size(),
+ ")");
+ effective_compiled_model = submodels[target_idx].compiled_model;
+ OPENVINO_ASSERT(effective_compiled_model,
+ "NPUW routing: submodel ",
+ idx,
+ " replaced_by target ",
+ target_idx,
+ " has no compiled model");
+ }
+
+ const auto& closure_desc = subm.closure.get();
+ const std::size_t closure_size = closure_desc.closure.size();
+ const bool has_compiled_model = static_cast<bool>(effective_compiled_model);
+ const std::size_t n_model_inputs = has_compiled_model ? effective_compiled_model->inputs().size() : 0u;
+
+ validate_submodel_indices(subm.host_gather,
+ subm.quant_unpack_gather,
+ subm.param_base,
+ closure_size,
+ has_compiled_model,
+ n_model_inputs);
+ }
+}
+
void ov::npuw::CompiledModel::CompiledModelDesc::serialize(ov::npuw::s11n::Stream& stream,
const ov::npuw::s11n::WeightsContext& ctx,
std::optional<std::size_t> orc_device_index,
@@ -955,6 +1135,9 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(ov::npuw::s11n::Strea
host_gather.idx_idx & quant_unpack_gather.dst_idx & quant_unpack_gather.src_w_idx &
quant_unpack_gather.src_z_idx & quant_unpack_gather.src_s_idx & quant_unpack_gather.idx_idx & spatial;
+ // Closure size is not yet known at this point; full index bounds validation (including
+ // src_idx vs closure) is deferred to after closure_size is read – see calls below.
+
// Function calls share pipeline.context with their function body at runtime.
// There is no need to serialize the compiled moe/attn state for each call –
// doing so would re-import NPU blobs for every repeated layer (one per call),
@@ -994,12 +1177,28 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(ov::npuw::s11n::Strea
stream & closure_desc.is_remote & closure_desc.closure_uid;
+ // A funcall's own compiled_model lives in the function body and cannot be resolved here, so only
+ // the model-independent checks run for it - validate_submodels() covers the rest once all descs are read.
+ auto validate_routing_indices = [&](std::size_t closure_size) {
+ if (is_fcall) {
+ validate_routing_self_consistency(host_gather, quant_unpack_gather, param_base, closure_size);
+ } else {
+ ov::npuw::validate_submodel_indices(host_gather,
+ quant_unpack_gather,
+ param_base,
+ closure_size,
+ static_cast<bool>(compiled_model),
+ compiled_model ? compiled_model->inputs().size() : 0u);
+ }
+ };
+
if (ctx.is_weightless) {
serialize_weightless(stream, scales, ctx);
serialize_weightless(stream, zerops, ctx);
std::size_t closure_size = closure_desc.closure.size();
stream & closure_size;
+ validate_routing_indices(closure_size);
if (stream.input()) {
validate_closure_metadata_sizes(closure_size,
closure_size,
@@ -1054,6 +1253,7 @@ void ov::npuw::CompiledModel::CompiledModelDesc::serialize(ov::npuw::s11n::Strea
std::size_t closure_size = closure_desc.closure.size();
stream & closure_size;
+ validate_routing_indices(closure_size);
if (stream.input()) {
validate_closure_metadata_sizes(closure_size,
closure_size,
@@ -1162,6 +1362,8 @@ void ov::npuw::CompiledModel::serialize_orc_container(std::ostream& stream,
const ov::npuw::s11n::BF16Cache* bf16_consts) const {
using namespace ov::npuw;
+ validate_submodels(m_compiled_submodels);
+
if (m_cfg.get<::intel_npu::NPUW_ENSURE_COMPATIBILITY>()) {
if (encrypt) {
OPENVINO_THROW("Cannot produce ORC-compatible blob: encrypted export is not yet supported in "
@@ -1406,6 +1608,7 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize_or
OPENVINO_THROW("Missing ORC weights bank container");
}
+ validate_submodels(compiled->m_compiled_submodels);
validate_import_routing_tables(compiled);
compiled->implement_properties();
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
index d7fe7bb4553..fe638604dd1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -71,6 +71,7 @@ public:
// Forward declarations
class InferRequest;
+struct CompiledModelDescTestAccessor;
namespace v1::subgraphs {
class Context;
@@ -136,6 +137,7 @@ public:
private:
// FIXME: This class has many friends..
+ friend struct CompiledModelDescTestAccessor;
friend class IBaseInferRequest;
friend class JustInferRequest;
friend class UnfoldInferRequest;
@@ -321,6 +323,47 @@ private:
ov::npuw::s11n::WeightsContext m_import_weights_ctx;
std::shared_future<void> m_eval_future;
+
+ static void validate_submodels(const std::vector<CompiledModelDesc>& submodels);
+};
+
+struct CompiledModelDescTestAccessor {
+ using SubmodelDesc = CompiledModel::CompiledModelDesc;
+ using SubmodelVec = std::vector<CompiledModel::CompiledModelDesc>;
+
+ static SubmodelDesc make() {
+ return {};
+ }
+ static auto& compiled_model(SubmodelDesc& desc) {
+ return desc.compiled_model;
+ }
+ static auto& host_gather(SubmodelDesc& desc) {
+ return desc.host_gather;
+ }
+ static auto& quant_unpack_gather(SubmodelDesc& desc) {
+ return desc.quant_unpack_gather;
+ }
+ static auto& param_base(SubmodelDesc& desc) {
+ return desc.param_base;
+ }
+ static auto& closure(SubmodelDesc& desc) {
+ return desc.closure;
+ }
+ static void validate_submodels(const SubmodelVec& submodels) {
+ CompiledModel::validate_submodels(submodels);
+ }
};
+
+// Validates that submodel routing indices are in bounds. Runs on both export and import.
+// Throws ov::Exception when an index would cause an out-of-bounds vector access
+// in bind_global_params / unpack_closure at inference time.
+// has_compiled_model: true when a compiled_model was loaded (enables input-bounds checks).
+void validate_submodel_indices(const Subgraph::Gather& host_gather,
+ const Subgraph::QuantUnpackGather& quant_unpack_gather,
+ std::size_t param_base,
+ std::size_t closure_size,
+ bool has_compiled_model,
+ std::size_t n_model_inputs);
+
} // namespace npuw
} // namespace ov
diff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
index 327216445d7..1dba79b7304 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp
@@ -20,6 +20,7 @@
#include "intel_npu/config/config.hpp"
#include "intel_npu/config/npuw.hpp"
#include "lazy_tensor.hpp"
+#include "llm_test_helpers.hpp"
#include "model_builder.hpp"
#include "moe_transformations/moe_transformation.hpp"
#include "openvino/core/memory_util.hpp"
@@ -34,9 +35,72 @@
#include "weights_bank.hpp"
using ov::test::npuw::ModelBuilder;
+using Gather = ov::npuw::Subgraph::Gather;
+using QuantUnpackGather = ov::npuw::Subgraph::QuantUnpackGather;
+using Stream = ov::npuw::s11n::Stream;
+using ov::test::npuw::MockSubCompiledModel;
+using ov::test::npuw::NullPlugin;
namespace {
+std::shared_ptr<ov::Model> make_validation_model(std::size_t n_inputs) {
+ ov::ParameterVector parameters;
+ for (std::size_t i = 0; i < n_inputs; ++i) {
+ parameters.push_back(std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1}));
+ }
+ return std::make_shared<ov::Model>(ov::ResultVector{}, parameters);
+}
+
+void expect_serialize_valid(const Gather& hg,
+ const QuantUnpackGather& qug,
+ std::size_t param_base,
+ std::size_t closure_size,
+ std::size_t n_model_inputs) {
+ auto writer = ov::npuw::CompiledModelDescTestAccessor::make();
+ auto plugin = std::make_shared<NullPlugin>();
+ if (n_model_inputs != 0) {
+ auto model = make_validation_model(n_model_inputs);
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(writer) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+ }
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(writer) = hg;
+ ov::npuw::CompiledModelDescTestAccessor::quant_unpack_gather(writer) = qug;
+ ov::npuw::CompiledModelDescTestAccessor::param_base(writer) = param_base;
+ auto& closure = ov::npuw::CompiledModelDescTestAccessor::closure(writer);
+ closure.get().closure.resize(closure_size);
+ closure.get().closure_uid.resize(closure_size, -1);
+ closure.get().is_remote.resize(closure_size, false);
+
+ std::stringstream ss;
+ auto stream = Stream::writer(ss);
+ writer.serialize(stream, {});
+}
+
+void expect_serialize_throws(const Gather& hg,
+ const QuantUnpackGather& qug,
+ std::size_t param_base,
+ std::size_t closure_size,
+ std::size_t n_model_inputs) {
+ auto writer = ov::npuw::CompiledModelDescTestAccessor::make();
+ auto plugin = std::make_shared<NullPlugin>();
+ if (n_model_inputs != 0) {
+ auto model = make_validation_model(n_model_inputs);
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(writer) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+ }
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(writer) = hg;
+ ov::npuw::CompiledModelDescTestAccessor::quant_unpack_gather(writer) = qug;
+ ov::npuw::CompiledModelDescTestAccessor::param_base(writer) = param_base;
+ auto& closure = ov::npuw::CompiledModelDescTestAccessor::closure(writer);
+ closure.get().closure.resize(closure_size);
+ closure.get().closure_uid.resize(closure_size, -1);
+ closure.get().is_remote.resize(closure_size, false);
+
+ std::stringstream ss;
+ auto stream = Stream::writer(ss);
+ writer.serialize(stream, {});
+}
+
class SerializationNullPlugin final : public ov::IPlugin {
public:
std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,
@@ -78,6 +142,7 @@ public:
return {};
}
};
+
constexpr char kExpectedOobIndexMessage[] = "CPU closure index is out of range";
constexpr char kExpectedClosureUidSizeMessage[] = "closure_uid size does not match closure size";
constexpr char kExpectedIsRemoteSizeMessage[] = "is_remote size does not match closure size";
@@ -1561,6 +1626,394 @@ TEST(SerializationTest, OVTypes_WeightsBank_cpu_roundtrip) {
expect_tensors_equal(var.get(uid1, "CPU"), res.get(uid1, "CPU"));
}
+TEST(SerializationTest, AllSentinelsPass) {
+ EXPECT_NO_THROW(expect_serialize_valid({-1, -1, -1}, {-1, -1, -1, -1, -1}, 0, 0, 0));
+}
+
+TEST(SerializationTest, AllSentinelsPassWithNonZeroInputCount) {
+ EXPECT_NO_THROW(expect_serialize_valid({-1, -1, -1}, {-1, -1, -1, -1, -1}, 2, 4, 8));
+}
+
+TEST(SerializationTest, ValidHostGatherIndicesPass) {
+ Gather hg{5, 3, 6};
+ EXPECT_NO_THROW(expect_serialize_valid(hg, {-1, -1, -1, -1, -1}, 2, 4, 8));
+}
+
+TEST(SerializationTest, ValidQuantUnpackGatherIndicesPass) {
+ QuantUnpackGather qug{0, 1, 2, 3, 4};
+ EXPECT_NO_THROW(expect_serialize_valid({-1, -1, -1}, qug, 0, 0, 8));
+}
+
+TEST(SerializationTest, HostGatherDstIdxExactlyAtBoundFails) {
+ Gather hg{8, 5, 0};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherDstIdxFarOOBFails) {
+ Gather hg{1000, 5, 0};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherDstIdxNegativeNonSentinelFails) {
+ Gather hg{-2, 5, 0};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherIdxIdxOOBFails) {
+ Gather hg{0, 5, 8};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherSrcIdxBelowParamBaseFails) {
+ Gather hg{0, 1, 0};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherSrcIdxExactlyAtClosureEndFails) {
+ Gather hg{0, 6, 0};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherSrcIdxFarPastClosureFails) {
+ Gather hg{0, 1000, 0};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherSrcIdxAtLastValidClosureSlotPasses) {
+ Gather hg{0, 5, 0};
+ EXPECT_NO_THROW(expect_serialize_valid(hg, {-1, -1, -1, -1, -1}, 2, 4, 8));
+}
+
+TEST(SerializationTest, HostGatherActiveMissingSrcIdxFails) {
+ Gather hg{0, -1, 1};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherActiveMissingIdxIdxFails) {
+ Gather hg{0, 5, -1};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherInactiveNonSentinelSrcIdxFails) {
+ Gather hg{-1, 5, -1};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, HostGatherInactiveNonSentinelIdxIdxFails) {
+ Gather hg{-1, -1, 1};
+ EXPECT_THROW(expect_serialize_throws(hg, {-1, -1, -1, -1, -1}, 2, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherDstIdxOOBFails) {
+ QuantUnpackGather qug{8, 0, -1, 1, 1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherSrcWIdxOOBFails) {
+ QuantUnpackGather qug{0, 8, -1, 1, 1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherSrcZIdxOOBFails) {
+ QuantUnpackGather qug{0, 1, 8, 1, 1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherSrcSIdxOOBFails) {
+ QuantUnpackGather qug{0, 1, -1, 8, 1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherIdxIdxOOBFails) {
+ QuantUnpackGather qug{0, 1, -1, 1, 8};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherActiveMissingSrcWIdxFails) {
+ QuantUnpackGather qug{0, -1, -1, 1, 1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherActiveMissingSrcSIdxFails) {
+ QuantUnpackGather qug{0, 1, -1, -1, 1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherActiveMissingIdxIdxFails) {
+ QuantUnpackGather qug{0, 1, -1, 1, -1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, QuantUnpackGatherActiveOptionalSrcZIdxPasses) {
+ QuantUnpackGather qug{0, 1, -1, 2, 3};
+ EXPECT_NO_THROW(expect_serialize_valid({-1, -1, -1}, qug, 0, 0, 8));
+}
+
+TEST(SerializationTest, QuantUnpackGatherInactiveNonSentinelSrcWIdxFails) {
+ QuantUnpackGather qug{-1, 1, -1, -1, -1};
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, qug, 0, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, ParamBaseClosureSizeExceedsInputsFails) {
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, {-1, -1, -1, -1, -1}, 6, 4, 8), ov::Exception);
+}
+
+TEST(SerializationTest, ParamBaseClosureSizeOverflowWrapFails) {
+ EXPECT_THROW(
+ expect_serialize_throws({-1, -1, -1}, {-1, -1, -1, -1, -1}, std::numeric_limits<std::size_t>::max(), 2, 8),
+ ov::Exception);
+}
+
+TEST(SerializationTest, ParamBaseClosureSizeExactlyAtBoundPasses) {
+ EXPECT_NO_THROW(expect_serialize_valid({-1, -1, -1}, {-1, -1, -1, -1, -1}, 4, 4, 8));
+}
+
+TEST(SerializationTest, ParamBaseLargerThanInputsFails) {
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, {-1, -1, -1, -1, -1}, 9, 1, 8), ov::Exception);
+}
+
+TEST(SerializationTest, ParamBaseLargerThanInputsZeroClosureFails) {
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, {-1, -1, -1, -1, -1}, 9, 0, 8), ov::Exception);
+}
+
+TEST(SerializationTest, NoCompiledModelAllSentinelsPasses) {
+ EXPECT_NO_THROW(expect_serialize_valid({-1, -1, -1}, {-1, -1, -1, -1, -1}, 0, 0, 0));
+}
+
+TEST(SerializationTest, NoCompiledModelNonSentinelDstIdxFails) {
+ EXPECT_THROW(expect_serialize_throws({0, -1, -1}, {-1, -1, -1, -1, -1}, 0, 0, 0), ov::Exception);
+}
+
+TEST(SerializationTest, NoCompiledModelNonSentinelSrcIdxFails) {
+ EXPECT_THROW(expect_serialize_throws({-1, 0, -1}, {-1, -1, -1, -1, -1}, 0, 0, 0), ov::Exception);
+}
+
+TEST(SerializationTest, NoCompiledModelNonSentinelQuantDstIdxFails) {
+ EXPECT_THROW(expect_serialize_throws({-1, -1, -1}, {0, -1, -1, -1, -1}, 0, 0, 0), ov::Exception);
+}
+
+TEST(SerializationTest, FuncallSubmodelValidHostGatherPasses) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = make_validation_model(8);
+
+ // Submodel 0: function body
+ auto sub0 = ov::npuw::CompiledModelDescTestAccessor::make();
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(sub0) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+
+ // Submodel 1: function call referencing submodel 0
+ auto sub1 = ov::npuw::CompiledModelDescTestAccessor::make();
+ sub1.replaced_by = 0;
+ Gather hg{5, 3, 6};
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(sub1) = hg;
+ ov::npuw::CompiledModelDescTestAccessor::param_base(sub1) = 2;
+ auto& closure1 = ov::npuw::CompiledModelDescTestAccessor::closure(sub1);
+ closure1.get().closure.resize(4);
+ closure1.get().closure_uid.resize(4, -1);
+ closure1.get().is_remote.resize(4, false);
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec submodels;
+ submodels.push_back(std::move(sub0));
+ submodels.push_back(std::move(sub1));
+
+ std::stringstream ss;
+ auto stream = Stream::writer(ss);
+ EXPECT_NO_THROW(submodels[0].serialize(stream, {}));
+ EXPECT_NO_THROW(submodels[1].serialize(stream, {}));
+ EXPECT_NO_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels));
+}
+
+TEST(SerializationTest, FuncallSubmodelHostGatherDstIdxOOBFails) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = make_validation_model(8);
+
+ // Submodel 0: function body
+ auto sub0 = ov::npuw::CompiledModelDescTestAccessor::make();
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(sub0) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+
+ // Submodel 1: function call referencing submodel 0, but host_gather dst_idx=8 is out of bounds [0, 8)
+ auto sub1 = ov::npuw::CompiledModelDescTestAccessor::make();
+ sub1.replaced_by = 0;
+ Gather hg{8, 0, 0};
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(sub1) = hg;
+ auto& closure1 = ov::npuw::CompiledModelDescTestAccessor::closure(sub1);
+ closure1.get().closure.resize(1);
+ closure1.get().closure_uid.resize(1, -1);
+ closure1.get().is_remote.resize(1, false);
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec submodels;
+ submodels.push_back(std::move(sub0));
+ submodels.push_back(std::move(sub1));
+
+ std::stringstream ss;
+ auto stream = Stream::writer(ss);
+ EXPECT_NO_THROW(submodels[0].serialize(stream, {}));
+ EXPECT_NO_THROW(submodels[1].serialize(stream, {}));
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+}
+
+TEST(SerializationTest, FuncallSubmodelClosureOverflowFails) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = make_validation_model(8);
+
+ // Submodel 0: function body with 8 inputs
+ auto sub0 = ov::npuw::CompiledModelDescTestAccessor::make();
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(sub0) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+
+ // Submodel 1: function call referencing submodel 0, param_base=6 + closure_size=4 = 10 > 8
+ auto sub1 = ov::npuw::CompiledModelDescTestAccessor::make();
+ sub1.replaced_by = 0;
+ ov::npuw::CompiledModelDescTestAccessor::param_base(sub1) = 6;
+ auto& closure1 = ov::npuw::CompiledModelDescTestAccessor::closure(sub1);
+ closure1.get().closure.resize(4);
+ closure1.get().closure_uid.resize(4, -1);
+ closure1.get().is_remote.resize(4, false);
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec submodels;
+ submodels.push_back(std::move(sub0));
+ submodels.push_back(std::move(sub1));
+
+ std::stringstream ss;
+ auto stream = Stream::writer(ss);
+ EXPECT_NO_THROW(submodels[0].serialize(stream, {}));
+ EXPECT_NO_THROW(submodels[1].serialize(stream, {}));
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+}
+
+TEST(SerializationTest, FuncallSubmodelRoundTripThroughReadPath) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = make_validation_model(8);
+ ov::SoPtr<ov::ICompiledModel> body_cm{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+
+ auto sub0 = ov::npuw::CompiledModelDescTestAccessor::make();
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(sub0) = body_cm;
+
+ // Submodel 1: function call into submodel 0, carrying an active host gather
+ auto sub1 = ov::npuw::CompiledModelDescTestAccessor::make();
+ sub1.replaced_by = 0;
+ Gather hg{5, 3, 6};
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(sub1) = hg;
+ ov::npuw::CompiledModelDescTestAccessor::param_base(sub1) = 2;
+ auto& closure1 = ov::npuw::CompiledModelDescTestAccessor::closure(sub1);
+ closure1.get().closure.resize(4);
+ closure1.get().closure_uid.resize(4, -1);
+ closure1.get().is_remote.resize(4, false);
+
+ std::stringstream ss;
+ auto writer = Stream::writer(ss);
+ ASSERT_NO_THROW(sub0.serialize(writer, {}));
+ ASSERT_NO_THROW(sub1.serialize(writer, {}));
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec imported;
+ imported.push_back(ov::npuw::CompiledModelDescTestAccessor::make());
+ imported.push_back(ov::npuw::CompiledModelDescTestAccessor::make());
+
+ // Reading a funcall desc must not trip the in-codec index check, which is skipped
+ // because the funcall has no compiled model of its own at this point.
+ auto reader = Stream::reader(ss);
+ ASSERT_NO_THROW(imported[0].serialize(reader, {}));
+ ASSERT_NO_THROW(imported[1].serialize(reader, {}));
+
+ ASSERT_EQ(ov::npuw::CompiledModelDescTestAccessor::host_gather(imported[1]).dst_idx, 5);
+ ASSERT_FALSE(ov::npuw::CompiledModelDescTestAccessor::compiled_model(imported[1]));
+
+ // Import attaches a compiled model to the function body only.
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(imported[0]) = body_cm;
+
+ EXPECT_NO_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(imported));
+
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(imported[1]).dst_idx = 8;
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(imported), ov::Exception);
+}
+
+// The checks below need no compiled model, so a funcall desc must be rejected by the codec itself.
+TEST(SerializationTest, FuncallSubmodelNegativeGatherIndexFails) {
+ auto desc = ov::npuw::CompiledModelDescTestAccessor::make();
+ desc.replaced_by = 0;
+ Gather hg{-5, 3, 6};
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(desc) = hg;
+ ov::npuw::CompiledModelDescTestAccessor::param_base(desc) = 2;
+ auto& closure = ov::npuw::CompiledModelDescTestAccessor::closure(desc);
+ closure.get().closure.resize(4);
+ closure.get().closure_uid.resize(4, -1);
+ closure.get().is_remote.resize(4, false);
+
+ std::stringstream ss;
+ auto stream = Stream::writer(ss);
+ EXPECT_THROW(desc.serialize(stream, {}), ov::Exception);
+}
+
+TEST(SerializationTest, FuncallSubmodelHostGatherSrcIdxOutOfClosureFails) {
+ auto desc = ov::npuw::CompiledModelDescTestAccessor::make();
+ desc.replaced_by = 0;
+ // src_idx - param_base == 7, past the end of a 4-entry closure
+ Gather hg{5, 9, 6};
+ ov::npuw::CompiledModelDescTestAccessor::host_gather(desc) = hg;
+ ov::npuw::CompiledModelDescTestAccessor::param_base(desc) = 2;
+ auto& closure = ov::npuw::CompiledModelDescTestAccessor::closure(desc);
+ closure.get().closure.resize(4);
+ closure.get().closure_uid.resize(4, -1);
+ closure.get().is_remote.resize(4, false);
+
+ std::stringstream ss;
+ auto stream = Stream::writer(ss);
+ EXPECT_THROW(desc.serialize(stream, {}), ov::Exception);
+}
+
+TEST(SerializationTest, ReplacedByOutOfRangeFails) {
+ // Submodel 0: replaced_by points to submodel 5 (out of range)
+ auto sub0 = ov::npuw::CompiledModelDescTestAccessor::make();
+ sub0.replaced_by = 5;
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec submodels;
+ submodels.push_back(std::move(sub0));
+
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+}
+
+TEST(SerializationTest, FunctionBodyWithCorruptedReplacedByFails) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = make_validation_model(8);
+
+ // Submodel 0: Function body that has a compiled_model, but its replaced_by field is corrupted (points OOB to 99)
+ auto sub0 = ov::npuw::CompiledModelDescTestAccessor::make();
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(sub0) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+ sub0.replaced_by = 99;
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec submodels;
+ submodels.push_back(std::move(sub0));
+
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+}
+
+TEST(SerializationTest, MultiHopReplacedByFails) {
+ auto plugin = std::make_shared<NullPlugin>();
+ auto model = make_validation_model(8);
+
+ // Submodel 0: function body with compiled_model
+ auto sub0 = ov::npuw::CompiledModelDescTestAccessor::make();
+ ov::npuw::CompiledModelDescTestAccessor::compiled_model(sub0) =
+ ov::SoPtr<ov::ICompiledModel>{std::make_shared<MockSubCompiledModel>(model, plugin, ov::AnyMap{}), {}};
+
+ // Submodel 1: function call targeting submodel 0 (no compiled_model)
+ auto sub1 = ov::npuw::CompiledModelDescTestAccessor::make();
+ sub1.replaced_by = 0;
+
+ // Submodel 2: multi-hop function call targeting submodel 1 instead of function body submodel 0 directly
+ auto sub2 = ov::npuw::CompiledModelDescTestAccessor::make();
+ sub2.replaced_by = 1;
+
+ ov::npuw::CompiledModelDescTestAccessor::SubmodelVec submodels;
+ submodels.push_back(std::move(sub0));
+ submodels.push_back(std::move(sub1));
+ submodels.push_back(std::move(sub2));
+
+ EXPECT_THROW(ov::npuw::CompiledModelDescTestAccessor::validate_submodels(submodels), ov::Exception);
+}
+
TEST(SerializationTest, ReconstructClosureRejectsMismatchedClosureMetadata) {
auto compiled = ov::npuw::CompiledModelDescSerializationAccess::make_serialized_compiled_model();
auto& submodel = ov::npuw::CompiledModelDescSerializationAccess::append_submodel(*compiled);