Skip to content

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

[GPU] Support RoPE kernel for ChatGLM4 (#26799)

Paul Youngsoo Ahnb5a0c60a9b4b
diff --git a/src/common/transformations/include/ov_ops/rotary_positional_embeddings.hpp b/src/common/transformations/include/ov_ops/rotary_positional_embeddings.hpp
index 5d839c19600..dcb9aef187d 100644
--- a/src/common/transformations/include/ov_ops/rotary_positional_embeddings.hpp
+++ b/src/common/transformations/include/ov_ops/rotary_positional_embeddings.hpp
@@ -27,6 +27,8 @@ public:
bool is_interleaved = false; // interleaved mode, implies trans0213 happens after RoPE
size_t rotary_ndims = 0; // dimensions to be embedded (d in the description)
bool is_chatglm = false; // chatglm is special which overrides other setting
+ bool support_2d_rope = false; // 2d rope mode, Support 2 dimentional rope which is independant of batch and
+ // each head. change input order to [batch, head_cnt, 4608] to support 2d rope
bool is_qwen = false; // Qwen is special which overrides other setting
size_t head_cnt = 0;
size_t head_size = 0;
diff --git a/src/common/transformations/include/transformations/common_optimizations/fuse_rotary_positional_embeddings.hpp b/src/common/transformations/include/transformations/common_optimizations/fuse_rotary_positional_embeddings.hpp
index 5cd99f88d13..eb1c92bcf96 100644
--- a/src/common/transformations/include/transformations/common_optimizations/fuse_rotary_positional_embeddings.hpp
+++ b/src/common/transformations/include/transformations/common_optimizations/fuse_rotary_positional_embeddings.hpp
@@ -38,7 +38,7 @@ public:
class ov::pass::RoPEFusionChatGLM : public ov::pass::MatcherPass {
public:
OPENVINO_RTTI("RoPEFusionChatGLM", "0");
- RoPEFusionChatGLM(int split_output_id);
+ RoPEFusionChatGLM(int split_output_id, const bool support_2d_rope = false);
};
class ov::pass::RoPEFusionQwen : public ov::pass::MatcherPass {
@@ -84,7 +84,7 @@ private:
class ov::pass::RoPEFusion : public ov::pass::GraphRewrite {
public:
OPENVINO_RTTI("RoPEFusion", "0");
- RoPEFusion() {
+ RoPEFusion(bool support_2d_rope = false) {
add_matcher<ov::pass::RoPEFusionGPTNEOX>();
add_matcher<ov::pass::RoPEFusionGPTJ>();
// optional heads & tails are fused in separate matcher pass,
@@ -95,6 +95,10 @@ public:
add_matcher<ov::pass::RoPEFusionChatGLM>(0);
add_matcher<ov::pass::RoPEFusionChatGLM>(1);
+ if (support_2d_rope) {
+ add_matcher<ov::pass::RoPEFusionChatGLM>(0, true);
+ add_matcher<ov::pass::RoPEFusionChatGLM>(1, true);
+ }
add_matcher<ov::pass::RoPEFusionQwen>(0);
add_matcher<ov::pass::RoPEFusionQwen>(1);
diff --git a/src/common/transformations/src/ov_ops/rotary_positional_embeddings.cpp b/src/common/transformations/src/ov_ops/rotary_positional_embeddings.cpp
index 915adecda0a..3e75e2b88df 100644
--- a/src/common/transformations/src/ov_ops/rotary_positional_embeddings.cpp
+++ b/src/common/transformations/src/ov_ops/rotary_positional_embeddings.cpp
@@ -45,13 +45,27 @@ void RoPE::validate_and_infer_types() {
}
if (m_config.is_chatglm) {
- // chatGLM specific RoPE
- // input [length, batch_size, (hidden_states_q + hidden_states_k + hidden_states_v)]
- // output [length, batch_size, head_cnt, hidden_states_k]
- set_output_type(
- 0,
- get_input_element_type(0),
- {input_pshape[0], input_pshape[1], ov::Dimension(m_config.head_cnt), ov::Dimension(m_config.head_size)});
+ if (m_config.support_2d_rope) {
+ // chatGLM specific RoPE
+ // input [batch_size, length, (hidden_states_q + hidden_states_k + hidden_states_v)]
+ // output [batch_size, head_cnt, length, hidden_states_k]
+ set_output_type(0,
+ get_input_element_type(0),
+ {input_pshape[0],
+ ov::Dimension(m_config.head_cnt),
+ input_pshape[1],
+ ov::Dimension(m_config.head_size)});
+ } else {
+ // chatGLM specific RoPE
+ // input [length, batch_size, (hidden_states_q + hidden_states_k + hidden_states_v)]
+ // output [length, batch_size, head_cnt, hidden_states_k]
+ set_output_type(0,
+ get_input_element_type(0),
+ {input_pshape[0],
+ input_pshape[1],
+ ov::Dimension(m_config.head_cnt),
+ ov::Dimension(m_config.head_size)});
+ }
return;
}
@@ -79,6 +93,7 @@ bool RoPE::visit_attributes(ov::AttributeVisitor& visitor) {
visitor.on_attribute("is_interleaved", m_config.is_interleaved);
visitor.on_attribute("rotary_ndims", m_config.rotary_ndims);
visitor.on_attribute("is_chatglm", m_config.is_chatglm);
+ visitor.on_attribute("support_2d_rope", m_config.support_2d_rope);
visitor.on_attribute("is_qwen", m_config.is_qwen);
visitor.on_attribute("head_cnt", m_config.head_cnt);
visitor.on_attribute("head_size", m_config.head_size);
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.cpp
index b6c19a0a039..143603f0415 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
@@ -417,12 +417,16 @@ ov::pass::RoPEFusionGPTJ::RoPEFusionGPTJ() {
this->register_matcher(m, callback);
}
-ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id) {
+ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool support_2d_rope) {
MATCHER_SCOPE(RoPEFusionChatGLM);
- auto qkv_linear = makePattern("[?,?,?]"); // [seq_length, batch_size, 4608]
+ // [seq_length, batch_size, input_size(will be cropped to match hidden state size)]
+ // [batch_size, seq_length, input_size] support_2d_rope
+ auto qkv_linear = makePattern("[?,?,?]");
auto seq_length = makePattern("i32[1]");
- auto cos_sin_cache = makePattern("[?,?,?,?]"); // [max_pos_embeddings, batch_size, 32, 2]
+ // [max_pos_embeddings, batch_size, half_rotary_dims, 2]
+ // [batch_size, max_pos_embeddings, half_rotary_dims, 2] support_2d_rope
+ auto cos_sin_cache = makePattern("[?,?,?,?]");
auto ndims = ov::gen_pattern::Symbol("ndims");
auto head_cnt = ov::gen_pattern::Symbol("head_cnt");
@@ -436,37 +440,76 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id) {
auto qkv_proj = makePattern<opset1::VariadicSplit>({qkv_linear, -1, {total_size_q, total_size_k, total_size_v}});
qkv_proj->set_output_size(3);
- // get key [L, B, Hkv, S]
auto cur_key = makePattern<opset1::Reshape>({qkv_proj->output(split_output_id), {0, 0, head_cnt, head_size}},
{{"special_zero", true}});
- auto slice_Slice_437 = GenSlice(cur_key, 0, ndims, 1, 3);
- auto var_split_1 = makePattern<opset1::VariadicSplit>({cur_key, 3, {ndims, ov::gen_pattern::Symbol("end")}});
+ std::shared_ptr<ov::Node> input_key = nullptr;
+ // Extended the RoPE to a two-dimensional form to accommodate the 2D positional encoding in GLM.
+ // Calculate positional embedding independent of batch and each head
+ if (support_2d_rope) {
+ // Get transposed key [batch, head_cnt, seq_length, head_size]
+ input_key = makePattern<opset1::Transpose>({cur_key, {0, 2, 1, 3}});
+ } else {
+ // Get key [seq_length, batch, head_cnt, head_size]
+ input_key = std::move(cur_key);
+ }
+
+ auto slice_Slice_437 = GenSlice(input_key, 0, ndims, 1, 3);
+ auto var_split_1 = makePattern<opset1::VariadicSplit>({input_key, 3, {ndims, ov::gen_pattern::Symbol("end")}});
var_split_1->set_output_size(2);
// rotate half
- auto ListConstruct_452_Concat =
- makePattern<opset1::Concat>({seq_length, {-1}, {head_cnt}, {ndims / 2}, {2}}, {{"axis", 0}});
- auto const_target_shape_1 = makeConst({seq_len, batch, head_cnt, ndims / 2, 2});
-
- auto ListConstruct_379_Concat =
- makePattern<opset1::Concat>({seq_length, {-1}, {1}, {ndims / 2}, {2}}, {{"axis", 0}});
- auto const_target_shape_2 = makeConst({seq_len, batch, 1, ndims / 2, 2});
-
- auto reshape_Reshape_453 = makePattern<opset1::Reshape>(
- {slice_Slice_437 | var_split_1->output(0), ListConstruct_452_Concat | const_target_shape_1});
+ std::shared_ptr<ov::Node> reshape_Reshape_453 = nullptr;
+ if (support_2d_rope) {
+ auto const_target_shape_1 = makeConst({0, head_cnt, 0, ndims / 2, 2});
+ reshape_Reshape_453 =
+ makePattern<opset1::Reshape>({slice_Slice_437 | var_split_1->output(0), const_target_shape_1},
+ {{"special_zero", true}});
+ } else {
+ auto ListConstruct_452_Concat =
+ makePattern<opset1::Concat>({seq_length, {-1}, {head_cnt}, {ndims / 2}, {2}}, {{"axis", 0}});
+ auto const_target_shape_1 = makeConst({seq_len, batch, head_cnt, ndims / 2, 2});
+ reshape_Reshape_453 = makePattern<opset1::Reshape>(
+ {slice_Slice_437 | var_split_1->output(0), ListConstruct_452_Concat | const_target_shape_1});
+ }
auto x_even = makePattern<opset8::Gather>({reshape_Reshape_453, 0, -1}, {{"batch_dims", 0}});
auto x_odd = makePattern<opset8::Gather>({reshape_Reshape_453, 1, -1}, {{"batch_dims", 0}});
- auto slice_Slice_449 = makePattern<ov::opset8::Slice>({cos_sin_cache, {0}, seq_length, {1}, {0}});
- auto slice_StridedSlice_449 = GenStridedSlice(cos_sin_cache, {0}, seq_length, {1}, 0);
+
auto var_split_2 = makePattern<opset1::VariadicSplit>({cos_sin_cache, 0, {0, ov::gen_pattern::Symbol("end")}});
var_split_2->set_output_size(2);
- auto view_Reshape_460 =
- makePattern<opset1::Reshape>({slice_StridedSlice_449 | slice_Slice_449 | var_split_2->output(0),
- ListConstruct_379_Concat | const_target_shape_2},
- {{"special_zero", false}});
+ std::shared_ptr<ov::Node> view_Reshape_460 = nullptr;
+ if (support_2d_rope) {
+ auto ListConstruct_379_Concat =
+ makePattern<opset1::Concat>({{-1}, {1}, seq_length, {ndims / 2}, {2}}, {{"axis", 0}});
+ auto const_target_shape_2 = makeConst({batch, 1, seq_len, ndims / 2, 2});
+
+ // Slice cos_sin_cache to support 2-dimentional RoPE
+ auto ScatterUpdate = makePattern<opset3::ScatterUpdate>({{0, 0}, {1}, seq_length, {0}}, {});
+ auto slice_Slice_449_1d = makePattern<ov::opset8::Slice>({cos_sin_cache, {0}, seq_length, {1}, {1}});
+ auto slice_Slice_449_2d = makePattern<ov::opset8::Slice>({cos_sin_cache, {0, 0}, ScatterUpdate, {1, 1}, {0}});
+ auto slice_StridedSlice_449 = GenStridedSlice(cos_sin_cache, {0, 0}, ScatterUpdate, {1, 1}, 1);
+
+ // [batch, 1, seq_length, half_rotary_dims, 2]
+ view_Reshape_460 = makePattern<opset1::Reshape>(
+ {slice_StridedSlice_449 | slice_Slice_449_1d | slice_Slice_449_2d | var_split_2->output(0),
+ ListConstruct_379_Concat | const_target_shape_2},
+ {{"special_zero", false}});
+ } else {
+ auto ListConstruct_379_Concat =
+ makePattern<opset1::Concat>({seq_length, {-1}, {1}, {ndims / 2}, {2}}, {{"axis", 0}});
+ auto const_target_shape_2 = makeConst({seq_len, batch, 1, ndims / 2, 2});
+
+ auto slice_Slice_449 = makePattern<ov::opset8::Slice>({cos_sin_cache, {0}, seq_length, {1}, {0}});
+ auto slice_StridedSlice_449 = GenStridedSlice(cos_sin_cache, {0}, seq_length, {1}, 0);
+
+ // [seq_length, 1, batch, half_rotary_dims, 2]
+ view_Reshape_460 =
+ makePattern<opset1::Reshape>({slice_StridedSlice_449 | slice_Slice_449 | var_split_2->output(0),
+ ListConstruct_379_Concat | const_target_shape_2},
+ {{"special_zero", false}});
+ }
auto cos_tab = makePattern<opset8::Gather>({view_Reshape_460, 0, -1}, {{"batch_dims", 0}});
auto x_even_cos = makePattern<opset1::Multiply>({x_even, cos_tab}, {{"auto_broadcast", "numpy"}});
@@ -487,11 +530,21 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id) {
auto ShapeOf_135133 = makePattern<opset1::ShapeOf>({stack_481});
auto flatten_Slice_497 = GenSlice(ShapeOf_135133, 0, 3, 1, 0);
auto flatten_Concat_500 = makePattern<opset1::Concat>({flatten_Slice_497, {-1}}, {{"axis", 0}});
- auto const_target_shape_3 = makeConst({seq_len, batch, head_cnt, ndims});
- // [length, batch, head_cnt, half_rotary_dims, 2]
- auto flatten_Reshape_501 =
- makePattern<opset1::Reshape>({stack_481, flatten_Concat_500 | const_target_shape_3}, {{"special_zero", true}});
- auto slice_Slice_443 = GenSlice(cur_key, ndims, INT_MAX, 1, 3);
+
+ std::shared_ptr<ov::Node> const_target_shape_3 = nullptr;
+ std::shared_ptr<ov::Node> flatten_Reshape_501 = nullptr;
+ if (support_2d_rope) {
+ // [batch, head_cnt, length, half_rotary_dims, 2]
+ const_target_shape_3 = makeConst({batch, head_cnt, seq_len, ndims});
+ flatten_Reshape_501 = makePattern<opset1::Reshape>({stack_481, flatten_Concat_500 | const_target_shape_3},
+ {{"special_zero", true}});
+ } else {
+ // [length, batch, head_cnt, half_rotary_dims, 2]
+ const_target_shape_3 = makeConst({seq_len, batch, head_cnt, ndims});
+ flatten_Reshape_501 = makePattern<opset1::Reshape>({stack_481, flatten_Concat_500 | const_target_shape_3},
+ {{"special_zero", true}});
+ }
+ auto slice_Slice_443 = GenSlice(input_key, ndims, INT_MAX, 1, 3);
auto cat_Concat_505 =
makePattern<opset1::Concat>({flatten_Reshape_501, slice_Slice_443 | var_split_1->output(1)}, {{"axis", -1}});
@@ -510,6 +563,7 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id) {
OutputVector new_args;
config.rotary_ndims = static_cast<size_t>(validator["ndims"]);
config.is_chatglm = true;
+ config.support_2d_rope = support_2d_rope;
config.head_cnt = static_cast<size_t>(validator["head_cnt"]);
config.head_size = static_cast<size_t>(validator["head_size"]);
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.cpp
index 5b54b4a7cce..6eb0add525c 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
@@ -135,6 +135,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_LLama2_no_gather) {
{"config.input_trans0213", true},
{"config.is_interleaved", false},
{"config.is_chatglm", false},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", 0},
{"config.head_size", 0},
@@ -170,6 +171,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_LLama2_with_gather) {
{"config.input_trans0213", true},
{"config.is_interleaved", false},
{"config.is_chatglm", false},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", 0},
{"config.head_size", 0},
@@ -308,6 +310,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTNEOX_no_gather) {
{"config.input_trans0213", true},
{"config.is_interleaved", false},
{"config.is_chatglm", false},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", 0},
{"config.head_size", 0},
@@ -342,6 +345,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTNEOX_with_gather) {
{"config.input_trans0213", true},
{"config.is_interleaved", false},
{"config.is_chatglm", false},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", 0},
{"config.head_size", 0},
@@ -457,6 +461,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTJ) {
{"config.input_trans0213", false},
{"config.is_interleaved", true},
{"config.is_chatglm", false},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", 0},
{"config.head_size", 0},
@@ -566,6 +571,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_chatGML) {
{"config.is_interleaved", false},
{"config.rotary_ndims", rotary_ndims},
{"config.is_chatglm", true},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", num_heads},
{"config.head_size", ndims},
@@ -643,6 +649,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_chatGML_Slice) {
{"config.is_interleaved", false},
{"config.rotary_ndims", rotary_ndims},
{"config.is_chatglm", true},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", num_heads},
{"config.head_size", ndims},
@@ -723,6 +730,7 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTJ_Slice) {
{"config.input_trans0213", false},
{"config.is_interleaved", true},
{"config.is_chatglm", false},
+ {"config.support_2d_rope", false},
{"config.is_qwen", false},
{"config.head_cnt", 0},
{"config.head_size", 0},
@@ -730,4 +738,120 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTJ_Slice) {
{"config.gather_position_arg_id", 0}});
model_ref = std::make_shared<ov::Model>(ov::NodeVector{rope}, ov::ParameterVector{input, cos_sin});
}
+}
+
+TEST_F(TransformationTestsF, ConvertToROPE_chatGML_2d_rope) {
+ disable_rt_info_check();
+ const int batch = 2;
+ const int seq_len = 7;
+ const int num_heads = 32;
+ const int ndims = 128;
+ const int rotary_ndims = 64;
+ const int max_pos_length = 2048;
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{batch, seq_len, 4608});
+ auto cos_sin_cache =
+ std::make_shared<ov::opset1::Parameter>(ov::element::f32,
+ ov::PartialShape{max_pos_length, (rotary_ndims / 2), 2});
+ auto position_ids = std::make_shared<ov::opset1::Parameter>(ov::element::i32, ov::PartialShape{batch, seq_len});
+
+ auto __module_transformer_index_67_Gather =
+ makeOP<ov::opset8::Gather>({cos_sin_cache, position_ids, 0}, {{"batch_dims", 0}});
+
+ auto ListUnpack_321 = makeOP<ov::opset1::VariadicSplit>({input, -1, {4096, 256, 256}});
+ auto view_Reshape = makeOP<ov::opset1::Reshape>({ListUnpack_321->output(0), {0, 0, num_heads, ndims}},
+ {{"special_zero", true}});
+
+ auto permute_Transpose = makeOP<ov::opset1::Transpose>({view_Reshape, {0, 2, 1, 3}}, {});
+
+ auto slice_Slice_357 =
+ makeOP<ov::opset1::StridedSlice>({permute_Transpose, {0, 0, 0, 0}, {0, 0, 0, rotary_ndims}, {1, 1, 1, 1}},
+ {{"begin_mask", {1, 1, 1, 0}},
+ {"end_mask", {1, 1, 1, 0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+
+ auto aten_view_Reshape_1 =
+ makeOP<ov::opset1::Reshape>({ListUnpack_321->output(1), {0, 0, 2, ndims}}, {{"special_zero", true}});
+ auto aten_transpose_1 = makeOP<ov::opset8::Transpose>({aten_view_Reshape_1, {0, 2, 1, 3}});
+ auto shape_of_105249 = makeOP<ov::opset8::ShapeOf>({aten_transpose_1}, {{"output_type", "i32"}});
+ auto gather_105252 = makeOP<ov::opset8::Gather>({shape_of_105249, {2}, {0}}, {{"batch_dims", 0}});
+ auto scatter_update_63441 = makeOP<ov::opset8::ScatterUpdate>({{0, 0}, {1}, gather_105252, {0}});
+ // connected to cos_sin_cache
+ auto slice_Slice_369 = makeOP<ov::opset1::StridedSlice>(
+ {__module_transformer_index_67_Gather, {0, 0}, scatter_update_63441, {1, 1}},
+ {{"begin_mask", {1, 0}},
+ {"end_mask", {1, 0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+ auto list_construct_concat_1 =
+ makeOP<ov::opset1::Concat>({{-1}, {1}, gather_105252, {rotary_ndims / 2}, {2}}, {{"axis", 0}});
+
+ auto reshape_Reshape_373 =
+ makeOP<ov::opset1::Reshape>({slice_Slice_357, {0, 32, 0, 32, 2}}, {{"special_zero", true}});
+ auto select_Gather_384 =
+ makeOP<ov::opset8::Gather>({reshape_Reshape_373, 0, -1}, {{"batch_dims", 0}}); // x_even
+ auto select_Gather_381 =
+ makeOP<ov::opset8::Gather>({reshape_Reshape_373, 1, -1}, {{"batch_dims", 0}}); // x_odd
+ auto view_Reshape_380 =
+ makeOP<ov::opset1::Reshape>({slice_Slice_369, list_construct_concat_1}, {{"special_zero", false}});
+ auto select_Gather_385 = makeOP<ov::opset8::Gather>({view_Reshape_380, 0, -1}, {{"batch_dims", 0}}); // cos_tab
+ auto select_Gather_382 = makeOP<ov::opset8::Gather>({view_Reshape_380, 1, -1}, {{"batch_dims", 0}}); // sin_tab
+
+ auto mul_Multiply_386 = makeOP<ov::opset1::Multiply>({select_Gather_381, select_Gather_382},
+ {{"auto_broadcast", "numpy"}}); // x_odd_sin
+ auto mul_Multiply_383 = makeOP<ov::opset1::Multiply>({select_Gather_384, select_Gather_385},
+ {{"auto_broadcast", "numpy"}}); // x_even_cos
+ auto Multiply_101315 =
+ makeOP<ov::opset1::Multiply>({mul_Multiply_386, -1.000000f}, {{"auto_broadcast", "numpy"}});
+ auto sub_Subtract_389 =
+ makeOP<ov::opset1::Add>({mul_Multiply_383, Multiply_101315}, {{"auto_broadcast", "numpy"}});
+
+ auto mul_Multiply_391 = makeOP<ov::opset1::Multiply>({select_Gather_381, select_Gather_385},
+ {{"auto_broadcast", "numpy"}}); // x_odd_cos
+ auto mul_Multiply_393 = makeOP<ov::opset1::Multiply>({select_Gather_384, select_Gather_382},
+ {{"auto_broadcast", "numpy"}}); // x_even_sin
+ auto add_Add_396 = makeOP<ov::opset1::Add>({mul_Multiply_391, mul_Multiply_393}, {{"auto_broadcast", "numpy"}});
+
+ auto Unsqueeze_62716 = makeOP<ov::opset1::Unsqueeze>({sub_Subtract_389, -1}, {});
+ auto Unsqueeze_62717 = makeOP<ov::opset1::Unsqueeze>({add_Add_396, -1}, {});
+
+ auto stack_401 = makeOP<ov::opset1::Concat>({Unsqueeze_62716, Unsqueeze_62717}, {{"axis", -1}});
+ auto flatten_Reshape_421 =
+ makeOP<ov::opset1::Reshape>({stack_401, {0, num_heads, 0, rotary_ndims}}, {{"special_zero", true}});
+ auto slice_Slice_363 = makeOP<ov::opset1::StridedSlice>(
+ {permute_Transpose, {0, 0, 0, rotary_ndims}, {0, 0, 0, INT_MAX}, {1, 1, 1, 1}},
+ {{"begin_mask", {1, 1, 1, 0}},
+ {"end_mask", {1, 1, 1, 0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+ auto cat_Concat_425 = makeOP<ov::opset1::Concat>({flatten_Reshape_421, slice_Slice_363}, {{"axis", -1}});
+ model = std::make_shared<ov::Model>(ov::NodeVector{cat_Concat_425},
+ ov::ParameterVector{input, cos_sin_cache, position_ids});
+ }
+ manager.register_pass<ov::pass::RoPEFusion>(true);
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{batch, seq_len, 4608});
+ auto cos_sin_cache =
+ std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{max_pos_length, (rotary_ndims / 2), 2});
+ auto position_ids = std::make_shared<ov::opset1::Parameter>(ov::element::i32, ov::PartialShape{batch, seq_len});
+ auto gather_cos_sin = makeOP<ov::opset8::Gather>({cos_sin_cache, position_ids, 0}, {{"batch_dims", 0}});
+ auto rope = makeOP<ov::op::internal::RoPE>({input, gather_cos_sin, gather_cos_sin},
+ {{"config.slice_start", 0},
+ {"config.slice_stop", 4096},
+ {"config.input_trans0213", false},
+ {"config.is_interleaved", false},
+ {"config.rotary_ndims", rotary_ndims},
+ {"config.is_chatglm", true},
+ {"config.support_2d_rope", true},
+ {"config.is_qwen", false},
+ {"config.head_cnt", num_heads},
+ {"config.head_size", ndims},
+ {"config.gather_position_arg_id", 0}});
+ model_ref =
+ std::make_shared<ov::Model>(ov::NodeVector{rope}, ov::ParameterVector{input, cos_sin_cache, position_ids});
+ }
}
\ No newline at end of file
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/rope.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/rope.hpp
index 8f4ae2c6633..d7933e2180f 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/rope.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/rope.hpp
@@ -26,7 +26,11 @@ struct rope : public primitive_base<rope> {
size_t gather_rank = 0)
: primitive_base(id, inputs),
config(config),
- gather_rank(gather_rank) {}
+ gather_rank(gather_rank) {
+ OPENVINO_ASSERT((!config.support_2d_rope
+ || (config.support_2d_rope && config.is_chatglm)),
+ "2D RoPE is currently only supported in Chatglm!");
+ }
RoPE::Config config;
size_t gather_rank = 0;
@@ -38,6 +42,7 @@ struct rope : public primitive_base<rope> {
seed = hash_combine(seed, config.head_size);
seed = hash_combine(seed, config.input_trans0213);
seed = hash_combine(seed, config.is_chatglm);
+ seed = hash_combine(seed, config.support_2d_rope);
seed = hash_combine(seed, config.is_interleaved);
seed = hash_combine(seed, config.is_qwen);
seed = hash_combine(seed, config.rotary_ndims);
@@ -58,6 +63,7 @@ struct rope : public primitive_base<rope> {
config.head_size == rhs_casted.config.head_size &&
config.input_trans0213 == rhs_casted.config.input_trans0213 &&
config.is_chatglm == rhs_casted.config.is_chatglm &&
+ config.support_2d_rope == rhs_casted.config.support_2d_rope &&
config.is_interleaved == rhs_casted.config.is_interleaved &&
config.is_qwen == rhs_casted.config.is_qwen &&
config.rotary_ndims == rhs_casted.config.rotary_ndims &&
@@ -73,6 +79,7 @@ struct rope : public primitive_base<rope> {
ob << config.head_size;
ob << config.input_trans0213;
ob << config.is_chatglm;
+ ob << config.support_2d_rope;
ob << config.is_interleaved;
ob << config.is_qwen;
ob << config.rotary_ndims;
@@ -88,6 +95,7 @@ struct rope : public primitive_base<rope> {
ib >> config.head_size;
ib >> config.input_trans0213;
ib >> config.is_chatglm;
+ ib >> config.support_2d_rope;
ib >> config.is_interleaved;
ib >> config.is_qwen;
ib >> config.rotary_ndims;
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/rope.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/rope.cpp
index f65768b8e6e..7764b7b0964 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/rope.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/rope.cpp
@@ -53,6 +53,7 @@ struct rope_impl : typed_primitive_impl_ocl<rope> {
params.is_qwen = primitive->config.is_qwen;
params.is_chatglm = primitive->config.is_chatglm;
+ params.support_2d_rope = primitive->config.support_2d_rope;
params.transposed_input = primitive->config.input_trans0213;
for (size_t i = 1; i < impl_param.input_layouts.size(); ++i) {
diff --git a/src/plugins/intel_gpu/src/graph/rope.cpp b/src/plugins/intel_gpu/src/graph/rope.cpp
index ea904916d4c..e168626f8d6 100644
--- a/src/plugins/intel_gpu/src/graph/rope.cpp
+++ b/src/plugins/intel_gpu/src/graph/rope.cpp
@@ -30,11 +30,24 @@ std::vector<layout> rope_inst::calc_output_layouts(rope_node const& node, kernel
ShapeType output_shape = input0_shape;
- if (desc->config.is_qwen || desc->config.is_chatglm) {
+ if (desc->config.is_qwen) {
output_shape = { input0_shape[0],
input0_shape[1],
ov::Dimension(desc->config.head_cnt),
ov::Dimension(desc->config.head_size) };
+ } else if (desc->config.is_chatglm) {
+ if (desc->config.support_2d_rope) {
+ // input0_shape = [batch_size, seq_length]
+ output_shape = { input0_shape[0],
+ ov::Dimension(desc->config.head_cnt),
+ input0_shape[1],
+ ov::Dimension(desc->config.head_size) };
+ } else {
+ output_shape = { input0_shape[0],
+ input0_shape[1],
+ ov::Dimension(desc->config.head_cnt),
+ ov::Dimension(desc->config.head_size) };
+ }
} else {
auto input_slice_size = desc->config.slice_stop - desc->config.slice_start;
if (input_slice_size > 0) {
@@ -63,6 +76,7 @@ std::string rope_inst::to_string(rope_node const& node) {
rope_info.add("head_size", desc->config.head_size);
rope_info.add("input_trans0213", desc->config.input_trans0213);
rope_info.add("is_chatglm", desc->config.is_chatglm);
+ rope_info.add("support_2d_rope", desc->config.support_2d_rope);
rope_info.add("is_interleaved", desc->config.is_interleaved);
rope_info.add("is_qwen", desc->config.is_qwen);
rope_info.add("rotary_ndims", desc->config.rotary_ndims);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rope_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rope_ref.cl
index 36d4306b59b..38066b4461d 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rope_ref.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/rope_ref.cl
@@ -11,12 +11,22 @@ KERNEL(rope_ref)(
const __global INPUT1_TYPE* cos_sin,
__global OUTPUT_TYPE* output)
{
+#ifdef SUPPORT_2D_ROPE
+ const uint p = get_global_id(0) / HEAD_COUNT;
+ const uint h = get_global_id(0) % HEAD_COUNT;
+ const uint b = get_global_id(1);//sequence length
+ const uint rf = get_global_id(2);//max(HALF_ROTARY_NDIMS, HEAD_SIZE - ROTARY_NDIMS)
+ uint output_idx = OUTPUT_GET_INDEX(p, h, b, 0);
+#else
const uint p = get_global_id(0);
const uint b = get_global_id(1);
const uint h = (uint)get_global_id(2) % HEAD_COUNT;
const uint rf = (uint)get_global_id(2) / HEAD_COUNT;
+ uint output_idx = OUTPUT_GET_INDEX(p, b, h, 0);
+#endif
+
uint r = rf < HALF_ROTARY_NDIMS ? rf * 2 : 0;
- uint f = rf < HEAD_SIZE - ROTARY_NDIMS ? rf : 0;
+ uint f = rf < HEAD_SIZE - ROTARY_NDIMS ? rf * 2 : 0;
#ifdef ENABLE_SLICE
uint input_idx = GET_DATA_INDEX(SLICED_INPUT0, p, b, h * HEAD_SIZE, 0);
@@ -30,19 +40,18 @@ KERNEL(rope_ref)(
uint cos_sin_b = b < INPUT1_FEATURE_NUM ? b : 0;
uint cos_sin_idx = INPUT1_GET_INDEX(cos_sin_p, cos_sin_b, 0, 0);
- uint output_idx = OUTPUT_GET_INDEX(p, b, h, 0);
-
- INPUT1_TYPE cosv = cos_sin[cos_sin_idx + r];
- INPUT1_TYPE sinv = cos_sin[cos_sin_idx + r + 1];
+ float cosv = convert_float(cos_sin[cos_sin_idx + r]);
+ float sinv = convert_float(cos_sin[cos_sin_idx + r + 1]);
- INPUT0_TYPE in1 = input[input_idx + r];
- INPUT0_TYPE in2 = input[input_idx + r + 1];
+ float in1 = convert_float(input[input_idx + r]);
+ float in2 = convert_float(input[input_idx + r + 1]);
- output[output_idx + r] = cosv * in1 - sinv * in2;
- output[output_idx + r + 1] = sinv * in1 + cosv * in2;
+ output[output_idx + r] = TO_OUTPUT_TYPE(cosv * in1 - sinv * in2);
+ output[output_idx + r + 1] = TO_OUTPUT_TYPE(sinv * in1 + cosv * in2);
#ifdef ENABLE_IO_COPY
output[output_idx + ROTARY_NDIMS + f] = input[input_idx + ROTARY_NDIMS + f];
+ output[output_idx + ROTARY_NDIMS + f + 1] = input[input_idx + ROTARY_NDIMS + f + 1];
#endif
}
#endif
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.cpp
index a9e0818aeae..a48632f6c45 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.cpp
@@ -70,6 +70,9 @@ JitConstants RoPEKernelBase::GetJitConstants(const rope_params& params, RoPEKern
if (params.is_qwen) {
jit.AddConstant(MakeJitConstant("QWEN", true));
} else if (params.is_chatglm) {
+ if (params.support_2d_rope) {
+ jit.AddConstant(MakeJitConstant("SUPPORT_2D_ROPE", true));
+ }
jit.AddConstant(MakeJitConstant("CHATGLM", true));
} else {
jit.AddConstant(MakeJitConstant("RotateHalf", true));
@@ -85,10 +88,22 @@ RoPEKernelBase::DispatchData RoPEKernelBase::SetDefault(const rope_params& param
std::vector<std::vector<Tensor::DataChannelName>> dims_by_gws = {{ Tensor::DataChannelName::BATCH }, { Tensor::DataChannelName::FEATURE },
{ Tensor::DataChannelName::Y, Tensor::DataChannelName::X }};
- if (params.is_chatglm || params.is_qwen) {
+ if (params.is_qwen) {
dispatchData.gws = {input.Batch().v,
input.Feature().v,
params.head_cnt * std::max(params.rotary_ndims / 2ul, params.head_size - params.rotary_ndims)};
+ } else if (params.is_chatglm) {
+ if (params.support_2d_rope) {
+ // input [batch_size, seq_length]
+ // output [batch_size, head_count, seq_length, half_rotary_ndims]
+ dispatchData.gws = {input.Batch().v * params.head_cnt,
+ input.Feature().v,
+ params.rotary_ndims / 2ul};
+ } else {
+ dispatchData.gws = {input.Batch().v,
+ input.Feature().v,
+ params.head_cnt * (params.rotary_ndims / 2ul)};
+ }
} else {
dispatchData.gws = {output.Batch().v,
output.Feature().v,
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.h
index 5d55fd08276..472131eba5d 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.h
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/rope/rope_kernel_base.h
@@ -24,6 +24,7 @@ struct rope_params : public base_params {
bool is_qwen = false;
bool is_chatglm = false;
+ bool support_2d_rope = false;
bool transposed_input = false;
};
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 40c7ab48c48..f173e378fca 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -862,7 +862,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
const size_t zp_pad_size = device_info.supports_immad ? 16 : 32;
manager.register_pass<ov::intel_gpu::BroadcastAndPadZeroPointBuffers>(zp_pad_size, device_info.supports_immad);
- manager.register_pass<ov::pass::RoPEFusion>();
+ manager.register_pass<ov::pass::RoPEFusion>(true);
pass_config->disable<ov::pass::RoPEFusionGPTJ>();
pass_config->disable<ov::pass::RoPEFusionIOSlicing>();
pass_config->disable<ov::pass::RoPEShareCosSin>();
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cpp
index 9565036f7b4..741014b461e 100644
--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cpp
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/rotary_pos_emb.cpp
@@ -44,5 +44,11 @@ INSTANTIATE_TEST_SUITE_P(smoke_RoPETestLlama2,
::testing::Values(ov::test::utils::DEVICE_GPU),
RoPETestLlama2Slice::getTestCaseName);
+INSTANTIATE_TEST_SUITE_P(smoke_RoPETestChatGLM,
+ RoPETestChatGLM2DRoPEStridedSlice,
+ ::testing::Values(ov::test::utils::DEVICE_GPU),
+ RoPETestChatGLM2DRoPEStridedSlice::getTestCaseName);
+
+
} // namespace test
} // namespace ov
diff --git a/src/tests/functional/plugin/shared/include/subgraph_tests/rotary_pos_emb.hpp b/src/tests/functional/plugin/shared/include/subgraph_tests/rotary_pos_emb.hpp
index f2b19a6748a..7100ddca108 100644
--- a/src/tests/functional/plugin/shared/include/subgraph_tests/rotary_pos_emb.hpp
+++ b/src/tests/functional/plugin/shared/include/subgraph_tests/rotary_pos_emb.hpp
@@ -87,5 +87,12 @@ TEST_P(RoPETestGPTJSlice, CompareWithRefs) {
CheckNumberOfNodesWithType(function, {"RoPE"}, 1);
};
+TEST_P(RoPETestChatGLM2DRoPEStridedSlice, CompareWithRefs) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+ run();
+ auto function = compiledModel.get_runtime_model();
+ CheckNumberOfNodesWithType(function, {"RoPE"}, 1);
+};
+
} // namespace test
} // namespace ov
diff --git a/src/tests/functional/shared_test_classes/include/shared_test_classes/subgraph/rotary_pos_emb.hpp b/src/tests/functional/shared_test_classes/include/shared_test_classes/subgraph/rotary_pos_emb.hpp
index 2663a6f5ad3..e1182bd3b16 100644
--- a/src/tests/functional/shared_test_classes/include/shared_test_classes/subgraph/rotary_pos_emb.hpp
+++ b/src/tests/functional/shared_test_classes/include/shared_test_classes/subgraph/rotary_pos_emb.hpp
@@ -115,5 +115,17 @@ protected:
void SetUp() override;
};
+class RoPETestChatGLM2DRoPEStridedSlice : public SubgraphBaseTest, public testing::WithParamInterface<std::string> {
+private:
+ std::shared_ptr<ov::Model> buildROPE_ChatGLM(int batch, int head_cnt, int rotary_dims);
+protected:
+ ov::Tensor create_i32_tensor(const ov::Shape& shape, int start, int step = 1);
+ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override;
+ void SetUp() override;
+
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<std::string>& obj);
+};
+
} // namespace test
} // namespace ov
diff --git a/src/tests/functional/shared_test_classes/src/subgraph/rotary_pos_emb.cpp b/src/tests/functional/shared_test_classes/src/subgraph/rotary_pos_emb.cpp
index 46ea730ac32..a1848903bb7 100644
--- a/src/tests/functional/shared_test_classes/src/subgraph/rotary_pos_emb.cpp
+++ b/src/tests/functional/shared_test_classes/src/subgraph/rotary_pos_emb.cpp
@@ -1027,5 +1027,129 @@ std::shared_ptr<ov::Model> RoPETestGPTJSlice::buildROPE_GPTJ(int num_head,
return std::make_shared<ov::Model>(model_output, ov::ParameterVector{input, sincos});
}
+std::shared_ptr<ov::Model> RoPETestChatGLM2DRoPEStridedSlice::buildROPE_ChatGLM(int batch, int head_cnt, int rotary_dims) {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, PartialShape{batch, -1, 4096 + 256 + 256});
+ auto cos_sin_cache = std::make_shared<ov::opset1::Parameter>(ov::element::f32, PartialShape{32768, 32, 2});
+ auto position_ids = std::make_shared<ov::opset1::Parameter>(ov::element::i32, PartialShape{-1, -1});
+
+ auto __module_transformer_index_67_Gather =
+ makeOP<opset8::Gather>({cos_sin_cache, position_ids, 0}, {{"batch_dims", 0}});
+
+ auto ListUnpack_321 = makeOP<opset1::VariadicSplit>({input, -1, {4096, 256, 256}});
+ auto view_Reshape = makeOP<opset1::Reshape>({ListUnpack_321->output(0), {0, 0, 32, 128}}, {{"special_zero", true}});
+
+ auto permute_Transpose = makeOP<opset1::Transpose>({view_Reshape, {0, 2, 1, 3}}, {});
+
+ auto slice_Slice_357 =
+ makeOP<opset1::StridedSlice>({permute_Transpose, {0, 0, 0, 0}, {0, 0, 0, 64}, {1, 1, 1, 1}},
+ {{"begin_mask", {1, 1, 1, 0}},
+ {"end_mask", {1, 1, 1, 0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+
+ auto aten_view_Reshape_1 = makeOP<opset1::Reshape>({ListUnpack_321->output(1), {0, 0, 2, 128}}, {{"special_zero", true}});
+ auto aten_transpose_1 = makeOP<opset8::Transpose>({aten_view_Reshape_1, {0, 2, 1, 3}});
+ auto shape_of_105249 = makeOP<opset8::ShapeOf>({aten_transpose_1}, {{"output_type", "i32"}});
+ auto gather_105252 = makeOP<opset8::Gather>({shape_of_105249, {2}, {0}}, {{"batch_dims", 0}});
+ auto scatter_update_63441 = makeOP<opset8::ScatterUpdate>({{0, 0}, {1}, gather_105252, {0}});
+ // connected to cos_sin_cache
+ auto slice_Slice_369 =
+ makeOP<opset1::StridedSlice>({__module_transformer_index_67_Gather, {0, 0}, scatter_update_63441, {1, 1}},
+ {{"begin_mask", {1, 0}},
+ {"end_mask", {1, 0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+ auto list_construct_concat_1 = makeOP<opset1::Concat>({{-1}, {1}, gather_105252, {32}, {2}}, {{"axis", 0}});
+
+ auto reshape_Reshape_373 =
+ makeOP<opset1::Reshape>({slice_Slice_357, {0, 32, 0, 32, 2}}, {{"special_zero", true}});
+ auto select_Gather_384 = makeOP<opset8::Gather>({reshape_Reshape_373, 0, -1}, {{"batch_dims", 0}});//x_even
+ auto select_Gather_381 = makeOP<opset8::Gather>({reshape_Reshape_373, 1, -1}, {{"batch_dims", 0}});//x_odd
+
+ auto view_Reshape_380 =
+ makeOP<opset1::Reshape>({slice_Slice_369, list_construct_concat_1}, {{"special_zero", false}});
+ auto select_Gather_385 = makeOP<opset8::Gather>({view_Reshape_380, 0, -1}, {{"batch_dims", 0}});//cos_tab
+ auto select_Gather_382 = makeOP<opset8::Gather>({view_Reshape_380, 1, -1}, {{"batch_dims", 0}});//sin_tab
+
+ auto mul_Multiply_386 =
+ makeOP<opset1::Multiply>({select_Gather_381, select_Gather_382}, {{"auto_broadcast", "numpy"}});//x_odd_sin
+ auto mul_Multiply_383 =
+ makeOP<opset1::Multiply>({select_Gather_384, select_Gather_385}, {{"auto_broadcast", "numpy"}});//x_even_cos
+ auto sub_Subtract_389 =
+ makeOP<opset1::Subtract>({mul_Multiply_383, mul_Multiply_386}, {{"auto_broadcast", "numpy"}});
+
+ auto mul_Multiply_391 =
+ makeOP<opset1::Multiply>({select_Gather_381, select_Gather_385}, {{"auto_broadcast", "numpy"}});//x_odd_cos
+ auto mul_Multiply_393 =
+ makeOP<opset1::Multiply>({select_Gather_384, select_Gather_382}, {{"auto_broadcast", "numpy"}});//x_even_sin
+ auto add_Add_396 = makeOP<opset1::Add>({mul_Multiply_391, mul_Multiply_393}, {{"auto_broadcast", "numpy"}});
+
+ auto Unsqueeze_62716 = makeOP<opset1::Unsqueeze>({sub_Subtract_389, -1}, {});
+ auto Unsqueeze_62717 = makeOP<opset1::Unsqueeze>({add_Add_396, -1}, {});
+
+ auto stack_401 = makeOP<opset1::Concat>({Unsqueeze_62716, Unsqueeze_62717}, {{"axis", -1}});
+ auto flatten_Reshape_421 = makeOP<opset1::Reshape>({stack_401, {0, 32, 0, 64}}, {{"special_zero", true}});
+ auto slice_Slice_363 =
+ makeOP<opset1::StridedSlice>({permute_Transpose, {0, 0, 0, 64}, {0, 0, 0, INT_MAX}, {1, 1, 1, 1}},
+ {{"begin_mask", {1, 1, 1, 0}},
+ {"end_mask", {1, 1, 1, 0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+ auto cat_Concat_425 = makeOP<opset1::Concat>({flatten_Reshape_421, slice_Slice_363}, {{"axis", -1}});
+ return std::make_shared<ov::Model>(ov::NodeVector{cat_Concat_425},
+ ov::ParameterVector{input, cos_sin_cache, position_ids});
+}
+
+ov::Tensor RoPETestChatGLM2DRoPEStridedSlice::create_i32_tensor(const ov::Shape& shape, int start, int step) {
+ auto tensor = ov::Tensor(ov::element::i32, shape);
+ auto* ptr = static_cast<int32_t*>(tensor.data());
+ for (size_t i = 0; i < tensor.get_size(); i++) {
+ ptr[i] = start;
+ start += step;
+ }
+ return tensor;
+}
+
+void RoPETestChatGLM2DRoPEStridedSlice::generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) {
+ const auto& funcInputs = function->inputs();
+
+ auto& input_shape = targetInputStaticShapes[0];
+ auto batch = input_shape[0];
+ auto seq_length = input_shape[1];
+
+ ov::Tensor t_input = utils::create_and_fill_tensor(funcInputs[0].get_element_type(), input_shape, 2, -1.0f, 32768);
+ ov::Tensor t_cos_sin_cache =
+ utils::create_and_fill_tensor(funcInputs[1].get_element_type(), {32768, 32, 2}, 2, -1.0f, 32768);
+ ov::Tensor t_position_ids = create_i32_tensor(ov::Shape({batch, seq_length}), 15);
+
+ inputs.clear();
+ inputs.insert({funcInputs[0].get_node_shared_ptr(), t_input});
+ inputs.insert({funcInputs[1].get_node_shared_ptr(), t_cos_sin_cache});
+ inputs.insert({funcInputs[2].get_node_shared_ptr(), t_position_ids});
+}
+
+void RoPETestChatGLM2DRoPEStridedSlice::SetUp() {
+ targetDevice = this->GetParam();
+
+ const int batch = 2;
+ const int seq_length = 7;
+ const int num_head = 32;
+ const int rotary_dims = 64;
+
+ InputShape inpShape = {{batch, -1, 4096 + 256 + 256}, {{batch, seq_length, 4096 + 256 + 256}}};
+ init_input_shapes({inpShape});
+ function = buildROPE_ChatGLM(-1, num_head, rotary_dims);
+}
+
+std::string RoPETestChatGLM2DRoPEStridedSlice::getTestCaseName(const testing::TestParamInfo<std::string>& obj) {
+ std::string targetDevice = obj.param;
+ std::ostringstream result;
+ result << "targetDevice=" << targetDevice;
+ return result.str();
+}
+
} // namespace test
} // namespace ov