Skip to content

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

Update GLM RopeFusion pattern to support GLM4 with PagedAttention (#29191)

Andrii Staikovffd545f48202
diff --git a/src/common/transformations/include/transformations/utils/gen_pattern.hpp b/src/common/transformations/include/transformations/utils/gen_pattern.hpp
index 132046e15e5..f68255939be 100644
--- a/src/common/transformations/include/transformations/utils/gen_pattern.hpp
+++ b/src/common/transformations/include/transformations/utils/gen_pattern.hpp
@@ -274,6 +274,7 @@ public:
bool operator<(const Symbol& rhs) const {
return get_id() < rhs.get_id();
}
+ bool validate = true;
};
inline Symbol operator-(const Symbol& lhs) {
@@ -1398,7 +1399,7 @@ public:
if (sym.is_independent_var()) {
auto id = sym.get_id();
if (symbol_value_map.count(id)) {
- if (symbol_value_map[id] != value) {
+ if (sym.validate && symbol_value_map[id] != value) {
_VERBOSE_LOG(" in-consistency between multiple references of same symbol(",
sym.get_name(),
"): ",
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 77b51d34acd..28a3eeed0a9 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
@@ -23,6 +23,28 @@
using namespace ov::gen_pattern;
+// This is a utility function used in the work around in ChatGLM pattern.
+// Since the existing implementation of Symbols don't allow for checking
+// permutations of the same Symbols in a shape, we need to check the
+// possible combinations manually. This will be resolved in the
+// implementation of new Symbols.
+static bool chatglm_validate_reshape_symbols(PatternValidator& validator) {
+ // checking ABC
+ auto A = static_cast<int>(validator["A"]);
+ auto B = static_cast<int>(validator["B"]);
+ auto C = static_cast<int>(validator["C"]);
+
+ auto head_cnt = static_cast<int>(validator["head_cnt"]);
+
+ if ((A == -1 && B == head_cnt && C == 1) || // ChatGLM4
+ (A == 1 && B == -1 && C == head_cnt) || // ChatGLM3
+ (A == 0 && B == 0 && C == 0)) { // ChatGLM nano
+ return true;
+ }
+
+ return false;
+}
+
ov::pass::RoPEFusionFlux::RoPEFusionFlux() {
MATCHER_SCOPE(RoPEFusionFlux);
// x[?,24,?,128]
@@ -542,20 +564,35 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
auto total_size_k = ov::gen_pattern::Symbol("total_size_k");
auto total_size_v = ov::gen_pattern::Symbol("total_size_v");
auto batch = ov::gen_pattern::Symbol("batch");
+ // temporarily disable validation of the batch and seq_len symbols, it's a bad idea to determine these values from
+ // reshape constants, because Reshape op constants might contain special values (-1, 0), not the real batch, seq_len
+ // value
+ batch.validate = false;
+
auto seq_len = ov::gen_pattern::Symbol("seq_len");
+ seq_len.validate = false;
+
+ auto A = ov::gen_pattern::Symbol("A");
+ auto B = ov::gen_pattern::Symbol("B");
+ auto C = ov::gen_pattern::Symbol("C");
auto qkv_proj = makePattern<opset1::VariadicSplit>({qkv_linear, -1, {total_size_q, total_size_k, total_size_v}});
qkv_proj->set_output_size(3);
-
auto cur_key = makePattern<opset1::Reshape>({qkv_proj->output(split_output_id), {0, 0, head_cnt, head_size}},
{{"special_zero", true}});
-
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}});
+ // For Models, where SDPA to PagedAttention transformation was applied,
+ // all sequences have the size == 1, we move sequences to the batch, this is the PagedAttention specific,
+ // so seq_length dim will be always 1, this means that Transpose is unnecessary and Reshape op can be used.
+ auto transposed_cur_key =
+ makePattern<opset1::Reshape>({qkv_proj->output(split_output_id), {-1, head_cnt, 1, head_size}},
+ {{"special_zero", false}});
+ // Transpose for SDPA version:
+ input_key = makePattern<opset1::Transpose>({cur_key, {0, 2, 1, 3}}) | transposed_cur_key;
} else {
// Get key [seq_length, batch, head_cnt, head_size]
input_key = std::move(cur_key);
@@ -603,8 +640,7 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
// [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}});
+ ListConstruct_379_Concat | const_target_shape_2});
} else {
auto ListConstruct_379_Concat =
makePattern<opset1::Concat>({seq_length, {-1}, {1}, {ndims / 2}, {2}}, {{"axis", 0}});
@@ -617,8 +653,7 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
// [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_0 | const_target_shape_2},
- {{"special_zero", false}});
+ ListConstruct_379_Concat | const_target_shape_0 | const_target_shape_2});
}
auto cos_tab = makePattern<opset8::Gather>({view_Reshape_460, 0, -1}, {{"batch_dims", 0}});
@@ -628,19 +663,15 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
auto x_odd_sin = makePattern<opset1::Multiply>({x_odd, sin_tab}, {{"auto_broadcast", "numpy"}});
auto neg_x_odd_sin = makePattern<opset1::Multiply>({x_odd_sin, -1.000000f}, {{"auto_broadcast", "numpy"}});
auto sub_Subtract_469 = makePattern<opset1::Add>({x_even_cos, neg_x_odd_sin}, {{"auto_broadcast", "numpy"}});
-
- auto y_even = makePattern<opset1::Unsqueeze>({sub_Subtract_469, -1});
- auto const_y_even_reshape = makeConst({1, -1, head_cnt, ndims / 2, 1});
- auto y_even_reshape =
- makePattern<opset1::Reshape>({sub_Subtract_469, const_y_even_reshape}, {{"special_zero", false}});
+ auto y_even = makePattern<opset1::Unsqueeze>({sub_Subtract_469, -1}) |
+ makePattern<opset1::Reshape>({sub_Subtract_469, {A, B, C, ndims / 2, 1}}, {{"special_zero", false}});
auto x_odd_cos = makePattern<opset1::Multiply>({x_odd, cos_tab}, {{"auto_broadcast", "numpy"}});
auto x_even_sin = makePattern<opset1::Multiply>({x_even, sin_tab}, {{"auto_broadcast", "numpy"}});
auto add_Add_476 = makePattern<opset1::Add>({x_odd_cos, x_even_sin}, {{"auto_broadcast", "numpy"}});
- auto y_odd = makePattern<opset1::Unsqueeze>({add_Add_476, -1});
- auto const_y_odd_reshape = makeConst({1, -1, head_cnt, ndims / 2, 1});
- auto y_odd_reshape = makePattern<opset1::Reshape>({add_Add_476, const_y_odd_reshape}, {{"special_zero", false}});
+ auto y_odd = makePattern<opset1::Unsqueeze>({add_Add_476, -1}) |
+ makePattern<opset1::Reshape>({add_Add_476, {A, B, C, ndims / 2, 1}}, {{"special_zero", false}});
- auto stack_481 = makePattern<opset1::Concat>({y_even | y_even_reshape, y_odd | y_odd_reshape}, {{"axis", -1}});
+ auto stack_481 = makePattern<opset1::Concat>({y_even, y_odd}, {{"axis", -1}});
auto ShapeOf_135133 = makePattern<opset1::ShapeOf>({stack_481});
auto flatten_Slice_497 = GenSlice(ShapeOf_135133, 0, 3, 1, 0);
@@ -665,7 +696,6 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
auto cat_Concat_505 =
makePattern<opset1::Concat>({flatten_Reshape_501, slice_Slice_443 | var_split_1->output(1)}, {{"axis", -1}});
-
auto result = cat_Concat_505 | flatten_Reshape_501;
matcher_pass_callback callback = [=](ov::pass::pattern::Matcher& m) {
@@ -676,6 +706,10 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
return false;
}
+ // A temporarily work around until we move to the new Symbols
+ if (!chatglm_validate_reshape_symbols(validator))
+ return false;
+
op::internal::RoPE::Config config;
OutputVector new_args;
config.rotary_ndims = static_cast<size_t>(validator["ndims"]);
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 6c6739e9ef3..d43bdf2110f 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
@@ -1379,3 +1379,76 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTJ_PagedAttention) {
std::make_shared<ov::Model>(ov::NodeVector{rope}, ov::ParameterVector{input, aten_gather_GatherElements});
}
}
+
+std::vector<int> MOCK_VALUE = {1};
+
+TEST_F(TransformationTestsF, ConvertToROPE_chatGLM4_PagedAttention) {
+ {
+ disable_rt_info_check();
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{DYN, 1, 4608});
+ auto input1 = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{DYN, 1, 32, 2});
+
+ auto aten_slice_Slice_2 = makeOP<ov::opset8::Slice>({input1, {0}, {1}, {1}, {1}});
+ auto aten_view_Reshape =
+ makeOP<ov::opset1::Reshape>({aten_slice_Slice_2, {-1, 1, 1, 32, 2}}, {{"special_zero", false}});
+ auto aten_select_Gather_1 = makeOP<ov::opset8::Gather>({aten_view_Reshape, 0, -1}, {{"batch_dims", 0}});
+ auto aten_select_Gather_3 = makeOP<ov::opset8::Gather>({aten_view_Reshape, 1, -1}, {{"batch_dims", 0}});
+
+ auto module_transformer_encoder_layers_0_self_attention_prim_ListUnpack =
+ makeOP<ov::opset1::VariadicSplit>({input, -1, {4096, 256, 256}});
+ auto module_transformer_encoder_layers_0_self_attention_aten_transpose_Transpose = makeOP<ov::opset1::Reshape>(
+ {module_transformer_encoder_layers_0_self_attention_prim_ListUnpack->output(0), {-1, 32, 1, 128}},
+ {{"special_zero", false}});
+ auto VariadicSplit_41226 = makeOP<ov::opset1::VariadicSplit>(
+ {module_transformer_encoder_layers_0_self_attention_aten_transpose_Transpose, 3, {64, 64}});
+ auto aten_reshape_Reshape =
+ makeOP<ov::opset1::Reshape>({VariadicSplit_41226->output(0), {0, 32, 0, 32, 2}}, {{"special_zero", true}});
+ auto aten_select_Gather = makeOP<ov::opset8::Gather>({aten_reshape_Reshape, 0, -1}, {{"batch_dims", 0}});
+ auto aten_mul_Multiply_1 =
+ makeOP<ov::opset1::Multiply>({aten_select_Gather, aten_select_Gather_1}, {{"auto_broadcast", "numpy"}});
+ auto aten_select_Gather_2 = makeOP<ov::opset8::Gather>({aten_reshape_Reshape, 1, -1}, {{"batch_dims", 0}});
+ auto aten_mul_Multiply_2 =
+ makeOP<ov::opset1::Multiply>({aten_select_Gather_2, aten_select_Gather_3}, {{"auto_broadcast", "numpy"}});
+ auto Multiply_45963 =
+ makeOP<ov::opset1::Multiply>({aten_mul_Multiply_2, -1.000000f}, {{"auto_broadcast", "numpy"}});
+ auto aten_sub_Subtract =
+ makeOP<ov::opset1::Add>({aten_mul_Multiply_1, Multiply_45963}, {{"auto_broadcast", "numpy"}});
+ auto Unsqueeze_81695 =
+ makeOP<ov::opset1::Reshape>({aten_sub_Subtract, {-1, 32, 1, 32, 1}}, {{"special_zero", false}});
+ auto aten_mul_Multiply_3 =
+ makeOP<ov::opset1::Multiply>({aten_select_Gather_2, aten_select_Gather_1}, {{"auto_broadcast", "numpy"}});
+ auto aten_mul_Multiply_4 =
+ makeOP<ov::opset1::Multiply>({aten_select_Gather, aten_select_Gather_3}, {{"auto_broadcast", "numpy"}});
+ auto aten_add_Add =
+ makeOP<ov::opset1::Add>({aten_mul_Multiply_3, aten_mul_Multiply_4}, {{"auto_broadcast", "numpy"}});
+ auto Unsqueeze_81696 =
+ makeOP<ov::opset1::Reshape>({aten_add_Add, {-1, 32, 1, 32, 1}}, {{"special_zero", false}});
+ auto aten_stack = makeOP<ov::opset1::Concat>({Unsqueeze_81695, Unsqueeze_81696}, {{"axis", -1}});
+ auto aten_flatten_Reshape = makeOP<ov::opset1::Reshape>({aten_stack, {0, 32, 0, 64}}, {{"special_zero", true}});
+ auto aten_cat_Concat =
+ makeOP<ov::opset1::Concat>({aten_flatten_Reshape, VariadicSplit_41226->output(1)}, {{"axis", -1}});
+
+ model = std::make_shared<ov::Model>(ov::NodeVector{aten_cat_Concat}, ov::ParameterVector{input, input1});
+ }
+ manager.register_pass<ov::pass::RoPEFusion>(true);
+ {
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{DYN, 1, 4608});
+ auto input1 = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::PartialShape{DYN, 1, 32, 2});
+
+ auto rope = makeOP<ov::op::internal::RoPE>({input, input1, input1},
+ {{"config.slice_start", 0},
+ {"config.slice_stop", 4096},
+ {"config.input_trans0213", false},
+ {"config.output_trans0213", false},
+ {"config.is_interleaved", false},
+ {"config.rotary_ndims", 64},
+ {"config.is_chatglm", true},
+ {"config.support_2d_rope", true},
+ {"config.is_qwen", false},
+ {"config.head_cnt", 32},
+ {"config.head_size", 128},
+ {"config.gather_position_arg_id", 0}});
+
+ model_ref = std::make_shared<ov::Model>(ov::NodeVector{rope}, ov::ParameterVector{input, input1});
+ }
+}