Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion onnxruntime/core/providers/openvino/ov_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ void OVInferRequest::Infer() {
StatefulOVInferRequest::StatefulOVInferRequest(ov::InferRequest infer_request, std::string device)
: OVInferRequest(std::move(infer_request)), target_device(device) {
bool gpu_or_npu = ((device.find("NPU") != std::string::npos) || (device.find("GPU") != std::string::npos));
if (gpu_or_npu) {

// check if there is input_ids tensors and if the tensor type is int64,
// because logic prefill_use_full_chat_history is only for specific inputs and data type
auto input_ids_opt = FindTensor("input_ids");
if (gpu_or_npu && input_ids_opt.has_value() && input_ids_opt->get_element_type() == ov::element::i64) {
prefill_use_full_chat_history = true;
}
}
Expand Down
32 changes: 28 additions & 4 deletions onnxruntime/core/providers/openvino/ov_stateful_patch_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ bool ModelHasInputOutputNames(std::shared_ptr<ov::Model> model, const std::strin
return false;
}

std::string GetInputOutputName(std::shared_ptr<ov::Model> ov_model,
const std::vector<std::string>& candidate_names) {
for (const auto& name : candidate_names) {
if (ModelHasInputOutputNames(ov_model, name)) {
return name;
}
}
// Return the first candidate as default if none are found
return candidate_names.empty() ? "" : candidate_names[0];
}

void FuseCacheReorder(std::shared_ptr<ov::Model> ov_model,
std::vector<std::string>& not_kv_inputs,
const std::vector<std::string>& key_value_input_names,
Expand All @@ -67,10 +78,15 @@ void FuseCacheReorder(std::shared_ptr<ov::Model> ov_model,
throw std::runtime_error("Model already has fused cache");
}

std::string main_input_name = "inputs_embeds";
if (ModelHasInputOutputNames(ov_model, "input_ids")) {
main_input_name = "input_ids";
}
// Define input name candidates in priority order
const std::vector<std::string> input_name_candidates = {
"inputs_embeds", // Default fallback
"input_ids", // Most common
"input_hidden_states", // Alternative
"/model/embed_tokens/Gather_output_0" // Specific model type
};

std::string main_input_name = GetInputOutputName(ov_model, input_name_candidates);

auto input_batch = ov_model->input(main_input_name).get_partial_shape()[0];

Expand Down Expand Up @@ -130,6 +146,14 @@ void PatchStatefulDecoder(std::shared_ptr<ov::Model> model) {
key_value_input_names.push_back(name);
found = true;
break;
} else if (name.find("keys") != std::string::npos) {
key_value_input_names.push_back(name);
found = true;
break;
} else if (name.find("values") != std::string::npos) {
key_value_input_names.push_back(name);
found = true;
break;
Comment on lines +149 to +156
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for detecting 'keys' and 'values' patterns treats them separately with identical code blocks. This could lead to only finding one type of cache input when both should be collected. Consider restructuring to collect all matching key-value inputs rather than breaking after the first match.

Copilot uses AI. Check for mistakes.
}
}

Expand Down
Loading