Skip to content

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

NPUW: Extensible subgraph handling (#35476)

Dmitry Matveev2ce0c4c79e2c
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 97fa07bc28a..15a478dc97d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -3,8 +3,7 @@
//
#include "compiled_model.hpp"
-#include <fstream>
-#include <iostream>
+#include <algorithm>
#include <memory>
#include <set>
#include <string>
@@ -226,6 +225,12 @@ ov::npuw::ICompiledModel::ICompiledModel(const std::shared_ptr<ov::Model>& model
ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties)
+ : CompiledModel(model, plugin, properties, nullptr) {}
+
+ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties,
+ const ov::npuw::v1::subgraphs::PatternRegistry* subgraph_patterns)
: ov::npuw::ICompiledModel_v0(model, plugin),
m_options_desc(std::make_shared<::intel_npu::OptionsDesc>()),
m_cfg(m_options_desc),
@@ -286,6 +291,7 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
ov::npuw::PartitioningContext ctx;
// Identify based on compiler version, user config and pattern
ctx.use_host_gather_quant = should_use_quantized_host_gather(model, npuw_props);
+ ctx.subgraph_patterns = subgraph_patterns;
ov::npuw::Partitioning partitioning;
m_profile["partitioning"].record([&]() {
@@ -415,6 +421,12 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
subgraph._sinks,
subgraph._parameters,
m_name + '_' + std::to_string(id));
+ m_compiled_submodels[id].pipeline.registration = subgraph._pipeline.registration;
+ m_compiled_submodels[id].pipeline.context = subgraph._pipeline.context;
+ if (subgraph._pipeline.compile_stage) {
+ subgraph._pipeline.compile_stage(m_compiled_submodels[id].pipeline,
+ m_compiled_submodels[id].pipeline.context);
+ }
} else {
LOG_BLOCK();
auto& fcn_template = partitioning.functions.at(subgraph._funcall);
@@ -485,6 +497,18 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
}
auto& closure_desc = m_compiled_submodels[id].closure.get();
+ m_compiled_submodels[id].pipeline.registration = fcn_template._pipeline.registration;
+ m_compiled_submodels[id].pipeline.context = fcn_template._pipeline.context;
+ if (compiled_fcn_iter == compiledFunctions.end()) {
+ if (fcn_template._pipeline.compile_stage) {
+ fcn_template._pipeline.compile_stage(m_compiled_submodels[id].pipeline,
+ m_compiled_submodels[id].pipeline.context);
+ }
+ } else {
+ const auto real_id = m_compiled_submodels[id].replaced_by.value();
+ m_compiled_submodels[id].pipeline.runtime_behavior =
+ m_compiled_submodels[real_id].pipeline.runtime_behavior;
+ }
m_compiled_submodels[id].host_gather = subgraph._host_gather;
m_compiled_submodels[id].quant_unpack_gather = subgraph._quant_unpack_gather;
m_compiled_submodels[id].param_base = fcn_template._param_offset;
@@ -501,6 +525,10 @@ ov::npuw::CompiledModel::CompiledModel(const std::shared_ptr<ov::Model>& model,
OPENVINO_THROW("Fatal: submodel ", id, " is neither a model nor a function call!");
}
const std::size_t real_id = m_compiled_submodels[id].replaced_by.value_or(id);
+ auto& pipeline = m_compiled_submodels[id].pipeline;
+ pipeline.is_function_call =
+ m_compiled_submodels[id].replaced_by.has_value() && m_compiled_submodels[id].replaced_by.value() != id;
+ pipeline.function_body_subgraph_idx = m_compiled_submodels[id].replaced_by;
// FIXME: a hotfix for a crash where we have too many names in submodel's output
// Do it just once if that's a function
@@ -2194,8 +2222,19 @@ std::shared_ptr<ov::npuw::IBaseInferRequest> ov::npuw::CompiledModel::create_bas
return m_dev_list.size() == 1 || !m_cfg.get<::intel_npu::NPUW_FALLBACK_EXEC>();
};
+ auto no_subgraph_behavior_concern = [&]() {
+ for (std::size_t idx = 0u; idx < m_compiled_submodels.size(); idx++) {
+ if (m_compiled_submodels[idx].pipeline.runtime_behavior.has_value()) {
+ LOG_WARN("Subgraph[" << idx << "] has a runtime behavior, unfold can't be done");
+ return false;
+ }
+ }
+ return true;
+ };
+
std::shared_ptr<ov::npuw::IBaseInferRequest> result;
- if (m_cfg.get<::intel_npu::NPUW_UNFOLD_IREQS>() && no_spatial_unpack() && no_failsafe_concern()) {
+ if (m_cfg.get<::intel_npu::NPUW_UNFOLD_IREQS>() && no_spatial_unpack() && no_failsafe_concern() &&
+ no_subgraph_behavior_concern()) {
result = std::make_shared<ov::npuw::UnfoldInferRequest>(non_const_this_sptr);
} else {
result = std::make_shared<ov::npuw::JustInferRequest>(non_const_this_sptr);
@@ -2227,12 +2266,10 @@ std::shared_ptr<const ov::Model> ov::npuw::CompiledModel::get_runtime_model() co
OPENVINO_NOT_IMPLEMENTED;
}
-std::shared_ptr<const ::intel_npu::Plugin> ov::npuw::CompiledModel::get_npuw_plugin() const {
+std::shared_ptr<const ov::IPlugin> ov::npuw::CompiledModel::get_npuw_plugin() const {
auto plugin = get_plugin();
OPENVINO_ASSERT(plugin);
- auto npuw_plugin = std::dynamic_pointer_cast<const ::intel_npu::Plugin>(plugin);
- OPENVINO_ASSERT(npuw_plugin);
- return npuw_plugin;
+ return plugin;
}
ov::Any ov::npuw::CompiledModel::get_property(const std::string& name) const {
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 807f390576f..7c9e9712eb7 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -23,6 +23,7 @@
#include "pyramid_attention.hpp"
#include "serialization.hpp"
#include "spatial.hpp"
+#include "v1/subgraph_pipeline.hpp"
#include "weights_bank.hpp"
namespace intel_npu {
@@ -83,6 +84,10 @@ public:
CompiledModel(const std::shared_ptr<ov::Model>& model,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties);
+ CompiledModel(const std::shared_ptr<ov::Model>& model,
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties,
+ const ov::npuw::v1::subgraphs::PatternRegistry* subgraph_patterns);
CompiledModel(const std::shared_ptr<ov::Model>& model,
const std::shared_ptr<const ov::IPlugin>& plugin,
const bool serialized);
@@ -146,7 +151,7 @@ private:
void remove_long_output_names(const std::shared_ptr<ov::Model>& model);
void fill_empty_tensor_names(const std::shared_ptr<ov::Model>& model);
- std::shared_ptr<const ::intel_npu::Plugin> get_npuw_plugin() const;
+ std::shared_ptr<const ov::IPlugin> get_npuw_plugin() const;
std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override;
// API for easily create and manage NPUW infer-requests (promoted to ICompiledModel_v0)
@@ -226,6 +231,7 @@ private:
std::optional<ov::npuw::compiled::HostFlashAttention> host_flash_attention;
std::optional<ov::npuw::compiled::MoEExperts> moe_experts;
std::optional<ov::npuw::compiled::MoEDownstream> moe_experts_downstream;
+ ov::npuw::v1::subgraphs::CompiledPipeline pipeline;
// Infer requests for pyramid attention models (if pyramid_attention is present)
std::vector<ov::SoPtr<ov::IAsyncInferRequest>> pyramid_infer_requests;
diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
index fb298bb28e4..a378ac4fa47 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.cpp
@@ -5,6 +5,8 @@
#include "just_sync_infer_request.hpp"
#include <algorithm>
+#include <cstring>
+#include <fstream>
#include <memory>
#include <string>
@@ -422,6 +424,7 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
alloc_quant_gather();
connect_subrequests();
+ initialize_subgraph_behaviors();
init_gio();
for (size_t i = 0; i < m_num_submodels; i++) {
@@ -516,6 +519,49 @@ ov::npuw::JustInferRequest::JustInferRequest(const std::shared_ptr<ov::npuw::Com
}
}
+void ov::npuw::JustInferRequest::initialize_subgraph_behaviors() {
+ m_subgraph_behaviors.resize(m_num_submodels);
+ for (std::size_t idx = 0; idx < m_num_submodels; idx++) {
+ const auto& comp_model_desc = m_npuw_model->m_compiled_submodels[idx];
+ if (!comp_model_desc.compiled_model && !comp_model_desc.replaced_by) {
+ continue;
+ }
+ if (!comp_model_desc.pipeline.runtime_behavior.has_value()) {
+ continue;
+ }
+ const auto& spec = comp_model_desc.pipeline.runtime_behavior.value();
+ if (spec.factory) {
+ m_subgraph_behaviors[idx] = spec.factory(spec.context);
+ }
+ if (m_subgraph_behaviors[idx]) {
+ continue;
+ }
+ const auto* post_legacy_hook = spec.context.get_if<ov::npuw::v1::subgraphs::PostLegacyHook>();
+ if (!post_legacy_hook) {
+ continue;
+ }
+ m_subgraph_behaviors[idx] = std::make_unique<ov::npuw::v1::subgraphs::DirectBehavior>(
+ [post_legacy_hook = *post_legacy_hook](ov::npuw::v1::subgraphs::InferContext& ctx) {
+ ctx.legacy_infer();
+ post_legacy_hook(ctx);
+ });
+ }
+}
+
+ov::npuw::v1::subgraphs::InferContext ov::npuw::JustInferRequest::make_behavior_context(std::size_t real_idx,
+ std::size_t idx) {
+ return ov::npuw::v1::subgraphs::InferContext{*m_npuw_model, *this, idx, real_idx, [this, real_idx, idx]() {
+ legacy_infer(real_idx, idx);
+ }};
+}
+
+ov::npuw::v1::subgraphs::ISubgraphBehavior* ov::npuw::JustInferRequest::get_subgraph_behavior(std::size_t idx) const {
+ if (idx >= m_subgraph_behaviors.size()) {
+ return nullptr;
+ }
+ return m_subgraph_behaviors[idx].get();
+}
+
void ov::npuw::JustInferRequest::set_tensor(const ov::Output<const ov::Node>& port,
const ov::SoPtr<ov::ITensor>& tensor) {
std::unique_lock lock(m_io_storages_mutex);
@@ -1254,6 +1300,7 @@ void ov::npuw::JustInferRequest::run_subrequest_for_success(std::size_t idx) {
auto& comp_model_desc = m_npuw_model->m_compiled_submodels[idx];
const auto real_idx = comp_model_desc.replaced_by.value_or(idx);
bool next_prepared = false;
+ auto* behavior = get_subgraph_behavior(idx);
// Feeding the global Parameters is now part of the common
// execution pipeline: See how it is done in
@@ -1264,6 +1311,10 @@ void ov::npuw::JustInferRequest::run_subrequest_for_success(std::size_t idx) {
if (comp_model_desc.replaced_by) {
function_prologue(idx);
}
+ if (behavior != nullptr) {
+ auto ctx = make_behavior_context(real_idx, idx);
+ behavior->prologue(ctx);
+ }
dump_input_tensors(idx);
LOG_DEBUG("Trying to run subrequest[" << idx << "]...");
@@ -1273,6 +1324,10 @@ void ov::npuw::JustInferRequest::run_subrequest_for_success(std::size_t idx) {
LOG_DEBUG("Done: " << idx << "(exec subrequest)");
dump_output_tensors(idx); // FIXME: Called here unconditionally, need to refactor
+ if (behavior != nullptr) {
+ auto ctx = make_behavior_context(real_idx, idx);
+ behavior->epilogue(ctx);
+ }
if (is_pipelined(idx) && m_funcall_pipeline[idx].next) {
// Swap the next (pipelined, semi-prepared) infer request in the chain
// with the default (to be accessed next) one.
@@ -1754,7 +1809,7 @@ void ov::npuw::JustInferRequest::unsafe_infer_spatial(std::size_t real_idx, std:
}
}
-void ov::npuw::JustInferRequest::unsafe_infer(std::size_t real_idx, std::size_t idx) {
+void ov::npuw::JustInferRequest::legacy_infer(std::size_t real_idx, std::size_t idx) {
auto& comp_model_desc = m_npuw_model->m_compiled_submodels[real_idx];
auto& r = m_subrequests[real_idx];
if (comp_model_desc.spatial) {
@@ -1769,6 +1824,15 @@ void ov::npuw::JustInferRequest::unsafe_infer(std::size_t real_idx, std::size_t
}
}
+void ov::npuw::JustInferRequest::unsafe_infer(std::size_t real_idx, std::size_t idx) {
+ if (auto* behavior = get_subgraph_behavior(idx)) {
+ auto ctx = make_behavior_context(real_idx, idx);
+ behavior->run(ctx);
+ return;
+ }
+ legacy_infer(real_idx, idx);
+}
+
void ov::npuw::JustInferRequest::unsafe_run_this_prep_next(std::size_t idx, bool& next_prepared) {
auto& comp_model_desc = m_npuw_model->m_compiled_submodels[idx];
auto real_idx = comp_model_desc.replaced_by.value_or(idx);
diff --git a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
index c1916e0911e..b37548ff484 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/just_sync_infer_request.hpp
@@ -19,6 +19,7 @@
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/runtime/tensor.hpp"
#include "spatial.hpp"
+#include "v1/subgraph_pipeline.hpp"
namespace ov {
namespace npuw {
@@ -120,6 +121,9 @@ protected:
void unsafe_run_this_prep_next(std::size_t idx, bool& next_prepared_p);
void run_hfa_tiled_inference(std::size_t real_idx, std::size_t idx);
+ void legacy_infer(std::size_t real_idx, std::size_t idx);
+ ov::npuw::v1::subgraphs::InferContext make_behavior_context(std::size_t real_idx, std::size_t idx);
+ ov::npuw::v1::subgraphs::ISubgraphBehavior* get_subgraph_behavior(std::size_t idx) const;
// HFA helper functions
static void hfa_extract_and_copy_tile(const ov::SoPtr<ov::ITensor>& source_tensor,
@@ -136,6 +140,7 @@ protected:
int64_t tile_length);
void connect_subrequests();
+ void initialize_subgraph_behaviors();
// Helper function to setup pyramid attention infer requests
void setup_pyramid_infer_requests(std::size_t real_idx, bool is_piped);
@@ -186,6 +191,10 @@ protected:
// freeing the buffer while pyramid requests still hold raw pointers into it.
// Each entry is {real_idx, tensor} so recreate can selectively remove by submodel.
std::vector<std::pair<std::size_t, ov::SoPtr<ov::ITensor>>> m_pyramid_anchor_tensors;
+
+ // Foundation for the upcoming behavior-driven execution pipeline.
+ // These slots are populated now but legacy execution still owns the scheduling.
+ std::vector<ov::npuw::v1::subgraphs::ISubgraphBehavior::Ptr> m_subgraph_behaviors;
};
} // namespace npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
index 68d9f66ac57..30e1adb30b1 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.cpp
@@ -223,11 +223,14 @@ class Compiler {
}
public:
- Compiler(const std::shared_ptr<ov::Model>& model, ::intel_npu::Config& cfg)
+ Compiler(const std::shared_ptr<ov::Model>& model,
+ ::intel_npu::Config& cfg,
+ const ov::npuw::v1::subgraphs::PatternRegistry* subgraph_patterns)
: m_model(model),
m_snapshot(std::make_shared<Snapshot>(model)),
m_cfg(cfg) {
PassContext ctx(m_cfg);
+ ctx.subgraph_patterns = subgraph_patterns;
if (currentPipeline() == Pipeline::NONE && ctx.avoids.empty()) {
none_fast();
@@ -351,10 +354,12 @@ private:
} // namespace ov
// TODO: decouple configuration for partitioning from the plugin's cfg.
-ov::npuw::Ensemble ov::npuw::online::buildPartitioning(const std::shared_ptr<ov::Model>& model,
- ::intel_npu::Config& cfg) {
+ov::npuw::Ensemble ov::npuw::online::buildPartitioning(
+ const std::shared_ptr<ov::Model>& model,
+ ::intel_npu::Config& cfg,
+ const ov::npuw::v1::subgraphs::PatternRegistry* subgraph_patterns) {
// Creates compiler and runs partitioning algorithm.
- ov::npuw::online::Compiler partitioner(model, cfg);
+ ov::npuw::online::Compiler partitioner(model, cfg, subgraph_patterns);
// Convert groups formed after passes into plugin-compatible data structure.
return partitioner.getPartitioning();
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.hpp
index a07eec70d97..dd0ad57ce00 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/compiler.hpp
@@ -14,7 +14,9 @@ namespace ov {
namespace npuw {
namespace online {
-ov::npuw::Ensemble buildPartitioning(const std::shared_ptr<ov::Model>& model, ::intel_npu::Config& cfg);
+ov::npuw::Ensemble buildPartitioning(const std::shared_ptr<ov::Model>& model,
+ ::intel_npu::Config& cfg,
+ const ov::npuw::v1::subgraphs::PatternRegistry* subgraph_patterns = nullptr);
} // namespace online
} // namespace npuw
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cpp
index 7fc42289b13..ab58c5e7d72 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/snapshot.cpp
@@ -723,6 +723,11 @@ void Snapshot::earlyRegroup() {
break;
}
case PatternType::PATTERN: {
+ if (m_ctx.subgraph_patterns != nullptr) {
+ handle_patterns =
+ m_ctx.subgraph_patterns->register_matcher(rewr, shared_from_this(), isolate.pattern, isolate.tag) ||
+ handle_patterns;
+ }
#define HNDL(p) \
if (isolate.pattern == #p) { \
rewr.add_matcher<ov::npuw::patterns::compute::p>(shared_from_this(), isolate.tag); \
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hpp
index abd5800ec1f..6a191f91f49 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/online/utils/utils.hpp
@@ -10,6 +10,7 @@
#include <sstream>
#include <unordered_map>
+#include "../../../v1/subgraph_pipeline.hpp"
#include "attribute_visitor.hpp"
#include "intel_npu/config/config.hpp"
#include "openvino/openvino.hpp"
@@ -42,6 +43,7 @@ struct PassContext {
std::vector<Avoid> avoids;
std::vector<Isolate> isolates;
std::vector<std::string> nofolds;
+ const ov::npuw::v1::subgraphs::PatternRegistry* subgraph_patterns = nullptr;
};
// Forward declaration
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
index cf270dd58a3..9dc34902125 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.cpp
@@ -35,6 +35,90 @@ inline bool operator==(const std::reference_wrapper<Subgraph>& lhs, const std::r
} // namespace npuw
} // namespace ov
+void ov::npuw::v1::subgraphs::PatternRegistry::apply(ov::npuw::Function& function) const {
+ for (const auto& registration : m_registrations) {
+ if (function.gettag() != registration.tag) {
+ continue;
+ }
+
+ auto& func_pipeline = function._pipeline;
+ if (func_pipeline.registration.group.empty()) {
+ func_pipeline.registration.group = registration.group;
+ }
+ if (func_pipeline.registration.name.empty()) {
+ func_pipeline.registration.name = registration.name;
+ }
+ if (std::find(func_pipeline.registration.patterns.begin(),
+ func_pipeline.registration.patterns.end(),
+ registration.pattern) == func_pipeline.registration.patterns.end()) {
+ func_pipeline.registration.patterns.push_back(registration.pattern);
+ }
+
+ if (registration.partition_stage) {
+ registration.partition_stage(function, func_pipeline.context);
+ }
+
+ auto previous_compile_stage = func_pipeline.compile_stage;
+ func_pipeline.compile_stage = [previous_compile_stage, registration](
+ ov::npuw::v1::subgraphs::CompiledPipeline& compiled_pipeline,
+ ov::npuw::v1::subgraphs::Context& compiled_context) {
+ if (previous_compile_stage) {
+ previous_compile_stage(compiled_pipeline, compiled_context);
+ }
+ if (registration.compile_stage) {
+ registration.compile_stage(compiled_pipeline, compiled_context);
+ }
+ if (registration.runtime_factory) {
+ ov::npuw::v1::subgraphs::RuntimeBehaviorSpec spec;
+ spec.registration = compiled_pipeline.registration;
+ spec.context = compiled_context;
+ spec.factory = registration.runtime_factory;
+ compiled_pipeline.runtime_behavior = std::move(spec);
+ }
+ };
+ }
+}
+
+void ov::npuw::v1::subgraphs::PatternRegistry::apply(ov::npuw::Subgraph& subgraph) const {
+ for (const auto& registration : m_registrations) {
+ if (subgraph.gettag() != registration.tag) {
+ continue;
+ }
+
+ auto& subgraph_pipeline = subgraph._pipeline;
+ if (subgraph_pipeline.registration.group.empty()) {
+ subgraph_pipeline.registration.group = registration.group;
+ }
+ if (subgraph_pipeline.registration.name.empty()) {
+ subgraph_pipeline.registration.name = registration.name;
+ }
+ if (std::find(subgraph_pipeline.registration.patterns.begin(),
+ subgraph_pipeline.registration.patterns.end(),
+ registration.pattern) == subgraph_pipeline.registration.patterns.end()) {
+ subgraph_pipeline.registration.patterns.push_back(registration.pattern);
+ }
+
+ auto previous_compile_stage = subgraph_pipeline.compile_stage;
+ subgraph_pipeline.compile_stage = [previous_compile_stage, registration](
+ ov::npuw::v1::subgraphs::CompiledPipeline& compiled_pipeline,
+ ov::npuw::v1::subgraphs::Context& compiled_context) {
+ if (previous_compile_stage) {
+ previous_compile_stage(compiled_pipeline, compiled_context);
+ }
+ if (registration.compile_stage) {
+ registration.compile_stage(compiled_pipeline, compiled_context);
+ }
+ if (registration.runtime_factory) {
+ ov::npuw::v1::subgraphs::RuntimeBehaviorSpec spec;
+ spec.registration = compiled_pipeline.registration;
+ spec.context = compiled_context;
+ spec.factory = registration.runtime_factory;
+ compiled_pipeline.runtime_behavior = std::move(spec);
+ }
+ };
+ }
+}
+
template <typename T2>
struct std::hash<std::pair<ov::npuw::Subgraph::Ref, T2>> {
std::size_t operator()(const std::pair<ov::npuw::Subgraph::Ref, T2>& p) const noexcept {
@@ -437,6 +521,9 @@ void Partitioner::identifySubgraphs() {
group.sg._avoid_list = group.avoid_list;
group.sg.settag(group.gettag());
+ if (part_ctx.subgraph_patterns != nullptr) {
+ part_ctx.subgraph_patterns->apply(group.sg);
+ }
// Note inputs and outputs are included in the above set, so if
// we are here, those nodes should be present in the model.
@@ -1658,6 +1745,9 @@ void Partitioner::createFunction(FunctionPipeline& func_ggg) {
function._model = func_ggg.mdls.front();
function._param_offset = body_sg._parameters.size();
function.settag(body_sg.gettag());
+ if (part_ctx.subgraph_patterns != nullptr) {
+ part_ctx.subgraph_patterns->apply(function);
+ }
std::size_t new_param_idx = function._param_offset;
for (auto&& node_ptr : function._model->get_ordered_ops()) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
index c8dff61741a..b37da683ef0 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/partitioning.hpp
@@ -15,6 +15,7 @@
#include "../moe_transformations/moe_transformation.hpp"
#include "../pyramid_attention.hpp"
#include "../spatial.hpp"
+#include "../v1/subgraph_pipeline.hpp"
#include "intel_npu/config/config.hpp"
#include "intel_npu/config/npuw.hpp"
#include "openvino/openvino.hpp"
@@ -73,12 +74,17 @@ struct Subgraph {
int64_t idx_idx = -1;
};
QuantUnpackGather _quant_unpack_gather;
+ ov::npuw::v1::subgraphs::FunctionPipeline _pipeline;
using Ref = std::reference_wrapper<Subgraph>;
void settag(const std::string& t) {
LOG_DEBUG("Subgraph set-tag=" << t);
_tag = t;
+ _pipeline.registration.patterns.clear();
+ if (!t.empty()) {
+ _pipeline.registration.patterns.push_back(t);
+ }
}
std::string gettag() const {
return _tag;
@@ -107,6 +113,7 @@ struct Function {
// MoE expert information - single expert model
std::optional<ov::npuw::function::MoEExperts> _moe_experts;
std::optional<ov::npuw::function::MoEDownstream> _moe_experts_downstream;
+ ov::npuw::v1::subgraphs::FunctionPipeline _pipeline;
// FIXME: They should exclude each other (introduce a hierarchy, finally?)
// FIXME: shouldn't be here. Needed to not unpack some lazy closures in DCOFF
std::set<std::size_t> _idx_lazy_unpack;
@@ -114,6 +121,10 @@ struct Function {
void settag(const std::string& t) {
LOG_DEBUG("Function set-tag=" << t);
_tag = t;
+ _pipeline.registration.patterns.clear();
+ if (!t.empty()) {
+ _pipeline.registration.patterns.push_back(t);
+ }
}
std::string gettag() const {
return _tag;
@@ -193,6 +204,7 @@ struct Partitioning {
struct PartitioningContext {
bool use_host_gather_quant = false;
+ const ov::npuw::v1::subgraphs::PatternRegistry* subgraph_patterns = nullptr;
// Router model for MoE (shared during partitioning process)
// Will be populated during first pass and used in second pass
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.hpp
index 7bb70827000..294050839ba 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/sdpa.hpp
@@ -25,12 +25,30 @@ namespace attn {
class SDPA : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::attn::SDPA");
+ static constexpr const char* pattern_name() {
+ return "SDPA";
+ }
+ static constexpr const char* isolation_tag() {
+ return "attn";
+ }
+ static constexpr const char* group_name() {
+ return "attn";
+ }
SDPA(const std::shared_ptr<ov::npuw::online::Snapshot>& snapshot, const std::string& isol_tag);
};
class SDPADecomposed : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("npuw::patterns::attn::SDPADecomposed");
+ static constexpr const char* pattern_name() {
+ return "SDPADecomposed";
+ }
+ static constexpr const char* isolation_tag() {
+ return "attn";
+ }
+ static constexpr const char* group_name() {
+ return "attn";
+ }
SDPADecomposed(const std::shared_ptr<ov::npuw::online::Snapshot>& snapshot, const std::string& isol_tag);
};
diff --git a/src/plugins/intel_npu/src/plugin/npuw/v1/elements/accuracy_checked.cpp b/src/plugins/intel_npu/src/plugin/npuw/v1/elements/accuracy_checked.cpp
index be2da0e99db..f713f1f4a1f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/v1/elements/accuracy_checked.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/v1/elements/accuracy_checked.cpp
@@ -6,6 +6,7 @@
#include <utility>
+#include "../../logging.hpp"
#include "openvino/core/except.hpp"
#include "openvino/runtime/make_tensor.hpp"
@@ -160,6 +161,7 @@ void InferRequest::infer() {
}
if (!accurate) {
+ LOG_WARN("Accuracy issue spotted, switching inference to the reference target");
// Copy reference outputs into the main output buffers so that any
// downstream request already bound to those buffers sees the corrected
// values immediately.
diff --git a/src/plugins/intel_npu/src/plugin/npuw/v1/subgraph_pipeline.hpp b/src/plugins/intel_npu/src/plugin/npuw/v1/subgraph_pipeline.hpp
new file mode 100644
index 00000000000..9865e89ac29
--- /dev/null
+++ b/src/plugins/intel_npu/src/plugin/npuw/v1/subgraph_pipeline.hpp
@@ -0,0 +1,325 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <algorithm>
+#include <any>
+#include <atomic>
+#include <functional>
+#include <memory>
+#include <optional>
+#include <string>
+#include <typeindex>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include "openvino/core/except.hpp"
+#include "openvino/pass/graph_rewrite.hpp"
+
+namespace ov::npuw {
+class CompiledModel;
+class IBaseInferRequest;
+struct Function;
+struct Subgraph;
+namespace online {
+class Snapshot;
+}
+} // namespace ov::npuw
+
+namespace ov::npuw::v1::subgraphs {
+
+class Context {
+public:
+ template <typename T>
+ T& put(T value) {
+ auto [it, inserted] = m_entries.insert_or_assign(std::type_index(typeid(T)), std::any(std::move(value)));
+ return *std::any_cast<T>(&it->second);
+ }
+
+ template <typename T, typename... Args>
+ T& emplace(Args&&... args) {
+ auto [it, inserted] = m_entries.insert_or_assign(std::type_index(typeid(T)),
+ std::any(std::in_place_type<T>, std::forward<Args>(args)...));
+ return *std::any_cast<T>(&it->second);
+ }
+
+ template <typename T>
+ bool contains() const {
+ return m_entries.find(std::type_index(typeid(T))) != m_entries.end();
+ }
+
+ template <typename T>
+ T* get_if() {
+ auto it = m_entries.find(std::type_index(typeid(T)));
+ return it == m_entries.end() ? nullptr : std::any_cast<T>(&it->second);
+ }
+
+ template <typename T>
+ const T* get_if() const {
+ auto it = m_entries.find(std::type_index(typeid(T)));
+ return it == m_entries.end() ? nullptr : std::any_cast<T>(&it->second);
+ }
+
+ template <typename T>
+ T& get() {
+ auto* value = get_if<T>();
+ if (!value) {
+ OPENVINO_THROW("NPUW subgraph pipeline context is missing entry for type ", typeid(T).name());
+ }
+ return *value;
+ }
+
+ template <typename T>
+ const T& get() const {
+ auto* value = get_if<T>();
+ if (!value) {
+ OPENVINO_THROW("NPUW subgraph pipeline context is missing entry for type ", typeid(T).name());
+ }
+ return *value;
+ }
+
+ bool empty() const {
+ return m_entries.empty();
+ }
+
+ std::size_t size() const {
+ return m_entries.size();
+ }
+
+private:
+ std::unordered_map<std::type_index, std::any> m_entries;
+};
+
+struct Registration {
+ std::string group;
+ std::string name;
+ std::vector<std::string> patterns;
+ std::size_t order = 0u;
+
+ bool empty() const {
+ return group.empty() && name.empty() && patterns.empty() && order == 0u;
+ }
+};
+
+struct CompiledPipeline;
+struct InferContext;
+
+struct FunctionPipeline {
+ Registration registration;
+ Context context;
+ std::function<void(CompiledPipeline&, Context&)> compile_stage;
+};
+
+class ISubgraphBehavior {
+public:
+ using Ptr = std::unique_ptr<ISubgraphBehavior>;
+
+ virtual void prologue(InferContext&) {}
+ virtual void run(InferContext&) = 0;
+ virtual void epilogue(InferContext&) {}
+
+ virtual ~ISubgraphBehavior() = default;
+};
+
+using RuntimeBehaviorFactory = std::function<ISubgraphBehavior::Ptr(const Context&)>;
+
+struct RuntimeBehaviorSpec {
+ Registration registration;
+ Context context;
+ RuntimeBehaviorFactory factory;
+};
+
+struct CompiledPipeline {
+ Registration registration;
+ Context context;
+ std::optional<RuntimeBehaviorSpec> runtime_behavior;
+ bool is_function_call = false;
+ std::optional<std::size_t> function_body_subgraph_idx;
+};
+
+struct InferContext {
+ ov::npuw::CompiledModel& compiled_model;
+ ov::npuw::IBaseInferRequest& infer_request;
+ std::size_t subgraph_idx = 0u;
+ std::size_t real_subgraph_idx = 0u;
+ std::function<void()> legacy_infer;
+};
+
+using PostLegacyHook = std::function<void(InferContext&)>;
+
+class DirectBehavior final : public ISubgraphBehavior {
+public:
+ using Runner = std::function<void(InferContext&)>;
+
+ explicit DirectBehavior(Runner runner = {}) : m_runner(std::move(runner)) {}
+
+ void run(InferContext& ctx) override {
+ if (m_runner) {
+ m_runner(ctx);
+ }
+ }
+
+private:
+ Runner m_runner;
+};
+
+inline ISubgraphBehavior::Ptr make_direct_behavior() {
+ return std::make_unique<DirectBehavior>();
+}
+
+struct PatternRegistration {
+ using PartitionStage = std::function<void(ov::npuw::Function&, Context&)>;
+ using CompileStage = std::function<void(CompiledPipeline&, Context&)>;
+ using MatcherRegistrar = std::function<
+ void(ov::pass::GraphRewrite&, const std::shared_ptr<ov::npuw::online::Snapshot>&, const std::string&)>;
+
+ std::size_t id = 0u;
+ std::string pattern;
+ std::string tag;
+ std::string group;
+ std::string name;
+ PartitionStage partition_stage;
+ CompileStage compile_stage;
+ RuntimeBehaviorFactory runtime_factory;
+ MatcherRegistrar matcher_registrar;
+};
+
+class PatternRegistry;
+
+class ScopedPatternRegistration {
+public:
+ ScopedPatternRegistration() = default;
+ ScopedPatternRegistration(PatternRegistry* registry, std::size_t id) : m_registry(registry), m_id(id) {}
+ ScopedPatternRegistration(const ScopedPatternRegistration&) = delete;
+ ScopedPatternRegistration& operator=(const ScopedPatternRegistration&) = delete;
+ ScopedPatternRegistration(ScopedPatternRegistration&& other) noexcept
+ : m_registry(std::exchange(other.m_registry, nullptr)),
+ m_id(std::exchange(other.m_id, 0u)) {}
+ ScopedPatternRegistration& operator=(ScopedPatternRegistration&& other) noexcept {
+ if (this != &other) {
+ reset();
+ m_registry = std::exchange(other.m_registry, nullptr);
+ m_id = std::exchange(other.m_id, 0u);
+ }
+ return *this;
+ }
+ ~ScopedPatternRegistration() {
+ reset();
+ }
+
+ void reset();
+
+private:
+ PatternRegistry* m_registry = nullptr;
+ std::size_t m_id = 0u;
+};
+
+template <typename Pattern>
+class OnPatternBuilder {
+public:
+ explicit OnPatternBuilder(PatternRegistry& registry) : m_registry(registry) {
+ m_registration.pattern = Pattern::pattern_name();
+ m_registration.tag = Pattern::isolation_tag();
+ m_registration.group = Pattern::group_name();
+ m_registration.name = Pattern::pattern_name();
+ m_registration.matcher_registrar = [](ov::pass::GraphRewrite& rewr,
+ const std::shared_ptr<ov::npuw::online::Snapshot>& snapshot,
+ const std::string& isol_tag) {
+ rewr.add_matcher<Pattern>(snapshot, isol_tag);
+ };
+ }
+
+ OnPatternBuilder& tag(std::string value) {
+ m_registration.tag = std::move(value);
+ return *this;
+ }
+
+ OnPatternBuilder& group(std::string value) {
+ m_registration.group = std::move(value);
+ return *this;
+ }
+
+ OnPatternBuilder& at_partition(PatternRegistration::PartitionStage stage) {
+ m_registration.partition_stage = std::move(stage);
+ return *this;
+ }
+
+ OnPatternBuilder& at_compile(PatternRegistration::CompileStage stage) {
+ m_registration.compile_stage = std::move(stage);
+ return *this;
+ }
+
+ OnPatternBuilder& at_runtime(RuntimeBehaviorFactory factory) {
+ m_registration.runtime_factory = std::move(factory);
+ return *this;
+ }
+
+ ScopedPatternRegistration scoped();
+
+private:
+ PatternRegistry& m_registry;
+ PatternRegistration m_registration;
+};
+
+class PatternRegistry {
+public:
+ template <typename Pattern>
+ OnPatternBuilder<Pattern> on() {
+ return OnPatternBuilder<Pattern>(*this);
+ }
+
+ ScopedPatternRegistration add(PatternRegistration registration) {
+ registration.id = ++m_next_id;
+ m_registrations.push_back(std::move(registration));
+ return ScopedPatternRegistration(this, m_registrations.back().id);
+ }
+
+ void erase(std::size_t id) {
+ m_registrations.erase(std::remove_if(m_registrations.begin(),
+ m_registrations.end(),
+ [id](const PatternRegistration& registration) {
+ return registration.id == id;
+ }),
+ m_registrations.end());
+ }
+
+ bool register_matcher(ov::pass::GraphRewrite& rewr,
+ const std::shared_ptr<ov::npuw::online::Snapshot>& snapshot,
+ const std::string& pattern,
+ const std::string& isol_tag) const {
+ bool handled = false;
+ for (const auto& registration : m_registrations) {
+ if (registration.pattern != pattern || !registration.matcher_registrar) {
+ continue;
+ }
+ registration.matcher_registrar(rewr, snapshot, isol_tag);
+ handled = true;
+ }
+ return handled;
+ }
+
+ void apply(ov::npuw::Function& function) const;
+ void apply(ov::npuw::Subgraph& subgraph) const;
+
+private:
+ std::vector<PatternRegistration> m_registrations;
+ std::size_t m_next_id = 0u;
+};
+
+inline void ScopedPatternRegistration::reset() {
+ if (m_registry != nullptr) {
+ m_registry->erase(m_id);
+ m_registry = nullptr;
+ m_id = 0u;
+ }
+}
+
+template <typename Pattern>
+inline ScopedPatternRegistration OnPatternBuilder<Pattern>::scoped() {
+ return m_registry.add(std::move(m_registration));
+}
+
+} // namespace ov::npuw::v1::subgraphs
diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
index 122f3013ce0..99300764c5f 100644
--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt
+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
@@ -124,5 +124,7 @@ target_sources(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/npuw/partitioning_options_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_options_smoke_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/sdpa_pattern_nodes_test.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/subgraph_pipeline.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/subgraph_behavior_infer_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/remove_empty_kv_inputs_test.cpp
)
diff --git a/src/plugins/intel_npu/tests/unit/npuw/online_partitioning.cpp b/src/plugins/intel_npu/tests/unit/npuw/online_partitioning.cpp
index 10a9e761a42..436bb3d2939 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/online_partitioning.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/online_partitioning.cpp
@@ -125,6 +125,7 @@ TEST(OnlinePartitioningTest, Partitioning_IsTheSame_SmallModel) {
}
}
+
TEST(OnlinePartitioningTest, Partitioning_IsTheSame_RepeatedModel) {
ModelBuilder mb;
auto model = mb.get_model_with_repeated_blocks();
diff --git a/src/plugins/intel_npu/tests/unit/npuw/subgraph_behavior_infer_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/subgraph_behavior_infer_test.cpp
new file mode 100644
index 00000000000..14cff900c3c
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/subgraph_behavior_infer_test.cpp
@@ -0,0 +1,442 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <any>
+#include <atomic>
+#include <cstring>
+#include <functional>
+#include <map>
+#include <numeric>
+#include <mutex>
+#include <vector>
+
+#define private public
+#include "compiled_model.hpp"
+#undef private
+#include "just_sync_infer_request.hpp"
+#include "llm_test_helpers.hpp"
+#include "model_builder.hpp"
+#include "unfold_sync_infer_request.hpp"
+#include "openvino/op/scaled_dot_product_attention.hpp"
+#include "openvino/openvino.hpp"
+#include "openvino/pass/stateful_to_stateless.hpp"
+#include "unit_test_utils/mocks/openvino/runtime/mock_icore.hpp"
+
+namespace {
+
+using ov::test::npuw::build_llm_test_model;
+
+constexpr std::size_t kSeqLen = 4u;
+constexpr std::size_t kPastKvLen = 4u;
+constexpr std::size_t kKVCacheSize = kSeqLen + kPastKvLen;
+struct BehaviorHits {
+ std::mutex mutex;
+ std::vector<std::pair<std::size_t, std::size_t>> values;
+};
+
+class TestPlugin final : public ov::IPlugin {
+public:
+ std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,
+ const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::compile_model call in subgraph behavior test");
+ }
+ std::shared_ptr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>&,
+ const ov::AnyMap&,
+ const ov::SoPtr<ov::IRemoteContext>&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::compile_model(context) call in subgraph behavior test");
+ }
+ std::shared_ptr<ov::ICompiledModel> import_model(std::istream&, const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::import_model(stream) call in subgraph behavior test");
+ }
+ std::shared_ptr<ov::ICompiledModel> import_model(std::istream&,
+ const ov::SoPtr<ov::IRemoteContext>&,
+ const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::import_model(stream, context) call in subgraph behavior test");
+ }
+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor&, const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::import_model(blob) call in subgraph behavior test");
+ }
+ std::shared_ptr<ov::ICompiledModel> import_model(const ov::Tensor&,
+ const ov::SoPtr<ov::IRemoteContext>&,
+ const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::import_model(blob, context) call in subgraph behavior test");
+ }
+ ov::SupportedOpsMap query_model(const std::shared_ptr<const ov::Model>&, const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::query_model call in subgraph behavior test");
+ }
+ void set_property(const ov::AnyMap&) override {}
+ ov::Any get_property(const std::string&, const ov::AnyMap&) const override {
+ OPENVINO_THROW("Test plugin does not expose properties");
+ }
+ bool is_property_supported(const std::string&, const ov::AnyMap&) const override {
+ return false;
+ }
+ ov::SoPtr<ov::IRemoteContext> create_context(const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::create_context call in subgraph behavior test");
+ }
+ ov::SoPtr<ov::IRemoteContext> get_default_context(const ov::AnyMap&) const override {
+ OPENVINO_THROW("Unexpected TestPlugin::get_default_context call in subgraph behavior test");
+ }
+};
+
+std::shared_ptr<ov::Model> build_static_llm_model() {
+ auto model = build_llm_test_model();
+ ov::pass::StatefulToStateless().run_on_model(model);
+ model = model->clone();
+
+ std::map<std::string, ov::PartialShape> new_shapes;
+ for (const auto& input : model->inputs()) {
+ const auto& name = input.get_any_name();
+ const auto& pshape = input.get_partial_shape();
+
+ if (name.find("input_ids") != std::string::npos || name.find("token_type_ids") != std::string::npos) {
+ new_shapes[name] = ov::PartialShape{1, kSeqLen};
+ } else if (name.find("attention_mask") != std::string::npos) {
+ new_shapes[name] = ov::PartialShape{1, kKVCacheSize};
+ } else if (name.find("position_ids") != std::string::npos) {
+ new_shapes[name] = ov::PartialShape{1, kSeqLen};
+ } else {
+ auto static_shape = pshape;
+ static_shape[0] = 1;
+ static_shape[2] = kPastKvLen;
+ new_shapes[name] = static_shape;
+ }
+ }
+
+ model->reshape(new_shapes);
+ model->validate_nodes_and_infer_types();
+ return model;
+}
+
+std::size_t count_sdpa_nodes(const std::shared_ptr<ov::Model>& model) {
+ const auto& ordered_ops = model->get_ordered_ops();
+ return std::count_if(ordered_ops.begin(), ordered_ops.end(), [](const std::shared_ptr<ov::Node>& op) {
+ return ov::is_type<ov::op::v13::ScaledDotProductAttention>(op);
+ });
+}
+
+std::size_t count_runtime_behaviors(const std::shared_ptr<ov::npuw::CompiledModel>& compiled_model) {
+ return std::count_if(compiled_model->m_compiled_submodels.begin(),
+ compiled_model->m_compiled_submodels.end(),
+ [](const auto& desc) {
+ return desc.pipeline.runtime_behavior.has_value();
+ });
+}
+
+class FakeSubCompiledModel;
+
+class FakeSubInferRequest final : public ov::ISyncInferRequest {
+public:
+ explicit FakeSubInferRequest(std::shared_ptr<const FakeSubCompiledModel> compiled_model);
+
+ void infer() override;
+ ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override {
+ return ov::ISyncInferRequest::get_tensor(port);
+ }
+ void set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) override {
+ ov::ISyncInferRequest::set_tensor(port, tensor);
+ }
+ void check_tensors() const override {}
+ std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override {
+ return {};
+ }
+ std::vector<ov::ProfilingInfo> get_profiling_info() const override {
+ return {};
+ }
+};
+
+class FakeSubAsyncInferRequest final : public ov::IAsyncInferRequest {
+public:
+ explicit FakeSubAsyncInferRequest(const std::shared_ptr<ov::ISyncInferRequest>& request)
+ : ov::IAsyncInferRequest(nullptr, nullptr, nullptr),
+ m_request(request) {}
+
+ void start_async() override {
+ try {
+ m_request->infer();
+ if (m_callback) {
+ m_callback(nullptr);
+ }
+ } catch (...) {
+ if (m_callback) {
+ m_callback(std::current_exception());
+ return;
+ }
+ throw;
+ }
+ }
+
+ void wait() override {}
+
+ bool wait_for(const std::chrono::milliseconds&) override {
+ return true;
+ }
+
+ void cancel() override {}
+
+ void set_callback(std::function<void(std::exception_ptr)> callback) override {
+ m_callback = std::move(callback);
+ }
+
+ void infer() override {
+ m_request->infer();
+ }
+
+ std::vector<ov::ProfilingInfo> get_profiling_info() const override {
+ return m_request->get_profiling_info();
+ }
+
+ ov::SoPtr<ov::ITensor> get_tensor(const ov::Output<const ov::Node>& port) const override {
+ return m_request->get_tensor(port);
+ }
+
+ void set_tensor(const ov::Output<const ov::Node>& port, const ov::SoPtr<ov::ITensor>& tensor) override {
+ m_request->set_tensor(port, tensor);
+ }
+
+ std::vector<ov::SoPtr<ov::ITensor>> get_tensors(const ov::Output<const ov::Node>& port) const override {
+ return m_request->get_tensors(port);
+ }
+
+ void set_tensors(const ov::Output<const ov::Node>& port,
+ const std::vector<ov::SoPtr<ov::ITensor>>& tensors) override {
+ m_request->set_tensors(port, tensors);
+ }
+
+ std::vector<ov::SoPtr<ov::IVariableState>> query_state() const override {
+ return m_request->query_state();
+ }
+
+ const std::shared_ptr<const ov::ICompiledModel>& get_compiled_model() const override {
+ return m_request->get_compiled_model();
+ }
+
+ const std::vector<ov::Output<const ov::Node>>& get_inputs() const override {
+ return m_request->get_inputs();
+ }
+
+ const std::vector<ov::Output<const ov::Node>>& get_outputs() const override {
+ return m_request->get_outputs();
+ }
+
+private:
+ std::shared_ptr<ov::ISyncInferRequest> m_request;
+ std::function<void(std::exception_ptr)> m_callback;
+};
+
+ class FakeSubCompiledModel final : public ov::ICompiledModel {
+public:
+ FakeSubCompiledModel(const std::shared_ptr<ov::Model>& model, const std::shared_ptr<const ov::IPlugin>& plugin)
+ : ov::ICompiledModel(model, plugin, nullptr, nullptr),
+ m_model(model) {}
+
+ void export_model(std::ostream&) const override {}
+ std::shared_ptr<const ov::Model> get_runtime_model() const override {
+ return m_model;
+ }
+ void set_property(const ov::AnyMap&) override {}
+ ov::Any get_property(const std::string& name) const override {
+ if (name == ov::execution_devices.name()) {
+ return std::vector<std::string>{"CPU"};
+ }
+ OPENVINO_THROW("Unsupported property: ", name);
+ }
+ std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override {
+ auto self = std::static_pointer_cast<const FakeSubCompiledModel>(shared_from_this());
+ return std::make_shared<FakeSubInferRequest>(std::move(self));
+ }
+ std::shared_ptr<ov::IAsyncInferRequest> create_infer_request() const override {
+ return std::make_shared<FakeSubAsyncInferRequest>(create_sync_infer_request());
+ }
+
+private:
+ std::shared_ptr<ov::Model> m_model;
+};
+
+FakeSubInferRequest::FakeSubInferRequest(std::shared_ptr<const FakeSubCompiledModel> compiled_model)
+ : ov::ISyncInferRequest(std::move(compiled_model)) {
+ for (const auto& input : get_compiled_model()->inputs()) {
+ ov::ISyncInferRequest::set_tensor(input,
+ ov::get_tensor_impl(ov::Tensor(input.get_element_type(), input.get_shape())));
+ }
+ for (const auto& output : get_compiled_model()->outputs()) {
+ ov::ISyncInferRequest::set_tensor(output,
+ ov::get_tensor_impl(ov::Tensor(output.get_element_type(), output.get_shape())));
+ }
+}
+
+void FakeSubInferRequest::infer() {
+ for (const auto& output : get_compiled_model()->outputs()) {
+ auto tensor = ov::ISyncInferRequest::get_tensor(output);
+ std::memset(tensor->data(), 0, tensor->get_byte_size());
+ }
+}
+
+class SubgraphBehaviorInferTest : public ::testing::Test {
+protected:
+ ov::AnyMap base_props() const {
+ return {{"NPU_USE_NPUW", "YES"},
+ {"NPUW_DEVICES", "CPU"},
+ {"NPUW_UNFOLD_IREQS", "NO"},
+ {"NPUW_ATTN", "DYNAMIC"},
+ {"NPUW_ONLINE_PIPELINE", "REP"},
+ {"NPUW_ONLINE_ISOLATE", "ATTN"}};
+ }
+
+ ov::AnyMap unfold_props() const {
+ auto props = base_props();
+ props["NPUW_UNFOLD_IREQS"] = "YES";
+ return props;
+ }
+ std::shared_ptr<testing::NiceMock<ov::MockICore>> make_core(const std::shared_ptr<const ov::IPlugin>& plugin) const {
+ auto core = std::make_shared<testing::NiceMock<ov::MockICore>>();
+
+ ON_CALL(*core, get_supported_property(testing::_, testing::_, testing::_))
+ .WillByDefault([](const std::string&, const ov::AnyMap& properties, const bool) {
+ return properties;
+ });
+ ON_CALL(*core, get_property(testing::_, testing::_, testing::_))
+ .WillByDefault([](const std::string&, const std::string& name, const ov::AnyMap&) -> ov::Any {
+ if (name == ov::available_devices.name()) {
+ return std::vector<std::string>{};
+ }
+ if (name == ov::intel_npu::compiler_version.name()) {
+ return int64_t{0};
+ }
+ if (name == ov::device::architecture.name()) {
+ return std::string{};
+ }
+ if (name == ov::supported_properties.name() || name == ov::internal::supported_properties.name()) {
+ return std::vector<ov::PropertyName>{};
+ }
+ return {};
+ });
+ ON_CALL(*core, get_property(testing::_, testing::_))
+ .WillByDefault([](const std::string&, const std::string& name) -> ov::Any {
+ if (name == ov::available_devices.name()) {
+ return std::vector<std::string>{};
+ }
+ if (name == ov::supported_properties.name()) {
+ return std::vector<ov::PropertyName>{};
+ }
+ if (name == ov::intel_npu::compiler_version.name()) {
+ return static_cast<int64_t>(0);
+ }
+ if (name == ov::device::architecture.name()) {
+ return std::string{};
+ }
+ return {};
+ });
+ ON_CALL(*core,
+ compile_model(testing::Matcher<const std::shared_ptr<const ov::Model>&>(testing::_),
+ testing::Matcher<const std::string&>(testing::StrEq("CPU")),
+ testing::Matcher<const ov::AnyMap&>(testing::_)))
+ .WillByDefault([plugin](const std::shared_ptr<const ov::Model>& submodel, const std::string&, const ov::AnyMap&) {
+ return ov::SoPtr<ov::ICompiledModel>{std::make_shared<FakeSubCompiledModel>(
+ std::const_pointer_cast<ov::Model>(submodel), plugin)};
+ });
+
+ return core;
+ }
+};
+
+TEST_F(SubgraphBehaviorInferTest, SdpaBehaviorCanOverrideStaticLlmSubgraphExecution) {
+ auto baseline_model = build_static_llm_model();
+ ASSERT_GT(count_sdpa_nodes(baseline_model), 0u) << "The synthesized LLM model must contain SDPA nodes";
+ auto hits = std::make_shared<BehaviorHits>();
+
+ auto plugin = std::make_shared<TestPlugin>();
+ auto core = make_core(plugin);
+ plugin->set_core(core);
+
+ auto baseline_compiled = std::make_shared<ov::npuw::CompiledModel>(baseline_model, plugin, base_props());
+ EXPECT_EQ(count_runtime_behaviors(baseline_compiled), 0u);
+ auto baseline_request = baseline_compiled->create_infer_request();
+ ASSERT_NE(baseline_request, nullptr);
+ baseline_request->infer();
+ EXPECT_TRUE(hits->values.empty());
+
+ auto behavior_model = build_static_llm_model();
+ ASSERT_GT(count_sdpa_nodes(behavior_model), 0u);
+ ov::npuw::v1::subgraphs::PatternRegistry behavior_registry;
+ auto behavior_compiled = std::make_shared<ov::npuw::CompiledModel>(behavior_model, plugin, base_props(), &behavior_registry);
+ bool attached_behavior = false;
+ for (auto& desc : behavior_compiled->m_compiled_submodels) {
+ if (!desc.compiled_model) {
+ continue;
+ }
+
+ ov::npuw::v1::subgraphs::RuntimeBehaviorSpec spec;
+ spec.registration.group = "test";
+ spec.registration.name = "record-hit";
+ spec.context.put<std::shared_ptr<BehaviorHits>>(hits);
+ spec.factory = [](const ov::npuw::v1::subgraphs::Context& ctx) -> ov::npuw::v1::subgraphs::ISubgraphBehavior::Ptr {
+ const auto recorder = ctx.get<std::shared_ptr<BehaviorHits>>();
+ return std::make_unique<ov::npuw::v1::subgraphs::DirectBehavior>(
+ [recorder](ov::npuw::v1::subgraphs::InferContext& infer_ctx) {
+ infer_ctx.legacy_infer();
+ std::lock_guard<std::mutex> lock(recorder->mutex);
+ recorder->values.emplace_back(infer_ctx.subgraph_idx, infer_ctx.real_subgraph_idx);
+ });
+ };
+ desc.pipeline.runtime_behavior = std::move(spec);
+ attached_behavior = true;
+ }
+ ASSERT_TRUE(attached_behavior) << "No compiled subgraph was available for runtime behavior injection";
+ auto behavior_request = behavior_compiled->create_infer_request();
+ ASSERT_NE(behavior_request, nullptr);
+ behavior_request->infer();
+
+ ASSERT_FALSE(hits->values.empty()) << "The SDPA stub behavior was not invoked during inference";
+}
+
+TEST_F(SubgraphBehaviorInferTest, RuntimeBehaviorForcesJustInferRequestWhenUnfoldIsEnabled) {
+ auto plugin = std::make_shared<TestPlugin>();
+ auto core = make_core(plugin);
+ plugin->set_core(core);
+
+ auto baseline_model = build_static_llm_model();
+ auto baseline_compiled = std::make_shared<ov::npuw::CompiledModel>(baseline_model, plugin, unfold_props());
+ auto baseline_request = baseline_compiled->create_sync_infer_request();
+ ASSERT_NE(baseline_request, nullptr);
+ EXPECT_NE(std::dynamic_pointer_cast<ov::npuw::UnfoldInferRequest>(baseline_request), nullptr);
+
+ auto behavior_model = build_static_llm_model();
+ auto hits = std::make_shared<BehaviorHits>();
+ ov::npuw::v1::subgraphs::PatternRegistry behavior_registry;
+ auto behavior_compiled = std::make_shared<ov::npuw::CompiledModel>(behavior_model, plugin, unfold_props(), &behavior_registry);
+
+ bool attached_behavior = false;
+ for (auto& desc : behavior_compiled->m_compiled_submodels) {
+ if (!desc.compiled_model) {
+ continue;
+ }
+
+ ov::npuw::v1::subgraphs::RuntimeBehaviorSpec spec;
+ spec.registration.group = "test";
+ spec.registration.name = "record-hit";
+ spec.context.put<std::shared_ptr<BehaviorHits>>(hits);
+ spec.factory = [](const ov::npuw::v1::subgraphs::Context& ctx) -> ov::npuw::v1::subgraphs::ISubgraphBehavior::Ptr {
+ const auto recorder = ctx.get<std::shared_ptr<BehaviorHits>>();
+ return std::make_unique<ov::npuw::v1::subgraphs::DirectBehavior>(
+ [recorder](ov::npuw::v1::subgraphs::InferContext& infer_ctx) {
+ infer_ctx.legacy_infer();
+ std::lock_guard<std::mutex> lock(recorder->mutex);
+ recorder->values.emplace_back(infer_ctx.subgraph_idx, infer_ctx.real_subgraph_idx);
+ });
+ };
+ desc.pipeline.runtime_behavior = std::move(spec);
+ attached_behavior = true;
+ }
+ ASSERT_TRUE(attached_behavior);
+
+ auto behavior_request = behavior_compiled->create_sync_infer_request();
+ ASSERT_NE(behavior_request, nullptr);
+ EXPECT_NE(std::dynamic_pointer_cast<ov::npuw::JustInferRequest>(behavior_request), nullptr);
+ EXPECT_EQ(std::dynamic_pointer_cast<ov::npuw::UnfoldInferRequest>(behavior_request), nullptr);
+}
+
+} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/subgraph_pipeline.cpp b/src/plugins/intel_npu/tests/unit/npuw/subgraph_pipeline.cpp
new file mode 100644
index 00000000000..9fea01d5952
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/subgraph_pipeline.cpp
@@ -0,0 +1,100 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <string>
+
+#include "partitioning/partitioning.hpp"
+#include "partitioning/patterns/sdpa.hpp"
+#include "v1/subgraph_pipeline.hpp"
+
+namespace {
+
+struct TestPayload {
+ int value = 0;
+ std::string name;
+
+ TestPayload() = default;
+ TestPayload(int value, std::string name) : value(value), name(std::move(name)) {}
+};
+
+} // namespace
+
+TEST(SubgraphPipelineContextTest, StoresAndRetrievesTypedValues) {
+ ov::npuw::v1::subgraphs::Context ctx;
+
+ auto& label = ctx.put<std::string>("moe");
+ auto& payload = ctx.emplace<TestPayload>(7, "router");
+
+ EXPECT_EQ(label, "moe");
+ EXPECT_EQ(payload.value, 7);
+ EXPECT_EQ(payload.name, "router");
+ EXPECT_TRUE(ctx.contains<std::string>());
+ EXPECT_TRUE(ctx.contains<TestPayload>());
+ EXPECT_EQ(ctx.size(), 2u);
+ EXPECT_EQ(ctx.get<std::string>(), "moe");
+
+ const auto* found = ctx.get_if<TestPayload>();
+ ASSERT_NE(found, nullptr);
+ EXPECT_EQ(found->value, 7);
+ EXPECT_EQ(found->name, "router");
+}
+
+TEST(SubgraphPipelineContextTest, MissingTypeReturnsNull) {
+ ov::npuw::v1::subgraphs::Context ctx;
+
+ EXPECT_FALSE(ctx.contains<int>());
+ EXPECT_EQ(ctx.get_if<int>(), nullptr);
+ EXPECT_TRUE(ctx.empty());
+}
+
+TEST(SubgraphPipelineFunctionTest, FunctionTagSeedsPipelineRegistrationPattern) {
+ ov::npuw::Function function;
+
+ function.settag("GPTOSSExpert");
+
+ EXPECT_EQ(function.gettag(), "GPTOSSExpert");
+ ASSERT_EQ(function._pipeline.registration.patterns.size(), 1u);
+ EXPECT_EQ(function._pipeline.registration.patterns.front(), "GPTOSSExpert");
+}
+
+TEST(SubgraphPipelineBehaviorTest, FactoryCreatesBehaviorObject) {
+ auto behavior = ov::npuw::v1::subgraphs::make_direct_behavior();
+
+ EXPECT_NE(behavior, nullptr);
+}
+
+TEST(SubgraphPipelineBehaviorTest, RealPatternRegistrationBuildsRuntimeBehaviorForTaggedSubgraph) {
+ ov::npuw::Subgraph subgraph;
+ subgraph.settag(ov::npuw::patterns::attn::SDPA::isolation_tag());
+ ov::npuw::v1::subgraphs::PatternRegistry registry;
+
+ auto scoped_registration = registry.on<ov::npuw::patterns::attn::SDPA>()
+ .at_compile([](ov::npuw::v1::subgraphs::CompiledPipeline&,
+ ov::npuw::v1::subgraphs::Context& ctx) {
+ ctx.put<std::string>("marker");
+ })
+ .at_runtime([](const ov::npuw::v1::subgraphs::Context& ctx)
+ -> ov::npuw::v1::subgraphs::ISubgraphBehavior::Ptr {
+ EXPECT_EQ(ctx.get<std::string>(), "marker");
+ return ov::npuw::v1::subgraphs::make_direct_behavior();
+ })
+ .scoped();
+
+ registry.apply(subgraph);
+
+ ASSERT_TRUE(static_cast<bool>(subgraph._pipeline.compile_stage));
+ ov::npuw::v1::subgraphs::CompiledPipeline compiled_pipeline;
+ compiled_pipeline.registration = subgraph._pipeline.registration;
+ compiled_pipeline.context = subgraph._pipeline.context;
+ subgraph._pipeline.compile_stage(compiled_pipeline, compiled_pipeline.context);
+
+ ASSERT_TRUE(compiled_pipeline.runtime_behavior.has_value());
+ EXPECT_EQ(compiled_pipeline.registration.name, ov::npuw::patterns::attn::SDPA::pattern_name());
+ EXPECT_EQ(compiled_pipeline.runtime_behavior->registration.name,
+ ov::npuw::patterns::attn::SDPA::pattern_name());
+ auto behavior = compiled_pipeline.runtime_behavior->factory(compiled_pipeline.runtime_behavior->context);
+ EXPECT_NE(behavior, nullptr);
+}