Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPUW][VLM] Transformers 4.57.6 Gemma sliding mask fix (#34424)

Alexander Kalistratov62b345fe6dc5
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
index 764164b166a..b00973605e5 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
@@ -614,12 +614,16 @@ public:
};
auto past_kv_len = opp::wrap_type<ov::op::v8::Gather>({opp::any_input(), opp::any_input(), opp::any_input()});
- auto full_ctx_len = opp::wrap_type<ov::op::v1::Add>({past_kv_len, opp::any_input()});
- auto query_range = opp::wrap_type<ov::op::v4::Range>({past_kv_len, full_ctx_len, opp::any_input()});
+ // TODO: ov::op::v0::Squeeze may accept or not accept optional axes input, so we need to cover both cases in
+ // pattern If Squeeze accepts axes it doesn't match with pattern without axes, and if it doesn't accept axes it
+ // doesn't match with pattern with axes. So we need to add two branches in pattern to cover both cases.
+ auto past_kv_len_squeeze = opp::optional<ov::op::v0::Squeeze>({past_kv_len});
+ auto full_ctx_len = opp::wrap_type<ov::op::v1::Add>({past_kv_len_squeeze, opp::any_input()});
+ auto query_range = opp::wrap_type<ov::op::v4::Range>({past_kv_len_squeeze, full_ctx_len, opp::any_input()});
auto query_range_column = unsqueeze_sequence(query_range);
auto zero_const = opp::wrap_type<ov::op::v0::Constant>();
- auto full_ctx_len_2 = opp::wrap_type<ov::op::v1::Add>({opp::any_input(), past_kv_len});
+ auto full_ctx_len_2 = opp::wrap_type<ov::op::v1::Add>({opp::any_input(), past_kv_len_squeeze});
auto key_range = opp::wrap_type<ov::op::v4::Range>({zero_const, full_ctx_len_2, opp::any_input()});
auto key_range_row = unsqueeze_sequence(key_range);
auto opt_key_range_row_f32 = opp::optional<ov::op::v0::Convert>({key_range_row->output(0)});
@@ -638,26 +642,20 @@ public:
LOG_INFO("Found (4.53) pattern for Phi-3 Sliding Window Attention, will be replaced with custom for static "
"shapes.");
auto& node_to_output = m.get_pattern_value_map();
- auto node_past_kv_len = node_to_output.at(past_kv_len).get_node_shared_ptr();
- auto node_full_ctx_len = node_to_output.at(full_ctx_len).get_node_shared_ptr();
+ auto optional_squeeze = node_to_output.find(past_kv_len_squeeze);
+ auto matched_past_kv_len = optional_squeeze != node_to_output.end()
+ ? optional_squeeze->second.get_node_shared_ptr()
+ : node_to_output.at(past_kv_len).get_node_shared_ptr();
+ auto matched_full_ctx_len = node_to_output.at(full_ctx_len).get_node_shared_ptr();
auto node_neg_window_size = node_to_output.at(neg_window_size).get_node_shared_ptr();
- auto node_sliding_mask = node_to_output.at(sliding_mask).get_node_shared_ptr();
- auto node_sliding_and_causal_mask = node_to_output.at(sliding_and_causal_mask).get_node_shared_ptr();
+ auto matched_sliding_mask = node_to_output.at(sliding_mask).get_node_shared_ptr();
+ auto matched_sliding_and_causal_mask = node_to_output.at(sliding_and_causal_mask).get_node_shared_ptr();
- auto matched_past_kv_len = std::static_pointer_cast<ov::op::v8::Gather>(node_past_kv_len);
- auto matched_full_ctx_len = std::static_pointer_cast<ov::op::v1::Add>(node_full_ctx_len);
- std::shared_ptr<ov::Node> matched_key_range_row = nullptr;
- if (node_to_output.count(opt_key_range_row_f32)) {
- auto node_key_range_row_f32 = node_to_output[opt_key_range_row_f32].get_node_shared_ptr();
- matched_key_range_row = std::static_pointer_cast<ov::op::v0::Convert>(node_key_range_row_f32);
- } else {
- auto node_key_range_row = node_to_output.at(key_range_row).get_node_shared_ptr();
- matched_key_range_row = std::static_pointer_cast<ov::op::v0::Unsqueeze>(node_key_range_row);
- }
+ auto optional_convert = node_to_output.find(opt_key_range_row_f32);
+ std::shared_ptr<ov::Node> matched_key_range_row =
+ optional_convert != node_to_output.end() ? optional_convert->second.get_node_shared_ptr()
+ : node_to_output.at(key_range_row).get_node_shared_ptr();
auto matched_neg_window_size = std::static_pointer_cast<ov::op::v0::Constant>(node_neg_window_size);
- auto matched_sliding_mask = std::static_pointer_cast<ov::op::v1::Greater>(node_sliding_mask);
- auto matched_sliding_and_causal_mask =
- std::static_pointer_cast<ov::op::v13::BitwiseAnd>(node_sliding_and_causal_mask);
OPENVINO_ASSERT(matched_neg_window_size->get_output_size() == 1,
"Sliding window size constant must be of size 1, but got " +
std::to_string(matched_neg_window_size->get_output_size()));