-
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
base: ovep-develop
Are you sure you want to change the base?
CVS-175736 - [OVEP] Optimize Stateful Path: use output-to-input strategy to get the pairs of KV name #845
Changes from 3 commits
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 | ||
|---|---|---|---|---|
|
|
@@ -134,50 +134,113 @@ | |||
|
|
||||
| // 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 | ||||
| std::pair<std::vector<std::string>, std::vector<std::string>> ExtractKVPatternsFromOutputs(const std::shared_ptr<ov::Model>& model) { | ||||
| std::set<std::string> unique_patterns; | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider switching to |
||||
| std::vector<std::string> key_value_output_names; | ||||
|
|
||||
| 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) { | ||||
| key_value_output_names.push_back(name); | ||||
| 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); | ||||
| } | ||||
| } | ||||
| break; | ||||
| } | ||||
| } | ||||
| } | ||||
| std::vector<std::string> extracted_patterns(unique_patterns.begin(), unique_patterns.end()); | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to construct a |
||||
|
|
||||
| return std::make_pair(key_value_output_names, extracted_patterns); | ||||
| } | ||||
|
|
||||
| // Main function to extract KV tensors using dynamic pattern matching | ||||
| std::pair<std::vector<std::string>, std::vector<std::string>> ExtractInputKVTensors( | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here, consider switching to |
||||
| const std::shared_ptr<ov::Model>& model, const std::vector<std::string>& patterns) { | ||||
|
|
||||
| std::vector<std::string> key_value_input_names; | ||||
| std::vector<std::string> not_kv_inputs; | ||||
|
|
||||
| if (patterns.empty()) { | ||||
| // Fallback: use original substring matching | ||||
| for (const ov::Output<ov::Node>& input : model->inputs()) { | ||||
| const auto& names = input.get_names(); | ||||
| const std::string input_name = input.get_any_name(); | ||||
|
|
||||
| bool is_kv_input = false; | ||||
| for (const auto& name : names) { | ||||
| if (name.find("key_values") != std::string::npos || | ||||
| name.find("keys") != std::string::npos || | ||||
| name.find("values") != std::string::npos) { | ||||
| key_value_input_names.push_back(name); | ||||
| is_kv_input = true; | ||||
| break; | ||||
| } | ||||
| } | ||||
|
|
||||
| if (!is_kv_input) { | ||||
| not_kv_inputs.push_back(input_name); | ||||
| } | ||||
| } | ||||
|
|
||||
| return std::make_pair(key_value_input_names, not_kv_inputs); | ||||
| } | ||||
|
|
||||
| std::set<std::string> found_kv_inputs; | ||||
|
Check warning on line 200 in onnxruntime/core/providers/openvino/ov_stateful_patch_utils.cc
|
||||
|
||||
| std::set<std::string> found_kv_inputs; |
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.
This logic contradicts the comment above. If we find a pattern in the name, we won't check other names. Is that an expected behavior?
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.
yes, it is expected behavior. I rephrased the comment.
Outdated
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.
looks like a debug statement here.
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.
removed
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 know this line not something you added -- but can we add some more strict checking here? If all goes well, then key_value_output_names and key_value_output_names should be non-empty and have the same number of elements correct?
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.
updated
Outdated
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.
Same for here as below -- I think there should be a runtime exception thrown here. I don't think we'd ever intend for the stateful flow to get enabled, and not identify pairs of tensors to perform a make_stateful transformation on.
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.
updated
Check warning on line 240 in onnxruntime/core/providers/openvino/ov_stateful_patch_utils.cc
GitHub Actions / Optional Lint C++
[cpplint] reported by reviewdog 🐶
Add #include <iostream> for cout [build/include_what_you_use] [4]
Raw Output:
onnxruntime/core/providers/openvino/ov_stateful_patch_utils.cc:240: Add #include <iostream> for cout [build/include_what_you_use] [4]
Outdated
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 think that this one should be a runtime exception of some sort. I don't think we'd ever want to hit this state, return, and have the rest of the stateful flow continue on.
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.
updated
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.
This function returns two
std::vectors only to check that the first one is non-empty and the second one is used as a sort of lookup table. Therefore, it can returnstd::optional<T>instead.