Skip to content

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

NPUW: Fix the container nesting structure (#35856)

Dmitry Matveev3463cd0155f7
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 0d0fe2fed5f..41d11381b85 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -687,8 +687,8 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
auto forced_dev_it = std::find(m_dev_list.begin(), m_dev_list.end(), forced_device);
if (forced_dev_it == m_dev_list.end()) {
LOG_WARN("Target device for Subgraph[" << id << "] was set to " << forced_device
- << ", but was not found in the device list: " << "["
- << dev_list_str << "] -- ignoring");
+ << ", but was not found in the device list: "
+ << "[" << dev_list_str << "] -- ignoring");
} else {
LOG_INFO("Force Subgraph[" << id << "] target device to " << *forced_dev_it);
devices.push_back(*forced_dev_it);
@@ -1105,25 +1105,28 @@ void ov::npuw::CompiledModel::serialize_orc_container(std::ostream& stream,
const auto root_flags =
encrypt ? static_cast<ov::npuw::orc::SectionFlags>(ov::npuw::orc::SectionFlag::ENCRYPTED) : 0u;
ov::npuw::orc::with_section(stream, kOrcType, kOrcVersion, root_flags, [&] {
- auto meta_stream = ov::npuw::s11n::Stream::writer(stream);
- meta_stream & m_name;
- meta_stream& inputs() & outputs();
- meta_stream & m_inputs_to_submodels_inputs & m_outputs_to_submodels_outputs & m_param_subscribers &
- m_submodels_input_to_prev_output;
- meta_stream & m_dev_list;
- meta_stream& const_cast<::intel_npu::Config&>(m_cfg);
- meta_stream & serializable_props;
- meta_stream & is_weightless;
- // Persist the BF16 interpretation map that weightless import later feeds
- // into LazyTensor::read_weight() when it decides whether raw bytes should
- // be read as FP16 directly or converted from BF16 source storage.
- meta_stream& const_cast<ov::npuw::s11n::BF16Cache&>(bf16_cache);
- if (encrypt) {
- std::stringstream payload_stream(std::ios::in | std::ios::out | std::ios::binary);
- write_children(payload_stream);
- auto encrypted_payload = encrypt(payload_stream.str());
- meta_stream & encrypted_payload;
- } else {
+ ov::npuw::orc::with_leaf_section(stream, ov::npuw::orc::META_SECTION_TYPE, 0u, [&] {
+ auto meta_stream = ov::npuw::s11n::Stream::writer(stream);
+ meta_stream & m_name;
+ meta_stream& inputs() & outputs();
+ meta_stream & m_inputs_to_submodels_inputs & m_outputs_to_submodels_outputs & m_param_subscribers &
+ m_submodels_input_to_prev_output;
+ meta_stream & m_dev_list;
+ meta_stream& const_cast<::intel_npu::Config&>(m_cfg);
+ meta_stream & serializable_props;
+ meta_stream & is_weightless;
+ // Persist the BF16 interpretation map that weightless import later feeds
+ // into LazyTensor::read_weight() when it decides whether raw bytes should
+ // be read as FP16 directly or converted from BF16 source storage.
+ meta_stream& const_cast<ov::npuw::s11n::BF16Cache&>(bf16_cache);
+ if (encrypt) {
+ std::stringstream payload_stream(std::ios::in | std::ios::out | std::ios::binary);
+ write_children(payload_stream);
+ auto encrypted_payload = encrypt(payload_stream.str());
+ meta_stream & encrypted_payload;
+ }
+ });
+ if (!encrypt) {
write_children(stream);
}
});
@@ -1152,28 +1155,59 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize_or
OPENVINO_THROW("Unsupported ORC NPUW root section");
}
- auto meta_stream = ov::npuw::s11n::Stream::reader(stream);
-
- std::string model_name;
- ov::ParameterVector parameters;
- ov::NodeVector results;
+ // Variables populated during metadata read but used in the child-reading
+ // phase below. Extracted before the version branch so both paths share
+ // the same child-consuming lambdas.
+ std::shared_ptr<ov::npuw::CompiledModel> compiled;
bool is_weightless = false;
- meta_stream & model_name & parameters & results;
-
- auto ov_model = std::make_shared<ov::Model>(ov::as_output_vector(results), parameters, model_name);
- auto compiled = std::make_shared<ov::npuw::CompiledModel>(ov_model, plugin, true);
- compiled->m_name = std::move(model_name);
- meta_stream & compiled->m_inputs_to_submodels_inputs & compiled->m_outputs_to_submodels_outputs &
- compiled->m_param_subscribers & compiled->m_submodels_input_to_prev_output;
- meta_stream & compiled->m_dev_list;
- meta_stream & compiled->m_cfg;
- compiled->m_cfg.parseEnvVars();
- meta_stream & compiled->m_non_npuw_props;
- meta_stream & is_weightless;
- meta_stream & compiled->m_bf16_consts;
- compiled->m_import_weights_ctx = make_import_weights_ctx(properties, is_weightless, compiled->m_bf16_consts);
+ std::string encrypted_payload;
const bool encrypted = ov::npuw::orc::has_flag(root.header().flags, ov::npuw::orc::SectionFlag::ENCRYPTED);
+
+ // Read the model-level metadata fields. In v0 these were written as raw
+ // s11n bytes directly into the container body; v1 wraps them in an
+ // explicit META leaf child so the section tree is fully self-describing.
+ auto read_meta_fields = [&]() {
+ auto meta_stream = ov::npuw::s11n::Stream::reader(stream);
+
+ std::string model_name;
+ ov::ParameterVector parameters;
+ ov::NodeVector results;
+ meta_stream & model_name & parameters & results;
+
+ auto ov_model = std::make_shared<ov::Model>(ov::as_output_vector(results), parameters, model_name);
+ compiled = std::make_shared<ov::npuw::CompiledModel>(ov_model, plugin, true);
+ compiled->m_name = std::move(model_name);
+ meta_stream & compiled->m_inputs_to_submodels_inputs & compiled->m_outputs_to_submodels_outputs &
+ compiled->m_param_subscribers & compiled->m_submodels_input_to_prev_output;
+ meta_stream & compiled->m_dev_list;
+ meta_stream & compiled->m_cfg;
+ compiled->m_cfg.parseEnvVars();
+ meta_stream & compiled->m_non_npuw_props;
+ meta_stream & is_weightless;
+ meta_stream & compiled->m_bf16_consts;
+ if (encrypted) {
+ meta_stream & encrypted_payload;
+ }
+ };
+
+ if (root.header().version == 0) {
+ // v0: metadata written as raw s11n bytes at the start of the container body.
+ read_meta_fields();
+ } else if (root.header().version == 1) {
+ // v1: metadata wrapped in a META leaf child section.
+ ov::npuw::orc::ScopedReadSection meta(stream);
+ 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);
+ }
+ read_meta_fields();
+ meta.expect_end();
+ } else {
+ OPENVINO_THROW("Unsupported ORC NPUW PartitionedModel version ", root.header().version);
+ }
+
+ compiled->m_import_weights_ctx = make_import_weights_ctx(properties, is_weightless, compiled->m_bf16_consts);
bool have_weights = false;
auto peek_child_header = [](std::istream& child_source) {
const auto saved = child_source.tellg();
@@ -1233,8 +1267,6 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize_or
};
if (encrypted) {
- std::string encrypted_payload;
- meta_stream & encrypted_payload;
root.expect_end();
const auto decrypt_fn = decrypt ? decrypt : get_decrypt_callback_or_throw(properties);
@@ -1890,7 +1922,8 @@ void ov::npuw::CompiledModel::dump_subgraph_model(std::size_t id,
LOG_INFO("Dumping Subgraph[" << id << "]");
LOG_BLOCK();
if (real_id != id) {
- LOG_INFO("NOTE: Dumping Subgraph[" << real_id << "]" << " as it is a function body for Subgraph[" << id << "]");
+ LOG_INFO("NOTE: Dumping Subgraph[" << real_id << "]"
+ << " as it is a function body for Subgraph[" << id << "]");
}
const std::string dump_dir = m_cfg.get<::intel_npu::NPUW_DUMP_SUBS_DIR>();
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 3a75490d986..29022332790 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -88,9 +88,11 @@ class CompiledModel : public ov::npuw::ICompiledModel_v0 {
public:
static constexpr ov::npuw::orc::TypeId kOrcType =
static_cast<ov::npuw::orc::TypeId>(ov::npuw::orc::schema_npuw::PartitionedModel::ID);
- // Version 0 is the frozen baseline on the wire. Any further layout changes
- // must be introduced through a new versioned payload rather than by mutating v0.
- static constexpr ov::npuw::orc::Version kOrcVersion = 0u;
+ // Version 1 introduced an explicit META leaf child section for the model's
+ // own serialized fields, making the PartitionedModel container fully
+ // navigable without schema knowledge. Version 0 (pre-release only) wrote
+ // those fields as raw s11n bytes at the start of the container body.
+ static constexpr ov::npuw::orc::Version kOrcVersion = 1u;
CompiledModel(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.hpp b/src/plugins/intel_npu/src/plugin/npuw/orc.hpp
index c0f8cf34e15..93aebbe4b2f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/orc.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/orc.hpp
@@ -35,6 +35,14 @@ using Version = std::uint16_t;
using SectionFlags = std::uint64_t;
using SchemaUUID = std::array<std::uint8_t, 16>;
+// Reserved cross-schema type ID for the metadata leaf of any container section.
+// Every container that carries its own serialized fields before child sections
+// wraps those fields in a META leaf child (type = META_SECTION_TYPE, LEAF flag
+// set). The container's own deserializer knows what fields to read; the type
+// ID is schema-neutral so all containers share the same reserved slot.
+// Type IDs 1–19 are reserved for future ORC infrastructure use.
+constexpr TypeId META_SECTION_TYPE = 20u;
+
enum class SectionFlag : SectionFlags {
OPTIONAL = 1ull << 0,
LEAF = 1ull << 1, // payload contains raw bytes only, no child sections