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] Apply SDPA scale after MatMul(Q, K^T) per specification (#34177)
Evgeny Kotov6eb738bb3040
diff --git a/src/common/transformations/src/transformations/op_conversions/scaled_dot_product_attention_decomposition.cpp b/src/common/transformations/src/transformations/op_conversions/scaled_dot_product_attention_decomposition.cppindex 1cce88954de..eaa4774e737 100644--- a/src/common/transformations/src/transformations/op_conversions/scaled_dot_product_attention_decomposition.cpp+++ b/src/common/transformations/src/transformations/op_conversions/scaled_dot_product_attention_decomposition.cpp@@ -43,35 +43,6 @@ namespace v3 = ov::op::v3;namespace v4 = ov::op::v4;namespace v8 = ov::op::v8;namespace v13 = ov::op::v13;-namespace {--bool can_move_scale_after_matmul(const ov::Output<ov::Node>& query,- const ov::Output<ov::Node>& kT,- const ov::Output<ov::Node>& scale) {- const auto& scale_pshape = scale.get_partial_shape();- const auto& query_pshape = query.get_partial_shape();- if (scale_pshape.is_dynamic() || query_pshape.is_dynamic()) {- return false;- }-- // According to the ov SDPA specification, the scale input have to be 1d with 1 element- // or scalar.- if (ov::shape_size(scale_pshape.to_shape()) != 1) {- return false;- }-- // using the original implementation to calculate the shapes.- // we need to move the scale after MatMul only if the tensor after MatMul is smaller.- auto q_scaled = std::make_shared<v1::Multiply>(query, scale);- auto scaled_attn = std::make_shared<v0::MatMul>(q_scaled, kT);- const auto& scaled_attn_pshape = scaled_attn->output(0).get_partial_shape();- if (scaled_attn_pshape.is_static()) {- return ov::shape_size(query_pshape.to_shape()) > ov::shape_size(scaled_attn_pshape.to_shape());- }- return false;-}--} // namespaceov::pass::ScaledDotProductAttentionDecomposition::ScaledDotProductAttentionDecomposition() {MATCHER_SCOPE(ScaledDotProductAttentionDecomposition);@@ -153,14 +124,11 @@ std::shared_ptr<ov::Node> ov::pass::ScaledDotProductAttentionDecomposition::decoregister_new_node<v0::Concat>(OutputVector{k_dims_before_transpose, k_last_dim, k_next_dim}, 0);auto k_transposed = register_new_node<v1::Transpose>(key, transpose_dims);- ov::Output<Node> scaled_atten;- if (can_move_scale_after_matmul(query, k_transposed, scale)) {- auto atten = register_new_node<v0::MatMul>(query, k_transposed)->output(0);- scaled_atten = register_new_node<v1::Multiply>(atten, scale)->output(0);- } else {- auto q_scaled = register_new_node<v1::Multiply>(query, scale);- scaled_atten = register_new_node<v0::MatMul>(q_scaled, k_transposed)->output(0);- }+ // Apply scale after MatMul(Q, K^T) per SDPA specification:+ // attn_weight = Q @ K^T * scale+ // Scale is scalar or single-element tensor, so Multiply broadcasts safely over [S_q, S_kv].+ auto atten = register_new_node<v0::MatMul>(query, k_transposed)->output(0);+ auto scaled_atten = register_new_node<v1::Multiply>(atten, scale)->output(0);minus_inf = register_new_node<v1::ConvertLike>(minus_inf, scaled_atten);diff --git a/src/common/transformations/tests/op_conversions/scaled_dot_product_decomposition_test.cpp b/src/common/transformations/tests/op_conversions/scaled_dot_product_decomposition_test.cppindex 7ef98a33aa1..b7089dc7434 100644--- a/src/common/transformations/tests/op_conversions/scaled_dot_product_decomposition_test.cpp+++ b/src/common/transformations/tests/op_conversions/scaled_dot_product_decomposition_test.cpp@@ -46,7 +46,6 @@ const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::std::shared_ptr<ov::Node> attention_mask,std::shared_ptr<ov::Node> scale,bool casual,- bool scale_after_matmul = false,std::shared_ptr<ov::Node> sinks = nullptr);TEST_F(TransformationTestsF, ScaledDotProductAttentionDecompositionStaticBasic) {@@ -133,7 +132,7 @@ TEST_F(TransformationTestsF, ScaledDotProductAttentionDecompositionStaticBroadca{const auto scaled_dot_product_attention =- scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, true);+ scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual);model_ref = std::make_shared<ov::Model>(OutputVector{scaled_dot_product_attention},ParameterVector{query, key, value, attention_mask, scale});}@@ -220,12 +219,12 @@ TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_ScalarScale_}{- auto ref = scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, true);+ auto ref = scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual);model_ref = std::make_shared<ov::Model>(OutputVector{ref}, ParameterVector{query, key, value, attention_mask});}}-TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_DynamicScale_MultiplyBeforeMatMul) {+TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_DynamicScale) {const PartialShape query_shape{-1, -1, 64};const PartialShape key_shape{-1, -1, 64};const PartialShape value_shape{-1, -1, 64};@@ -248,7 +247,7 @@ TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_DynamicScale}{- auto ref = scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, false);+ auto ref = scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual);model_ref =std::make_shared<ov::Model>(OutputVector{ref}, ParameterVector{query, key, value, attention_mask, scale});}@@ -260,7 +259,6 @@ const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::std::shared_ptr<ov::Node> attention_mask,std::shared_ptr<ov::Node> scale,bool casual,- bool scale_after_matmul,std::shared_ptr<ov::Node> sinks) {const auto q_shape = std::make_shared<v3::ShapeOf>(query, element::i32);const auto k_shape = std::make_shared<v3::ShapeOf>(key, element::i32);@@ -300,14 +298,8 @@ const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::std::make_shared<v0::Concat>(OutputVector{k_dims_before_transpose, k_last_dim, k_next_dim}, 0);const auto k_transposed = std::make_shared<v1::Transpose>(key, transpose_dims);- Output<Node> scaled_atten;- if (scale_after_matmul) {- const auto atten = std::make_shared<v0::MatMul>(query, k_transposed)->output(0);- scaled_atten = std::make_shared<v1::Multiply>(atten, scale);- } else {- const auto q_scaled = std::make_shared<v1::Multiply>(query, scale);- scaled_atten = std::make_shared<v0::MatMul>(q_scaled, k_transposed)->output(0);- }+ const auto atten = std::make_shared<v0::MatMul>(query, k_transposed)->output(0);+ Output<Node> scaled_atten = std::make_shared<v1::Multiply>(atten, scale);minus_inf = std::make_shared<v1::ConvertLike>(minus_inf, scaled_atten);Output<Node> mask;@@ -361,6 +353,49 @@ const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::return result;}+TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_PreScaledQuery) {+ // Scale is always applied after MatMul(Q, K^T) per SDPA specification,+ // regardless of whether Q is pre-scaled.+ // This produces: (Q_prescaled @ K^T) * scale.+ const PartialShape raw_query_shape{1, 6, 101, 64};+ const PartialShape key_shape{1, 6, 101, 64};+ const PartialShape value_shape{1, 6, 101, 64};+ const PartialShape attention_mask_shape{1, 6, 101, 101};++ auto raw_query = std::make_shared<v0::Parameter>(element::f32, raw_query_shape);+ auto key = std::make_shared<v0::Parameter>(element::f32, key_shape);+ auto value = std::make_shared<v0::Parameter>(element::f32, value_shape);+ auto attention_mask = std::make_shared<v0::Parameter>(element::f32, attention_mask_shape);++ // Q is pre-scaled by a scalar constant (as SDPAFusion leaves it)+ auto q_scale_const = v0::Constant::create(element::f32, Shape{}, {0.353553f});+ auto query_prescaled = std::make_shared<v1::Multiply>(raw_query, q_scale_const);++ // SDPA scale = the K-side scale that was absorbed by SDPAFusion+ auto sdpa_scale = v0::Constant::create(element::f32, Shape{}, {0.353553f});++ const bool casual = false;++ {+ auto sdpa = std::make_shared<v13::ScaledDotProductAttention>(query_prescaled,+ key,+ value,+ attention_mask,+ sdpa_scale,+ casual);+ model = std::make_shared<ov::Model>(OutputVector{sdpa}, ParameterVector{raw_query, key, value, attention_mask});+ manager.register_pass<ov::pass::ScaledDotProductAttentionDecomposition>();+ }++ {+ // Expected: scale applied after MatMul(Q, K^T) per SDPA spec+ auto ref =+ scaled_dot_product_attention_decomposition(query_prescaled, key, value, attention_mask, sdpa_scale, casual);+ model_ref =+ std::make_shared<ov::Model>(OutputVector{ref}, ParameterVector{raw_query, key, value, attention_mask});+ }+}+TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_Sinks) {const PartialShape query_shape{-1, 64, -1, 64};const PartialShape key_shape{-1, 64, -1, 64};@@ -387,8 +422,7 @@ TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_Sinks) {}{- auto ref =- scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, false, sinks);+ auto ref = scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, sinks);model_ref = std::make_shared<ov::Model>(OutputVector{ref},ParameterVector{query, key, value, attention_mask, scale, sinks});}