-
Notifications
You must be signed in to change notification settings - Fork 3k
Paged attention transformation #24177
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
Closed
CuriousPanCake
wants to merge
8
commits into
openvinotoolkit:master
from
CuriousPanCake:paged_attention_transformation
Closed
Changes from all commits
Commits
Show all changes
8 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 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
56 changes: 56 additions & 0 deletions
56
...mmon/transformations/include/transformations/common_optimizations/PositionIDsReplacer.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,56 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/op/parameter.hpp" | ||
| #include "openvino/op/add.hpp" | ||
| #include "openvino/pass/graph_rewrite.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "openvino/cc/pass/itt.hpp" | ||
| #include "transformations_visibility.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| class TRANSFORMATIONS_API PositionIDsReplacer; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov | ||
|
|
||
| using namespace ov::op; | ||
|
|
||
| class ov::pass::PositionIDsReplacer : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_RTTI("PositionIDsReplacer", "0"); | ||
|
|
||
| // 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. | ||
| 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) { | ||
| std::cout << "____" << matcher_name << "___Matched___" << std::endl; | ||
| 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()); | ||
| std::cout << "APPLIED position_ids PARAMETER INSTEAD OF attention_mask-BASED SUB-GRAPH" << std::endl; | ||
| return true; | ||
| }; | ||
|
|
||
| auto m = std::make_shared<ov::pass::pattern::Matcher>(add, matcher_name); | ||
| register_matcher(m, callback); | ||
| } | ||
| }; |
56 changes: 56 additions & 0 deletions
56
...ransformations/include/transformations/common_optimizations/PrevSequenceLengthPattern.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,56 @@ | ||
| // Copyright (C) 2018-2024 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/pass/graph_rewrite.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "openvino/cc/pass/itt.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
| #include "transformations_visibility.hpp" | ||
| #include "openvino/op/subtract.hpp" | ||
| #include "openvino/op/shape_of.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| class TRANSFORMATIONS_API PrevSequenceLengthPattern; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov | ||
|
|
||
| using namespace ov::op; | ||
|
|
||
| class ov::pass::PrevSequenceLengthPattern : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_RTTI("PrevSequenceLengthPattern", "0"); | ||
| 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) { | ||
| std::cout << "____" << matcher_name << "___Matched___" << std::endl; | ||
| // 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) { | ||
| std::cout << "Converting " << prev_max_seq_len->get_output_element_type(0) << " of max_context_len to " << target_type << std::endl; | ||
itikhono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| replacement = std::make_shared<v0::Convert>(prev_max_seq_len, target_type); | ||
| } else { | ||
| replacement = prev_max_seq_len; | ||
| } | ||
| replace_node(gather, replacement); | ||
| std::cout << "DETECTED PATTERN PrevSequenceLengthPattern, CONNECTED TO A DEDICATED PARAMETER" << std::endl; | ||
| return true; | ||
| }; | ||
|
|
||
| auto m = std::make_shared<ov::pass::pattern::Matcher>(seq, matcher_name); | ||
| register_matcher(m, callback); | ||
| } | ||
| }; | ||
27 changes: 27 additions & 0 deletions
27
...n/transformations/include/transformations/common_optimizations/StateManagementPattern.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 TRANSFORMATIONS_API 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); | ||
| }; |
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.