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] Refactor SDPAFusion transformation (#32229)
Andrii Staikov8cad1caa673f
diff --git a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cppindex d64a63b3542..ac74ebc342e 100644--- a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp@@ -19,6 +19,7 @@#include "openvino/op/reshape.hpp"#include "openvino/op/scaled_dot_product_attention.hpp"#include "openvino/op/shape_of.hpp"+#include "openvino/op/slice.hpp"#include "openvino/op/softmax.hpp"#include "openvino/op/squeeze.hpp"#include "openvino/op/strided_slice.hpp"@@ -32,6 +33,8 @@#include "transformations/symbolic_transformations/symbolic_optimizations.hpp"#include "transformations/utils/utils.hpp"+using namespace ov::op;+namespace {std::vector<size_t> get_order(const ov::pass::pattern::PatternSymbolValue& any_layout_sym,@@ -194,6 +197,132 @@ SDPAReshapeFusion::SDPAReshapeFusion() {this->register_matcher(m, callback);}+static std::shared_ptr<ov::Node> get_scale(std::shared_ptr<ov::Node> scale_pattern,+ element::Type default_scale_type,+ ov::pass::pattern::Matcher& matcher) {+ auto& pm = matcher.get_pattern_value_map();+ if (pm.count(scale_pattern)) {+ auto scale_node = pm.at(scale_pattern);++ // According to the spec, scale should be a scalar or 1D with 1 element+ auto pshape = scale_node.get_partial_shape();+ auto rank = pshape.rank();+ if (rank.is_dynamic()) {+ return nullptr;+ }++ if (pshape.is_static() && ov::shape_size(pshape.get_shape()) != 1) {+ return nullptr;+ } else {+ if (rank.get_length() > 1) {+ scale_node = op::util::make_try_fold<v1::Reshape>(scale_node,+ v0::Constant::create(ov::element::i64, {1}, {1}),+ false);+ }+ return scale_node.get_node_shared_ptr();+ }+ } else {+ return v0::Constant::create(default_scale_type, ov::Shape{}, {1.0});+ }+}++static std::shared_ptr<ov::Node> get_mask(std::shared_ptr<ov::Node> mask_pattern,+ std::shared_ptr<ov::Node> opt_mask_add,+ element::Type default_mask_type,+ bool mask_present,+ ov::pass::pattern::Matcher& matcher) {+ auto& pm = matcher.get_pattern_value_map();+ if (mask_present && pm.count(opt_mask_add)) {+ ov::Output<ov::Node> qk_out = pm.at(opt_mask_add);+ // Get shape of the first input+ auto qk_out_ps = qk_out.get_target_inputs().begin()->get_partial_shape();++ auto mask_node = pm.at(mask_pattern);+ auto mask_input_ps = mask_node.get_partial_shape();++ if (!qk_out_ps.rank().is_static() || !mask_input_ps.rank().is_static())+ return nullptr;+ if (qk_out_ps.size() > 4)+ return nullptr;+ // mask should be broadcastable to qk shape+ if (!ov::PartialShape::broadcast_merge_into(qk_out_ps, mask_input_ps, AutoBroadcastType::NUMPY))+ return nullptr;++ if (mask_input_ps.size() < 2) {+ // OpenVINO SDPA specification requires the attention mask to have rank >= 2.+ auto diff = 2 - mask_input_ps.size();+ std::vector<int64_t> axes(diff);+ std::iota(axes.begin(), axes.end(), 0);+ auto axes_const = v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);+ auto mask_unsqueeze = std::make_shared<v0::Unsqueeze>(mask_node, axes_const);+ mask_unsqueeze->set_friendly_name(mask_node.get_node_shared_ptr()->get_friendly_name() + "/Squeeze");+ ov::copy_runtime_info(matcher.get_matched_nodes(), mask_unsqueeze);+ mask_node = mask_unsqueeze;+ } else {+ std::vector<int64_t> axes;+ // -2 because OpenVINO SDPA specification requires the attention mask to have rank >= 2.+ for (size_t i = 0; i < (mask_input_ps.size() - 2); ++i) {+ if (mask_input_ps[i].is_static() && mask_input_ps[i].get_length() == 1) {+ axes.push_back(i);+ } else {+ break;+ }+ }+ if (!axes.empty()) {+ auto axes_const = v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);+ auto mask_squeeze = std::make_shared<v0::Squeeze>(mask_node, axes_const);+ mask_squeeze->set_friendly_name(mask_node.get_node_shared_ptr()->get_friendly_name() + "/Squeeze");+ ov::copy_runtime_info(matcher.get_matched_nodes(), mask_squeeze);+ mask_node = mask_squeeze;+ }+ }+ return mask_node.get_node_shared_ptr();+ } else {+ return v0::Constant::create(default_mask_type, ov::Shape{}, {0});+ }+}++static ov::OutputVector get_qkv(ov::OutputVector qkv,+ Output<ov::Node> mask_node,+ element::Type default_mask_type,+ bool matmul_transposes_k,+ ov::pass::pattern::Matcher& matcher) {+ // 3 is the min supported rank according to the SDPA spec+ int64_t supported_rank = std::max(mask_node.get_partial_shape().rank().get_length(), static_cast<int64_t>(3));+ for (size_t i = 0; i < qkv.size(); ++i) {+ auto pshape = qkv[i].get_partial_shape();+ if (pshape.rank().is_dynamic())+ return {};+ // align all inputs+ supported_rank = std::max(static_cast<int64_t>(pshape.size()), supported_rank);+ }++ for (size_t i = 0; i < qkv.size(); ++i) {+ auto pshape = qkv[i].get_partial_shape();+ int64_t diff = supported_rank - static_cast<int64_t>(pshape.size());+ if (diff > 0) {+ std::vector<size_t> axes(diff, 0);+ std::iota(axes.begin(), axes.end(), 0);+ auto axes_node = v0::Constant::create(ov::element::i64, ov::Shape{static_cast<size_t>(diff)}, axes);+ auto reshape = std::make_shared<v0::Unsqueeze>(qkv[i], axes_node);+ qkv[i] = reshape;+ ov::copy_runtime_info(matcher.get_matched_nodes(), {reshape, axes_node});+ }++ if (i == 1 && !matmul_transposes_k) {+ // Transpose k+ pshape = qkv[i].get_partial_shape();+ std::vector<int> axes_values(pshape.size());+ std::iota(axes_values.begin(), axes_values.end(), 0);+ std::swap(axes_values[axes_values.size() - 1], axes_values[axes_values.size() - 2]);+ auto axes = v0::Constant::create(ov::element::i64, {axes_values.size()}, axes_values);+ qkv[i] = std::make_shared<v1::Transpose>(qkv[i], axes);+ ov::copy_runtime_info(matcher.get_matched_nodes(), {axes, qkv[i].get_node_shared_ptr()});+ }+ }+ return qkv;+}+SDPAFusionMatcher::SDPAFusionMatcher() {MATCHER_SCOPE(SDPAFusionMatcher);@@ -281,140 +410,40 @@ SDPAFusionMatcher::SDPAFusionMatcher() {wrap_type<v0::MatMul>({softmax_opt_reshaped, v}, qkv_shape, {{"transpose_a", false}, {"transpose_b", false}});ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {- const auto& pm = m.get_pattern_value_map();+ auto pm = m.get_pattern_value_map();if (transformation_callback(m.get_match_root())) {return false;}// 2 scales detected- if (pm.count(qk_scaled) && (pm.count(opt_k_scale) || pm.count(opt_kT_scale))) {+ if (pm.count(qk_scaled) && (pm.count(opt_k_scale) || pm.count(opt_kT_scale)))return false;- }bool mask_present = pm.count(mask);- bool matmul_trasposes_k = pm.count(qk_transpose_b);+ bool matmul_transposes_k = pm.count(qk_transpose_b);auto q_node = pm.at(q);auto k_node = pm.count(k) ? pm.at(k) : pm.at(kT);auto v_node = pm.at(v);- if (mask_present && pm.at(mask).get_partial_shape().size() > 4) {+ if (mask_present && pm.at(mask).get_partial_shape().size() > 4)return false;- }auto T = q_node.get_element_type();- ov::Output<ov::Node> scale_node;- if (pm.count(attn_scale)) {- scale_node = pm.at(attn_scale);-- // According to the spec, scale should be a scalar or 1D with 1 element- auto pshape = scale_node.get_partial_shape();- auto rank = pshape.rank();- if (rank.is_dynamic()) {- return false;- }-- if (pshape.is_static() && ov::shape_size(pshape.get_shape()) != 1) {- return false;- } else {- if (rank.get_length() > 1) {- scale_node =- ov::op::util::make_try_fold<v1::Reshape>(scale_node,- v0::Constant::create(ov::element::i64, {1}, {1}),- false);- }- }- } else {- scale_node = v0::Constant::create(T, ov::Shape{}, {1.0});- }- Output<ov::Node> mask_input;- if (mask_present && pm.count(qk_opt_scaled_opt_mask_added)) {- ov::Output<ov::Node> qk_out = pm.at(qk_opt_scaled_opt_mask_added);- // Get shape of the first input- auto qk_out_ps = qk_out.get_target_inputs().begin()->get_partial_shape();-- mask_input = pm.at(mask);- auto mask_input_ps = mask_input.get_partial_shape();-- if (!qk_out_ps.rank().is_static() || !mask_input_ps.rank().is_static())- return false;- if (qk_out_ps.size() > 4)- return false;-- // mask should be broadcastable to qk shape- if (!ov::PartialShape::broadcast_merge_into(qk_out_ps, mask_input_ps, AutoBroadcastType::NUMPY))- return false;-- if (mask_input_ps.size() < 2) {- // OpenVINO SDPA specification requires the attention mask to have rank >= 2.- auto diff = 2 - mask_input_ps.size();- std::vector<int64_t> axes(diff);- std::iota(axes.begin(), axes.end(), 0);- auto axes_const = v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);- auto mask_unsqueeze = std::make_shared<v0::Unsqueeze>(mask_input, axes_const);- mask_unsqueeze->set_friendly_name(mask->get_friendly_name());- ov::copy_runtime_info(m.get_matched_nodes(), mask_unsqueeze);- mask_input = mask_unsqueeze;- } else {- std::vector<int64_t> axes;- // -2 because OpenVINO SDPA specification requires the attention mask to have rank >= 2.- for (size_t i = 0; i < (mask_input_ps.size() - 2); ++i) {- if (mask_input_ps[i].is_static() && mask_input_ps[i].get_length() == 1) {- axes.push_back(i);- } else {- break;- }- }- if (!axes.empty()) {- auto axes_const = v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);- auto mask_squeeze = std::make_shared<v0::Squeeze>(mask_input, axes_const);- mask_squeeze->set_friendly_name(mask->get_friendly_name());- ov::copy_runtime_info(m.get_matched_nodes(), mask_squeeze);- mask_input = mask_squeeze;- }- }- } else {- mask_input = v0::Constant::create(T, ov::Shape{}, {0});- }-- ov::OutputVector vec = {q_node, k_node, v_node};- // 3 is the min supported rank according to the SDPA spec- size_t supported_rank = std::max(mask_input.get_partial_shape().rank().get_length(), static_cast<int64_t>(3));- for (size_t i = 0; i < vec.size(); ++i) {- auto pshape = vec[i].get_partial_shape();- if (pshape.rank().is_dynamic()) {- return false;- }- // align all inputs- supported_rank = std::max(pshape.size(), supported_rank);- }+ std::shared_ptr<ov::Node> scale_node;+ if (!(scale_node = get_scale(attn_scale, T, m)))+ return false;- for (size_t i = 0; i < vec.size(); ++i) {- auto pshape = vec[i].get_partial_shape();- int diff = static_cast<int>(supported_rank - pshape.size());- if (diff > 0) {- std::vector<size_t> axes(diff, 0);- std::iota(axes.begin(), axes.end(), 0);- auto axes_node = v0::Constant::create(ov::element::i64, ov::Shape{static_cast<size_t>(diff)}, axes);- auto reshape = std::make_shared<v0::Unsqueeze>(vec[i], axes_node);- vec[i] = reshape;- ov::copy_runtime_info(m.get_matched_nodes(), {reshape, axes_node});- }+ std::shared_ptr<ov::Node> mask_input;+ if (!(mask_input = get_mask(mask, qk_opt_scaled_opt_mask_added, T, mask_present, m)))+ return false;- if (i == 1 && !matmul_trasposes_k) {- // Transpose k- pshape = vec[i].get_partial_shape();- std::vector<int> axes_values(pshape.size());- std::iota(axes_values.begin(), axes_values.end(), 0);- std::swap(axes_values[axes_values.size() - 1], axes_values[axes_values.size() - 2]);- auto axes = v0::Constant::create(ov::element::i64, {axes_values.size()}, axes_values);- vec[i] = std::make_shared<v1::Transpose>(vec[i], axes);- ov::copy_runtime_info(m.get_matched_nodes(), {axes, vec[i].get_node_shared_ptr()});- }- }+ ov::OutputVector qkv = get_qkv({q_node, k_node, v_node}, mask_input, T, matmul_transposes_k, m);+ if (qkv.size() == 0)+ return false;std::shared_ptr<ov::Node> sdpa =- std::make_shared<v13::ScaledDotProductAttention>(vec[0], vec[1], vec[2], mask_input, scale_node, false);+ std::make_shared<v13::ScaledDotProductAttention>(qkv[0], qkv[1], qkv[2], mask_input, scale_node, false);sdpa->set_friendly_name(m.get_match_root()->get_friendly_name());ov::copy_runtime_info(m.get_matched_nodes(), sdpa);ov::replace_node(m.get_match_root(), sdpa);@@ -467,148 +496,49 @@ SDPAFusionMatcherSinks::SDPAFusionMatcherSinks() {auto softmax_pred = consumers_count(1) && axis_predicate;auto softmax = wrap_type<v8::Softmax>({sinks_sub}, softmax_pred);- auto sinks_ss = wrap_type<v1::StridedSlice>({softmax, any_input(), any_input(), any_input()});- auto qkv = wrap_type<v0::MatMul>({sinks_ss, v});+ auto sinks_slice = wrap_type<v1::StridedSlice>({softmax, any_input(), any_input(), any_input()}) |+ wrap_type<v8::Slice>({softmax, any_input(), any_input(), any_input(), any_input()});+ auto qkv = wrap_type<v0::MatMul>({sinks_slice, v});ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {const auto& pm = m.get_pattern_value_map();- if (transformation_callback(m.get_match_root())) {+ if (transformation_callback(m.get_match_root()))return false;- }bool mask_present = pm.count(mask);- bool matmul_trasposes_k = pm.count(qk_transpose_b);+ bool matmul_transposes_k = pm.count(qk_transpose_b);auto q_node = pm.at(q);auto k_node = pm.at(k);auto v_node = pm.at(v);- if (pm.at(mask).get_partial_shape().rank().get_length() > 4) {+ if (pm.at(mask).get_partial_shape().rank().get_length() > 4)return false;- }auto T = q_node.get_element_type();- ov::Output<ov::Node> scale_node;- if (pm.count(attn_scale)) {- scale_node = pm.at(attn_scale);-- // According to the spec, scale should be a scalar or 1D with 1 element- auto pshape = scale_node.get_partial_shape();- auto rank = pshape.rank();- if (rank.is_dynamic()) {- return false;- }-- if (pshape.is_static() && ov::shape_size(pshape.get_shape()) != 1) {- return false;- } else {- if (rank.get_length() > 1) {- scale_node =- ov::op::util::make_try_fold<v1::Reshape>(scale_node,- v0::Constant::create(ov::element::i64, {1}, {1}),- false);- }- }- } else {- scale_node = v0::Constant::create(T, ov::Shape{}, {1.0});- }- Output<ov::Node> mask_input;- if (mask_present && pm.count(opt_mask_add)) {- ov::Output<ov::Node> qk_out = pm.at(opt_mask_add);- // Get shape of the first input- auto qk_out_ps = qk_out.get_target_inputs().begin()->get_partial_shape();-- mask_input = pm.at(mask);- auto mask_input_ps = mask_input.get_partial_shape();-- if (!qk_out_ps.rank().is_static() || !mask_input_ps.rank().is_static())- return false;- if (qk_out_ps.size() > 4)- return false;-- // mask should be broadcastable to qk shape- if (!ov::PartialShape::broadcast_merge_into(qk_out_ps, mask_input_ps, AutoBroadcastType::NUMPY))- return false;-- if (mask_input_ps.size() < 2) {- // OpenVINO SDPA specification requires the attention mask to have rank >= 2.- auto diff = 2 - mask_input_ps.size();- std::vector<int64_t> axes(diff);- std::iota(axes.begin(), axes.end(), 0);- auto axes_const = v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);- auto mask_unsqueeze = std::make_shared<v0::Unsqueeze>(mask_input, axes_const);- mask_unsqueeze->set_friendly_name(mask->get_friendly_name());- ov::copy_runtime_info(m.get_matched_nodes(), mask_unsqueeze);- mask_input = mask_unsqueeze;- } else {- std::vector<int64_t> axes;- // -2 because OpenVINO SDPA specification requires the attention mask to have rank >= 2.- for (size_t i = 0; i < (mask_input_ps.size() - 2); ++i) {- if (mask_input_ps[i].is_static() && mask_input_ps[i].get_length() == 1) {- axes.push_back(i);- } else {- break;- }- }- if (!axes.empty()) {- auto axes_const = v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);- auto mask_squeeze = std::make_shared<v0::Squeeze>(mask_input, axes_const);- mask_squeeze->set_friendly_name(mask->get_friendly_name());- ov::copy_runtime_info(m.get_matched_nodes(), mask_squeeze);- mask_input = mask_squeeze;- }- }- } else {- mask_input = v0::Constant::create(T, ov::Shape{}, {0});- }-- ov::OutputVector vec = {q_node, k_node, v_node};- // 3 is the min supported rank according to the SDPA spec- size_t supported_rank = std::max(mask_input.get_partial_shape().rank().get_length(), static_cast<int64_t>(3));- for (size_t i = 0; i < vec.size(); ++i) {- auto pshape = vec[i].get_partial_shape();- if (pshape.rank().is_dynamic()) {- return false;- }- // align all inputs- supported_rank = std::max(pshape.size(), supported_rank);- }+ std::shared_ptr<ov::Node> scale_node;+ if (!(scale_node = get_scale(attn_scale, T, m)))+ return false;- for (size_t i = 0; i < vec.size(); ++i) {- auto pshape = vec[i].get_partial_shape();- int diff = static_cast<int>(supported_rank - pshape.size());- if (diff > 0) {- std::vector<size_t> axes(diff, 0);- std::iota(axes.begin(), axes.end(), 0);- auto axes_node = v0::Constant::create(ov::element::i64, ov::Shape{static_cast<size_t>(diff)}, axes);- auto reshape = std::make_shared<v0::Unsqueeze>(vec[i], axes_node);- vec[i] = reshape;- ov::copy_runtime_info(m.get_matched_nodes(), {reshape, axes_node});- }+ std::shared_ptr<ov::Node> mask_input;+ if (!(mask_input = get_mask(mask, opt_mask_add, T, mask_present, m)))+ return false;- if (i == 1 && !matmul_trasposes_k) {- // Transpose k- pshape = vec[i].get_partial_shape();- std::vector<int> axes_values(pshape.size());- std::iota(axes_values.begin(), axes_values.end(), 0);- std::swap(axes_values[axes_values.size() - 1], axes_values[axes_values.size() - 2]);- auto axes = v0::Constant::create(ov::element::i64, {axes_values.size()}, axes_values);- vec[i] = std::make_shared<v1::Transpose>(vec[i], axes);- ov::copy_runtime_info(m.get_matched_nodes(), {axes, vec[i].get_node_shared_ptr()});- }- }+ ov::OutputVector qkv = get_qkv({q_node, k_node, v_node}, mask_input, T, matmul_transposes_k, m);+ if (qkv.size() == 0)+ return false;bool sinks_present = pm.count(sinks);- std::shared_ptr<ov::Node> sdpa = sinks_present ? std::make_shared<v13::ScaledDotProductAttention>(vec[0],- vec[1],- vec[2],+ std::shared_ptr<ov::Node> sdpa = sinks_present ? std::make_shared<v13::ScaledDotProductAttention>(qkv[0],+ qkv[1],+ qkv[2],mask_input,scale_node,pm.at(sinks),false)- : std::make_shared<v13::ScaledDotProductAttention>(vec[0],- vec[1],- vec[2],+ : std::make_shared<v13::ScaledDotProductAttention>(qkv[0],+ qkv[1],+ qkv[2],mask_input,scale_node,false);diff --git a/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp b/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cppindex 5510ecd3d67..e7824c83028 100644--- a/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp+++ b/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp@@ -20,6 +20,7 @@#include "openvino/op/reduce_max.hpp"#include "openvino/op/reshape.hpp"#include "openvino/op/scaled_dot_product_attention.hpp"+#include "openvino/op/slice.hpp"#include "openvino/op/softmax.hpp"#include "openvino/op/split.hpp"#include "openvino/op/squeeze.hpp"@@ -37,6 +38,7 @@ using namespace ov::pass;using namespace ov::element;enum class InputType : int { Q, K, V, SDPA };+enum class SinksSliceType : int { None, Slice, StridedSlice };class SDPA {public:@@ -62,10 +64,13 @@ public:m_scale = new_scale;}- void set_sinks(const PartialShape& sinks_shape, const std::vector<size_t>& sinks_broadcast_shape) {+ void set_sinks(const PartialShape& sinks_shape,+ const std::vector<size_t>& sinks_broadcast_shape,+ SinksSliceType sinks_slice_type = SinksSliceType::None) {// Adjust the code for sinks if you see other values for them.// For now, there has been only one model with sinks: gpt-osswith_sinks = true;+ m_sinks_slice_type = sinks_slice_type;std::vector<size_t> broadcast_shape_value(sinks_broadcast_shape.begin(), sinks_broadcast_shape.end());auto broadcast_to_shape =v0::Constant::create(element::i32, Shape{sinks_broadcast_shape.size()}, broadcast_shape_value);@@ -153,25 +158,34 @@ public:}std::shared_ptr<ov::Node> softmax = make_shared<op::v8::Softmax>(attn_scores_with_mask, softmax_axis);- if (with_sinks) {+ if (with_sinks && m_sinks_slice_type != SinksSliceType::None) {// Adjust the code for sinks if you see other values for them.// For now, there has been only one model with sinks: gpt-oss- std::vector<int> start(m_sinks_rank, 0);- std::vector<int> stop(m_sinks_rank, 0);- std::vector<int> step(m_sinks_rank, 1);- stop[stop.size() - 1] = -1;-- std::vector<int64_t> begin_mask(m_sinks_rank, 1);- std::vector<int64_t> end_mask(m_sinks_rank, 1);- begin_mask[begin_mask.size() - 1] = 0;- end_mask[end_mask.size() - 1] = 0;-- softmax = make_shared<op::v1::StridedSlice>(softmax,- v0::Constant::create(element::i64, Shape{m_sinks_rank}, start),- v0::Constant::create(element::i64, Shape{m_sinks_rank}, stop),- v0::Constant::create(element::i64, Shape{m_sinks_rank}, step),- begin_mask,- end_mask);+ if (m_sinks_slice_type == SinksSliceType::Slice) {+ softmax = make_shared<op::v8::Slice>(softmax,+ v0::Constant::create(element::i64, Shape{1}, {0}),+ v0::Constant::create(element::i64, Shape{1}, {-1}),+ v0::Constant::create(element::i64, Shape{1}, {1}),+ v0::Constant::create(element::i64, Shape{1}, {m_sinks_rank - 1}));+ } else {+ std::vector<int> start(m_sinks_rank, 0);+ std::vector<int> stop(m_sinks_rank, 0);+ std::vector<int> step(m_sinks_rank, 1);+ stop[stop.size() - 1] = -1;++ std::vector<int64_t> begin_mask(m_sinks_rank, 1);+ std::vector<int64_t> end_mask(m_sinks_rank, 1);+ begin_mask[begin_mask.size() - 1] = 0;+ end_mask[end_mask.size() - 1] = 0;++ softmax =+ make_shared<op::v1::StridedSlice>(softmax,+ v0::Constant::create(element::i64, Shape{m_sinks_rank}, start),+ v0::Constant::create(element::i64, Shape{m_sinks_rank}, stop),+ v0::Constant::create(element::i64, Shape{m_sinks_rank}, step),+ begin_mask,+ end_mask);+ }}auto output = make_shared<op::v0::MatMul>(softmax, nodes[InputType::V]);@@ -258,6 +272,7 @@ private:bool with_mask = false;bool with_scale = false;bool with_sinks = false;+ SinksSliceType m_sinks_slice_type = SinksSliceType::None;bool causal = false;element::Type m_type = f32;@@ -898,13 +913,62 @@ TEST_F(TransformationTestsF, SDPAFusionTest12) {TEST_F(TransformationTestsF, SDPAFusionTest13) {const PartialShape query_shape{1, 64, 1, 64};- const PartialShape key_shape{1, 8, 8, 1, 64};- const PartialShape value_shape{1, 8, 8, 1, 64};+ const PartialShape key_shape{1, 8, 8, 3, 64};+ const PartialShape value_shape{1, 8, 8, 3, 64};++ const PartialShape mask_shape{1, 1, 1, 3};++ const Shape key_reshaped{1, 64, 3, 64};+ const Shape value_reshaped{1, 64, 3, 64};++ const Shape sinks_shape{1, 64, 1, 1};+ std::vector<size_t> sinks_broadcast_shape{1, 64, 1, 1};++ const Shape final_order{0, 2, 1, 3};++ SDPA sdpa(f16, query_shape, key_shape, value_shape);+ SDPA sdpa_ref(f16, query_shape, key_shape, value_shape);++ {+ sdpa.set_mask(mask_shape);+ sdpa.set_scale(0.125f);+ sdpa.set_sinks(sinks_shape, sinks_broadcast_shape, SinksSliceType::Slice);++ sdpa.reshape_k(key_reshaped);+ sdpa.reshape_v(value_reshaped);++ sdpa.create_pattern_sdpa(true);+ sdpa.transpose_sdpa(final_order);+ model = sdpa.build_model();+ manager.register_pass<ov::pass::SDPAFusion>();+ }++ {+ sdpa_ref.set_mask(mask_shape);+ sdpa_ref.set_scale(0.125f);+ sdpa_ref.set_sinks(sinks_shape, sinks_broadcast_shape);++ sdpa_ref.reshape_k(key_reshaped);+ sdpa_ref.reshape_v(value_reshaped);++ sdpa_ref.create_reference_sdpa();+ sdpa_ref.transpose_sdpa(final_order);+ model_ref = sdpa_ref.build_model();+ }++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);+}++TEST_F(TransformationTestsF, SDPAFusionTest14) {+ const PartialShape query_shape{1, 64, 1, 64};+ const PartialShape key_shape{1, 8, 8, 3, 64};+ const PartialShape value_shape{1, 8, 8, 3, 64};- const PartialShape mask_shape{1, 1, 1, 1};+ const PartialShape mask_shape{1, 1, 1, 3};- const Shape key_reshaped{1, 64, 1, 64};- const Shape value_reshaped{1, 64, 1, 64};+ const Shape key_reshaped{1, 64, 3, 64};+ const Shape value_reshaped{1, 64, 3, 64};const Shape sinks_shape{1, 64, 1, 1};std::vector<size_t> sinks_broadcast_shape{1, 64, 1, 1};@@ -917,7 +981,7 @@ TEST_F(TransformationTestsF, SDPAFusionTest13) {{sdpa.set_mask(mask_shape);sdpa.set_scale(0.125f);- sdpa.set_sinks(sinks_shape, sinks_broadcast_shape);+ sdpa.set_sinks(sinks_shape, sinks_broadcast_shape, SinksSliceType::StridedSlice);sdpa.reshape_k(key_reshaped);sdpa.reshape_v(value_reshaped);