Skip to content

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

[NPUW] Bypass keep_blocks count check when merging groups with isolation tag in keep_block_tags (#37342)

Alexander Kalistratove505dc869016
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 67ae14b88f3..cdd4a1640f7 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
@@ -1020,7 +1020,12 @@ std::shared_ptr<Repeated> Snapshot::tryMergeTriangles(const std::vector<Group::G
return {};
}
- if (prods.size() < m_ctx.keep_blocks) {
+ // Only bypass the keep_blocks size floor for tags the user explicitly asked to preserve via
+ // NPUW_ONLINE_KEEP_BLOCKS_TAGGED
+ const auto& isolate_tag = prods.front()->isolatedTag();
+ const bool has_isolated_tag =
+ !isolate_tag.empty() && m_ctx.keep_block_tags.find(isolate_tag) != m_ctx.keep_block_tags.end();
+ if (!has_isolated_tag && prods.size() < m_ctx.keep_blocks) {
// In some cases (specifically mixed precision) during MergeUniques() pass we could be left with
// E.g. 10 repeated blocks with tag AAA and 2 repeated blocks with tag BBB
// TryMergeTriangles() pass checks that producer and consumer have a different tag to be merged further.
@@ -1309,7 +1314,12 @@ std::shared_ptr<Repeated> Snapshot::tryMergeRepeating(const std::vector<Group::G
}
}
- if (prods.size() < m_ctx.keep_blocks) {
+ // Only bypass the keep_blocks size floor for tags the user explicitly asked to preserve via
+ // NPUW_ONLINE_KEEP_BLOCKS_TAGGED
+ const auto& isolate_tag = conss.front()->isolatedTag();
+ const bool has_isolated_tag =
+ !isolate_tag.empty() && m_ctx.keep_block_tags.find(isolate_tag) != m_ctx.keep_block_tags.end();
+ if (!has_isolated_tag && prods.size() < m_ctx.keep_blocks) {
// In some cases (specifically mixed precision) during MergeUniques() pass we could be left with
// E.g. 10 repeated blocks with tag AAA and 2 repeated blocks with tag BBB
// TryMergeRepeating() pass checks that producer and consumer have a different tag to be merged further.
diff --git a/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cpp
index a117c07d75c..0ae7e263b96 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/partitioning_options_test.cpp
@@ -10,6 +10,7 @@
#include <fstream>
#include <map>
#include <memory>
+#include <set>
#include <string>
#include "attention.hpp"
@@ -218,6 +219,50 @@ std::shared_ptr<ov::Model> build_abc_attn_model(std::size_t repetitions = 30) {
return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input});
}
+// Build a chain with exactly two Relu and two Sigmoid ops, but only ONE
+// Relu -> Sigmoid adjacency. The unary filler ops are all distinct types so they
+// never form repeating families of their own, and the chain is long enough to stay
+// above NPUW_ONLINE_MIN_SIZE (which is clamped to 10 groups).
+std::shared_ptr<ov::Model> build_single_pair_model() {
+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 32});
+ input->set_friendly_name("input");
+
+ std::shared_ptr<ov::Node> prev = input;
+ const auto chain = [&](const std::shared_ptr<ov::Node>& node, const std::string& name) {
+ node->set_friendly_name(name);
+ prev = node;
+ };
+
+ chain(std::make_shared<ov::op::v0::Relu>(prev), "relu_0");
+ chain(std::make_shared<ov::op::v0::Sigmoid>(prev), "sigmoid_0");
+ chain(std::make_shared<ov::op::v0::Abs>(prev), "abs_0");
+ chain(std::make_shared<ov::op::v0::Ceiling>(prev), "ceil_0");
+ chain(std::make_shared<ov::op::v0::Floor>(prev), "floor_0");
+ chain(std::make_shared<ov::op::v0::Sqrt>(prev), "sqrt_0");
+ chain(std::make_shared<ov::op::v0::Exp>(prev), "exp_0");
+ chain(std::make_shared<ov::op::v0::Log>(prev), "log_0");
+ chain(std::make_shared<ov::op::v0::Sin>(prev), "sin_0");
+ chain(std::make_shared<ov::op::v0::Cos>(prev), "cos_0");
+ chain(std::make_shared<ov::op::v0::Negative>(prev), "neg_0");
+ chain(std::make_shared<ov::op::v0::Relu>(prev), "relu_1");
+ chain(std::make_shared<ov::op::v0::Tanh>(prev), "tanh_0");
+ chain(std::make_shared<ov::op::v0::Sigmoid>(prev), "sigmoid_1");
+
+ auto result = std::make_shared<ov::op::v0::Result>(prev);
+ result->set_friendly_name("output");
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input});
+}
+
+// Partition boundaries as an order-independent set of layer sets, so the assertions
+// don't depend on the (unspecified) group or intra-group layer ordering.
+std::set<std::set<std::string>> partition_boundaries(const ov::npuw::Ensemble& ens) {
+ std::set<std::set<std::string>> boundaries;
+ for (const auto& group : ens.groups) {
+ boundaries.emplace(group.all_layers.begin(), group.all_layers.end());
+ }
+ return boundaries;
+}
+
TEST(PartitioningOptionsTest, PipelineNoneMergesUnaryModelIntoSingleGroup) {
auto cfg = make_cfg({{"NPUW_ONLINE_PIPELINE", "NONE"}});
auto ens = ov::npuw::online::buildPartitioning(build_unary_chain_model(), cfg);
@@ -487,6 +532,95 @@ TEST(PartitioningOptionsTest, OnlyAttnIsolatedFamiliesIgnoreKeepBlockSizeThresho
EXPECT_EQ(folded, N);
}
+// Same three op families, but all of them share one isolate tag, so producer and
+// consumer families can actually be merged into a single repeating block.
+static const ::intel_npu::Config::ConfigMap attn_only_base_cfg = {
+ {"NPUW_ONLINE_PIPELINE", "REP"},
+ {"NPUW_ONLINE_ISOLATE", "Op:Relu/attn,Op:Sigmoid/attn,Op:Tanh/attn"},
+ {"NPUW_ONLINE_KEEP_BLOCKS", "5"},
+ {"NPUW_ONLINE_KEEP_BLOCK_SIZE", "1"},
+};
+
+TEST(PartitioningOptionsTest, KeepBlocksTaggedMergesFewerOccurrencesThanKeepBlocks) {
+ // 4 repetitions produce only 3 mergeable producer/consumer pairs - less than
+ // KEEP_BLOCKS=5. Without NPUW_ONLINE_KEEP_BLOCKS_TAGGED the count floor rejects
+ // the merge and the model stays at one group per op.
+ constexpr std::size_t N = 4;
+ auto model = build_abc_attn_model(N);
+
+ auto plain_cfg = make_cfg(attn_only_base_cfg);
+ auto plain_ens = ov::npuw::online::buildPartitioning(model, plain_cfg);
+
+ EXPECT_TRUE(plain_ens.repeated.empty());
+ EXPECT_EQ(partition_boundaries(plain_ens),
+ (std::set<std::set<std::string>>{{"relu_0", "sigmoid_0"},
+ {"relu_1", "tanh_0"},
+ {"sigmoid_1"},
+ {"tanh_1"},
+ {"relu_2"},
+ {"sigmoid_2"},
+ {"tanh_2"},
+ {"relu_3"},
+ {"sigmoid_3"},
+ {"tanh_3"}}));
+
+ auto tagged_map = attn_only_base_cfg;
+ tagged_map["NPUW_ONLINE_KEEP_BLOCKS_TAGGED"] = "attn";
+ auto tagged_cfg = make_cfg(tagged_map);
+ auto tagged_ens = ov::npuw::online::buildPartitioning(model, tagged_cfg);
+
+ // With the tag, the floor is bypassed and the 3 pairs are coalesced into a
+ // repeating block of 3 layers, leaving only the chain head and tail behind.
+ EXPECT_EQ(partition_boundaries(tagged_ens),
+ (std::set<std::set<std::string>>{{"relu_0"},
+ {"sigmoid_0"},
+ {"relu_1", "sigmoid_1", "tanh_0"},
+ {"relu_2", "sigmoid_2", "tanh_1"},
+ {"relu_3", "sigmoid_3", "tanh_2"},
+ {"tanh_3"}}));
+
+ // matches[i] is the set of instances of the i-th layer of the block, so a
+ // 3-layer block repeated 3 times is expected here.
+ const auto three_layer_block =
+ std::find_if(tagged_ens.repeated.begin(), tagged_ens.repeated.end(), [](const auto& rep) {
+ return rep.second.matches.size() == 3u;
+ });
+ ASSERT_NE(three_layer_block, tagged_ens.repeated.end());
+ EXPECT_EQ(three_layer_block->second.matches.front().size(), 3u);
+}
+
+TEST(PartitioningOptionsTest, KeepBlocksTaggedDoesNotCoalesceSingleOccurrencePairs) {
+ // Only relu_0 -> sigmoid_0 is a mergeable pair here; relu_1 and sigmoid_1 are
+ // separated by tanh_0. Merging that lone pair would split both two-instance
+ // families and leave degenerate one-instance repeated blocks, so the
+ // "at least two instances" guard stays in force even for a keep tag.
+ auto model = build_single_pair_model();
+
+ auto tagged_map = attn_only_base_cfg;
+ tagged_map["NPUW_ONLINE_KEEP_BLOCKS_TAGGED"] = "attn";
+ auto tagged_cfg = make_cfg(tagged_map);
+ auto tagged_ens = ov::npuw::online::buildPartitioning(model, tagged_cfg);
+
+ EXPECT_EQ(partition_boundaries(tagged_ens),
+ (std::set<std::set<std::string>>{{"relu_0"},
+ {"sigmoid_0"},
+ {"abs_0", "ceil_0"},
+ {"floor_0", "sqrt_0"},
+ {"exp_0", "log_0"},
+ {"sin_0", "cos_0"},
+ {"neg_0"},
+ {"relu_1"},
+ {"tanh_0"},
+ {"sigmoid_1"}}));
+
+ // The two tagged families are kept as-is: single-layer blocks with two instances each.
+ EXPECT_EQ(tagged_ens.repeated.size(), 2u);
+ for (const auto& rep : tagged_ens.repeated) {
+ EXPECT_EQ(rep.second.matches.size(), 1u);
+ EXPECT_EQ(rep.second.matches.front().size(), 2u);
+ }
+}
+
TEST(PartitioningOptionsTest, FuseUnfoldedMergesNonFoldOnlyRepeatedBlocks) {
// With NPUW_FUSE_UNFOLDED, blockA (Relu) and blockB (Sigmoid) groups lose
// their reptag and are merged by fuseRemnants (frozen attn blocks act as