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
Allow the RotaryEmbedding decomposed by ONNX frontend to be fused by RoPEFusion (#34527)
Wei Wang4d59cfb0f17e
diff --git a/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hpp b/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hppindex 3e61e50cbf4..5fcf8c848ee 100644--- a/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hpp+++ b/src/common/transformations/include/transformations/op_conversions/group_query_attention_decomposition.hpp@@ -27,7 +27,6 @@ private:std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<op::v3::ShapeOf>& shape,const std::vector<int>& dims);std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<ov::Node>& node, const std::vector<int>& dims);- ov::OutputVector make_split(const ov::Output<ov::Node>& value, int64_t num_splits, int64_t axis);std::shared_ptr<ov::Node> rotaryEmbedding(ov::Output<ov::Node> input,ov::Output<ov::Node> cos,ov::Output<ov::Node> sin,diff --git a/src/common/transformations/src/transformations/common_optimizations/fuse_rotary_positional_embeddings.cpp b/src/common/transformations/src/transformations/common_optimizations/fuse_rotary_positional_embeddings.cppindex 8cc86793958..da356a41efc 100644--- a/src/common/transformations/src/transformations/common_optimizations/fuse_rotary_positional_embeddings.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/fuse_rotary_positional_embeddings.cpp@@ -1162,7 +1162,7 @@ RoPEFusionGPTOSS::RoPEFusionGPTOSS() {auto first_half_mul_sin = pattern::wrap_type<v1::Multiply>({vsplit_out0, t_sin}, {{"auto_broadcast", "numpy"}});auto add_Add =pattern::wrap_type<v1::Add>({second_half_mul_cos, first_half_mul_sin}, {{"auto_broadcast", "numpy"}});- auto concat_result = pattern::wrap_type<opset1::Concat>({sub_Subtract, add_Add}, {{"axis", -1}});+ auto concat_result = pattern::wrap_type<opset1::Concat>({sub_Subtract, add_Add});auto result = concat_result;@@ -1172,6 +1172,17 @@ RoPEFusionGPTOSS::RoPEFusionGPTOSS() {const auto& x_val = pattern_map.at(x);const auto& v_cos = pattern_map.at(t_cos);+ // Verify concat axis is the last dimension (accepts both -1 and positive equivalent)+ auto concat_node = ov::as_type_ptr<v0::Concat>(root);+ if (!concat_node)+ return false;+ auto concat_rank = concat_node->get_output_partial_shape(0).rank();+ if (concat_rank.is_dynamic())+ return false;+ int64_t concat_axis = concat_node->get_axis();+ if (concat_axis != -1 && concat_axis != concat_rank.get_length() - 1)+ return false;+auto symbols = m.get_symbols();const auto& half_ndims = symbols["half_ndims"];if (!half_ndims.is_integer()) {diff --git a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cppindex ce1d86b258d..728596da41d 100644--- a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp+++ b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp@@ -24,11 +24,11 @@#include "openvino/op/select.hpp"#include "openvino/op/shape_of.hpp"#include "openvino/op/slice.hpp"-#include "openvino/op/split.hpp"#include "openvino/op/squeeze.hpp"#include "openvino/op/subtract.hpp"#include "openvino/op/transpose.hpp"#include "openvino/op/unsqueeze.hpp"+#include "openvino/op/variadic_split.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"using ov::pass::pattern::Matcher;@@ -110,7 +110,10 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(ov::Output<ov::Node> position_ids =register_new_node<v4::Range>(zero_without_shape, curr_seqlen_scalar, one_without_shape, ov::element::i64);if (node->get_input_size() > 9 && !is_null(node->input_value(9))) {- position_ids = node->input_value(9).get_node_shared_ptr();+ // Flatten position_ids to 1D so that Gather produces 2D [seqlen, head_size/2] output,+ // ensuring correct 4D shapes after Unsqueeze in rotaryEmbedding.+ const auto neg_one = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}));+ position_ids = register_new_node<v1::Reshape>(node->input_value(9), neg_one, false);} else {position_ids = register_new_node<v1::Add>(position_ids, past_seqlen);}@@ -230,16 +233,6 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(return {output, present_k, present_v};}-// make split functions is a copy-past from ONNX FE. TODO: move it to one place-ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::make_split(const ov::Output<ov::Node>& value,- int64_t num_splits,- int64_t axis) {- const auto axis_node = register_new_node(v0::Constant::create(ov::element::i64, ov::Shape{}, {axis}));- const auto split = register_new_node<v1::Split>(value, axis_node, num_splits);-- return split->outputs();-}-std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::get_dimensions(const std::shared_ptr<v3::ShapeOf>& shape,const std::vector<int>& dims) {@@ -258,42 +251,66 @@ std::shared_ptr<ov::Node> ov::pass::GroupQueryAttentionDecomposition::rotaryEmbeov::Output<ov::Node> cos,ov::Output<ov::Node> sin,bool interleaved) {- auto zero = v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});- auto one = v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});-+ auto two = v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});++ // Unsqueeze cos/sin to 4D [1, 1, seqlen, head_size/2] to match RoPE fusion pattern+ auto unsqueeze_axes = v0::Constant::create(ov::element::i64, ov::Shape{2}, {0, 1});+ auto cos_4d = register_new_node<v0::Unsqueeze>(cos, unsqueeze_axes);+ auto sin_4d = register_new_node<v0::Unsqueeze>(sin, unsqueeze_axes);++ // For interleaved mode, deinterleave first so the core RoPE formula is identical+ ov::Output<ov::Node> rope_input = input;+ std::shared_ptr<v3::ShapeOf> input_shape;+ std::shared_ptr<ov::Node> dim_bns, half_head_size;+ std::shared_ptr<v0::Constant> perm_5d;if (interleaved) {- auto two = v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});- auto cos_last_dim = get_dimensions(cos.get_node_shared_ptr(), {-1});- auto input_shape = register_new_node<v3::ShapeOf>(input);- auto dim_bns = get_dimensions(input_shape, {0, 1, 2});-- auto negtive_one = v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1});- auto split_input_shape = register_new_node<v0::Concat>(ov::NodeVector{dim_bns, cos_last_dim, two}, 0);- auto reshaped_input = register_new_node<v1::Reshape>(input, split_input_shape, false);-- auto in_split = make_split(reshaped_input, 2, -1);- split_input_shape = register_new_node<v0::Concat>(ov::NodeVector{dim_bns, cos_last_dim}, 0);- auto in_split_0 = register_new_node<v1::Reshape>(in_split[0], split_input_shape, false);- auto in_split_1 = register_new_node<v1::Reshape>(in_split[1], split_input_shape, false);-- auto res_0 = register_new_node<v1::Subtract>(register_new_node<v1::Multiply>(in_split_0, cos),- register_new_node<v1::Multiply>(in_split_1, sin));- auto res_1 = register_new_node<v1::Add>(register_new_node<v1::Multiply>(in_split_0, sin),- register_new_node<v1::Multiply>(in_split_1, cos));-- split_input_shape = register_new_node<v0::Concat>(ov::NodeVector{dim_bns, cos_last_dim, one}, 0);- auto res_0_5d = register_new_node<v1::Reshape>(res_0, split_input_shape, false);- auto res_1_5d = register_new_node<v1::Reshape>(res_1, split_input_shape, false);-- auto concat_ret = register_new_node<v0::Concat>(ov::NodeVector{res_0_5d, res_1_5d}, -1);- return register_new_node<v1::Reshape>(concat_ret, input_shape, false);- } else {- auto in_split = make_split(input, 2, -1);- auto res_0 = register_new_node<v1::Subtract>(register_new_node<v1::Multiply>(in_split[0], cos),- register_new_node<v1::Multiply>(in_split[1], sin));- auto res_1 = register_new_node<v1::Add>(register_new_node<v1::Multiply>(in_split[0], sin),- register_new_node<v1::Multiply>(in_split[1], cos));+ input_shape = register_new_node<v3::ShapeOf>(input);+ dim_bns = get_dimensions(input_shape, {0, 1, 2});+ half_head_size = get_dimensions(cos.get_node_shared_ptr(), {-1});+ perm_5d = v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 1, 2, 4, 3});++ // Deinterleave: [bs,nh,seq,head_size]+ // -> reshape [bs,nh,seq,head_size/2,2]+ // -> transpose [bs,nh,seq,2,head_size/2]+ // -> reshape [bs,nh,seq,head_size] (now [first_half, second_half])+ auto deinterleave_5d = register_new_node<v0::Concat>(ov::NodeVector{dim_bns, half_head_size, two}, 0);+ auto reshaped_5d = register_new_node<v1::Reshape>(input, deinterleave_5d, false);+ auto transposed_5d = register_new_node<v1::Transpose>(reshaped_5d, perm_5d);+ rope_input = register_new_node<v1::Reshape>(transposed_5d, input_shape, false);+ }- return register_new_node<v0::Concat>(ov::NodeVector{res_0, res_1}, -1);+ // Core RoPE formula (matches RoPEFusionGPTOSS pattern for both modes)+ // first_ = first_half * cos - second_half * sin+ // second_ = second_half * cos + first_half * sin+ const auto& cos_partial_shape = cos.get_partial_shape();+ const auto half_head_size_val =+ static_cast<int64_t>(cos_partial_shape[cos_partial_shape.rank().get_length() - 1].get_length());+ const auto split_axis = v0::Constant::create(ov::element::i64, ov::Shape{}, {-1});+ const auto split_lengths =+ v0::Constant::create(ov::element::i64, ov::Shape{2}, {half_head_size_val, half_head_size_val});+ // Split along last axis using constant split_lengths to enable RoPE fusion pattern matching+ auto in_split = register_new_node<v1::VariadicSplit>(rope_input, split_axis, split_lengths)->outputs();+ auto first_half_mul_cos = register_new_node<v1::Multiply>(in_split[0], cos_4d);+ auto second_half_mul_sin = register_new_node<v1::Multiply>(in_split[1], sin_4d);+ auto neg_one = v0::Constant::create(ov::element::f32, ov::Shape{}, {-1.0f});+ auto neg_second_sin = register_new_node<v1::Multiply>(second_half_mul_sin, neg_one);+ auto res_0 = register_new_node<v1::Add>(first_half_mul_cos, neg_second_sin);+ auto second_half_mul_cos = register_new_node<v1::Multiply>(in_split[1], cos_4d);+ auto first_half_mul_sin = register_new_node<v1::Multiply>(in_split[0], sin_4d);+ auto res_1 = register_new_node<v1::Add>(second_half_mul_cos, first_half_mul_sin);+ ov::Output<ov::Node> output = register_new_node<v0::Concat>(ov::NodeVector{res_0, res_1}, -1);++ // For interleaved mode, re-interleave the result+ if (interleaved) {+ // Re-interleave: [bs,nh,seq,head_size]+ // -> reshape [bs,nh,seq,2,head_size/2]+ // -> transpose [bs,nh,seq,head_size/2,2]+ // -> reshape [bs,nh,seq,head_size]+ auto reinterleave_5d = register_new_node<v0::Concat>(ov::NodeVector{dim_bns, two, half_head_size}, 0);+ auto result_5d = register_new_node<v1::Reshape>(output, reinterleave_5d, false);+ auto result_transposed = register_new_node<v1::Transpose>(result_5d, perm_5d);+ output = register_new_node<v1::Reshape>(result_transposed, input_shape, false);}++ return output.get_node_shared_ptr();}diff --git a/src/common/transformations/tests/common_optimizations/fuse_rotary_positional_embeddings.cpp b/src/common/transformations/tests/common_optimizations/fuse_rotary_positional_embeddings.cppindex 781110418ed..8de0a7dfb61 100644--- a/src/common/transformations/tests/common_optimizations/fuse_rotary_positional_embeddings.cpp+++ b/src/common/transformations/tests/common_optimizations/fuse_rotary_positional_embeddings.cpp@@ -2123,3 +2123,125 @@ TEST_F(TransformationTestsF, ConvertToROPE_LtxVideo) {model_ref = std::make_shared<Model>(OutputVector{rope}, ParameterVector{input, cos_freqs, sin_freqs});}}++TEST_F(TransformationTestsF, ConvertToROPE_GPTOSS_concat_axis_negative) {+ disable_rt_info_check();+ const int batch = 2;+ const int num_head = 32;+ const int seq_len = 16;+ const int ndims = 128;+ const int half_ndims = ndims / 2;+ using namespace ov;+ {+ // gpt-oss style RoPE pattern with concat axis = -1+ auto input = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, num_head, seq_len, ndims});+ auto t_cos = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});+ auto t_sin = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});++ auto split_lengths = makeConst(element::i64, {2}, std::vector<int64_t>{half_ndims, half_ndims});+ auto axis_const = makeConst(element::i64, {}, std::vector<int64_t>{-1});+ auto vsplit = makeOP<opset1::VariadicSplit>({input, axis_const, split_lengths});++ // first_ = first_half * cos - second_half * sin+ auto first_half_mul_cos = makeOP<opset1::Multiply>({vsplit->output(0), t_cos}, {{"auto_broadcast", "numpy"}});+ auto second_half_mul_sin = makeOP<opset1::Multiply>({vsplit->output(1), t_sin}, {{"auto_broadcast", "numpy"}});+ auto neg = makeOP<opset1::Multiply>({second_half_mul_sin, -1.0f}, {{"auto_broadcast", "numpy"}});+ auto first_ = makeOP<opset1::Add>({first_half_mul_cos, neg}, {{"auto_broadcast", "numpy"}});++ // second_ = second_half * cos + first_half * sin+ auto second_half_mul_cos = makeOP<opset1::Multiply>({vsplit->output(1), t_cos}, {{"auto_broadcast", "numpy"}});+ auto first_half_mul_sin = makeOP<opset1::Multiply>({vsplit->output(0), t_sin}, {{"auto_broadcast", "numpy"}});+ auto second_ = makeOP<opset1::Add>({second_half_mul_cos, first_half_mul_sin}, {{"auto_broadcast", "numpy"}});++ auto result = makeOP<opset1::Concat>({first_, second_}, {{"axis", -1}});++ model = std::make_shared<Model>(OutputVector{result}, ParameterVector{input, t_cos, t_sin});+ }+ manager.register_pass<pass::RoPEFusion>();+ {+ auto input = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, num_head, seq_len, ndims});+ auto t_cos = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});+ auto t_sin = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});++ auto rope = makeOP<op::internal::RoPE>({input, t_cos, t_sin},+ {{"config.slice_start", 0},+ {"config.slice_stop", 0},+ {"config.input_trans0213", false},+ {"config.output_trans0213", false},+ {"config.is_interleaved", false},+ {"config.rotary_ndims", ndims},+ {"config.cos_sin_ndims", half_ndims},+ {"config.is_chatglm", false},+ {"config.support_2d_rope", false},+ {"config.support_3d_rope", false},+ {"config.is_qwen", false},+ {"config.use_rope_cache", false},+ {"config.is_ltx_video", false},+ {"config.head_cnt", 0},+ {"config.head_size", 0},+ {"config.gather_position_arg_id", 0}});++ model_ref = std::make_shared<Model>(OutputVector{rope}, ParameterVector{input, t_cos, t_sin});+ }+}++TEST_F(TransformationTestsF, ConvertToROPE_GPTOSS_concat_axis_positive) {+ disable_rt_info_check();+ const int batch = 2;+ const int num_head = 32;+ const int seq_len = 16;+ const int ndims = 128;+ const int half_ndims = ndims / 2;+ using namespace ov;+ {+ // gpt-oss style RoPE pattern with concat axis = 3 (positive last axis for rank-4 tensor)+ auto input = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, num_head, seq_len, ndims});+ auto t_cos = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});+ auto t_sin = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});++ auto split_lengths = makeConst(element::i64, {2}, std::vector<int64_t>{half_ndims, half_ndims});+ auto axis_const = makeConst(element::i64, {}, std::vector<int64_t>{-1});+ auto vsplit = makeOP<opset1::VariadicSplit>({input, axis_const, split_lengths});++ // first_ = first_half * cos - second_half * sin+ auto first_half_mul_cos = makeOP<opset1::Multiply>({vsplit->output(0), t_cos}, {{"auto_broadcast", "numpy"}});+ auto second_half_mul_sin = makeOP<opset1::Multiply>({vsplit->output(1), t_sin}, {{"auto_broadcast", "numpy"}});+ auto neg = makeOP<opset1::Multiply>({second_half_mul_sin, -1.0f}, {{"auto_broadcast", "numpy"}});+ auto first_ = makeOP<opset1::Add>({first_half_mul_cos, neg}, {{"auto_broadcast", "numpy"}});++ // second_ = second_half * cos + first_half * sin+ auto second_half_mul_cos = makeOP<opset1::Multiply>({vsplit->output(1), t_cos}, {{"auto_broadcast", "numpy"}});+ auto first_half_mul_sin = makeOP<opset1::Multiply>({vsplit->output(0), t_sin}, {{"auto_broadcast", "numpy"}});+ auto second_ = makeOP<opset1::Add>({second_half_mul_cos, first_half_mul_sin}, {{"auto_broadcast", "numpy"}});++ auto result = makeOP<opset1::Concat>({first_, second_}, {{"axis", 3}});++ model = std::make_shared<Model>(OutputVector{result}, ParameterVector{input, t_cos, t_sin});+ }+ manager.register_pass<pass::RoPEFusion>();+ {+ auto input = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, num_head, seq_len, ndims});+ auto t_cos = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});+ auto t_sin = std::make_shared<opset1::Parameter>(element::f32, PartialShape{batch, 1, seq_len, half_ndims});++ auto rope = makeOP<op::internal::RoPE>({input, t_cos, t_sin},+ {{"config.slice_start", 0},+ {"config.slice_stop", 0},+ {"config.input_trans0213", false},+ {"config.output_trans0213", false},+ {"config.is_interleaved", false},+ {"config.rotary_ndims", ndims},+ {"config.cos_sin_ndims", half_ndims},+ {"config.is_chatglm", false},+ {"config.support_2d_rope", false},+ {"config.support_3d_rope", false},+ {"config.is_qwen", false},+ {"config.use_rope_cache", false},+ {"config.is_ltx_video", false},+ {"config.head_cnt", 0},+ {"config.head_size", 0},+ {"config.gather_position_arg_id", 0}});++ model_ref = std::make_shared<Model>(OutputVector{rope}, ParameterVector{input, t_cos, t_sin});+ }+}diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/rotary_embedding.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/rotary_embedding.cppindex 6c8a4aa7434..647f0a5e377 100644--- a/src/frontends/onnx/frontend/src/op/com.microsoft/rotary_embedding.cpp+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/rotary_embedding.cpp@@ -10,25 +10,16 @@#include "openvino/core/rt_info.hpp"#include "openvino/frontend/exception.hpp"#include "openvino/op/add.hpp"-#include "openvino/op/broadcast.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/constant.hpp"-#include "openvino/op/convert.hpp"#include "openvino/op/gather.hpp"-#include "openvino/op/greater.hpp"-#include "openvino/op/greater_eq.hpp"#include "openvino/op/multiply.hpp"-#include "openvino/op/range.hpp"#include "openvino/op/reshape.hpp"-#include "openvino/op/scaled_dot_product_attention.hpp"-#include "openvino/op/select.hpp"#include "openvino/op/shape_of.hpp"-#include "openvino/op/slice.hpp"#include "openvino/op/split.hpp"-#include "openvino/op/squeeze.hpp"-#include "openvino/op/subtract.hpp"#include "openvino/op/transpose.hpp"#include "openvino/op/unsqueeze.hpp"+#include "openvino/op/variadic_split.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"#include "utils/common.hpp"#include "utils/reshape.hpp"@@ -41,14 +32,6 @@ namespace onnx {namespace com_microsoft {namespace opset_1 {-ov::OutputVector make_split(const ov::Output<ov::Node>& value, int64_t num_splits, int64_t axis) {- using namespace ov::op;- const auto axis_node = v0::Constant::create(ov::element::i64, ov::Shape{}, {axis});- const auto split = std::make_shared<v1::Split>(value, axis_node, num_splits);-- return split->outputs();-}-std::shared_ptr<ov::Node> get_dimensions(const std::shared_ptr<v3::ShapeOf>& shape, const std::vector<int>& dims) {static const auto zero = v0::Constant::create(ov::element::i32, ov::Shape{}, {0});const auto dims_const = v0::Constant::create(ov::element::i32, ov::Shape{dims.size()}, dims);@@ -60,7 +43,7 @@ ov::OutputVector rotary_embedding(const ov::frontend::onnx::Node& node) {// Original documentation:// https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.RotaryEmbeddingconst auto inputs = node.get_ov_inputs();- const auto& input = inputs[0]; // [bs,seqlen,hidden] or [bs,num_heads,seqlen,headsize]+ const auto& input = inputs[0]; // [bs,seqlen,hidden] or [bs,num_heads,seqlen,head_size]const auto& position_ids = inputs[1]; // [seqlen] or [bs, seqlen]const auto& cos_cache = inputs[2]; // [max_seqlen, head_size/2]const auto& sin_cache = inputs[3]; // [max_seqlen, head_size/2]@@ -68,7 +51,6 @@ ov::OutputVector rotary_embedding(const ov::frontend::onnx::Node& node) {const auto interleaved = node.get_attribute_value<int64_t>("interleaved"); // requiredconst auto minus_one = v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1});const auto zero = v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});- const auto one = v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});const auto two = v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});const auto cos = std::make_shared<v8::Gather>(cos_cache,@@ -89,7 +71,8 @@ ov::OutputVector rotary_embedding(const ov::frontend::onnx::Node& node) {last_dim.is_static(),"cos_cache last dimension must be static to derive head size, got: ",cos_cache_shape);- const auto headsize_val = static_cast<int64_t>(last_dim.get_length() * 2);+ const auto half_head_size_val = static_cast<int64_t>(last_dim.get_length());+ const auto head_size_val = half_head_size_val * 2;const auto input_shape = std::make_shared<v3::ShapeOf>(input);const auto input_rank = input.get_partial_shape().rank();@@ -98,46 +81,82 @@ ov::OutputVector rotary_embedding(const ov::frontend::onnx::Node& node) {ov::Output<ov::Node> input_4d = input;if (input_is_3d) {- const auto headsize = v0::Constant::create(ov::element::i64, ov::Shape{1}, {headsize_val});+ const auto headsize = v0::Constant::create(ov::element::i64, ov::Shape{1}, {head_size_val});const auto input_shape_prev_2 = get_dimensions(input_shape, {0, 1});auto new_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{input_shape_prev_2, minus_one, headsize}, 0);auto input_reshaped =- std::make_shared<v1::Reshape>(input, new_input_shape, false); // [bs,seqlen,num_heads,headsize]- input_4d = std::make_shared<v1::Transpose>(input_reshaped, perm); // [bs,num_heads,seqlen,headsize]+ std::make_shared<v1::Reshape>(input, new_input_shape, false); // [bs,seqlen,num_heads,head_size]+ input_4d = std::make_shared<v1::Transpose>(input_reshaped, perm); // [bs,num_heads,seqlen,head_size]}- ov::Output<ov::Node> output;- if (interleaved) {- auto input_4d_shape = std::make_shared<v3::ShapeOf>(input_4d);- auto dim_bns = get_dimensions(input_4d_shape, {0, 1, 2});- auto half_head_size = v0::Constant::create(ov::element::i64, ov::Shape{1}, {last_dim.get_length()});- auto split_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, half_head_size, two}, 0);- auto reshaped_input = std::make_shared<v1::Reshape>(input_4d, split_input_shape, false);-- auto in_split = make_split(reshaped_input, 2, -1);- split_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, half_head_size}, 0);- auto in_split_0 = std::make_shared<v1::Reshape>(in_split[0], split_input_shape, false);- auto in_split_1 = std::make_shared<v1::Reshape>(in_split[1], split_input_shape, false);-- auto res_0 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(in_split_0, cos),- std::make_shared<v1::Multiply>(in_split_1, sin));- auto res_1 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(in_split_0, sin),- std::make_shared<v1::Multiply>(in_split_1, cos));-- split_input_shape = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, half_head_size, one}, 0);- auto res_0_5d = std::make_shared<v1::Reshape>(res_0, split_input_shape, false);- auto res_1_5d = std::make_shared<v1::Reshape>(res_1, split_input_shape, false);-- auto concat_ret = std::make_shared<v0::Concat>(ov::NodeVector{res_0_5d, res_1_5d}, -1);- output = std::make_shared<v1::Reshape>(concat_ret, input_4d_shape,- false); // [bs,num_heads,seqlen,headsize]+ // Unsqueeze cos/sin to 4D [?, 1, ?, head_size/2] to match RoPE fusion pattern+ ov::Output<ov::Node> cos_4d, sin_4d;+ const auto cos_out_rank = cos->get_output_partial_shape(0).rank();+ if (cos_out_rank.is_static() && cos_out_rank.get_length() == 2) {+ // cos is [seqlen, head_size/2] → [1, 1, seqlen, head_size/2]+ auto axes = v0::Constant::create(ov::element::i64, ov::Shape{2}, {0, 1});+ cos_4d = std::make_shared<v0::Unsqueeze>(cos, axes);+ sin_4d = std::make_shared<v0::Unsqueeze>(sin, axes);} else {- auto in_split = make_split(input_4d, 2, -1); // [bs,num_heads,seqlen,headsize/2]- auto res_0 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(in_split[0], cos),- std::make_shared<v1::Multiply>(in_split[1], sin));- auto res_1 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(in_split[0], sin),- std::make_shared<v1::Multiply>(in_split[1], cos));- output = std::make_shared<v0::Concat>(ov::NodeVector{res_0, res_1}, -1); // [bs,num_heads,seqlen,headsize]+ // cos is [bs, seqlen, head_size/2] → [bs, 1, seqlen, head_size/2]+ auto axes = v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});+ cos_4d = std::make_shared<v0::Unsqueeze>(cos, axes);+ sin_4d = std::make_shared<v0::Unsqueeze>(sin, axes);+ }++ // For interleaved mode, deinterleave first so the core RoPE formula is identical+ ov::Output<ov::Node> rope_input = input_4d;+ std::shared_ptr<v3::ShapeOf> input_4d_shape;+ std::shared_ptr<ov::Node> dim_bns;+ std::shared_ptr<v0::Constant> half_head_size;+ std::shared_ptr<v0::Constant> perm_5d;+ if (interleaved) {+ input_4d_shape = std::make_shared<v3::ShapeOf>(input_4d);+ dim_bns = get_dimensions(input_4d_shape, {0, 1, 2});+ half_head_size = v0::Constant::create(ov::element::i64, ov::Shape{1}, {half_head_size_val});+ perm_5d = v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 1, 2, 4, 3});++ // Deinterleave: [bs,num_heads,seqlen,head_size]+ // → reshape [bs,num_heads,seqlen,head_size/2,2]+ // → transpose [bs,num_heads,seqlen,2,head_size/2]+ // → reshape [bs,num_heads,seqlen,head_size] (now [first_half, second_half])+ auto deinterleave_5d = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, half_head_size, two}, 0);+ auto reshaped_5d = std::make_shared<v1::Reshape>(input_4d, deinterleave_5d, false);+ auto transposed_5d = std::make_shared<v1::Transpose>(reshaped_5d, perm_5d);+ rope_input = std::make_shared<v1::Reshape>(transposed_5d, input_4d_shape, false);+ }++ // Core RoPE formula (matches RoPEFusionGPTOSS pattern for both modes)+ // first_ = first_half * cos - second_half * sin+ // second_ = second_half * cos + first_half * sin+ const auto split_axis = v0::Constant::create(ov::element::i64, ov::Shape{}, {-1});+ const auto split_lengths =+ v0::Constant::create(ov::element::i64, ov::Shape{2}, {half_head_size_val, half_head_size_val});+ // Split along last axis using constant split_lengths to enable RoPE fusion pattern matching+ auto in_split = std::make_shared<v1::VariadicSplit>(rope_input, split_axis, split_lengths)->outputs();+ auto first_half_mul_cos = std::make_shared<v1::Multiply>(in_split[0], cos_4d);+ auto second_half_mul_sin = std::make_shared<v1::Multiply>(in_split[1], sin_4d);+ const auto neg_one = v0::Constant::create(ov::element::f32, ov::Shape{}, {-1.0f});+ auto neg_second_sin = std::make_shared<v1::Multiply>(second_half_mul_sin, neg_one);+ auto res_0 = std::make_shared<v1::Add>(first_half_mul_cos, neg_second_sin);+ auto second_half_mul_cos = std::make_shared<v1::Multiply>(in_split[1], cos_4d);+ auto first_half_mul_sin = std::make_shared<v1::Multiply>(in_split[0], sin_4d);+ auto res_1 = std::make_shared<v1::Add>(second_half_mul_cos, first_half_mul_sin);+ ov::Output<ov::Node> output =+ std::make_shared<v0::Concat>(ov::NodeVector{res_0, res_1}, -1); // [bs,num_heads,seqlen,head_size]++ // For interleaved mode, re-interleave the result+ if (interleaved) {+ // Re-interleave: [bs,num_heads,seqlen,head_size]+ // → reshape [bs,num_heads,seqlen,2,head_size/2]+ // → transpose [bs,num_heads,seqlen,head_size/2,2]+ // → reshape [bs,num_heads,seqlen,head_size]+ auto reinterleave_5d = std::make_shared<v0::Concat>(ov::NodeVector{dim_bns, two, half_head_size}, 0);+ auto result_5d = std::make_shared<v1::Reshape>(output, reinterleave_5d, false);+ auto result_transposed = std::make_shared<v1::Transpose>(result_5d, perm_5d);+ output = std::make_shared<v1::Reshape>(result_transposed,+ input_4d_shape,+ false); // [bs,num_heads,seqlen,head_size]}if (input_is_3d) {