-
Notifications
You must be signed in to change notification settings - Fork 3k
Port SDPA to PagedAttention transformation #24336
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
Merged
ilya-lavrenov
merged 14 commits into
openvinotoolkit:master
from
itikhono:paged_attention_transformation
May 2, 2024
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b7785ec
PagedAttention transformation placeholder
slyalin facf469
wip: added the setup part and half of the first matcher
CuriousPanCake 268392a
finished first matcher
CuriousPanCake b69c8ce
rewrote base implementation of patterns
CuriousPanCake c6cc882
misc changes
CuriousPanCake 2f79649
debugging
CuriousPanCake d6730d9
wip
CuriousPanCake daef5f5
working
CuriousPanCake 0467c60
Merge remote-tracking branch 'upstream/master' into paged_attention_t…
itikhono d825cc6
Port SDPA to PagedAttention transformation
itikhono f827aec
delete debug print; fix warning
itikhono ef82db6
codestyle
itikhono 7f27c10
fix warning
itikhono d798152
Merge branch 'master' into paged_attention_transformation
itikhono 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
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
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
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
27 changes: 27 additions & 0 deletions
27
...transformations/include/transformations/sdpa_to_paged_attention/position_ids_replacer.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,27 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/cc/pass/itt.hpp" | ||
| #include "openvino/op/add.hpp" | ||
| #include "openvino/op/parameter.hpp" | ||
| #include "openvino/pass/graph_rewrite.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
| #include "transformations_visibility.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| class PositionIDsReplacer; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov | ||
|
|
||
| class ov::pass::PositionIDsReplacer : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_RTTI("PositionIDsReplacer", "0"); | ||
| explicit PositionIDsReplacer(const std::shared_ptr<Output<Node>>& position_ids); | ||
| }; |
27 changes: 27 additions & 0 deletions
27
...rmations/include/transformations/sdpa_to_paged_attention/prev_sequence_length_pattern.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,27 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/cc/pass/itt.hpp" | ||
| #include "openvino/op/shape_of.hpp" | ||
| #include "openvino/op/subtract.hpp" | ||
| #include "openvino/pass/graph_rewrite.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
| #include "transformations_visibility.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| class PrevSequenceLengthPattern; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov | ||
|
|
||
| class ov::pass::PrevSequenceLengthPattern : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_RTTI("PrevSequenceLengthPattern", "0"); | ||
| explicit PrevSequenceLengthPattern(const std::shared_ptr<ov::op::v1::Subtract>& prev_max_seq_len); | ||
| }; |
27 changes: 27 additions & 0 deletions
27
...nsformations/include/transformations/sdpa_to_paged_attention/state_management_pattern.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,27 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/pass/graph_rewrite.hpp" | ||
| #include "transformations_visibility.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| class StateManagementPattern; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov | ||
|
|
||
| class ov::pass::StateManagementPattern : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_RTTI("StateManagementPattern", "0"); | ||
| StateManagementPattern(ParameterVector& kv_parameters, | ||
| const ParameterVector& model_remaining_params, | ||
| const std::shared_ptr<ov::op::v0::Constant>& sliding_window, | ||
| ParameterVector& parameters_to_remove, | ||
| NodeVector& assignes_to_remove, | ||
| int& layer_index); | ||
| }; |
27 changes: 27 additions & 0 deletions
27
...mations/include/transformations/sdpa_to_paged_attention/total_sequence_length_pattern.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,27 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/cc/pass/itt.hpp" | ||
| #include "openvino/op/concat.hpp" | ||
| #include "openvino/op/parameter.hpp" | ||
| #include "openvino/pass/graph_rewrite.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
| #include "transformations_visibility.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| class TotalSequenceLengthPattern; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov | ||
|
|
||
| class ov::pass::TotalSequenceLengthPattern : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_RTTI("TotalSequenceLengthPattern", "0"); | ||
| explicit TotalSequenceLengthPattern(const std::shared_ptr<ov::op::v0::Parameter>& max_context_len); | ||
| }; |
39 changes: 39 additions & 0 deletions
39
...mon/transformations/src/transformations/sdpa_to_paged_attention/position_ids_replacer.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,39 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "transformations/sdpa_to_paged_attention/position_ids_replacer.hpp" | ||
|
|
||
| #include "openvino/cc/pass/itt.hpp" | ||
| #include "openvino/op/gather.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
|
|
||
| using namespace ov::op; | ||
|
|
||
| // TODO: Instead of using the following transformation that matches quite a specific place in a model graph in case when | ||
| // position_ids parameter is missing, consider replacing always existing attention_mask parameter with a sub-graph using | ||
| // a new slot_mapping parameter. | ||
| ov::pass::PositionIDsReplacer::PositionIDsReplacer(const std::shared_ptr<Output<Node>>& position_ids) { | ||
| MATCHER_SCOPE(PositionIDsReplacer); | ||
|
|
||
| auto input_ids = pattern::any_input(); | ||
| auto input_embed = pattern::wrap_type<v8::Gather>({pattern::any_input(), input_ids, pattern::any_input()}); | ||
|
|
||
| auto position_ids_pattern = pattern::any_input(); | ||
| auto offset = pattern::wrap_type<v0::Constant>(); | ||
| auto add_offset = pattern::wrap_type<v1::Add>({position_ids_pattern, offset}); | ||
| auto convert = pattern::wrap_type<v0::Convert>({add_offset}); | ||
| auto position_embed = pattern::wrap_type<v8::Gather>({pattern::any_input(), convert, pattern::any_input()}); | ||
|
|
||
| auto add = pattern::wrap_type<v1::Add>({input_embed, position_embed}); | ||
|
|
||
| ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) { | ||
| const auto& pattern_map = m.get_pattern_value_map(); | ||
| replace_node(pattern_map.at(position_ids_pattern).get_node_shared_ptr(), position_ids->get_node_shared_ptr()); | ||
| return true; | ||
| }; | ||
|
|
||
| auto m = std::make_shared<ov::pass::pattern::Matcher>(add, matcher_name); | ||
| register_matcher(m, callback); | ||
| } |
41 changes: 41 additions & 0 deletions
41
...nsformations/src/transformations/sdpa_to_paged_attention/prev_sequence_length_pattern.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,41 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "transformations/sdpa_to_paged_attention/prev_sequence_length_pattern.hpp" | ||
|
|
||
| #include "openvino/cc/pass/itt.hpp" | ||
| #include "openvino/op/gather.hpp" | ||
| #include "openvino/op/shape_of.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
|
|
||
| using namespace ov::op; | ||
|
|
||
| ov::pass::PrevSequenceLengthPattern::PrevSequenceLengthPattern( | ||
| const std::shared_ptr<ov::op::v1::Subtract>& prev_max_seq_len) { | ||
| MATCHER_SCOPE(PrevSequenceLengthPattern); | ||
|
|
||
| auto kv_past = pattern::wrap_type<v6::ReadValue>({pattern::any_input()}); | ||
| auto kv_gather = pattern::wrap_type<v8::Gather>({kv_past, pattern::any_input(), pattern::any_input()}); | ||
| auto kv_shape = pattern::wrap_type<v3::ShapeOf>({kv_gather}); | ||
| auto seq = pattern::wrap_type<v8::Gather>({kv_past, pattern::any_input(), pattern::any_input()}); | ||
|
|
||
| ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) { | ||
| // TODO: Check that seq has axis that really takes sequence len but not any other dimension -- use symbolics or | ||
| // look at the constant input | ||
| auto gather = m.get_match_root(); | ||
| auto target_type = gather->get_output_element_type(0); | ||
| std::shared_ptr<Node> replacement; | ||
| if (prev_max_seq_len->get_output_element_type(0) != target_type) { | ||
| replacement = std::make_shared<v0::Convert>(prev_max_seq_len, target_type); | ||
| } else { | ||
| replacement = prev_max_seq_len; | ||
| } | ||
| replace_node(gather, replacement); | ||
| return true; | ||
| }; | ||
|
|
||
| auto m = std::make_shared<ov::pass::pattern::Matcher>(seq, matcher_name); | ||
| register_matcher(m, callback); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.