OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[TRANSFORMATIONS] Fix Alibi slopes handling for Jais-13b model (#26338)
Andrii Staikov5c1166157b5d
diff --git a/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cpp b/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cppindex 6809e0d1524..bff78404aa5 100644--- a/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cpp+++ b/src/common/transformations/src/transformations/sdpa_to_paged_attention/state_management_pattern.cpp@@ -7,6 +7,8 @@#include <tuple>#include "openvino/cc/pass/itt.hpp"+#include "openvino/op/abs.hpp"+#include "openvino/op/add.hpp"#include "openvino/op/broadcast.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/divide.hpp"@@ -145,6 +147,14 @@ ov::pass::StateManagementPattern::StateManagementPattern(ParameterVector& kv_parsdpa_mask = pattern::wrap_type<v1::Reshape>({sdpa_mask, pattern::any_input()});sdpa_mask = pattern::wrap_type<v1::Select>({pattern::any_input(), pattern::any_input(), sdpa_mask});+ // For Jais (Jais-13b has a different pattern and handling of alibi slopes)+ auto mirroring_abs = pattern::wrap_type<v0::Abs>({pattern::any_input()});+ auto unsqueeze = pattern::wrap_type<v0::Unsqueeze>({mirroring_abs, pattern::any_input()});+ auto alibi_mask = pattern::wrap_type<v1::Multiply>({alibi, unsqueeze});+ alibi_mask = pattern::wrap_type<v3::Broadcast>({alibi_mask, pattern::any_input()});+ alibi_mask = pattern::wrap_type<v0::Unsqueeze>({alibi_mask, pattern::any_input()});+ alibi_mask = pattern::wrap_type<v1::Add>({pattern::any_input(), alibi_mask});+auto q = pattern::any_input();auto scale_input = pattern::any_input();@@ -152,7 +162,7 @@ ov::pass::StateManagementPattern::StateManagementPattern(ParameterVector& kv_parstd::make_shared<pattern::op::Or>(OutputVector{k_concat, k_shaped, k_shaped_transposed, k_simply_shaped});auto v_to_sdpa =std::make_shared<pattern::op::Or>(OutputVector{v_concat, v_shaped, v_shaped_transposed, v_simply_shaped});- auto mask_to_sdpa = std::make_shared<pattern::op::Or>(OutputVector{sdpa_mask, pattern::any_input()});+ auto mask_to_sdpa = std::make_shared<pattern::op::Or>(OutputVector{sdpa_mask, alibi_mask, pattern::any_input()});auto sdpa_with_4_inputs =pattern::wrap_type<v13::ScaledDotProductAttention>({q, k_to_sdpa, v_to_sdpa, mask_to_sdpa});@@ -324,6 +334,35 @@ ov::pass::StateManagementPattern::StateManagementPattern(ParameterVector& kv_parif (alibi_slopes->get_element_type() == element::f32) {alibi_slopes = std::make_shared<v0::Convert>(alibi_slopes, element::f32);}++ // Jais-13b case+ if (pattern_map.find(mirroring_abs) != pattern_map.end()) {+ // For now there's no such case with Alibi slopes being not a Constant,+ // however that may change in the future. That is why the presence of+ // Abs is the main sign of the Jais-like topology, thus we need to multiply+ // by -1. If we encounter the Alibi being a constant, we may do the additional+ // checking of the values to be negative and, if it fails, we won't multiply+ // the values by -1.+ if (auto alibi_constant =+ std::dynamic_pointer_cast<v0::Constant>(pattern_map.at(alibi).get_node_shared_ptr())) {+ auto alibi_constant_values = alibi_constant->cast_vector<float>();+ bool all_values_nagative =+ std::all_of(alibi_constant_values.begin(), alibi_constant_values.end(), [&](float value) {+ return value < 0.0;+ });++ if (all_values_nagative) {+ alibi_slopes = std::make_shared<v1::Multiply>(+ alibi_slopes,+ v0::Constant::create(alibi_slopes->get_element_type(), {}, {-1}));+ }+ } else {+ alibi_slopes = std::make_shared<v1::Multiply>(+ alibi_slopes,+ v0::Constant::create(alibi_slopes->get_element_type(), {}, {-1}));+ }+ }+} else {alibi_slopes = v0::Constant::create(element::f32, Shape{0}, {});}