Skip to content

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

[NPUW] Add RopeCache support for multiple independent RoPE blocks (#38843)

Guoliang Shi2fbf5de3bb18
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cpp
index bc01a6b17fe..6e7f533d643 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.cpp
@@ -169,10 +169,7 @@ ov::npuw::patterns::pre_compute::RopePatternLLama2::RopePatternLLama2() : matche
auto output_sin = opp::wrap_type<ov::op::v0::Sin>({concat_2});
auto output_cos = opp::wrap_type<ov::op::v0::Cos>({concat_2});
- init_cb = [=](const auto& matches) {
- const auto& map_sin = matches.at(output_sin)[0];
- const auto& map_cos = matches.at(output_cos)[0];
-
+ bind_match = [=](const auto& map_sin, const auto& map_cos) {
this->matched_position_ids = map_sin.at(position_ids).get_node_shared_ptr();
this->matched_concat = map_sin.at(concat_1).get_node_shared_ptr();
this->matched_inv_freq = map_sin.at(inv_freq).get_node_shared_ptr();
@@ -187,11 +184,9 @@ ov::npuw::patterns::pre_compute::RopePatternLLama2::RopePatternLLama2() : matche
LOG_VERB("Rope found : sin=" << matched_sin->get_name() << ", cos=" << matched_cos->get_name()
<< " (duplicate_freqs=" << duplicate_freqs << ")");
-
- return true;
};
- matcher.register_patterns({output_sin, output_cos}, make_matcher_callback());
+ matcher.register_patterns({output_sin, output_cos}, make_matcher_callback(output_sin, output_cos));
}
ov::npuw::patterns::pre_compute::LongRopePatternPhi::LongRopePatternPhi() : matcher("sin-cos-matcher") {
@@ -245,10 +240,7 @@ ov::npuw::patterns::pre_compute::LongRopePatternPhi::LongRopePatternPhi() : matc
auto output_sin = opp::wrap_type<ov::op::v0::Sin>({concat_2});
auto output_cos = opp::wrap_type<ov::op::v0::Cos>({concat_2});
- init_cb = [=](const auto& matches) {
- const auto& map_sin = matches.at(output_sin)[0];
- const auto& map_cos = matches.at(output_cos)[0];
-
+ bind_match = [=](const auto& map_sin, const auto& map_cos) {
this->matched_position_ids = map_sin.at(position_ids).get_node_shared_ptr();
this->matched_concat = map_sin.at(concat_1).get_node_shared_ptr();
this->matched_inv_freq = map_sin.at(inv_freq_short).get_node_shared_ptr();
@@ -262,11 +254,9 @@ ov::npuw::patterns::pre_compute::LongRopePatternPhi::LongRopePatternPhi() : matc
this->matched_sin = map_sin.at(output_sin).get_node_shared_ptr();
LOG_VERB("Rope found : sin=" << matched_sin->get_name() << ", cos=" << matched_cos->get_name());
-
- return true;
};
- matcher.register_patterns({output_sin, output_cos}, make_matcher_callback());
+ matcher.register_patterns({output_sin, output_cos}, make_matcher_callback(output_sin, output_cos));
}
ov::npuw::patterns::pre_compute::LongRopePatternPhi_v5::LongRopePatternPhi_v5() : matcher("sin-cos-matcher") {
@@ -328,10 +318,7 @@ ov::npuw::patterns::pre_compute::LongRopePatternPhi_v5::LongRopePatternPhi_v5()
auto output_sin = opp::wrap_type<ov::op::v0::Sin>({concat_2});
auto output_cos = opp::wrap_type<ov::op::v0::Cos>({concat_2});
- init_cb = [=](const auto& matches) {
- const auto& map_sin = matches.at(output_sin)[0];
- const auto& map_cos = matches.at(output_cos)[0];
-
+ bind_match = [=](const auto& map_sin, const auto& map_cos) {
this->matched_position_ids = map_sin.at(position_ids).get_node_shared_ptr();
this->matched_concat = map_sin.at(concat_1).get_node_shared_ptr();
this->matched_short_factor = map_sin.at(short_factor).get_node_shared_ptr();
@@ -347,11 +334,9 @@ ov::npuw::patterns::pre_compute::LongRopePatternPhi_v5::LongRopePatternPhi_v5()
this->matched_sin = map_sin.at(output_sin).get_node_shared_ptr();
LOG_VERB("Rope found : sin=" << matched_sin->get_name() << ", cos=" << matched_cos->get_name());
-
- return true;
};
- matcher.register_patterns({output_sin, output_cos}, make_matcher_callback());
+ matcher.register_patterns({output_sin, output_cos}, make_matcher_callback(output_sin, output_cos));
}
namespace {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hpp b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hpp
index e365a881649..c616cbbfe4b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/partitioning/patterns/pre_compute.hpp
@@ -7,6 +7,7 @@
#include <cstdint>
#include <optional>
#include <string>
+#include <unordered_map>
#include <vector>
#include "openvino/pass/pattern/multi_matcher.hpp"
@@ -21,7 +22,9 @@ namespace ov ::npuw ::patterns ::pre_compute {
class RopePatternDesc {
protected:
- ov::pass::MultiMatcher::Callback init_cb;
+ std::function<void(const ov::pass::pattern::PatternValueMap& map_sin,
+ const ov::pass::pattern::PatternValueMap& map_cos)>
+ bind_match;
public:
std::shared_ptr<ov::Node> matched_inv_freq;
@@ -33,10 +36,28 @@ public:
std::function<void()> transform_cb;
- ov::pass::MultiMatcher::Callback make_matcher_callback() {
- return [this](const auto& matches) {
- init_cb(matches);
- transform_cb();
+ // Support models with multiple independent RoPE blocks (e.g. hybrid/sliding window
+ // attention): binds and transforms every match found for (output_sin, output_cos).
+ ov::pass::MultiMatcher::Callback make_matcher_callback(const std::shared_ptr<ov::Node>& output_sin,
+ const std::shared_ptr<ov::Node>& output_cos) {
+ return [this, output_sin, output_cos](const auto& matches) {
+ const auto& sin_matches = matches.at(output_sin);
+ const auto& cos_matches = matches.at(output_cos);
+
+ std::unordered_map<ov::Node*, const ov::pass::pattern::PatternValueMap*> cos_by_shared_input;
+ for (const auto& cos_match : cos_matches) {
+ cos_by_shared_input[cos_match.at(output_cos).get_node()->input_value(0).get_node()] = &cos_match;
+ }
+
+ for (const auto& sin_match : sin_matches) {
+ auto* shared_input = sin_match.at(output_sin).get_node()->input_value(0).get_node();
+ auto it = cos_by_shared_input.find(shared_input);
+ if (it == cos_by_shared_input.end()) {
+ continue; // no matching Cos for this Sin - leave it untouched
+ }
+ bind_match(sin_match, *it->second);
+ transform_cb();
+ }
};
}
};
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pre_compute_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pre_compute_test.cpp
index 2d52493121d..0a841903678 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/pre_compute_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/pre_compute_test.cpp
@@ -180,6 +180,80 @@ std::shared_ptr<ov::Model> make_rope_model(bool with_concat2) {
with_concat2 ? "llama2_rope_test_model" : "gpt_rope_test_model");
}
+// Builds a Gemma 4-style model with hybrid attention, it has multiple independent RoPE blocks.
+// `result_order`, as (block index, is_cos) pairs, lets a test pick a non-default Results
+// ordering; empty means the natural per-block (sin_i, cos_i) grouping.
+std::shared_ptr<ov::Model> make_multi_rope_model(const std::vector<std::vector<float>>& inv_freqs,
+ const std::vector<std::pair<size_t, bool>>& result_order = {}) {
+ auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 4, 4});
+ auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::Shape{1, 4});
+
+ // Shared shape-compute chain (ShapeOf->Gather->Concat_1).
+ auto shape_of = std::make_shared<ov::op::v3::ShapeOf>(data);
+ auto gather_idx = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});
+ auto gather_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+ auto gather = std::make_shared<ov::op::v8::Gather>(shape_of, gather_idx, gather_axis);
+ auto ndims_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});
+ auto one_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});
+ auto concat_1 = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{gather, ndims_const, one_const}, 0);
+
+ // Shared position_ids branch (Unsqueeze->Convert).
+ auto unsq_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});
+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(position_ids, unsq_axis);
+ auto convert = std::make_shared<ov::op::v0::Convert>(unsqueeze, ov::element::f32);
+
+ std::vector<ov::Output<ov::Node>> sin_outs(inv_freqs.size());
+ std::vector<ov::Output<ov::Node>> cos_outs(inv_freqs.size());
+ for (size_t i = 0; i < inv_freqs.size(); ++i) {
+ const auto& freqs = inv_freqs[i];
+ auto inv_freq = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1, freqs.size(), 1}, freqs);
+
+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(inv_freq, concat_1);
+ auto matmul = std::make_shared<ov::op::v0::MatMul>(broadcast, convert); // [1, freqs.size(), 4]
+ auto perm = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, std::vector<int64_t>{0, 2, 1});
+ auto transpose = std::make_shared<ov::op::v1::Transpose>(matmul, perm); // [1, 4, freqs.size()]
+
+ auto zeros = ov::op::v0::Constant::create(ov::element::f32,
+ ov::Shape{1, 4, freqs.size()},
+ std::vector<float>(4 * freqs.size(), 0.f));
+ auto concat_2 = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{transpose, zeros}, -1);
+
+ auto sin = std::make_shared<ov::op::v0::Sin>(concat_2);
+ auto cos = std::make_shared<ov::op::v0::Cos>(concat_2);
+ sin->set_friendly_name("sin_" + std::to_string(i));
+ cos->set_friendly_name("cos_" + std::to_string(i));
+
+ sin_outs[i] = sin->output(0);
+ cos_outs[i] = cos->output(0);
+ }
+
+ ov::ResultVector results;
+ if (result_order.empty()) {
+ for (size_t i = 0; i < inv_freqs.size(); ++i) {
+ results.push_back(std::make_shared<ov::op::v0::Result>(sin_outs[i]));
+ results.push_back(std::make_shared<ov::op::v0::Result>(cos_outs[i]));
+ }
+ } else {
+ for (const auto& entry : result_order) {
+ results.push_back(
+ std::make_shared<ov::op::v0::Result>(entry.second ? cos_outs[entry.first] : sin_outs[entry.first]));
+ }
+ }
+
+ return std::make_shared<ov::Model>(results, ov::ParameterVector{data, position_ids}, "multi_rope_test_model");
+}
+
+// A model with one full RoPE block (Sin+Cos) plus a second, independent Cos with no Sin partner.
+std::shared_ptr<ov::Model> make_rope_model_with_lone_cos(const std::vector<float>& paired_freqs,
+ const std::vector<float>& lone_cos_freqs) {
+ auto model = make_multi_rope_model({paired_freqs, lone_cos_freqs});
+
+ // Drop the second block's Sin result, turning it into a Cos-only chain.
+ auto results = model->get_results();
+ model->remove_result(results[2]); // sin_1
+ return model;
+}
+
bool has_input_name(const std::shared_ptr<ov::Model>& model, const std::string& name) {
const auto inputs = model->inputs();
return std::any_of(inputs.begin(), inputs.end(), [&name](const auto& input) {
@@ -425,4 +499,101 @@ TEST(PreComputeTest, RopeCacheTransformsGPTPattern) {
EXPECT_EQ(cos_count, 0) << "Cos should be replaced by Gather from the non-duplicated LUT";
}
+// A model can contain more than one independent RoPE block (e.g. Gemma 4's hybrid attention)
+// and every block must be cached, not just the first.
+TEST(PreComputeTest, RopeCacheTransformsTwoIndependentRopeBlocks) {
+ auto model = make_multi_rope_model({{0.5f, 0.1f}, {0.2f, 0.05f}});
+
+ ov::npuw::patterns::pre_compute::RopeCache pass(/*max_prompt_len=*/16);
+ ASSERT_NO_THROW(pass.run_on_model(model));
+
+ EXPECT_EQ(count_ops_of_type_sin_cos(model), 0u) << "both RoPE blocks must be cached, not just the first";
+
+ const auto& ops = model->get_ops();
+ const auto gather_count = std::count_if(ops.begin(), ops.end(), [](const auto& op) {
+ return ov::is_type<ov::op::v8::Gather>(op);
+ });
+ EXPECT_EQ(gather_count, 4u) << "one Gather for cos and one for sin, per block";
+
+ // Each block must get its own cache, not accidentally share one.
+ std::vector<const ov::op::v0::Constant*> caches;
+ for (const auto& op : ops) {
+ if (auto gather_op = ov::as_type_ptr<ov::op::v8::Gather>(op)) {
+ if (auto cache = ov::as_type_ptr<ov::op::v0::Constant>(gather_op->input_value(0).get_node_shared_ptr())) {
+ caches.push_back(cache.get());
+ }
+ }
+ }
+ ASSERT_EQ(caches.size(), 4u);
+ for (size_t i = 0; i < caches.size(); ++i) {
+ for (size_t j = i + 1; j < caches.size(); ++j) {
+ EXPECT_NE(std::memcmp(caches[i]->get_data_ptr(), caches[j]->get_data_ptr(), caches[i]->get_byte_size()), 0)
+ << "cache constants " << i << " and " << j << " are unexpectedly identical";
+ }
+ }
+}
+
+// Reads the f16 cache row a Result's Squeeze->Convert->Gather chain was bound to.
+std::vector<float> read_cache_row(const ov::Output<ov::Node>& result_input, size_t row) {
+ auto convert = result_input.get_node()->input_value(0).get_node();
+ auto gather = convert->input_value(0).get_node();
+ auto cache = ov::as_type_ptr<ov::op::v0::Constant>(gather->input_value(0).get_node_shared_ptr());
+ OPENVINO_ASSERT(cache, "Expected a cache Constant feeding the Gather");
+
+ const auto* data = cache->get_data_ptr<ov::float16>();
+ const auto rotary_ndims = cache->get_shape().back();
+ std::vector<float> values(rotary_ndims);
+ for (size_t k = 0; k < rotary_ndims; ++k) {
+ values[k] = static_cast<float>(data[row * rotary_ndims + k]);
+ }
+ return values;
+}
+
+// Match pairing must key off the shared input node, not vector index - MultiMatcher doesn't
+// guarantee a block's Sin and Cos land at the same index. Results are ordered here so naive
+// index-based pairing would cross-wire the two blocks' caches.
+TEST(PreComputeTest, RopeCacheTransformsTwoIndependentRopeBlocksWithScrambledMatchOrder) {
+ auto model = make_multi_rope_model({{0.5f, 0.1f}, {0.2f, 0.05f}},
+ /*result_order=*/{{0, false}, {1, false}, {1, true}, {0, true}});
+
+ ov::npuw::patterns::pre_compute::RopeCache pass(/*max_prompt_len=*/16);
+ ASSERT_NO_THROW(pass.run_on_model(model));
+
+ const auto& results = model->get_results();
+ auto sin0 = read_cache_row(results[0]->input_value(0), /*row=*/1);
+ auto sin1 = read_cache_row(results[1]->input_value(0), /*row=*/1);
+ auto cos1 = read_cache_row(results[2]->input_value(0), /*row=*/1);
+ auto cos0 = read_cache_row(results[3]->input_value(0), /*row=*/1);
+
+ for (size_t k = 0; k < sin0.size(); ++k) {
+ EXPECT_NEAR(cos0[k] * cos0[k] + sin0[k] * sin0[k], 1.0f, 0.01f) << "block 0 cos/sin mismatch at " << k;
+ EXPECT_NEAR(cos1[k] * cos1[k] + sin1[k] * sin1[k], 1.0f, 0.01f) << "block 1 cos/sin mismatch at " << k;
+ }
+}
+
+// A pattern match without its sin/cos counterpart (e.g. an unrelated Cos-only computation that
+// coincidentally matches the same antecedent chain) must be left in place, not rejected.
+TEST(PreComputeTest, RopeCacheLeavesUnpairedCosUntouched) {
+ auto model = make_rope_model_with_lone_cos({0.5f, 0.1f}, {0.2f, 0.05f});
+
+ ov::npuw::patterns::pre_compute::RopeCache pass(/*max_prompt_len=*/16);
+ ASSERT_NO_THROW(pass.run_on_model(model));
+
+ const auto& ops = model->get_ops();
+ EXPECT_EQ(std::count_if(ops.begin(),
+ ops.end(),
+ [](const auto& op) {
+ return ov::is_type<ov::op::v0::Sin>(op);
+ }),
+ 0)
+ << "the paired Sin must be cached away";
+ EXPECT_EQ(std::count_if(ops.begin(),
+ ops.end(),
+ [](const auto& op) {
+ return ov::is_type<ov::op::v0::Cos>(op);
+ }),
+ 1)
+ << "only the lone, unpaired Cos should remain";
+}
+
} // namespace