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
[GPU] Support IncreasePositionIdsPrecisionForGemma4 for PA backend (#36915)
Paul Youngsoo Ahn927c516d81a2
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cpp b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cppindex dbf9963d0a3..4673aa6d10d 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cpp@@ -89,6 +89,7 @@ IncreasePositionIdsPrecisionForRoPE::IncreasePositionIdsPrecisionForRoPE() {auto matmul_node = ov::as_type_ptr<ov::op::v0::MatMul>(pattern_map.at(gemm_or_matmul).get_node_shared_ptr());auto cos_node = ov::as_type_ptr<ov::op::v0::Cos>(pattern_map.at(cos).get_node_shared_ptr());auto sin_node = ov::as_type_ptr<ov::op::v0::Sin>(pattern_map.at(sin).get_node_shared_ptr());+ auto rope_node = pattern_map.at(rope).get_node_shared_ptr();if (!matmul_node || transformation_callback(matmul_node))return false;@@ -98,13 +99,21 @@ IncreasePositionIdsPrecisionForRoPE::IncreasePositionIdsPrecisionForRoPE() {if (original_et == desired_et)return false;+ // Step 1: Ensure MatMul inputs are f32size_t input_idx = 0;bool is_changed = insert_converts_before_if_needed(matmul_node, desired_et, input_idx);-++ // Step 2: Insert restore converts only if RoPE expects non-f32 precision.if (is_changed) {size_t output_idx = 0;- insert_converts_after_if_needed(cos_node, original_et, output_idx);- insert_converts_after_if_needed(sin_node, original_et, output_idx);+ auto rope_cos_et = rope_node->get_input_element_type(1);+ if (rope_cos_et != desired_et) {+ insert_converts_after_if_needed(cos_node, rope_cos_et, output_idx);+ }+ auto rope_sin_et = rope_node->get_input_element_type(2);+ if (rope_sin_et != desired_et) {+ insert_converts_after_if_needed(sin_node, rope_sin_et, output_idx);+ }}return true;};@@ -476,86 +485,11 @@ IncreasePositionIdsPrecisionForGPTOSS::IncreasePositionIdsPrecisionForGPTOSS() {this->register_matcher(m, callback);}--IncreasePositionIdsPrecisionForGemma4::IncreasePositionIdsPrecisionForGemma4() {- using namespace ov::pass::pattern;- using ov::pass::pattern::op::Or;-- // Gemma4 RoPE pattern:- // MatMul (f16) → Convert (f16→f32) → Transpose → Concat → Sin/Cos → Unsqueeze → RoPE- // The difference from base IncreasePositionIdsPrecisionForRoPE is that- // there is a Convert node between MatMul and Transpose.- auto matmul = wrap_type<ov::op::v0::MatMul>();- auto convert_after_matmul = wrap_type<ov::op::v0::Convert>({matmul});- auto transpose_m = wrap_type<ov::op::v1::Transpose>({convert_after_matmul, any_input()});- auto concat = wrap_type<ov::op::v0::Concat>({transpose_m, transpose_m});- auto sin = wrap_type<ov::op::v0::Sin>({concat});- auto cos = wrap_type<ov::op::v0::Cos>({concat});-- auto sin_unsqueeze = wrap_type<ov::op::v0::Unsqueeze>({sin, wrap_type<ov::op::v0::Constant>()});- auto cos_unsqueeze = wrap_type<ov::op::v0::Unsqueeze>({cos, wrap_type<ov::op::v0::Constant>()});-- auto rope_sin_input = std::make_shared<Or>(OutputVector{sin_unsqueeze, sin});- auto rope_cos_input = std::make_shared<Or>(OutputVector{cos_unsqueeze, cos});-- auto rope = wrap_type<ov::op::internal::RoPE>({any_input(), rope_cos_input, rope_sin_input});-- ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {- const auto& pattern_map = m.get_pattern_value_map();-- auto matmul_node = ov::as_type_ptr<ov::op::v0::MatMul>(pattern_map.at(matmul).get_node_shared_ptr());- auto convert_node = ov::as_type_ptr<ov::op::v0::Convert>(pattern_map.at(convert_after_matmul).get_node_shared_ptr());-- if (!matmul_node || transformation_callback(matmul_node))- return false;-- const auto desired_et = ov::element::f32;- const auto original_et = matmul_node->get_output_element_type(0);- if (original_et == desired_et)- return false;-- // Ensure both MatMul inputs are f32.- // - If input is a Convert node (e.g. Convert_3: i32→f16), replace it with i32→f32.- // - If input is not a Convert (e.g. Broadcast: f16), insert a new Convert(f16→f32) before it.- for (auto& input : matmul_node->inputs()) {- auto src_output = input.get_source_output();- auto src_node = src_output.get_node_shared_ptr();- if (src_output.get_element_type() == desired_et)- continue;-- auto src_convert = ov::as_type_ptr<ov::op::v0::Convert>(src_node);- if (src_convert) {- auto new_convert = std::make_shared<ov::op::v0::Convert>(src_convert->input_value(0), desired_et);- new_convert->set_friendly_name(src_convert->get_friendly_name());- ov::copy_runtime_info(src_convert, new_convert);- ov::replace_node(src_convert, new_convert);- } else {- auto new_convert = std::make_shared<ov::op::v0::Convert>(src_output, desired_et);- new_convert->set_friendly_name(src_node->get_friendly_name() + "_to_f32");- ov::copy_runtime_info(src_node, new_convert);- input.replace_source_output(new_convert);- }- }-- // Remove the Convert after MatMul (f16→f32) since MatMul now outputs f32.- // Just replace it with MatMul's output directly.- if (convert_node) {- ov::replace_node(convert_node, matmul_node);- }-- return true;- };-- auto m = std::make_shared<ov::pass::pattern::Matcher>(rope, "IncreasePositionIdsPrecisionForGemma4");- this->register_matcher(m, callback);-}-IncreasePositionIdsPrecision::IncreasePositionIdsPrecision() = default;bool IncreasePositionIdsPrecision::run_on_model(const std::shared_ptr<ov::Model>& model) {ov::pass::SymbolicOptimizations symbolic_optimizations(false, get_pass_config());auto symbolic_ctx_manager = symbolic_optimizations.get_manager();- symbolic_ctx_manager->register_pass<IncreasePositionIdsPrecisionForGemma4>();symbolic_ctx_manager->register_pass<IncreasePositionIdsPrecisionForRoPE>();symbolic_ctx_manager->register_pass<IncreasePositionIdsPrecisionForQwen25VL>();symbolic_ctx_manager->register_pass<IncreasePositionIdsPrecisionForQwen3VL>();diff --git a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hpp b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hppindex 160050d60cd..8a96cf6d49d 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hpp@@ -38,13 +38,6 @@ public:IncreasePositionIdsPrecisionForGPTOSS();};-class IncreasePositionIdsPrecisionForGemma4 : public ov::pass::MatcherPass {-public:- OPENVINO_MATCHER_PASS_RTTI("IncreasePositionIdsPrecisionForGemma4");- IncreasePositionIdsPrecisionForGemma4();-};--/*** @brief This pass adds additional convert nodes on the position_ids input branch (around MatMul or Multiply operation),* targeting to improve runtime rotary position embeddings calculation for FP16 models.diff --git a/src/plugins/intel_gpu/src/plugin/transformations/utils.hpp b/src/plugins/intel_gpu/src/plugin/transformations/utils.hppindex 6ea5491f5a1..c0ff3b8056d 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/utils.hpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/utils.hpp@@ -1,4 +1,4 @@-// Copyright (C) 2024-2026 Intel Corporation+// Copyright (C) 2018-2026 Intel Corporation// SPDX-License-Identifier: Apache-2.0//@@ -36,11 +36,17 @@ inline bool insert_converts_before_if_needed(const std::shared_ptr<ov::Node>& noauto in_convert = ov::as_type_ptr<ov::op::v0::Convert>(incoming_node);- if (in_convert && in_convert->get_users().size() == 1 && input_et.bitwidth() <= desired_et.bitwidth()) {- auto convert = std::make_shared<ov::op::v0::Convert>(incoming_node->input_value(0), desired_et);- convert->set_friendly_name(in_convert->get_friendly_name() + "_increase_precision_" + std::to_string(input_idx));- ov::copy_runtime_info(incoming_node, convert);- ov::replace_node(incoming_node, convert);+ if (in_convert && input_et.bitwidth() <= desired_et.bitwidth()) {+ auto convert = std::make_shared<ov::op::v0::Convert>(in_convert->input_value(0), desired_et);+ convert->set_friendly_name(node->get_friendly_name() + "_increase_precision_" + std::to_string(input_idx));+ ov::copy_runtime_info(in_convert, convert);+ if (in_convert->get_users().size() == 1) {+ // This node is the only user, so the original Convert can be dropped entirely.+ ov::replace_node(in_convert, convert);+ } else {+ // The original Convert is kept for the remaining users; rewire only this input.+ input.replace_source_output(convert);+ }} else {auto convert = std::make_shared<ov::op::v0::Convert>(incoming_output, desired_et);convert->set_friendly_name(incoming_node->get_friendly_name() + "_increase_precision_" + std::to_string(input_idx));diff --git a/src/plugins/intel_gpu/tests/unit/transformations/increase_precision_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/increase_precision_test.cppindex 19664504569..0ba88abce87 100644--- a/src/plugins/intel_gpu/tests/unit/transformations/increase_precision_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/transformations/increase_precision_test.cpp@@ -1083,84 +1083,75 @@ TEST(IncreasePrecisionTest, IncreaseRMSInputPrecisionForQwenVLMerger) {<< "rms_m output should feed into Convert node";}-// Gemma4 pattern: MatMul → Convert(f16→f32) → Transpose → Concat → Sin/Cos → Unsqueeze → RoPE+// Gemma4 pattern (latest): MatMul(f16) -> Reshape(transpose) -> Concat -> Sin/Cos -> Reshape(unsqueeze) -> RoPE(f16 cos/sin)+// The local / global attention RoPEs share a single position_ids Convert, so that Convert has two users.TEST_F(TransformationTestsF, IncreasePositionIdsPrecisionForGemma4) {- {- // Gemma4 rotary embedding: position_ids path goes through Convert(i32→f16) then MatMul(f16)- // followed by Convert(f16→f32) → Transpose → Concat → Sin/Cos → Unsqueeze → RoPE- auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1, -1});- auto convert_i32 = std::make_shared<ov::op::v0::Convert>(position_ids, ov::element::i32);- auto unsqueeze_const = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{1});- auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(convert_i32, unsqueeze_const);- auto convert_f16 = std::make_shared<ov::op::v0::Convert>(unsqueeze, ov::element::f16);-- // Broadcast frequency matrix (f16)- auto freq_const = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 128, 1});+ // Builds one RoPE branch on top of the shared position_ids Convert.+ // theta_idx only makes the frequency constants of the two branches distinct.+ auto make_rope_branch = [](const ov::Output<ov::Node>& pos_ids_fp,+ const ov::Output<ov::Node>& rope_input,+ int theta_idx,+ bool promoted) {+ auto freq_const = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 128, 1},+ std::vector<float>(128, static_cast<float>(theta_idx + 1)));auto shape_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, std::vector<int32_t>{1, 128, 1});- auto broadcast = std::make_shared<ov::op::v3::Broadcast>(freq_const, shape_const);-- // MatMul in f16- auto matmul = std::make_shared<ov::op::v0::MatMul>(broadcast, convert_f16);-- // Convert after MatMul (f16→f32)- auto convert_after_matmul = std::make_shared<ov::op::v0::Convert>(matmul, ov::element::f32);-- // Transpose- auto transpose_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, std::vector<int32_t>{0, 2, 1});- auto transpose = std::make_shared<ov::op::v1::Transpose>(convert_after_matmul, transpose_const);-- // Concat → Sin/Cos- auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{transpose, transpose}, 2);- auto cos = std::make_shared<ov::op::v0::Cos>(concat);- auto sin = std::make_shared<ov::op::v0::Sin>(concat);+ ov::Output<ov::Node> matmul_lhs = std::make_shared<ov::op::v3::Broadcast>(freq_const, shape_const);+ if (promoted)+ matmul_lhs = std::make_shared<ov::op::v0::Convert>(matmul_lhs, ov::element::f32);++ auto matmul = std::make_shared<ov::op::v0::MatMul>(matmul_lhs, pos_ids_fp);++ // Reshape (acts as transpose) -> Concat -> Sin/Cos+ auto reshape_tr_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, std::vector<int32_t>{-1, 1, 128});+ auto reshape_transpose = std::make_shared<ov::op::v1::Reshape>(matmul, reshape_tr_const, false);+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{reshape_transpose, reshape_transpose}, 2);+ ov::Output<ov::Node> cos = std::make_shared<ov::op::v0::Cos>(concat);+ ov::Output<ov::Node> sin = std::make_shared<ov::op::v0::Sin>(concat);++ // RoPE stays in f16, so the promoted branch needs restore converts+ if (promoted) {+ cos = std::make_shared<ov::op::v0::Convert>(cos, ov::element::f16);+ sin = std::make_shared<ov::op::v0::Convert>(sin, ov::element::f16);+ }++ // Reshape (acts as unsqueeze) -> RoPE+ auto reshape_cos_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, std::vector<int32_t>{-1, 1, 1, 256});+ auto cos_reshape = std::make_shared<ov::op::v1::Reshape>(cos, reshape_cos_const, false);+ auto reshape_sin_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, std::vector<int32_t>{-1, 1, 1, 256});+ auto sin_reshape = std::make_shared<ov::op::v1::Reshape>(sin, reshape_sin_const, false);++ return std::make_shared<ov::op::internal::RoPE>(ov::OutputVector{rope_input, cos_reshape, sin_reshape},+ ov::op::internal::RoPE::Config());+ };- // Unsqueeze → RoPE- auto unsqueeze_const2 = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{2});- auto cos_unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(cos, unsqueeze_const2);- auto sin_unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(sin, unsqueeze_const2);+ {+ // position_ids(i64) -> Convert(i32) -> Reshape[?,1,1] -> Convert(f16) -> { MatMul_local, MatMul_global }+ auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1});+ auto convert_i32 = std::make_shared<ov::op::v0::Convert>(position_ids, ov::element::i32);+ auto reshape_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, std::vector<int32_t>{-1, 1, 1});+ auto reshape_pos = std::make_shared<ov::op::v1::Reshape>(convert_i32, reshape_const, false);+ auto convert_f16 = std::make_shared<ov::op::v0::Convert>(reshape_pos, ov::element::f16);- auto rope_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape::dynamic(4));- auto rope = std::make_shared<ov::op::internal::RoPE>(ov::OutputVector{rope_input, cos_unsqueeze, sin_unsqueeze}, ov::op::internal::RoPE::Config());+ auto rope_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic(4));+ auto rope_local = make_rope_branch(convert_f16, rope_input, 0, false);+ auto rope_global = make_rope_branch(convert_f16, rope_input, 1, false);- model = std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{position_ids, rope_input});+ model = std::make_shared<ov::Model>(ov::OutputVector{rope_local, rope_global}, ov::ParameterVector{position_ids, rope_input});manager.register_pass<IncreasePositionIdsPrecision>();}{- // Expected: Convert_3 becomes i32→f32, Broadcast gets a Convert(f16→f32),- // MatMul executes in f32, Convert after MatMul is removed.- auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1, -1});+ auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{-1});auto convert_i32 = std::make_shared<ov::op::v0::Convert>(position_ids, ov::element::i32);- auto unsqueeze_const = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{1});- auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(convert_i32, unsqueeze_const);- auto convert_f32 = std::make_shared<ov::op::v0::Convert>(unsqueeze, ov::element::f32);-- // Broadcast frequency matrix (f16) + Convert to f32- auto freq_const = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 128, 1});- auto shape_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, std::vector<int32_t>{1, 128, 1});- auto broadcast = std::make_shared<ov::op::v3::Broadcast>(freq_const, shape_const);- auto broadcast_to_f32 = std::make_shared<ov::op::v0::Convert>(broadcast, ov::element::f32);-- // MatMul in f32 (no Convert after)- auto matmul = std::make_shared<ov::op::v0::MatMul>(broadcast_to_f32, convert_f32);-- // Transpose (directly from MatMul, Convert removed)- auto transpose_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, std::vector<int32_t>{0, 2, 1});- auto transpose = std::make_shared<ov::op::v1::Transpose>(matmul, transpose_const);+ auto reshape_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{3}, std::vector<int32_t>{-1, 1, 1});+ auto reshape_pos = std::make_shared<ov::op::v1::Reshape>(convert_i32, reshape_const, false);+ auto convert_f32_local = std::make_shared<ov::op::v0::Convert>(reshape_pos, ov::element::f32);+ auto convert_f32_global = std::make_shared<ov::op::v0::Convert>(reshape_pos, ov::element::f32);- // Concat → Sin/Cos- auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{transpose, transpose}, 2);- auto cos = std::make_shared<ov::op::v0::Cos>(concat);- auto sin = std::make_shared<ov::op::v0::Sin>(concat);-- // Unsqueeze → RoPE- auto unsqueeze_const2 = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{1}, std::vector<int64_t>{2});- auto cos_unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(cos, unsqueeze_const2);- auto sin_unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(sin, unsqueeze_const2);-- auto rope_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape::dynamic(4));- auto rope = std::make_shared<ov::op::internal::RoPE>(ov::OutputVector{rope_input, cos_unsqueeze, sin_unsqueeze}, ov::op::internal::RoPE::Config());+ auto rope_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape::dynamic(4));+ auto rope_local = make_rope_branch(convert_f32_local, rope_input, 0, true);+ auto rope_global = make_rope_branch(convert_f32_global, rope_input, 1, true);- model_ref = std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{position_ids, rope_input});+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{rope_local, rope_global}, ov::ParameterVector{position_ids, rope_input});}comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);}\ No newline at end of file