-
Notifications
You must be signed in to change notification settings - Fork 3.3k
apply GPU transformation to avoid KV cache broadcast #36775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nazanin-beheshti
wants to merge
15
commits into
openvinotoolkit:master
Choose a base branch
from
nazanin-beheshti:naz/broadcast-kv-cache-fusion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cf9e9ad
apply GPU transformation to avoid KV cache broadcast
nazanin-beheshti 0b8f545
apply reviewer comments
nazanin-beheshti 5d3d40d
correct header name
nazanin-beheshti 2a0395a
change transformation name
nazanin-beheshti f3283ec
add missing header files
nazanin-beheshti 22e1e26
update header name
nazanin-beheshti 9f9a779
Add test case for GQA_decomposition_fusion
nazanin-beheshti 63d37fa
delete space
nazanin-beheshti 3bd0fad
change transformation name
nazanin-beheshti 7cdd7df
apply reviewer comments
nazanin-beheshti 54043f8
file name change (capital to small)
nazanin-beheshti 6aba2cb
update header file name
nazanin-beheshti e3d5b1f
apply reviwer comments
nazanin-beheshti 71db8e4
apply reviwer comments
nazanin-beheshti df811ad
change pattern matching
nazanin-beheshti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/plugins/intel_gpu/src/plugin/transformations/kvcache_gqa_broadcast_elimination.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "kvcache_gqa_broadcast_elimination.hpp" | ||
| #include "openvino/pass/pattern/matcher.hpp" | ||
| #include "openvino/pass/pattern/op/or.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/op/concat.hpp" | ||
| #include "openvino/core/partial_shape.hpp" | ||
| #include "intel_gpu/op/sdpa.hpp" | ||
| #include <openvino/util/pp.hpp> | ||
|
|
||
| namespace ov::intel_gpu { | ||
| KVCacheGQABroadcastElimination::KVCacheGQABroadcastElimination() { | ||
| using namespace ov::pass::pattern; | ||
| auto is_reshape_4d_to_5d = [](const ov::Node* node) { | ||
| if (!ov::is_type<ov::op::v1::Reshape>(node)) { | ||
| return false; | ||
| } | ||
| auto in_ps = node->get_input_partial_shape(0); | ||
| auto out_ps = node->get_output_partial_shape(0); | ||
| return in_ps.rank().is_static() && out_ps.rank().is_static() && in_ps.size() == 4 && out_ps.size() == 5; | ||
| }; | ||
|
|
||
| auto reshape_5d_to_4d = [](const ov::Output<ov::Node>& output) { | ||
| auto in_ps = output.get_node()->get_input_partial_shape(0); | ||
| auto out_ps = output.get_node()->get_output_partial_shape(0); | ||
| return in_ps.rank().is_static() && out_ps.rank().is_static() && in_ps.size() == 5 && out_ps.size() == 4; | ||
| }; | ||
|
|
||
| auto broadcast_concat = [is_reshape_4d_to_5d](const ov::Output<ov::Node>& output) { | ||
| auto concat = ov::as_type<ov::op::v0::Concat>(output.get_node()); | ||
| if (concat == nullptr || concat->get_input_size() < 2) { | ||
| return false; | ||
| } | ||
| auto first = concat->get_input_node_ptr(0); | ||
| if (!is_reshape_4d_to_5d(first)) { | ||
| return false; | ||
| } | ||
| for (size_t i = 1; i < concat->get_input_size(); ++i) { | ||
| if (concat->get_input_node_ptr(i) != first) { | ||
| return false; | ||
|
|
||
| } | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| auto input_a_m = any_input(); | ||
| auto input_attn_mask_m = any_input(); | ||
| auto input_scale_m = any_input(); | ||
|
|
||
| auto concat_key_m = wrap_type<ov::op::v0::Concat>(broadcast_concat); | ||
|
|
||
| auto reshape2_pattern_key_m = any_input(); | ||
| auto reshape_5d_to_4d_key_m = wrap_type<ov::op::v1::Reshape>({ concat_key_m, reshape2_pattern_key_m }, reshape_5d_to_4d); | ||
|
|
||
| auto concat_value_m = wrap_type<ov::op::v0::Concat>(broadcast_concat); | ||
|
|
||
| auto reshape2_pattern_value_m = any_input(); | ||
| auto reshape_5d_to_4d_value_m = wrap_type<ov::op::v1::Reshape>({ concat_value_m, reshape2_pattern_value_m }, reshape_5d_to_4d); | ||
|
|
||
| auto sdpa_without_attn_mask_m = wrap_type<op::SDPA>({ input_a_m, reshape_5d_to_4d_key_m, reshape_5d_to_4d_value_m }); | ||
| auto sdpa_with_attn_mask_m = | ||
| wrap_type<op::SDPA>({ input_a_m, reshape_5d_to_4d_key_m, reshape_5d_to_4d_value_m, any_input() }); | ||
| auto sdpa_with_attn_mask_and_scale_m = | ||
| wrap_type<op::SDPA>({ input_a_m, reshape_5d_to_4d_key_m, reshape_5d_to_4d_value_m, input_attn_mask_m, input_scale_m }); | ||
|
|
||
| auto sdpa_m = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{ sdpa_without_attn_mask_m, sdpa_with_attn_mask_m, sdpa_with_attn_mask_and_scale_m }); | ||
|
|
||
| ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) { | ||
| const auto& pattern_map = m.get_pattern_value_map(); | ||
| auto sdpa = ov::as_type_ptr<op::SDPA>(m.get_match_root()); | ||
| if (pattern_map.count(concat_key_m) == 0 || pattern_map.count(concat_value_m) == 0) { | ||
| return false; | ||
| } | ||
| // Concat -> Reshape(4d->5d) -> original source that was broadcast. | ||
| auto broadcast_source = [](const ov::Output<ov::Node>& concat_out) { | ||
| return concat_out.get_node()->input_value(0).get_node()->input_value(0); | ||
| }; | ||
| sdpa->input(1).replace_source_output(broadcast_source(pattern_map.at(concat_key_m))); | ||
| sdpa->input(2).replace_source_output(broadcast_source(pattern_map.at(concat_value_m))); | ||
| return true; | ||
| }; | ||
| auto m = std::make_shared<ov::pass::pattern::Matcher>(sdpa_m, "KVCacheGQABroadcastElimination"); | ||
| this->register_matcher(m, callback); | ||
| } | ||
|
|
||
| } // namespace ov::intel_gpu |
17 changes: 17 additions & 0 deletions
17
src/plugins/intel_gpu/src/plugin/transformations/kvcache_gqa_broadcast_elimination.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/pass/graph_rewrite.hpp" | ||
|
|
||
| namespace ov::intel_gpu { | ||
|
|
||
| class KVCacheGQABroadcastElimination : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_MATCHER_PASS_RTTI("KVCacheGQABroadcastElimination"); | ||
| KVCacheGQABroadcastElimination(); | ||
| }; | ||
|
|
||
| } // namespace ov::intel_gpu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/plugins/intel_gpu/tests/unit/transformations/kvcache_gqa_broadcast_elimination_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "common_test_utils/ov_test_utils.hpp" | ||
| #include "openvino/core/model.hpp" | ||
| #include "openvino/pass/manager.hpp" | ||
| #include "openvino/op/constant.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/op/concat.hpp" | ||
| #include "openvino/op/scatter_update.hpp" | ||
| #include "openvino/op/variadic_split.hpp" | ||
| #include "intel_gpu/op/sdpa.hpp" | ||
| #include "plugin/transformations/kvcache_gqa_broadcast_elimination.hpp" | ||
| #include <memory> | ||
|
|
||
| using namespace testing; | ||
| using namespace ov::intel_gpu; | ||
|
|
||
| namespace ov { | ||
| namespace test { | ||
| namespace intel_gpu { | ||
|
|
||
| TEST_F(TransformationTestsF, KVCacheGQABroadcastEliminationTest1) { | ||
| std::vector<int64_t> in0_order = {0, 1, 2, 3}; | ||
| std::vector<int64_t> in1_order = {0, 1, 2, 3}; | ||
| std::vector<int64_t> in2_order = {0, 1, 2, 3}; | ||
| std::vector<int64_t> out_order = {0, 1, 2, 3}; | ||
| const bool is_causal = false; | ||
| { | ||
| auto input_q = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 4, 8, 16}); | ||
| auto input_k = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 8, 16}); | ||
| auto input_v = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 8, 16}); | ||
|
|
||
| auto indices = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}); | ||
| auto axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {1}); | ||
| auto split_lengths = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{2}, {1, 1}); | ||
| auto k_updates = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{1, 1, 8, 16}, {0.0f}); | ||
| auto v_updates = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{1, 1, 8, 16}, {0.0f}); | ||
|
|
||
| auto scatter_k = std::make_shared<ov::op::v3::ScatterUpdate>(input_k, indices, k_updates, axis); | ||
| auto scatter_v = std::make_shared<ov::op::v3::ScatterUpdate>(input_v, indices, v_updates, axis); | ||
| auto split_k = std::make_shared<ov::op::v1::VariadicSplit>(scatter_k, axis, split_lengths); | ||
| auto split_v = std::make_shared<ov::op::v1::VariadicSplit>(scatter_v, axis, split_lengths); | ||
| auto split_k_out = split_k->output(0); | ||
| auto split_v_out = split_v->output(0); | ||
|
|
||
| auto reshape_k_5d_pattern = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {1, 1, 1, 8, 16}); | ||
| auto reshape_v_5d_pattern = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {1, 1, 1, 8, 16}); | ||
|
|
||
| auto reshape_k_5d = std::make_shared<ov::op::v1::Reshape>(split_k_out, reshape_k_5d_pattern, false); | ||
| auto reshape_v_5d = std::make_shared<ov::op::v1::Reshape>(split_v_out, reshape_v_5d_pattern, false); | ||
|
|
||
| auto concat_k = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{reshape_k_5d, reshape_k_5d, reshape_k_5d, reshape_k_5d},2); | ||
| auto concat_v = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{reshape_v_5d, reshape_v_5d, reshape_v_5d, reshape_v_5d},2); | ||
|
|
||
| auto reshape_k_4d_pattern = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{ 4 }, { 1, 4, 8, 16 }); | ||
| auto reshape_v_4d_pattern = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{ 4 }, { 1, 4, 8, 16 }); | ||
|
|
||
| auto reshape_k_4d = std::make_shared<ov::op::v1::Reshape>(concat_k, reshape_k_4d_pattern, false); | ||
| auto reshape_v_4d = std::make_shared<ov::op::v1::Reshape>(concat_v, reshape_v_4d_pattern, false); | ||
|
|
||
| auto inputs = ov::OutputVector{input_q, reshape_k_4d, reshape_v_4d}; | ||
| auto sdpa = std::make_shared<ov::intel_gpu::op::SDPA>(inputs, is_causal, in0_order, in1_order, in2_order, out_order); | ||
|
|
||
| model = std::make_shared<ov::Model>(ov::OutputVector{ sdpa }, ov::ParameterVector{input_q, input_k, input_v}); | ||
| manager.register_pass<KVCacheGQABroadcastElimination>(); | ||
| } | ||
| { | ||
| auto input_q = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 4, 8, 16}); | ||
| auto input_k = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 8, 16}); | ||
| auto input_v = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 8, 16}); | ||
|
|
||
| auto indices = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0}); | ||
| auto axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {1}); | ||
| auto split_lengths = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{2}, {1, 1}); | ||
| auto k_updates = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{1, 1, 8, 16}, {0.0f}); | ||
| auto v_updates = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{1, 1, 8, 16}, {0.0f}); | ||
|
|
||
| auto scatter_k = std::make_shared<ov::op::v3::ScatterUpdate>(input_k, indices, k_updates, axis); | ||
| auto scatter_v = std::make_shared<ov::op::v3::ScatterUpdate>(input_v, indices, v_updates, axis); | ||
| auto split_k = std::make_shared<ov::op::v1::VariadicSplit>(scatter_k, axis, split_lengths); | ||
| auto split_v = std::make_shared<ov::op::v1::VariadicSplit>(scatter_v, axis, split_lengths); | ||
|
|
||
| auto inputs = ov::OutputVector{ input_q, split_k->output(0), split_v->output(0) }; | ||
| auto sdpa = std::make_shared<ov::intel_gpu::op::SDPA>(inputs, is_causal, in0_order, in1_order, in2_order, out_order); | ||
|
|
||
| model_ref = std::make_shared<ov::Model>(ov::OutputVector{ sdpa }, ov::ParameterVector{ input_q, input_k, input_v }); | ||
| comparator.enable(FunctionsComparator::ATTRIBUTES); | ||
| } | ||
| } | ||
|
|
||
| } // namespace intel_gpu | ||
| } // namespace test | ||
| } // namespace ov | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add accuracy check too
comparator.enable(FunctionsComparator::CmpValues::ACCURACY);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got an exception Exception from src\inference\src\cpp\infer_request.cpp:224:
Check 'it != map.end()' failed at src\plugins\template\backend\int_executable.cpp:225:
Interpreter backend doesn't implement evaluate method for OP SDPA
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Lyamin-Roman the current transformation is similar with https://github.com/openvinotoolkit/openvino/blob/master/src/plugins/intel_gpu/tests/unit/transformations/unsqueeze_broadcast_reshape_sdpa_fusion_test.cpp which doesn't include
comparator.enable(FunctionsComparator::CmpValues::ACCURACY);in the unit test as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kotomi-Du, Yes, ok, it looks like there is a reference only for
PA, but accuracy test is still needed, and I suggest expanding thesrc/plugins/intel_gpu/tests/functional/subgraph_tests/sdpa.cpptest, which also tests the aforementionedUnsqueezeBroadcastReshapeSDPAFusionunderis_complex_gqaflag