Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Perf optimization in SDPA decomposition (#30533)

Ivan Tikhonove826ea32d9e4
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.cpp
index d6d17f657a3..f773f2764ba 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
@@ -33,6 +33,36 @@
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "transformations/utils/utils.hpp"
+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<ov::op::v1::Multiply>(query, scale);
+ auto scaled_attn = std::make_shared<ov::op::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;
+}
+
+} // namespace
+
ov::pass::ScaledDotProductAttentionDecomposition::ScaledDotProductAttentionDecomposition() {
MATCHER_SCOPE(ScaledDotProductAttentionDecomposition);
auto pattern_node = ov::pass::pattern::wrap_type<ov::op::v13::ScaledDotProductAttention>();
@@ -94,7 +124,6 @@ std::shared_ptr<ov::Node> ov::pass::ScaledDotProductAttentionDecomposition::deco
scale = node->input_value(4);
}
- auto q_scaled = register_new_node<v1::Multiply>(query, scale);
auto k_rank = register_new_node<v3::ShapeOf>(k_shape, element::i32)->output(0);
auto k_last_dim = register_new_node<v1::Add>(k_rank, minus_one);
auto k_next_dim = register_new_node<v1::Add>(k_rank, minus_two)->output(0);
@@ -108,7 +137,16 @@ std::shared_ptr<ov::Node> ov::pass::ScaledDotProductAttentionDecomposition::deco
auto transpose_dims =
register_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);
- auto scaled_atten = register_new_node<v0::MatMul>(q_scaled, k_transposed)->output(0);
+
+ 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);
+ }
+
minus_inf = register_new_node<v1::ConvertLike>(minus_inf, scaled_atten);
if (node->get_causal() || node->get_input_size() > 3) {
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.cpp
index f319c3e1466..7c2dc7e01ab 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
@@ -36,7 +36,8 @@ const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::
std::shared_ptr<ov::Node> value,
std::shared_ptr<ov::Node> attention_mask,
std::shared_ptr<ov::Node> scale,
- bool casual);
+ bool casual,
+ bool scale_after_matmul = false);
TEST_F(TransformationTestsF, ScaledDotProductAttentionDecompositionStaticBasic) {
const PartialShape query_shape{1, 32, 32};
@@ -122,7 +123,7 @@ TEST_F(TransformationTestsF, ScaledDotProductAttentionDecompositionStaticBroadca
{
const auto scaled_dot_product_attention =
- scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual);
+ scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, true);
model_ref = std::make_shared<ov::Model>(OutputVector{scaled_dot_product_attention},
ParameterVector{query, key, value, attention_mask, scale});
}
@@ -186,12 +187,72 @@ TEST_F(TransformationTestsF, ScaledDotProductAttentionDecompositionDynamic) {
}
}
+TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_ScalarScale_MultiplyAfterMatMul) {
+ const PartialShape query_shape{1, 32, 64};
+ const PartialShape key_shape{1, 32, 64};
+ const PartialShape value_shape{1, 32, 64};
+ const PartialShape attention_mask_shape{1, 32, 32};
+
+ // Constant scalar scale
+ auto scale = ov::op::v0::Constant::create(element::f32, Shape{}, {0.125f});
+
+ auto query = std::make_shared<ov::op::v0::Parameter>(element::f32, query_shape);
+ auto key = std::make_shared<ov::op::v0::Parameter>(element::f32, key_shape);
+ auto value = std::make_shared<ov::op::v0::Parameter>(element::f32, value_shape);
+ auto attention_mask = std::make_shared<ov::op::v0::Parameter>(element::f32, attention_mask_shape);
+
+ const bool casual = false;
+
+ {
+ auto sdp =
+ std::make_shared<ov::op::v13::ScaledDotProductAttention>(query, key, value, attention_mask, scale, casual);
+ model = std::make_shared<ov::Model>(OutputVector{sdp}, ParameterVector{query, key, value, attention_mask});
+ manager.register_pass<ov::pass::ScaledDotProductAttentionDecomposition>();
+ }
+
+ {
+ auto ref = scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, true);
+ model_ref = std::make_shared<ov::Model>(OutputVector{ref}, ParameterVector{query, key, value, attention_mask});
+ }
+}
+
+TEST_F(TransformationTestsF, ScaledDotProductAttentionDecomposition_DynamicScale_MultiplyBeforeMatMul) {
+ const PartialShape query_shape{-1, -1, 64};
+ const PartialShape key_shape{-1, -1, 64};
+ const PartialShape value_shape{-1, -1, 64};
+ const PartialShape attention_mask_shape{-1, -1, -1};
+ const PartialShape scale_shape{1};
+
+ auto scale = std::make_shared<ov::op::v0::Parameter>(element::f32, scale_shape);
+ auto query = std::make_shared<ov::op::v0::Parameter>(element::f32, query_shape);
+ auto key = std::make_shared<ov::op::v0::Parameter>(element::f32, key_shape);
+ auto value = std::make_shared<ov::op::v0::Parameter>(element::f32, value_shape);
+ auto attention_mask = std::make_shared<ov::op::v0::Parameter>(element::f32, attention_mask_shape);
+
+ const bool casual = false;
+
+ {
+ auto sdp =
+ std::make_shared<ov::op::v13::ScaledDotProductAttention>(query, key, value, attention_mask, scale, casual);
+ model =
+ std::make_shared<ov::Model>(OutputVector{sdp}, ParameterVector{query, key, value, attention_mask, scale});
+ manager.register_pass<ov::pass::ScaledDotProductAttentionDecomposition>();
+ }
+
+ {
+ auto ref = scaled_dot_product_attention_decomposition(query, key, value, attention_mask, scale, casual, false);
+ model_ref =
+ std::make_shared<ov::Model>(OutputVector{ref}, ParameterVector{query, key, value, attention_mask, scale});
+ }
+}
+
const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::shared_ptr<ov::Node> query,
std::shared_ptr<ov::Node> key,
std::shared_ptr<ov::Node> value,
std::shared_ptr<ov::Node> attention_mask,
std::shared_ptr<ov::Node> scale,
- bool casual) {
+ bool casual,
+ bool scale_after_matmul) {
const auto q_shape = std::make_shared<ov::op::v3::ShapeOf>(query, element::i32);
const auto k_shape = std::make_shared<ov::op::v3::ShapeOf>(key, element::i32);
const auto minus_one = ov::op::v0::Constant::create(element::i32, Shape{}, {-1});
@@ -218,7 +279,6 @@ const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::
auto sqrt_scale = std::make_shared<ov::op::v0::Sqrt>(scale);
scale = std::make_shared<ov::op::v1::Divide>(one_f, sqrt_scale);
}
- const auto q_scaled = std::make_shared<ov::op::v1::Multiply>(query, scale);
auto k_rank = std::make_shared<ov::op::v3::ShapeOf>(k_shape, element::i32)->output(0);
const auto k_last_dim = std::make_shared<ov::op::v1::Add>(k_rank, minus_one);
const auto k_next_dim = std::make_shared<ov::op::v1::Add>(k_rank, minus_two)->output(0);
@@ -232,7 +292,15 @@ const std::shared_ptr<ov::Node> scaled_dot_product_attention_decomposition(std::
const auto transpose_dims =
std::make_shared<ov::op::v0::Concat>(OutputVector{k_dims_before_transpose, k_last_dim, k_next_dim}, 0);
const auto k_transposed = std::make_shared<ov::op::v1::Transpose>(key, transpose_dims);
- auto scaled_atten = std::make_shared<ov::op::v0::MatMul>(q_scaled, k_transposed)->output(0);
+
+ Output<Node> scaled_atten;
+ if (scale_after_matmul) {
+ const auto atten = std::make_shared<ov::op::v0::MatMul>(query, k_transposed)->output(0);
+ scaled_atten = std::make_shared<ov::op::v1::Multiply>(atten, scale);
+ } else {
+ const auto q_scaled = std::make_shared<ov::op::v1::Multiply>(query, scale);
+ scaled_atten = std::make_shared<ov::op::v0::MatMul>(q_scaled, k_transposed)->output(0);
+ }
minus_inf = std::make_shared<ov::op::v1::ConvertLike>(minus_inf, scaled_atten);
Output<Node> mask;