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
Fix SDPAFusion: attention mask rank > inputs ranks (#31287)
Ivan Tikhonova2cd69c0eff9
diff --git a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cppindex 636dc57ae01..f360c8f3136 100644--- a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp@@ -357,14 +357,15 @@ SDPAFusionMatcher::SDPAFusionMatcher() {}ov::OutputVector vec = {q_node, k_node, v_node};- int supported_rank = 3; // this is the min supported rank according to the SDPA spec+ // 3 is the min supported rank according to the SDPA spec+ int64_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(static_cast<int>(pshape.size()), supported_rank);+ supported_rank = std::max(static_cast<int64_t>(pshape.size()), supported_rank);}for (size_t i = 0; i < vec.size(); ++i) {diff --git a/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp b/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cppindex 9792c1cde65..09320ed80bd 100644--- a/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp+++ b/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp@@ -812,6 +812,52 @@ TEST_F(TransformationTestsF, SDPAFusionTest_ReshapeOptimization) {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;+ const PartialShape query_shape{batch, 49, 52};+ const PartialShape key_shape{batch, 49, 52};+ const PartialShape value_shape{batch, 49, 52};++ SDPA sdpa(f16, query_shape, key_shape, value_shape);+ SDPA sdpa_ref(f16, query_shape, key_shape, value_shape);++ // Preprocessing callback.+ auto callback = [](auto& nodes) {+ int64_t axis = 0;+ auto axes_node = v0::Constant::create(ov::element::i64, ov::Shape{1}, {axis});+ nodes[InputType::Q] = std::make_shared<v0::Unsqueeze>(nodes[InputType::Q], axes_node)->output(0);++ auto axes_node1 = v0::Constant::create(ov::element::i64, ov::Shape{1}, {axis});+ nodes[InputType::K] = std::make_shared<v0::Unsqueeze>(nodes[InputType::K], axes_node1)->output(0);++ auto axes_node2 = v0::Constant::create(ov::element::i64, ov::Shape{1}, {axis});+ nodes[InputType::V] = std::make_shared<v0::Unsqueeze>(nodes[InputType::V], axes_node2)->output(0);+ };++ // SDPA model.+ {+ sdpa.set_mask({batch, 1, 1, 49});+ sdpa.create_pattern_sdpa(true);++ model = sdpa.build_model();++ manager.register_pass<ov::pass::SDPAFusion>();+ }++ // SDPA reference model.+ {+ sdpa_ref.set_mask({batch, 1, 1, 49});+ sdpa_ref.set_preprocessing_callback(callback);+ sdpa_ref.create_reference_sdpa();++ model_ref = sdpa_ref.build_model();+ }+comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);}\ No newline at end of file