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 SDPAReshapeFusion with reshape output shape (#32261)
Mikhail Ryzhov4b848e77bca4
diff --git a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cppindex ac74ebc342e..9f65a11e06c 100644--- a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp@@ -108,9 +108,10 @@ SDPAReshapeFusion::SDPAReshapeFusion() {using namespace ov::op;using namespace ov::pass::pattern;- auto q = any_input(shape_matches("Batches..., S_q, D") && rank_more_than(2));+ auto q = any_input(shape_matches("Batches..., S_q, D"));auto k = any_input(shape_matches("AnyLayout...") && rank_more_than(2));- auto v = any_input(shape_matches("Batches..., S_kv, D") && check_layout("AnyLayout") && rank_more_than(2));+ auto v = any_input(shape_matches("Batches..., S_kv, D") && check_layout("AnyLayout"));+ auto mask = any_input(has_static_rank());// these Reshape/Unsqueeze may already exist in the graphauto unsq_q = wrap_type<v1::Reshape, v0::Unsqueeze>({q, any_input()});@@ -126,21 +127,19 @@ SDPAReshapeFusion::SDPAReshapeFusion() {auto opt_unsq_v = optional<v1::Reshape, v0::Unsqueeze>({unsq_v, any_input()});// this Transpose may be inserted by SDPAFusionMatcher- auto opt_transpose_k =- optional<v1::Transpose>({opt_unsq_k, any_input()}, shape_matches("..., S_kv, D") && rank_more_than(2));+ auto opt_transpose_k = optional<v1::Transpose>({opt_unsq_k, any_input()}, shape_matches("..., S_kv, D"));- auto sdpa = wrap_type<v13::ScaledDotProductAttention>({+ auto sdpa_pattern = wrap_type<v13::ScaledDotProductAttention>({opt_unsq_q,opt_transpose_k,opt_unsq_v,- any_input(),+ mask,any_input(),});- auto opt_sdpa_reshape = optional<v1::Reshape, v0::Unsqueeze>({sdpa->output(0), any_input()});- auto opt_sdpa_transpose = optional<v1::Transpose>({opt_sdpa_reshape, any_input()});- auto post_sdpa = wrap_type<v1::Reshape, v0::Unsqueeze>({opt_sdpa_transpose, any_input()},- shape_matches("Batches..., S_q, D") && rank_more_than(2));+ auto opt_sdpa_reshape = optional<v1::Reshape, v0::Unsqueeze>({sdpa_pattern, any_input()});+ auto post_sdpa =+ wrap_type<v1::Reshape, v0::Unsqueeze>({opt_sdpa_reshape, any_input()}, shape_matches("Batches..., S_q, D"));ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {const auto& pm = m.get_pattern_value_map();@@ -148,9 +147,18 @@ SDPAReshapeFusion::SDPAReshapeFusion() {auto q_node = pm.at(q);auto k_node = pm.at(k);auto v_node = pm.at(v);- auto sdpa_node = pm.at(sdpa).get_node_shared_ptr();+ auto mask_node = pm.at(mask);auto post_sdpa_node = pm.at(post_sdpa).get_node_shared_ptr();+ auto sdpa = ov::as_type_ptr<ov::op::v13::ScaledDotProductAttention>(pm.at(sdpa_pattern).get_node_shared_ptr());++ // The mask will be ignored if causal is true; otherwise, the mask rank should be less than or equal to the SDPA+ // input rank.+ if (sdpa && !sdpa->get_causal() &&+ mask_node.get_partial_shape().rank().get_length() > q_node.get_partial_shape().rank().get_length()) {+ return false;+ }+const auto& sm = m.get_symbols();auto symbols_to_find = sm.at("Batches").g();@@ -185,11 +193,14 @@ SDPAReshapeFusion::SDPAReshapeFusion() {ov::copy_runtime_info(m.get_matched_nodes(), {shape_of_v, k_node.get_node_shared_ptr()});}}- auto new_sdpa_node = sdpa_node->clone_with_new_inputs(- {q_node, k_node, v_node, sdpa_node->input(3).get_source_output(), sdpa_node->input(4).get_source_output()});++ auto new_sdpa_node = sdpa->clone_with_new_inputs(+ {q_node, k_node, v_node, sdpa->input(3).get_source_output(), sdpa->input(4).get_source_output()});+new_sdpa_node->set_friendly_name(post_sdpa_node->get_friendly_name());- ov::copy_runtime_info(m.get_matched_nodes(), new_sdpa_node);+ ov::copy_runtime_info(post_sdpa_node, new_sdpa_node);ov::replace_node(post_sdpa_node, new_sdpa_node);+return true;};diff --git a/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp b/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cppindex e7824c83028..7adb23161ed 100644--- a/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp+++ b/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp@@ -193,7 +193,7 @@ public:}// fused ScaledDotProductAttention op- void create_reference_sdpa() {+ void create_reference_sdpa(bool causal = false) {auto scale_const = op::v0::Constant::create(m_type, Shape{}, {m_scale});shared_ptr<Node> mask_input = m_mask;@@ -273,7 +273,6 @@ private:bool with_scale = false;bool with_sinks = false;SinksSliceType m_sinks_slice_type = SinksSliceType::None;- bool causal = false;element::Type m_type = f32;@@ -1102,6 +1101,96 @@ TEST_F(TransformationTestsF, SDPAFusionTest_ReshapeOptimization) {comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);}+TEST_F(TransformationTestsF, SDPAFusionTest_ReshapeOptimizationWithMask) {+ // Init.+ const PartialShape query_shape{2, 49, 52};+ const PartialShape key_shape{2, 49, 52};+ const PartialShape value_shape{2, 49, 52};+ const PartialShape mask_shape{-1, 1, 49, 49};+ const Shape reshape_shape_4d{1, 2, 49, 52};+ const Shape reshape_shape_3d{2, 49, 52};++ SDPA sdpa(f16, query_shape, key_shape, value_shape);+ SDPA sdpa_ref(f16, query_shape, key_shape, value_shape);++ // SDPA model.+ {+ sdpa.reshape_q(reshape_shape_4d);+ sdpa.reshape_k(reshape_shape_4d);+ sdpa.reshape_v(reshape_shape_4d);+ sdpa.set_mask(mask_shape);++ sdpa.create_pattern_sdpa(true);++ sdpa.reshape_sdpa(reshape_shape_4d);+ sdpa.reshape_sdpa(reshape_shape_3d);++ model = sdpa.build_model();++ manager.register_pass<ov::pass::SDPAFusion>();+ }++ // SDPA reference model.+ {+ sdpa_ref.reshape_q(reshape_shape_4d);+ sdpa_ref.reshape_k(reshape_shape_4d);+ sdpa_ref.reshape_v(reshape_shape_4d);+ sdpa_ref.set_mask(mask_shape);++ sdpa_ref.create_reference_sdpa();++ sdpa_ref.reshape_sdpa(reshape_shape_4d);+ sdpa_ref.reshape_sdpa(reshape_shape_3d);++ model_ref = sdpa_ref.build_model();+ }++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);+}++TEST_F(TransformationTestsF, SDPAFusionTest_ReshapeOptimizationWithMaskCausal) {+ // Init.+ const PartialShape query_shape{2, 49, 52};+ const PartialShape key_shape{2, 49, 52};+ const PartialShape value_shape{2, 49, 52};+ const PartialShape mask_shape{-1, 1, 49, 49};+ const Shape reshape_shape_4d{1, 2, 49, 52};+ const Shape reshape_shape_3d{2, 49, 52};++ SDPA sdpa_ref(f16, query_shape, key_shape, value_shape);+ SDPA sdpa(f16, query_shape, key_shape, value_shape);++ // SDPA model.+ {+ sdpa.reshape_q(reshape_shape_4d);+ sdpa.reshape_k(reshape_shape_4d);+ sdpa.reshape_v(reshape_shape_4d);+ sdpa.set_mask(mask_shape);++ sdpa.create_reference_sdpa(/*causal=*/true);++ sdpa.reshape_sdpa(reshape_shape_4d);+ sdpa.reshape_sdpa(reshape_shape_3d);++ model = sdpa.build_model();++ manager.register_pass<ov::pass::SDPAFusion>();+ }++ // SDPA reference model.+ {+ sdpa_ref.set_mask(mask_shape);++ sdpa_ref.create_reference_sdpa(/*causal=*/true);++ model_ref = sdpa_ref.build_model();+ }++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);+}+TEST_F(TransformationTestsF, SDPAFusionTest_4dAttentionMaskWithBatch2) {// Init.int64_t batch = 2;