-
Notifications
You must be signed in to change notification settings - Fork 57
CVS-175736 - [OVEP] Optimize Stateful Path: use output-to-input strategy to get the pairs of KV name #845
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
CVS-175736 - [OVEP] Optimize Stateful Path: use output-to-input strategy to get the pairs of KV name #845
Changes from all commits
d44a556
1634684
f6b3c63
165a7cd
8565e29
a873e7b
5d5d1f2
cec822e
38ae639
de5bc93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |||||||
| // Licensed under the MIT License | ||||||||
|
|
||||||||
| #include "core/providers/openvino/ov_stateful_patch_utils.h" | ||||||||
| #include "core/providers/shared_library/provider_api.h" | ||||||||
| #include "core/common/common.h" | ||||||||
|
|
||||||||
| namespace onnxruntime { | ||||||||
| namespace openvino_ep { | ||||||||
|
|
@@ -132,50 +134,135 @@ | |||||||
| manager.run_passes(ov_model); | ||||||||
| } | ||||||||
|
|
||||||||
| // Converted to C++ from below reference URL: | ||||||||
| // https://github.com/huggingface/optimum-intel/blob/main/optimum/exporters/openvino/stateful.py#L281 | ||||||||
| void PatchStatefulDecoder(std::shared_ptr<ov::Model> model) { | ||||||||
| // Helper function to extract KV patterns from output names dynamically | ||||||||
| // | ||||||||
| // Example: Given output names ["present_key_cross_0", "present_key_cross_1", "present_value_cross_0", "present_value_cross_1", "logits"] | ||||||||
| // key_value_output_names = ["present_key_cross_0", "present_key_cross_1", "present_value_cross_0", "present_value_cross_1"] | ||||||||
| // unique_patterns = {"key_cross", "value_cross"} | ||||||||
| std::pair<std::vector<std::string>, std::unordered_set<std::string>> ExtractKVPatternsFromOutputs(const std::shared_ptr<ov::Model>& model) { | ||||||||
| std::vector<std::string> key_value_output_names; | ||||||||
| std::unordered_set<std::string> unique_patterns; | ||||||||
|
|
||||||||
| const std::string prefix = "present_"; | ||||||||
| const size_t prefix_len = prefix.length(); | ||||||||
| for (const ov::Output<ov::Node>& output : model->outputs()) { | ||||||||
| const auto& names = output.get_names(); | ||||||||
| for (const auto& name : names) { | ||||||||
| if (name.find(prefix) == 0 && name.length() > prefix_len) { | ||||||||
| size_t last_underscore_pos = name.rfind('_'); | ||||||||
| // Extract pattern between "present_" and the last underscore | ||||||||
| if (last_underscore_pos != std::string::npos && last_underscore_pos > prefix_len) { | ||||||||
| std::string pattern = name.substr(prefix_len, last_underscore_pos - prefix_len); | ||||||||
| if (!pattern.empty()) { | ||||||||
| unique_patterns.insert(pattern); | ||||||||
| key_value_output_names.push_back(name); | ||||||||
| } | ||||||||
| } | ||||||||
| break; | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| if (unique_patterns.size() > 2) { | ||||||||
RyanMetcalfeInt8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| ORT_THROW("More than two unique KV patterns found in output names."); | ||||||||
Kotomi-Du marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| } | ||||||||
|
Comment on lines
+166
to
+168
|
||||||||
| if (unique_patterns.size() > 2) { | |
| ORT_THROW("More than two unique KV patterns found in output names."); | |
| } |
Kotomi-Du marked this conversation as resolved.
Show resolved
Hide resolved
Check warning on line 180 in onnxruntime/core/providers/openvino/ov_stateful_patch_utils.cc
GitHub Actions / Optional Lint C++
[cpplint] reported by reviewdog 🐶
Add #include <unordered_set> for unordered_set<> [build/include_what_you_use] [4]
Raw Output:
onnxruntime/core/providers/openvino/ov_stateful_patch_utils.cc:180: Add #include <unordered_set> for unordered_set<> [build/include_what_you_use] [4]
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.
The pattern extraction assumes the format 'present__' but doesn't validate that the suffix after the last underscore is actually numeric. This could lead to incorrect pattern extraction if output names have different formats (e.g., 'present_key_cross_layer_0'). Consider validating the numeric suffix before extracting the pattern.