Skip to content

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

[TRANSFORMATIONS] Support new RoPE pattern of glm-4-9b-chat-hf for RoPEFusion (#33544)

Andrii Staikov55eda000c262
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 4c437d1f667..dbecf11685d 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
@@ -18,6 +18,7 @@
#include "openvino/op/cos.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/gather_elements.hpp"
+#include "openvino/op/gather_nd.hpp"
#include "openvino/op/matmul.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/reshape.hpp"
@@ -776,6 +777,19 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(const bool support_2d_rope) {
this->register_matcher(m, callback);
}
+static std::shared_ptr<ov::Node> build_ChatGLMHF_interleave_pattern(std::shared_ptr<ov::Node> cos_or_sin) {
+ auto transpose = pattern::wrap_type<ov::op::v1::Transpose>({cos_or_sin, pattern::any_input()});
+ auto reshape = pattern::wrap_type<ov::op::v1::Reshape>({transpose, pattern::any_input()});
+ auto multiply = pattern::wrap_type<ov::op::v1::Multiply>({reshape, pattern::any_input()});
+ auto gather_nd = pattern::wrap_type<ov::op::v8::GatherND>({multiply, pattern::any_input()});
+ auto transpose_1 = pattern::wrap_type<ov::op::v1::Transpose>({gather_nd, pattern::any_input()});
+
+ auto const_idx =
+ pattern::wrap_type<ov::opset1::Constant>(pattern::type_matches(ov::element::i32) && const_idx_predicate);
+ auto gather = pattern::wrap_type<v8::Gather>({cos_or_sin, const_idx, -1}, {{"batch_dims", 0}});
+ return transpose_1 | gather;
+}
+
ov::pass::RoPEFusionChatGLMHF::RoPEFusionChatGLMHF() {
using namespace ov::op::util;
MATCHER_SCOPE(RoPEFusionChatGLMHF);
@@ -796,10 +810,8 @@ ov::pass::RoPEFusionChatGLMHF::RoPEFusionChatGLMHF() {
pattern::output_index_matches(1) && pattern::shape_matches("[?, head_cnt, 1, ndims]"));
auto slice_1 = NewGenSlice(reshape, 0, "ndims", 1, 3) | vsplit_out0;
- auto const_idx =
- pattern::wrap_type<ov::opset1::Constant>(pattern::type_matches(ov::element::i32) && const_idx_predicate);
- auto repeat_interleave_cos = pattern::wrap_type<v8::Gather>({cos, const_idx, -1}, {{"batch_dims", 0}});
- auto repeat_interleave_sin = pattern::wrap_type<v8::Gather>({sin, const_idx, -1}, {{"batch_dims", 0}});
+ auto repeat_interleave_cos = build_ChatGLMHF_interleave_pattern(cos);
+ auto repeat_interleave_sin = build_ChatGLMHF_interleave_pattern(sin);
auto multiply = pattern::wrap_type<v1::Multiply>({slice_1, repeat_interleave_cos}, {{"auto_broadcast", "numpy"}});
auto slice_2 = NewGenSlice(slice_1, 1, INT_MAX, 2, 3);
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 7cf04ccd16a..7a0bc333be1 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
@@ -13,6 +13,7 @@
#include "openvino/op/concat.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/gather_elements.hpp"
+#include "openvino/op/gather_nd.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/scatter_update.hpp"
#include "openvino/op/shape_of.hpp"
@@ -33,6 +34,8 @@
using namespace testing;
using namespace ov::gen_pattern;
+const std::vector<int> MOCK_VALUE = {1};
+
static ov::OutputVector makeCosSinCache(size_t max_position_embeddings, size_t rotary_ndims) {
std::vector<float> lut_sin(max_position_embeddings * rotary_ndims, 0.0f);
std::vector<float> lut_cos(max_position_embeddings * rotary_ndims, 0.0f);
@@ -1135,6 +1138,81 @@ TEST_F(TransformationTestsF, ConvertToROPE_chatGLM_nano_2d_rope) {
}
}
+TEST_F(TransformationTestsF, ConvertToROPE_chatGLMHF_2d_rope_GatherND) {
+ disable_rt_info_check();
+ const int seq_len = 7;
+ const int num_heads = 32;
+ const int ndims = 128;
+ const int rotary_ndims = 64;
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{seq_len, 1, 4096});
+ auto Reshape = makeOP<ov::op::v1::Reshape>({input, {-1, 32, 1, 128}}, {{"special_zero", false}});
+ auto VariadicSplit = makeOP<ov::op::v1::VariadicSplit>({Reshape, 3, {64, 64}});
+
+ auto sin = std::make_shared<ov::opset1::Parameter>(ov::element::f32,
+ ov::PartialShape{seq_len, 1, 1, (rotary_ndims / 2)});
+ auto Transpose = makeOP<ov::op::v1::Transpose>({sin, {3, 1, 2, 0}});
+ auto Reshape0 = makeOP<ov::op::v1::Reshape>({Transpose, {32, 1, 1, 1, -1}}, {{"special_zero", false}});
+ auto Param = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 1, 1, seq_len});
+ auto Multiply = makeOP<ov::op::v1::Multiply>({Reshape0, Param}, {{"auto_broadcast", "numpy"}});
+ auto Constant = makeConst(ov::element::i32, ov::Shape({64, 2}), MOCK_VALUE);
+ auto GatherND = makeOP<ov::op::v8::GatherND>({Multiply, Constant}, {{"batch_dims", 0}});
+ auto Transpose0 = makeOP<ov::op::v1::Transpose>({GatherND, {3, 1, 2, 0}});
+
+ auto cos = std::make_shared<ov::opset1::Parameter>(ov::element::f32,
+ ov::PartialShape{seq_len, 1, 1, (rotary_ndims / 2)});
+ auto Transpose1 = makeOP<ov::op::v1::Transpose>({cos, {3, 1, 2, 0}});
+ auto Reshape1 = makeOP<ov::op::v1::Reshape>({Transpose1, {32, 1, 1, 1, -1}}, {{"special_zero", false}});
+ auto Param1 = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 1, 1, seq_len});
+ auto Multiply0 = makeOP<ov::op::v1::Multiply>({Reshape1, Param1}, {{"auto_broadcast", "numpy"}});
+ auto GatherND0 = makeOP<ov::op::v8::GatherND>({Multiply0, Constant}, {{"batch_dims", 0}});
+ auto Transpose2 = makeOP<ov::op::v1::Transpose>({GatherND0, {3, 1, 2, 0}});
+
+ auto Slice = makeOP<ov::op::v8::Slice>({VariadicSplit->output(0), {1}, {INT_MAX}, {2}, {3}});
+ auto Constant0 = makeConst(ov::element::f32, ov::Shape({1, 1, 1, 1}), {-1.000000f});
+ auto Multiply1 = makeOP<ov::op::v1::Multiply>({Slice, Constant0}, {{"auto_broadcast", "numpy"}});
+ auto Unsqueeze = makeOP<ov::op::v1::Reshape>({Multiply1, {-1, 32, 1, 32, 1}}, {{"special_zero", false}});
+ auto Slice0 = makeOP<ov::op::v8::Slice>({VariadicSplit->output(0), {0}, {INT_MAX}, {2}, {3}});
+ auto Unsqueeze0 = makeOP<ov::op::v1::Reshape>({Slice0, {-1, 32, 1, 32, 1}}, {{"special_zero", false}});
+ auto Concat = makeOP<ov::op::v0::Concat>({Unsqueeze, Unsqueeze0}, {{"axis", -1}});
+ auto Reshape2 = makeOP<ov::op::v1::Reshape>({Concat, {0, 32, 0, 64}}, {{"special_zero", true}});
+
+ auto Multiply2 = makeOP<ov::op::v1::Multiply>({Reshape2, Transpose2}, {{"auto_broadcast", "numpy"}});
+
+ auto Multiply3 =
+ makeOP<ov::op::v1::Multiply>({VariadicSplit->output(0), Transpose0}, {{"auto_broadcast", "numpy"}});
+ auto Add = makeOP<ov::op::v1::Add>({Multiply3, Multiply2}, {{"auto_broadcast", "numpy"}});
+ auto Concat0 = makeOP<ov::op::v0::Concat>({Add, VariadicSplit->output(1)}, {{"axis", -1}});
+
+ model =
+ std::make_shared<ov::Model>(ov::OutputVector{Concat0}, ov::ParameterVector{input, cos, sin, Param, Param1});
+ }
+ manager.register_pass<ov::pass::RoPEFusion>(true);
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{seq_len, 1, 4096});
+ auto cos = std::make_shared<ov::opset1::Parameter>(ov::element::f32,
+ ov::PartialShape{seq_len, 1, 1, (rotary_ndims / 2)});
+ auto sin = std::make_shared<ov::opset1::Parameter>(ov::element::f32,
+ ov::PartialShape{seq_len, 1, 1, (rotary_ndims / 2)});
+ auto rope = makeOP<ov::op::internal::RoPE>({input, cos, sin},
+ {{"config.slice_start", 0},
+ {"config.slice_stop", 0},
+ {"config.input_trans0213", false},
+ {"config.output_trans0213", false},
+ {"config.is_interleaved", false},
+ {"config.rotary_ndims", rotary_ndims},
+ {"config.is_chatglm", true},
+ {"config.support_2d_rope", true},
+ {"config.support_3d_rope", false},
+ {"config.is_qwen", false},
+ {"config.use_rope_cache", false},
+ {"config.head_cnt", num_heads},
+ {"config.head_size", ndims},
+ {"config.gather_position_arg_id", 0}});
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{input, cos, sin});
+ }
+}
+
TEST_F(TransformationTestsF, ConvertToROPE_chatGLMHF_2d_rope) {
disable_rt_info_check();
const int seq_len = 7;
@@ -1693,8 +1771,6 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTJ_PagedAttention) {
}
}
-std::vector<int> MOCK_VALUE = {1};
-
TEST_F(TransformationTestsF, ConvertToROPE_chatGLM4_PagedAttention) {
{
disable_rt_info_check();