Skip to content

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

[TRANSFORMATIONS] Support different split outputs in RoPEFusionChatGLM (#31584)

Mikhail Ryzhov8af84574cb13
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 35374510dfd..5c6333917dc 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
@@ -46,7 +46,7 @@ public:
class ov::pass::RoPEFusionChatGLM : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("RoPEFusionChatGLM");
- RoPEFusionChatGLM(int split_output_id, const bool support_2d_rope = false);
+ RoPEFusionChatGLM(const bool support_2d_rope = false);
};
class ov::pass::RoPEFusionChatGLMHF : public ov::pass::MatcherPass {
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 14e28623b15..60f3d277582 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
@@ -4,6 +4,7 @@
#include "transformations/common_optimizations/fuse_rotary_positional_embeddings.hpp"
+#include <climits>
#include <cstdint>
#include <limits>
#include <variant>
@@ -18,12 +19,15 @@
#include "openvino/op/gather.hpp"
#include "openvino/op/gather_elements.hpp"
#include "openvino/op/matmul.hpp"
+#include "openvino/op/multiply.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/scatter_update.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/sin.hpp"
+#include "openvino/op/slice.hpp"
#include "openvino/op/split.hpp"
#include "openvino/op/squeeze.hpp"
+#include "openvino/op/strided_slice.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "openvino/op/util/shape_of_base.hpp"
@@ -39,11 +43,10 @@
#include "ov_ops/rotary_positional_embeddings.hpp"
#include "ov_ops/type_relaxed.hpp"
#include "transformations/symbolic_transformations/symbolic_optimizations.hpp"
-#include "transformations/utils/gen_pattern.hpp"
#include "transformations/utils/utils.hpp"
-using namespace ov::gen_pattern;
using namespace ov::pass;
+using namespace ov::pass::pattern;
using namespace ov::op;
ov::pass::RoPEFusion::RoPEFusion(bool support_2d_rope) : m_support_2d_rope(support_2d_rope) {}
@@ -63,18 +66,15 @@ bool ov::pass::RoPEFusion::run_on_model(const std::shared_ptr<ov::Model>& model)
symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionIOSlicing>();
symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionPreprocess>();
- symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionChatGLM>(0);
- symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionChatGLM>(1);
+ symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionChatGLM>(false);
if (m_support_2d_rope) {
- symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionChatGLM>(0, true);
- symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionChatGLM>(1, true);
+ symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionChatGLM>(true);
symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionChatGLMHF>();
}
symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionQwen>(0);
symbolic_ctx_manager->register_pass<ov::pass::RoPEFusionQwen>(1);
symbolic_ctx_manager->register_pass<ov::pass::RoPEShareCosSin>();
-
return symbolic_optimizations.run_on_model(model);
}
@@ -571,7 +571,7 @@ ov::pass::RoPEFusionGPTJ::RoPEFusionGPTJ() {
this->register_matcher(m, callback);
}
-ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool support_2d_rope) {
+ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(const bool support_2d_rope) {
using namespace ov::op::util;
MATCHER_SCOPE(RoPEFusionChatGLM);
@@ -586,9 +586,8 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
auto qkv_proj =
pattern::wrap_type<v1::VariadicSplit>({qkv_linear, -1, {"total_size_q", "total_size_k", "total_size_v"}});
qkv_proj->set_output_size(3);
- auto cur_key =
- pattern::wrap_type<v1::Reshape>({qkv_proj->output(split_output_id), {"0", "0", "head_cnt", "head_size"}},
- {{"special_zero", true}});
+ auto reshape_pattern_const = pattern::wrap_type<v0::Constant>(pattern::value_matches("0, 0, head_cnt, head_size"));
+ auto cur_key = pattern::wrap_type<v1::Reshape>({qkv_proj, reshape_pattern_const}, {{"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
@@ -597,9 +596,8 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
// 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 =
- pattern::wrap_type<v1::Reshape>({qkv_proj->output(split_output_id), {"-1", "head_cnt", "1", "head_size"}},
- {{"special_zero", false}});
+ auto transposed_cur_key = pattern::wrap_type<v1::Reshape>({qkv_proj, {"-1", "head_cnt", "1", "head_size"}},
+ {{"special_zero", false}});
// Transpose for SDPA version:
input_key = pattern::wrap_type<v1::Transpose>({cur_key, {0, 2, 1, 3}}) | transposed_cur_key;
} else {
@@ -733,14 +731,18 @@ ov::pass::RoPEFusionChatGLM::RoPEFusionChatGLM(int split_output_id, const bool s
config.head_cnt = static_cast<size_t>(head_cnt.i());
config.head_size = static_cast<size_t>(head_size.i());
- if (split_output_id == 0) {
- // query : split_output_id == 0
+ const auto& qkv_proj_node = pattern_map.at(qkv_proj);
+ const size_t qkv_proj_output_id = qkv_proj_node.get_index();
+ if (qkv_proj_output_id == 0) {
+ // query : split output id == 0
config.slice_start = 0;
config.slice_stop = static_cast<size_t>(total_size_q.i());
- } else {
- // key : split_output_id == 1
+ } else if (qkv_proj_output_id == 1) {
+ // key : split output id == 1
config.slice_start = static_cast<size_t>(total_size_q.i());
config.slice_stop = static_cast<size_t>(config.slice_start + static_cast<size_t>(total_size_k.i()));
+ } else {
+ return false;
}
if (ov::is_type<opset1::Reshape>(root)) {
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 f36c9176c4e..d5b3f412232 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
@@ -497,102 +497,112 @@ TEST_F(TransformationTestsF, ConvertToROPE_GPTJ) {
}
}
-TEST_F(TransformationTestsF, ConvertToROPE_chatGLM) {
+// Parametrized ConvertToROPE_chatGLM tests to check both unpack->output(0) and unpack->output(1)
+class ConvertToROPETest : public TransformationTestsF, public ::testing::WithParamInterface<int> {};
+
+TEST_P(ConvertToROPETest, ConvertToROPE_chatGLM) {
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;
+
+ const int batch = 2, seq_len = 7, num_heads = 32, ndims = 128, rotary_ndims = 64, max_pos_length = 2048;
+ const int total_size_q = 4096, total_size_k = 256, total_size_v = 256;
+ const int total_size = total_size_q + total_size_k + total_size_v;
+
+ int unpack_output_idx = GetParam();
+
+ // Build the original model
{
- auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, 4608});
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, total_size});
auto seq_length = std::make_shared<ov::opset1::Parameter>(ov::element::i32, ov::Shape{1});
auto cos_sin_cache =
std::make_shared<ov::opset1::Parameter>(ov::element::f32,
ov::Shape{max_pos_length, batch, rotary_ndims / 2, 2});
- 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 aten_slice_Slice_357 =
- makeOP<ov::opset1::StridedSlice>({view_Reshape, {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 ListConstruct_372_Concat =
+ auto unpack = makeOP<ov::opset1::VariadicSplit>({input, -1, {total_size_q, total_size_k, total_size_v}});
+ auto reshaped = makeOP<ov::opset1::Reshape>({unpack->output(unpack_output_idx), {0, 0, num_heads, ndims}},
+ {{"special_zero", true}});
+ auto slice = makeOP<ov::opset1::StridedSlice>({reshaped, {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 shape_concat =
makeOP<ov::opset1::Concat>({seq_length, {-1}, {num_heads}, {rotary_ndims / 2}, {2}}, {{"axis", 0}});
- auto aten_reshape_Reshape_373 =
- makeOP<ov::opset1::Reshape>({aten_slice_Slice_357, ListConstruct_372_Concat}, {{"special_zero", false}});
- auto aten_select_Gather_381 =
- makeOP<ov::opset8::Gather>({aten_reshape_Reshape_373, 0, -1}, {{"batch_dims", 0}});
- auto aten_slice_Slice_369 = makeOP<ov::opset1::StridedSlice>({cos_sin_cache, {0}, seq_length, {1}},
- {{"begin_mask", {0}},
- {"end_mask", {0}},
- {"new_axis_mask", {}},
- {"shrink_axis_mask", {}},
- {"ellipsis_mask", {}}});
- auto ListConstruct_379_Concat =
- makeOP<ov::opset1::Concat>({seq_length, {-1}, {1}, {rotary_ndims / 2}, {2}}, {{"axis", 0}});
- auto aten_view_Reshape_380 =
- makeOP<ov::opset1::Reshape>({aten_slice_Slice_369, ListConstruct_379_Concat}, {{"special_zero", false}});
- auto aten_select_Gather_382 = makeOP<ov::opset8::Gather>({aten_view_Reshape_380, 0, -1}, {{"batch_dims", 0}});
- auto aten_mul_Multiply_383 = makeOP<ov::opset1::Multiply>({aten_select_Gather_381, aten_select_Gather_382},
- {{"auto_broadcast", "numpy"}});
- auto aten_select_Gather_384 =
- makeOP<ov::opset8::Gather>({aten_reshape_Reshape_373, 1, -1}, {{"batch_dims", 0}});
- auto aten_select_Gather_385 = makeOP<ov::opset8::Gather>({aten_view_Reshape_380, 1, -1}, {{"batch_dims", 0}});
- auto aten_mul_Multiply_386 = makeOP<ov::opset1::Multiply>({aten_select_Gather_384, aten_select_Gather_385},
- {{"auto_broadcast", "numpy"}});
- auto Multiply_101315 =
- makeOP<ov::opset1::Multiply>({aten_mul_Multiply_386, -1.000000f}, {{"auto_broadcast", "numpy"}});
- auto aten_sub_Subtract_389 =
- makeOP<ov::opset1::Add>({aten_mul_Multiply_383, Multiply_101315}, {{"auto_broadcast", "numpy"}});
- auto Unsqueeze_62716 = makeOP<ov::opset1::Unsqueeze>({aten_sub_Subtract_389, -1});
- auto aten_mul_Multiply_391 = makeOP<ov::opset1::Multiply>({aten_select_Gather_384, aten_select_Gather_382},
- {{"auto_broadcast", "numpy"}});
- auto aten_mul_Multiply_393 = makeOP<ov::opset1::Multiply>({aten_select_Gather_381, aten_select_Gather_385},
- {{"auto_broadcast", "numpy"}});
- auto aten_add_Add_396 =
- makeOP<ov::opset1::Add>({aten_mul_Multiply_391, aten_mul_Multiply_393}, {{"auto_broadcast", "numpy"}});
- auto Unsqueeze_62717 = makeOP<ov::opset1::Unsqueeze>({aten_add_Add_396, -1});
- auto aten_stack_401 = makeOP<ov::opset1::Concat>({Unsqueeze_62716, Unsqueeze_62717}, {{"axis", -1}});
- auto ShapeOf_134820 = makeOP<ov::op::TypeRelaxed<ov::opset1::ShapeOf>>(
- {aten_stack_401},
+ auto reshaped_slice = makeOP<ov::opset1::Reshape>({slice, shape_concat}, {{"special_zero", false}});
+
+ auto gather_even = makeOP<ov::opset8::Gather>({reshaped_slice, 0, -1}, {{"batch_dims", 0}});
+ auto gather_odd = makeOP<ov::opset8::Gather>({reshaped_slice, 1, -1}, {{"batch_dims", 0}});
+
+ auto cache_slice = makeOP<ov::opset1::StridedSlice>({cos_sin_cache, {0}, seq_length, {1}},
+ {{"begin_mask", {0}},
+ {"end_mask", {0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+ auto cache_shape = makeOP<ov::opset1::Concat>({seq_length, {-1}, {1}, {rotary_ndims / 2}, {2}}, {{"axis", 0}});
+ auto cache_reshaped = makeOP<ov::opset1::Reshape>({cache_slice, cache_shape}, {{"special_zero", false}});
+
+ auto gather_cos = makeOP<ov::opset8::Gather>({cache_reshaped, 0, -1}, {{"batch_dims", 0}});
+ auto gather_sin = makeOP<ov::opset8::Gather>({cache_reshaped, 1, -1}, {{"batch_dims", 0}});
+
+ auto mul_even_cos = makeOP<ov::opset1::Multiply>({gather_even, gather_cos}, {{"auto_broadcast", "numpy"}});
+ auto mul_odd_sin = makeOP<ov::opset1::Multiply>({gather_odd, gather_sin}, {{"auto_broadcast", "numpy"}});
+ auto neg_odd_sin = makeOP<ov::opset1::Multiply>({mul_odd_sin, -1.0f}, {{"auto_broadcast", "numpy"}});
+ auto add_even = makeOP<ov::opset1::Add>({mul_even_cos, neg_odd_sin}, {{"auto_broadcast", "numpy"}});
+
+ auto unsq_even = makeOP<ov::opset1::Unsqueeze>({add_even, -1});
+ auto mul_odd_cos = makeOP<ov::opset1::Multiply>({gather_odd, gather_cos}, {{"auto_broadcast", "numpy"}});
+ auto mul_even_sin = makeOP<ov::opset1::Multiply>({gather_even, gather_sin}, {{"auto_broadcast", "numpy"}});
+ auto add_odd = makeOP<ov::opset1::Add>({mul_odd_cos, mul_even_sin}, {{"auto_broadcast", "numpy"}});
+ auto unsq_odd = makeOP<ov::opset1::Unsqueeze>({add_odd, -1});
+
+ auto stack = makeOP<ov::opset1::Concat>({unsq_even, unsq_odd}, {{"axis", -1}});
+ auto shapeof = makeOP<ov::op::TypeRelaxed<ov::opset1::ShapeOf>>(
+ {stack},
{{"type_relax", true}, {"input_data_types", {}}, {"output_data_types", {ov::element::i32}}});
- auto aten_flatten_Slice_417 = makeOP<ov::opset1::StridedSlice>({ShapeOf_134820, {0}, {3}, {1}},
- {{"begin_mask", {0}},
- {"end_mask", {0}},
- {"new_axis_mask", {}},
- {"shrink_axis_mask", {}},
- {"ellipsis_mask", {}}});
- auto aten_flatten_Concat_420 = makeOP<ov::opset1::Concat>({aten_flatten_Slice_417, {-1}}, {{"axis", 0}});
- auto aten_flatten_Reshape_421 =
- makeOP<ov::opset1::Reshape>({aten_stack_401, aten_flatten_Concat_420}, {{"special_zero", true}});
- auto aten_slice_Slice_363 =
- makeOP<ov::opset1::StridedSlice>({view_Reshape, {0, 0, 0, rotary_ndims}, {0, 0, 0, INT_MAX}, {1, 1, 1, 1}},
+ auto flatten_shape = makeOP<ov::opset1::StridedSlice>({shapeof, {0}, {3}, {1}},
+ {{"begin_mask", {0}},
+ {"end_mask", {0}},
+ {"new_axis_mask", {}},
+ {"shrink_axis_mask", {}},
+ {"ellipsis_mask", {}}});
+ auto flatten_concat = makeOP<ov::opset1::Concat>({flatten_shape, {-1}}, {{"axis", 0}});
+ auto flatten = makeOP<ov::opset1::Reshape>({stack, flatten_concat}, {{"special_zero", true}});
+
+ auto slice_rest =
+ makeOP<ov::opset1::StridedSlice>({reshaped, {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 aten_cat_Concat_425 =
- makeOP<ov::opset1::Concat>({aten_flatten_Reshape_421, aten_slice_Slice_363}, {{"axis", -1}});
- model = std::make_shared<ov::Model>(ov::OutputVector{aten_cat_Concat_425},
+ auto concat = makeOP<ov::opset1::Concat>({flatten, slice_rest}, {{"axis", -1}});
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{concat},
ov::ParameterVector{input, seq_length, cos_sin_cache});
}
+
manager.register_pass<ov::pass::RoPEFusion>();
+
+ // Build the reference model
{
- auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, 4608});
+ int slice_start = 0, slice_stop = 0;
+ if (unpack_output_idx == 0) {
+ slice_stop = total_size_q;
+ } else {
+ slice_start = total_size_q;
+ slice_stop = slice_start + total_size_k;
+ }
+
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, total_size});
auto seq_length = std::make_shared<ov::opset1::Parameter>(ov::element::i32, ov::Shape{1});
auto cos_sin_cache =
std::make_shared<ov::opset1::Parameter>(ov::element::f32,
ov::Shape{max_pos_length, batch, rotary_ndims / 2, 2});
auto rope = makeOP<ov::op::internal::RoPE>({input, cos_sin_cache, cos_sin_cache},
- {{"config.slice_start", 0},
- {"config.slice_stop", 4096},
+ {{"config.slice_start", slice_start},
+ {"config.slice_stop", slice_stop},
{"config.input_trans0213", false},
{"config.output_trans0213", false},
{"config.is_interleaved", false},
@@ -607,72 +617,79 @@ TEST_F(TransformationTestsF, ConvertToROPE_chatGLM) {
model_ref =
std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{input, seq_length, cos_sin_cache});
}
+ comparator.enable(FunctionsComparator::ATTRIBUTES);
}
-TEST_F(TransformationTestsF, ConvertToROPE_chatGLM_Slice) {
- using namespace ov;
+TEST_P(ConvertToROPETest, ConvertToROPE_chatGLM_Slice) {
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;
+
+ const int batch = 2, seq_len = 7, num_heads = 32, ndims = 128, rotary_ndims = 64, max_pos_length = 2048;
+ const int total_size_q = 4096, total_size_k = 256, total_size_v = 256;
+ const int total_size = total_size_q + total_size_k + total_size_v;
+ const int unpack_output_idx = GetParam();
+
+ // Build original model
{
- auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, 4608});
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, total_size});
auto seq_length = std::make_shared<ov::opset1::Parameter>(ov::element::i32, ov::Shape{1});
auto cos_sin_cache =
std::make_shared<ov::opset1::Parameter>(ov::element::f32,
ov::Shape{max_pos_length, batch, rotary_ndims / 2, 2});
- auto ListUnpack = makeOP<opset1::VariadicSplit>({input, -1, {4096, 256, 256}});
- auto view_Reshape =
- makeOP<opset1::Reshape>({ListUnpack->output(0), {0, 0, num_heads, ndims}}, {{"special_zero", true}});
-
- auto VariadicSplit_20795 = makeOP<opset1::VariadicSplit>({view_Reshape, 3, {rotary_ndims, -1}});
- auto reshape_Reshape =
- makeOP<opset1::Reshape>({VariadicSplit_20795->output(0), {0, 0, num_heads, rotary_ndims / 2, 2}},
- {{"special_zero", true}});
- auto select_Gather = makeOP<opset8::Gather>({reshape_Reshape, 0, -1}, {{"batch_dims", 0}});
- auto slice_Slice_1 = makeOP<opset8::Slice>({cos_sin_cache, {0}, seq_length, {1}, {0}});
- auto ListConstruct_Concat_1 =
- makeOP<opset1::Concat>({seq_length, {-1}, {1}, {rotary_ndims / 2}, {2}}, {{"axis", 0}});
- auto view_Reshape_1 =
- makeOP<opset1::Reshape>({slice_Slice_1, ListConstruct_Concat_1}, {{"special_zero", false}});
-
- auto select_Gather_1 = makeOP<opset8::Gather>({view_Reshape_1, 0, -1}, {{"batch_dims", 0}});
- auto mul_Multiply_1 = makeOP<opset1::Multiply>({select_Gather, select_Gather_1}, {{"auto_broadcast", "numpy"}});
- auto select_Gather_2 = makeOP<opset8::Gather>({reshape_Reshape, 1, -1}, {{"batch_dims", 0}});
- auto select_Gather_3 = makeOP<opset8::Gather>({view_Reshape_1, 1, -1}, {{"batch_dims", 0}});
- auto mul_Multiply_2 =
- makeOP<opset1::Multiply>({select_Gather_2, select_Gather_3}, {{"auto_broadcast", "numpy"}});
- auto Multiply_23724 = makeOP<opset1::Multiply>({mul_Multiply_2, -1.000000f}, {{"auto_broadcast", "numpy"}});
- auto sub_Subtract = makeOP<opset1::Add>({mul_Multiply_1, Multiply_23724}, {{"auto_broadcast", "numpy"}});
-
- auto Unsqueeze_57121 = makeOP<opset1::Unsqueeze>({sub_Subtract, -1});
- auto mul_Multiply_3 =
- makeOP<opset1::Multiply>({select_Gather_2, select_Gather_1}, {{"auto_broadcast", "numpy"}});
- auto mul_Multiply_4 = makeOP<opset1::Multiply>({select_Gather, select_Gather_3}, {{"auto_broadcast", "numpy"}});
- auto add_Add = makeOP<opset1::Add>({mul_Multiply_3, mul_Multiply_4}, {{"auto_broadcast", "numpy"}});
- auto Unsqueeze_57122 = makeOP<opset1::Unsqueeze>({add_Add, -1});
- auto stack = makeOP<opset1::Concat>({Unsqueeze_57121, Unsqueeze_57122}, {{"axis", -1}});
- auto flatten_Reshape =
- makeOP<opset1::Reshape>({stack, {0, 0, num_heads, rotary_ndims}}, {{"special_zero", true}});
- auto cat_Concat = makeOP<opset1::Concat>({flatten_Reshape, VariadicSplit_20795->output(1)}, {{"axis", -1}});
-
- model = std::make_shared<ov::Model>(ov::OutputVector{cat_Concat},
+ auto unpack = makeOP<ov::opset1::VariadicSplit>({input, -1, {total_size_q, total_size_k, total_size_v}});
+ auto reshaped = makeOP<ov::opset1::Reshape>({unpack->output(unpack_output_idx), {0, 0, num_heads, ndims}},
+ {{"special_zero", true}});
+ auto split = makeOP<ov::opset1::VariadicSplit>({reshaped, 3, {rotary_ndims, -1}});
+ auto rope_part = makeOP<ov::opset1::Reshape>({split->output(0), {0, 0, num_heads, rotary_ndims / 2, 2}},
+ {{"special_zero", true}});
+ auto x_even = makeOP<ov::opset8::Gather>({rope_part, 0, -1}, {{"batch_dims", 0}});
+ auto x_odd = makeOP<ov::opset8::Gather>({rope_part, 1, -1}, {{"batch_dims", 0}});
+
+ auto cache_slice = makeOP<ov::opset8::Slice>({cos_sin_cache, {0}, seq_length, {1}, {0}});
+ auto cache_shape = makeOP<ov::opset1::Concat>({seq_length, {-1}, {1}, {rotary_ndims / 2}, {2}}, {{"axis", 0}});
+ auto cache_reshaped = makeOP<ov::opset1::Reshape>({cache_slice, cache_shape}, {{"special_zero", false}});
+ auto cos_tab = makeOP<ov::opset8::Gather>({cache_reshaped, 0, -1}, {{"batch_dims", 0}});
+ auto sin_tab = makeOP<ov::opset8::Gather>({cache_reshaped, 1, -1}, {{"batch_dims", 0}});
+
+ auto even_cos = makeOP<ov::opset1::Multiply>({x_even, cos_tab}, {{"auto_broadcast", "numpy"}});
+ auto odd_sin = makeOP<ov::opset1::Multiply>({x_odd, sin_tab}, {{"auto_broadcast", "numpy"}});
+ auto neg_odd_sin = makeOP<ov::opset1::Multiply>({odd_sin, -1.0f}, {{"auto_broadcast", "numpy"}});
+ auto add_even = makeOP<ov::opset1::Add>({even_cos, neg_odd_sin}, {{"auto_broadcast", "numpy"}});
+ auto unsq_even = makeOP<ov::opset1::Unsqueeze>({add_even, -1});
+
+ auto odd_cos = makeOP<ov::opset1::Multiply>({x_odd, cos_tab}, {{"auto_broadcast", "numpy"}});
+ auto even_sin = makeOP<ov::opset1::Multiply>({x_even, sin_tab}, {{"auto_broadcast", "numpy"}});
+ auto add_odd = makeOP<ov::opset1::Add>({odd_cos, even_sin}, {{"auto_broadcast", "numpy"}});
+ auto unsq_odd = makeOP<ov::opset1::Unsqueeze>({add_odd, -1});
+
+ auto stack = makeOP<ov::opset1::Concat>({unsq_even, unsq_odd}, {{"axis", -1}});
+ auto flatten = makeOP<ov::opset1::Reshape>({stack, {0, 0, num_heads, rotary_ndims}}, {{"special_zero", true}});
+ auto concat = makeOP<ov::opset1::Concat>({flatten, split->output(1)}, {{"axis", -1}});
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{concat},
ov::ParameterVector{input, seq_length, cos_sin_cache});
}
+
manager.register_pass<ov::pass::RoPEFusion>();
+
+ // Build reference model
{
- auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, 4608});
+ int slice_start = 0, slice_stop = 0;
+ if (unpack_output_idx == 0) {
+ slice_stop = total_size_q;
+ } else {
+ slice_start = total_size_q;
+ slice_stop = slice_start + total_size_k;
+ }
+
+ auto input = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{seq_len, batch, total_size});
auto seq_length = std::make_shared<ov::opset1::Parameter>(ov::element::i32, ov::Shape{1});
auto cos_sin_cache =
std::make_shared<ov::opset1::Parameter>(ov::element::f32,
ov::Shape{max_pos_length, batch, rotary_ndims / 2, 2});
auto rope = makeOP<ov::op::internal::RoPE>({input, cos_sin_cache, cos_sin_cache},
- {{"config.slice_start", 0},
- {"config.slice_stop", 4096},
+ {{"config.slice_start", slice_start},
+ {"config.slice_stop", slice_stop},
{"config.input_trans0213", false},
{"config.output_trans0213", false},
{"config.is_interleaved", false},
@@ -687,8 +704,11 @@ TEST_F(TransformationTestsF, ConvertToROPE_chatGLM_Slice) {
model_ref =
std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{input, seq_length, cos_sin_cache});
}
+ comparator.enable(FunctionsComparator::ATTRIBUTES);
}
+INSTANTIATE_TEST_SUITE_P(TransformationTestsF, ConvertToROPETest, ::testing::ValuesIn({0, 1}));
+
TEST_F(TransformationTestsF, ConvertToROPE_GPTJ_Slice) {
disable_rt_info_check();
using namespace ov;