Skip to content

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

[Transformations] Add position_ids input handling for Qwen3.5 text-only export (#39652)

Andrii Staikov9fb13b1ea1b0
diff --git a/src/common/transformations/include/transformations/paged_attention/position_ids_replacer.hpp b/src/common/transformations/include/transformations/paged_attention/position_ids_replacer.hpp
index 2827a308c60..3b6d44e121d 100644
--- a/src/common/transformations/include/transformations/paged_attention/position_ids_replacer.hpp
+++ b/src/common/transformations/include/transformations/paged_attention/position_ids_replacer.hpp
@@ -24,6 +24,7 @@ class TRANSFORMATIONS_API PositionIDsReplacerCodeGen2;
class TRANSFORMATIONS_API EliminateDropBatch;
class TRANSFORMATIONS_API RoPEUnsqueezeAxisReplacer;
class TRANSFORMATIONS_API PositionIDsReplacerGranite;
+class TRANSFORMATIONS_API PositionIDsReplacerQwen3_5;
} // namespace pass
} // namespace ov
@@ -187,3 +188,51 @@ public:
OPENVINO_MATCHER_PASS_RTTI("PositionIDsReplacerGranite");
explicit PositionIDsReplacerGranite(const Output<Node>& unsq_position_ids);
};
+
+/**
+ * @ingroup ov_transformation_common_api
+ * @brief Replace implicit text-only Qwen3.5 M-RoPE positions with explicit PagedAttention positions.
+ *
+ * The text-only export of Qwen3.5 generates positions from the past cache length and current sequence length.
+ * After SDPAToPA moves tokens to the batch dimension, the sequence length becomes one, so that
+ * implicit branch cannot represent prefill or packed requests. Rewire the existing Broadcast's
+ * data input to explicit positions, preserving its target input and the downstream M-RoPE graph.
+ *
+ * We change from this: to this:
+ *
+ * ┌──────────────────────┐ ┌──────────────────────┐
+ * │ Range(P, P + S, 1) │ │ unsq_position_ids │
+ * │ implicit positions │ │ [T, 1] │
+ * └──────────┬───────────┘ └──────────┬───────────┘
+ * │ │
+ * ┌──────────┴───────────┐ ┌──────────┴───────────┐
+ * │ Reshape([1, 1, -1]) │ │ NEW Unsqueeze(0) │
+ * │ │ │ [1, T, 1] │
+ * └──────────┬───────────┘ └──────────┬───────────┘
+ * │ input 0 │ input 0
+ * ┌──────────┴───────────┐ ┌──────────┴───────────┐
+ * │ Broadcast ├── target input 1 │ Broadcast ├── target input 1
+ * │ [3, T, 1] │ [3, T, 1] │ [3, T, 1] │ [3, T, 1]
+ * └──────────┬───────────┘ (unchanged) └──────────┬───────────┘ (unchanged)
+ * │ │
+ * ┌──────────┴───────────┐ ┌──────────┴───────────┐
+ * │ Unsqueeze(2) │ │ Unsqueeze(2) │
+ * │ [3, T, 1, 1] │ │ [3, T, 1, 1] │
+ * └──────────┬───────────┘ └──────────┬───────────┘
+ * │ │
+ * ┌──────────┴───────────┐ ┌──────────┴───────────┐
+ * │ Convert │ │ Convert │
+ * │ MatMul(inv_freq) │ │ MatMul(inv_freq) │
+ * │ Transpose │ │ Transpose │
+ * └──────────────────────┘ └──────────────────────┘
+ *
+ * T is the total token count. Both sides show the PA layout: the original branch broadcasts
+ * one implicit position across tokens; the replacement preserves each token's explicit position.
+ * The target is Concat([3], Reshape(Gather(ShapeOf(hidden_states), 0, 0), [1]), [1]).
+ * Since hidden_states has shape [T, features, 1], the target values are [3, T, 1].
+ */
+class ov::pass::PositionIDsReplacerQwen3_5 : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("PositionIDsReplacerQwen3_5");
+ explicit PositionIDsReplacerQwen3_5(const Output<Node>& unsq_position_ids);
+};
diff --git a/src/common/transformations/src/transformations/paged_attention/position_ids_replacer.cpp b/src/common/transformations/src/transformations/paged_attention/position_ids_replacer.cpp
index 6ae01ac866e..53592692122 100644
--- a/src/common/transformations/src/transformations/paged_attention/position_ids_replacer.cpp
+++ b/src/common/transformations/src/transformations/paged_attention/position_ids_replacer.cpp
@@ -384,3 +384,32 @@ ov::pass::PositionIDsReplacerGranite::PositionIDsReplacerGranite(const Output<No
auto m = std::make_shared<Matcher>(p_sin_cos, matcher_name);
register_matcher(m, callback);
}
+
+ov::pass::PositionIDsReplacerQwen3_5::PositionIDsReplacerQwen3_5(const Output<Node>& unsq_position_ids) {
+ MATCHER_SCOPE(PositionIDsReplacerQwen3_5);
+
+ auto p_start = any_input();
+ auto p_stop = any_input();
+ auto p_step = any_input();
+ auto p_range = wrap_type<v0::Range, ov::op::v4::Range>({p_start, p_stop, p_step});
+ auto p_reshape = wrap_type<v1::Reshape>({p_range, wrap_type<v0::Constant>(value_matches("1, 1, -1"))});
+ auto p_broadcast = wrap_type<v3::Broadcast>({p_reshape, any_input()});
+ auto p_unsqueeze = wrap_type<v0::Unsqueeze>({p_broadcast, wrap_type<v0::Constant>(value_matches("2"))});
+ auto p_convert = wrap_type<v0::Convert>({p_unsqueeze});
+ auto p_inv_freq = any_input();
+ auto p_matmul = wrap_type<v0::MatMul>({p_inv_freq, p_convert});
+ auto p_transpose = wrap_type<v1::Transpose>({p_matmul, any_input()});
+
+ matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](Matcher& m) {
+ const auto& pattern_map = m.get_pattern_value_map();
+ const auto broadcast = pattern_map.at(p_broadcast).get_node_shared_ptr();
+
+ auto axis = v0::Constant::create(element::i32, Shape{}, {0});
+ auto unsq = std::make_shared<v0::Unsqueeze>(unsq_position_ids, axis);
+ broadcast->input(0).replace_source_output(unsq);
+ return true;
+ };
+
+ auto m = std::make_shared<Matcher>(p_transpose, matcher_name);
+ register_matcher(m, callback);
+}
diff --git a/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp b/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp
index 177c64ecb62..a8e99c9d69e 100644
--- a/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp
+++ b/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp
@@ -1298,6 +1298,76 @@ INSTANTIATE_TEST_SUITE_P(SDPAToPA,
(exec_PrevSequenceLengthPattern ? "_PrevSequenceLengthPattern" : "");
});
+TEST_F(TransformationTestsF, SDPAToPA_Qwen3_5_PositionIDsReplacer) {
+ auto make_rope = [](const Output<Node>& positions, const Output<Node>& hidden_states) {
+ auto zero = v0::Constant::create(element::i64, Shape{}, {0});
+ auto hidden_shape = std::make_shared<v3::ShapeOf>(hidden_states);
+ auto batch = std::make_shared<v8::Gather>(hidden_shape, zero, zero);
+ auto batch_vector =
+ std::make_shared<v1::Reshape>(batch, v0::Constant::create(element::i64, Shape{1}, {1}), false);
+ auto target_shape =
+ std::make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i64, Shape{1}, {3}),
+ batch_vector,
+ v0::Constant::create(element::i64, Shape{1}, {1})},
+ 0);
+ auto broadcast = std::make_shared<v3::Broadcast>(positions, target_shape, op::BroadcastType::BIDIRECTIONAL);
+ auto unsqueeze = std::make_shared<v0::Unsqueeze>(broadcast, v0::Constant::create(element::i64, Shape{}, {2}));
+ auto convert = std::make_shared<v0::Convert>(unsqueeze, element::f32);
+ auto inv_freq = v0::Constant::create(element::f32, Shape{1, 1, 32, 1}, {1.f});
+ auto matmul = std::make_shared<v0::MatMul>(inv_freq, convert);
+ auto transpose =
+ std::make_shared<v1::Transpose>(matmul, v0::Constant::create(element::i64, Shape{4}, {0, 1, 3, 2}));
+ return transpose;
+ };
+
+ {
+ auto hidden_states = ov::test::utils::make_param(element::f32, PartialShape{DYN, 6144, 1}, "hidden_states");
+ auto position_ids = ov::test::utils::make_param(element::i64, PartialShape{DYN}, "position_ids");
+ auto unsq_position_ids =
+ std::make_shared<v0::Unsqueeze>(position_ids, v0::Constant::create(element::i32, Shape{}, {-1}));
+ auto beam_idx = ov::test::utils::make_param(element::i32, PartialShape{DYN}, "beam_idx");
+ auto init = v0::Constant::create(element::f32, Shape{1, 2, 0, 256}, {0.f});
+ auto variable = std::make_shared<op::util::Variable>(
+ op::util::VariableInfo{PartialShape{DYN, 2, DYN, 256}, element::f32, "key_cache"});
+ auto cache = std::make_shared<v6::ReadValue>(init, variable);
+ auto zero = v0::Constant::create(element::i64, Shape{}, {0});
+ auto reordered_cache = std::make_shared<v8::Gather>(cache, beam_idx, zero);
+ auto cache_shape = std::make_shared<v3::ShapeOf>(reordered_cache);
+ auto past_length =
+ std::make_shared<v8::Gather>(cache_shape, v0::Constant::create(element::i64, Shape{}, {2}), zero);
+ auto hidden_shape = std::make_shared<v3::ShapeOf>(hidden_states);
+ auto sequence_length_vector =
+ std::make_shared<v8::Gather>(hidden_shape, v0::Constant::create(element::i64, Shape{1}, {2}), zero);
+ auto sequence_length = std::make_shared<v0::Squeeze>(sequence_length_vector, zero);
+ auto end = std::make_shared<v1::Add>(sequence_length, past_length);
+ auto step = v0::Constant::create(element::i64, Shape{}, {1});
+ auto range = std::make_shared<v4::Range>(past_length, end, step, element::i64);
+ auto reshape =
+ std::make_shared<v1::Reshape>(range, v0::Constant::create(element::i64, Shape{3}, {1, 1, -1}), false);
+ auto rope = make_rope(reshape, hidden_states);
+
+ model = std::make_shared<Model>(OutputVector{rope}, ParameterVector{hidden_states, position_ids, beam_idx});
+ manager.register_pass<pass::PositionIDsReplacerQwen3_5>(unsq_position_ids);
+ }
+ {
+ auto hidden_states = ov::test::utils::make_param(element::f32, PartialShape{DYN, 6144, 1}, "hidden_states");
+ auto position_ids = ov::test::utils::make_param(element::i64, PartialShape{DYN}, "position_ids");
+ // This test runs only the matcher. The full SDPAToPA pipeline removes the unused beam_idx input later.
+ auto beam_idx = ov::test::utils::make_param(element::i32, PartialShape{DYN}, "beam_idx");
+ auto unsq_position_ids =
+ std::make_shared<v0::Unsqueeze>(position_ids, v0::Constant::create(element::i32, Shape{}, {-1}));
+ auto positions =
+ std::make_shared<v0::Unsqueeze>(unsq_position_ids, v0::Constant::create(element::i32, Shape{}, {0}));
+ auto rope = make_rope(positions, hidden_states);
+
+ model_ref = std::make_shared<Model>(OutputVector{rope}, ParameterVector{hidden_states, position_ids, beam_idx});
+ }
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+ comparator.enable(FunctionsComparator::ATTRIBUTES);
+ disable_result_friendly_names_check();
+ disable_rt_info_check();
+}
+
TEST_F(SDPAToPATest, SDPAToPA_PositionIDsReplacerLFM2_DequantStartBranch) {
{
auto max_context_len = std::make_shared<v0::Parameter>(element::i64, PartialShape{});
diff --git a/src/core/src/pass/sdpa_to_paged_attention.cpp b/src/core/src/pass/sdpa_to_paged_attention.cpp
index 3edff596a5b..bbb878eb67c 100644
--- a/src/core/src/pass/sdpa_to_paged_attention.cpp
+++ b/src/core/src/pass/sdpa_to_paged_attention.cpp
@@ -175,6 +175,7 @@ bool ov::pass::SDPAToPagedAttention::run_on_model(const std::shared_ptr<ov::Mode
manager.register_pass<PositionIDsReplacerCodeGen2>(position_ids);
manager.register_pass<PositionIDsReplacerLFM2>(position_ids);
manager.register_pass<PositionIDsReplacerGranite>(unsqueezed_position_ids);
+ manager.register_pass<PositionIDsReplacerQwen3_5>(unsqueezed_position_ids);
manager.run_passes(model);
OPENVINO_ASSERT(ssm_fusion->get_fused_count() == paged_ssm_fusion->get_fused_count(),