OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[NPUW] Add a separate GatheredPositionIdsMatcher in AddPositionIdsParam (#37585)
Wei Wang12ce23f459f1
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cppindex eec22d1fa7e..f8f63200b2c 100644--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp@@ -4,6 +4,8 @@#include "add_position_ids_param.hpp"+#include <unordered_set>+#include "openvino/op/ops.hpp"#include "openvino/openvino.hpp"#include "openvino/pass/manager.hpp"@@ -29,6 +31,16 @@ void set_node_name(std::shared_ptr<ov::Node> node, const std::string& name) {node->get_output_tensor(0).set_names({name});}+// Creates a fresh `position_ids` [batch, seq] parameter plus a batch-squeezed [seq] view of it.+std::pair<std::shared_ptr<ov::op::v0::Parameter>, std::shared_ptr<ov::op::v0::Squeeze>> make_position_ids() {+ auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1, -1});+ set_node_name(position_ids, "position_ids");+ auto position_ids_squeezed =+ std::make_shared<ov::op::v0::Squeeze>(position_ids,+ ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}));+ return {position_ids, position_ids_squeezed};+}+// TODO: Consolidate with similar pattern in prepare_embedding_model.cppclass HardcodedPositionIdsMatcher : public ov::pass::MultiMatcher {public:@@ -87,8 +99,7 @@ public:auto convert_node = pattern_to_output.at(convert).get_node_shared_ptr();// Create `position_ids` parameter- auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1, -1});- set_node_name(position_ids, "position_ids");+ auto [position_ids, position_ids_squeezed] = make_position_ids();// Create new Unsqueeze node for point of branchingauto unsqueeze1_node_copy =@@ -99,9 +110,6 @@ public:// positions in old IRs.// It seems to right to use the newly created `position_ids` for it as well, however, real tests show// no difference against usage of hardcoded QRange: both are similarly accurate.- auto position_ids_squeezed = std::make_shared<ov::op::v0::Squeeze>(- position_ids,- ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}));OPENVINO_ASSERT(range_node->get_output_size() == 1, "Range node should have exactly one output");auto range_consumers = range_node->get_output_target_inputs(0);for (auto&& consumer : range_consumers) {@@ -119,6 +127,58 @@ public:}};+// Gather-based (cache-table) RoPE. Instead of computing cos/sin, ONNX GroupQueryAttention-style exports+// precompute cos/sin cache tables and index into them with the position ids. The `position_ids` therefore+// drives a Gather rather than a Cos/Sin, and the branch has no MatMul/Transpose/Concat at all:+//+// Range -> [Convert] -> Unsqueeze -> [Add(past_len)] -> Squeeze -> [Maximum] -> [Minimum] --> Gather (cos)+// --> Gather (sin)+//+// The Maximum/Minimum pair is a clip of the absolute position into the cache bounds; both the+// clip and the past-length Add are optional across exports.+class GatheredPositionIdsMatcher : public ov::pass::MultiMatcher {+public:+ OPENVINO_MATCHER_PASS_RTTI("ov::npuw::GatheredPositionIdsMatcher");+ explicit GatheredPositionIdsMatcher(ov::ParameterVector& new_params) {+ auto range = opp::wrap_type<ov::op::v4::Range>();+ auto convert = opp::optional<ov::op::v0::Convert>({range->output(0)});+ auto unsqueeze = opp::wrap_type<ov::op::v0::Unsqueeze>({convert, opp::any_input()});+ auto add = opp::optional<ov::op::v1::Add>({unsqueeze->output(0), opp::any_input()});+ auto squeeze = opp::wrap_type<ov::op::v0::Squeeze>({add, opp::any_input()});+ auto clip_max = opp::optional<ov::op::v1::Maximum>({squeeze->output(0), opp::any_input()});+ auto clip_min = opp::optional<ov::op::v1::Minimum>({clip_max->output(0), opp::any_input()});+ // A single Gather root matches both the cos-cache and sin-cache lookups (identical structure);+ // they share the same `squeeze`, so one `position_ids` parameter is created regardless.+ auto gather = opp::wrap_type<ov::op::v8::Gather>({opp::any_input(), clip_min, opp::any_input()});++ ov::pass::MultiMatcher::Callback callback = [=, &new_params](const auto& m) {+ // The cos-cache and sin-cache Gathers share the same position-producing `Squeeze`; some exports+ // may also repeat it per layer. All of them carry the same logical positions, so create a single+ // `position_ids` parameter and rewire every distinct `Squeeze` to it. Dedupe by the matched Squeeze+ // node to avoid double-rewiring (and to avoid emitting colliding same-named parameters).+ //+ // The gathered index is 1-D [seq]; squeeze the batch dim off `position_ids` to match. This+ // bypasses the hardcoded Range/Add(past_len) while preserving the downstream clip and Gathers.+ std::unordered_set<ov::Node*> handled_squeezes;+ auto [position_ids, position_ids_squeezed] = make_position_ids();+ for (auto&& match : m.at(gather)) {+ auto squeeze_node = match.at(squeeze).get_node_shared_ptr();+ if (!handled_squeezes.insert(squeeze_node.get()).second) {+ continue;+ }+ OPENVINO_ASSERT(squeeze_node->get_output_size() == 1, "Squeeze node should have exactly one output");+ for (auto&& consumer : squeeze_node->get_output_target_inputs(0)) {+ consumer.replace_source_output(position_ids_squeezed->output(0));+ }+ }++ new_params.push_back(position_ids);+ };++ register_patterns({gather}, std::move(callback));+ }+};+#ifdef __GNUC__# pragma GCC diagnostic pop#endif@@ -126,10 +186,20 @@ public:bool ov::npuw::AddPositionIdsParam::run_on_model(const std::shared_ptr<ov::Model>& model) {ov::ParameterVector new_parameters;- ov::pass::Manager manager("add-position-ids-param");- manager.set_per_pass_validation(false);- manager.register_pass<HardcodedPositionIdsMatcher>(new_parameters);- manager.run_passes(model);+ {+ ov::pass::Manager manager("add-position-ids-param");+ manager.set_per_pass_validation(false);+ manager.register_pass<HardcodedPositionIdsMatcher>(new_parameters);+ manager.run_passes(model);+ }+ if (new_parameters.empty()) {+ // The Cos/Sin- and Gather-based RoPE flavors are mutually exclusive within a model: run the+ // Gather matcher only if the former didn't fire, so a single `position_ids` is ever introduced.+ ov::pass::Manager manager("add-position-ids-param-gathered");+ manager.set_per_pass_validation(false);+ manager.register_pass<GatheredPositionIdsMatcher>(new_parameters);+ manager.run_passes(model);+ }model->add_parameters(new_parameters);model->validate_nodes_and_infer_types();diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/add_position_ids_param_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/add_position_ids_param_test.cppindex 7edac2bd904..e81809f2131 100644--- a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/add_position_ids_param_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/add_position_ids_param_test.cpp@@ -16,8 +16,11 @@#include "openvino/op/constant.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/cos.hpp"+#include "openvino/op/gather.hpp"#include "openvino/op/less_eq.hpp"#include "openvino/op/matmul.hpp"+#include "openvino/op/maximum.hpp"+#include "openvino/op/minimum.hpp"#include "openvino/op/parameter.hpp"#include "openvino/op/range.hpp"#include "openvino/op/result.hpp"@@ -149,6 +152,85 @@ std::shared_ptr<ov::Model> build_model_with_lfm2_v2_like_pattern() {"model_with_lfm2_v2_like_pattern");}+// Gather-based (cache-table) RoPE, as in ONNX GroupQueryAttention-style exports. Instead of computing+// cos/sin from inv_freq*positions, precomputed cos/sin cache tables are indexed with the positions:+//+// Range -> [Convert] -> Unsqueeze -> [Add(past_len)] -> Squeeze -> [Maximum] -> [Minimum] --> Gather (cos)+// --> Gather (sin)+//+// with_convert inserts the optional Convert right after Range; offset_positions inserts the optional+// past-length Add; with_clip inserts the optional Maximum/Minimum clip of the position into cache bounds.+// The cos and sin Gathers share the same position-producing Squeeze, matching the real export.+std::shared_ptr<ov::Model> build_model_with_gather_rope_pattern(bool with_convert = false,+ bool offset_positions = false,+ bool with_clip = true) {+ // Range: start=0, stop=seq_len, step=1 (mimics position_ids generation)+ const auto range_type = with_convert ? ov::element::i32 : ov::element::i64;+ auto start = ov::op::v0::Constant::create(range_type, ov::Shape{}, {0});+ auto stop = ov::op::v0::Constant::create(range_type, ov::Shape{}, {128});+ auto step = ov::op::v0::Constant::create(range_type, ov::Shape{}, {1});+ auto range = std::make_shared<ov::op::v4::Range>(start, stop, step, range_type);+ range->set_friendly_name("range");++ ov::Output<ov::Node> positions = range->output(0);+ if (with_convert) {+ auto convert = std::make_shared<ov::op::v0::Convert>(positions, ov::element::i64);+ convert->set_friendly_name("positions_convert");+ positions = convert->output(0);+ }++ // Unsqueeze: add batch dim [seq_len] → [1, seq_len]+ auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(positions, unsqueeze_axes);+ unsqueeze->set_friendly_name("unsqueeze_batch");+ ov::Output<ov::Node> seq = unsqueeze->output(0);++ // transformers>=5.4: past length added after the Range+ if (offset_positions) {+ auto past_len = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {8});+ auto offset_add = std::make_shared<ov::op::v1::Add>(seq, past_len);+ offset_add->set_friendly_name("positions_offset_add");+ seq = offset_add->output(0);+ }++ // Squeeze: drop batch dim [1, seq_len] → [seq_len] to feed the 1-D Gather index+ auto squeeze_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});+ auto squeeze = std::make_shared<ov::op::v0::Squeeze>(seq, squeeze_axes);+ squeeze->set_friendly_name("positions_squeeze");+ ov::Output<ov::Node> indices = squeeze->output(0);++ // Optional clip of the absolute position into the cache bounds: Maximum(0) then Minimum(max_pos)+ if (with_clip) {+ auto lo = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});+ auto clip_max = std::make_shared<ov::op::v1::Maximum>(indices, lo);+ clip_max->set_friendly_name("clip_max");+ auto hi = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {255});+ auto clip_min = std::make_shared<ov::op::v1::Minimum>(clip_max, hi);+ clip_min->set_friendly_name("clip_min");+ indices = clip_min->output(0);+ }++ // Precomputed cos/sin cache tables [max_pos, head_dim], indexed by the (shared) position sequence.+ auto gather_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});+ auto cos_cache =+ ov::op::v0::Constant::create(ov::element::f32, ov::Shape{256, 8}, std::vector<float>(256 * 8, 1.0f));+ auto sin_cache =+ ov::op::v0::Constant::create(ov::element::f32, ov::Shape{256, 8}, std::vector<float>(256 * 8, 0.0f));+ auto gather_cos = std::make_shared<ov::op::v8::Gather>(cos_cache, indices, gather_axis);+ gather_cos->set_friendly_name("gather_cos");+ auto gather_sin = std::make_shared<ov::op::v8::Gather>(sin_cache, indices, gather_axis);+ gather_sin->set_friendly_name("gather_sin");++ auto cos_result = std::make_shared<ov::op::v0::Result>(gather_cos);+ cos_result->set_friendly_name("cos_result");+ auto sin_result = std::make_shared<ov::op::v0::Result>(gather_sin);+ sin_result->set_friendly_name("sin_result");++ return std::make_shared<ov::Model>(ov::ResultVector{cos_result, sin_result},+ ov::ParameterVector{},+ "model_with_gather_rope_pattern");+}+bool has_parameter_named(const std::shared_ptr<ov::Model>& model, const std::string& name) {for (const auto& p : model->get_parameters()) {for (const auto& n : p->output(0).get_names()) {@@ -160,6 +242,19 @@ bool has_parameter_named(const std::shared_ptr<ov::Model>& model, const std::strreturn false;}+size_t count_parameters_named(const std::shared_ptr<ov::Model>& model, const std::string& name) {+ size_t count = 0;+ for (const auto& p : model->get_parameters()) {+ for (const auto& n : p->output(0).get_names()) {+ if (n == name) {+ ++count;+ break;+ }+ }+ }+ return count;+}+size_t count_ops_of_type(const std::shared_ptr<ov::Model>& model, const std::string& type_name) {size_t count = 0;for (const auto& op : model->get_ops()) {@@ -170,6 +265,22 @@ size_t count_ops_of_type(const std::shared_ptr<ov::Model>& model, const std::strreturn count;}+// Returns true if position_ids (through the new Squeeze and any preserved clip) feeds the given+// Gather's index input (input 1). Walks back along the data input (input 0) of clip/squeeze nodes.+bool gather_index_fed_by_position_ids(const std::shared_ptr<ov::Node>& gather) {+ auto walk = gather->input_value(1).get_node_shared_ptr();+ for (int depth = 0; depth < 6; ++depth) {+ if (walk->get_type_name() == std::string("Parameter")) {+ return walk->output(0).get_names().count("position_ids") > 0;+ }+ if (walk->get_input_size() == 0) {+ break;+ }+ walk = walk->input_value(0).get_node_shared_ptr();+ }+ return false;+}+// ===================== TESTS =====================TEST(AddPositionIdsParamTest, AddsPositionIdsParameter) {auto model = build_model_with_lfm2_like_pattern();@@ -527,4 +638,94 @@ TEST(AddPositionIdsParamTest, OffsetAddWithClampReplacesClampInput) {<< "Squeeze should be fed by position_ids parameter";}}++// ===================== GATHER-BASED (CACHE-TABLE) RoPE TESTS =====================+// ONNX GroupQueryAttention-style exports index precomputed cos/sin cache tables with the positions,+// so the position sequence drives a Gather rather than a Cos/Sin. The pass must still synthesize a+// position_ids parameter for these models.++TEST(AddPositionIdsParamTest, GatherRopeAddsPositionIdsParameter) {+ auto model = build_model_with_gather_rope_pattern();++ EXPECT_FALSE(has_parameter_named(model, "position_ids"));+ ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));+ EXPECT_TRUE(has_parameter_named(model, "position_ids"));+}++// Both the cos-cache and sin-cache Gathers share one position-producing Squeeze, so exactly one+// position_ids parameter must be created (dedup), not one per Gather.+TEST(AddPositionIdsParamTest, GatherRopeCreatesSinglePositionIdsForCosAndSin) {+ auto model = build_model_with_gather_rope_pattern();+ ov::npuw::AddPositionIdsParam().run_on_model(model);++ EXPECT_EQ(count_parameters_named(model, "position_ids"), 1u)+ << "The shared cos/sin Gathers should yield exactly one position_ids parameter";+}++// After the pass, both Gathers must be indexed by position_ids (through the preserved clip), not by+// the original Range/Squeeze chain.+TEST(AddPositionIdsParamTest, GatherRopeGatherIndexUsesPositionIds) {+ auto model = build_model_with_gather_rope_pattern();+ ov::npuw::AddPositionIdsParam().run_on_model(model);++ size_t gathers_checked = 0;+ for (const auto& op : model->get_ops()) {+ if (op->get_type_name() != std::string("Gather")) {+ continue;+ }+ ++gathers_checked;+ EXPECT_TRUE(gather_index_fed_by_position_ids(op))+ << "Gather '" << op->get_friendly_name() << "' should be indexed by position_ids";+ }+ EXPECT_EQ(gathers_checked, 2u) << "Model should contain both the cos-cache and sin-cache Gathers";+}++// The optional Convert / Add(past_len) / clip nodes are all absent in some exports; the pass must+// match through the bare Range -> Unsqueeze -> Squeeze -> Gather chain too.+TEST(AddPositionIdsParamTest, GatherRopeMatchesWithoutOptionalNodes) {+ auto model = build_model_with_gather_rope_pattern(false /*with_convert*/, false /*offset*/, false /*with_clip*/);++ EXPECT_FALSE(has_parameter_named(model, "position_ids"));+ ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));+ EXPECT_TRUE(has_parameter_named(model, "position_ids"));+ EXPECT_EQ(count_parameters_named(model, "position_ids"), 1u);++ for (const auto& op : model->get_ops()) {+ if (op->get_type_name() == std::string("Gather")) {+ EXPECT_TRUE(gather_index_fed_by_position_ids(op))+ << "Gather should be indexed by position_ids even without clip/convert/add";+ }+ }+}++// The optional Convert (after Range) and Add(past_len) must not prevent the match.+TEST(AddPositionIdsParamTest, GatherRopeMatchesWithConvertAndOffsetAdd) {+ auto model = build_model_with_gather_rope_pattern(true /*with_convert*/, true /*offset*/, true /*with_clip*/);++ EXPECT_FALSE(has_parameter_named(model, "position_ids"));+ ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));+ EXPECT_TRUE(has_parameter_named(model, "position_ids"));+ EXPECT_EQ(count_parameters_named(model, "position_ids"), 1u);++ for (const auto& op : model->get_ops()) {+ if (op->get_type_name() == std::string("Gather")) {+ EXPECT_TRUE(gather_index_fed_by_position_ids(op))+ << "Gather should be indexed by position_ids through the optional Convert/Add";+ }+ }+}++// Reapplying the pass to an already-transformed Gather-RoPE model must be a no-op.+TEST(AddPositionIdsParamTest, GatherRopeReapplyDoesNotModifyGraph) {+ auto model = build_model_with_gather_rope_pattern();+ ov::npuw::AddPositionIdsParam().run_on_model(model);++ const size_t params_after_first = model->get_parameters().size();+ const size_t ops_after_first = model->get_ops().size();++ ov::npuw::AddPositionIdsParam().run_on_model(model);++ EXPECT_EQ(model->get_parameters().size(), params_after_first) << "Second pass should not add duplicate parameters";+ EXPECT_EQ(model->get_ops().size(), ops_after_first) << "Second pass should not add duplicate operations";+}} // namespace