Skip to content

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

[TRANSFORMATIONS] Align fused SDPA output (#34097)

Mikhail Ryzhov88251a96ae68
diff --git a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp
index 031741fa2f1..52e526dc627 100644
--- a/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp
@@ -97,6 +97,52 @@ ov::pass::pattern::op::Predicate check_layout(const std::string& layout) {
return !order.empty();
});
};
+
+std::shared_ptr<ov::Node> try_align_outputs(const std::shared_ptr<ov::Node>& src,
+ const std::shared_ptr<ov::Node>& dst) {
+ // skip alignment if node connected to Reshape node
+ auto dst_inputs = dst->get_output_target_inputs(0);
+ if (dst_inputs.size() == 1 && ov::is_type<v1::Reshape>(dst_inputs.begin()->get_node())) {
+ return src;
+ }
+
+ auto src_shape = src->get_output_partial_shape(0);
+ auto dst_shape = dst->get_output_partial_shape(0);
+ if (src_shape.compatible(dst_shape)) {
+ return src;
+ }
+
+ // if shapes are not compatible, we can try to insert an Unsqueeze/Squeeze to align them
+ std::shared_ptr<ov::Node> reshape = nullptr;
+ auto src_rank = src_shape.rank();
+ auto dst_rank = dst_shape.rank();
+
+ int64_t rank_diff = dst_rank.get_length() - src_rank.get_length();
+ std::vector<int64_t> axes(std::abs(rank_diff));
+ std::iota(axes.begin(), axes.end(), 0);
+ auto axes_const = v0::Constant::create(ov::element::i64, ov::Shape{axes.size()}, axes);
+
+ if (rank_diff > 0) {
+ reshape = std::make_shared<v0::Unsqueeze>(src, axes_const);
+ } else if (rank_diff < 0) {
+ for (auto axis : axes) {
+ if (src_shape[axis].is_dynamic() || src_shape[axis] != 1) {
+ return nullptr;
+ }
+ }
+ reshape = std::make_shared<v0::Squeeze>(src, axes_const);
+ }
+
+ if (!reshape) {
+ return nullptr;
+ }
+
+ reshape->set_friendly_name(src->get_friendly_name() + "/Reshape");
+ ov::copy_runtime_info(src, {axes_const, reshape});
+
+ return reshape;
+}
+
} // namespace
namespace ov::pass {
@@ -473,7 +519,17 @@ SDPAFusionMatcher::SDPAFusionMatcher() {
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);
+
+ // Align output shapes by inserting Squeeze/Unsqueeze nodes as needed when the fused SDPA output rank
+ // differs from the original output rank.
+
+ sdpa = try_align_outputs(sdpa, m.get_match_root());
+ if (!sdpa) {
+ return false;
+ }
+
ov::replace_node(m.get_match_root(), sdpa);
+
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.cpp
index da02810c57e..62e571b96e1 100644
--- a/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp
+++ b/src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp
@@ -91,6 +91,14 @@ public:
multiply_k(scale);
}
+ void squeeze(InputType which, const vector<int64_t>& axes) {
+ auto axes_const = op::v0::Constant::create(element::i64, {axes.size()}, axes);
+ nodes[which] = make_shared<op::v0::Squeeze>(nodes[which], axes_const);
+ }
+ void unsqueeze(InputType which, const vector<int64_t>& axes) {
+ auto axes_const = op::v0::Constant::create(element::i64, {axes.size()}, axes);
+ nodes[which] = make_shared<op::v0::Unsqueeze>(nodes[which], axes_const);
+ }
void reshape(InputType which, const Shape& shape) {
auto shape_const = op::v0::Constant::create(element::i64, {shape.size()}, shape);
nodes[which] = make_shared<op::v1::Reshape>(nodes[which], shape_const, false);
@@ -112,6 +120,33 @@ public:
void reshape_sdpa(const Shape& shape) {
reshape(InputType::SDPA, shape);
}
+
+ void unsqueeze_q(const vector<int64_t>& axes) {
+ unsqueeze(InputType::Q, axes);
+ }
+ void unsqueeze_k(const vector<int64_t>& axes) {
+ unsqueeze(InputType::K, axes);
+ }
+ void unsqueeze_v(const vector<int64_t>& axes) {
+ unsqueeze(InputType::V, axes);
+ }
+ void unsqueeze_sdpa(const vector<int64_t>& axes) {
+ unsqueeze(InputType::SDPA, axes);
+ }
+
+ void squeeze_q(const vector<int64_t>& axes) {
+ squeeze(InputType::Q, axes);
+ }
+ void squeeze_k(const vector<int64_t>& axes) {
+ squeeze(InputType::K, axes);
+ }
+ void squeeze_v(const vector<int64_t>& axes) {
+ squeeze(InputType::V, axes);
+ }
+ void squeeze_sdpa(const vector<int64_t>& axes) {
+ squeeze(InputType::SDPA, axes);
+ }
+
void transpose_q(const vector<size_t>& order) {
transpose(InputType::Q, order);
}
@@ -1193,6 +1228,85 @@ TEST_F(TransformationTestsF, SDPAFusionTest_ReshapeOptimizationWithMaskCausal) {
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
}
+class SDPASqueezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};
+
+TEST_P(SDPASqueezeOutput, SDPAFusionTest_SqueezeOutput) {
+ // Parametrization
+ const auto& shape = GetParam();
+
+ SDPA sdpa(f16, shape, shape, shape);
+ SDPA sdpa_ref(f16, shape, shape, shape);
+
+ // SDPA model.
+ {
+ sdpa.create_pattern_sdpa(true);
+ sdpa.transpose_sdpa({1, 0});
+ model = sdpa.build_model();
+
+ manager.register_pass<ov::pass::SDPAFusion>();
+ }
+
+ // SDPA reference model.
+ {
+ sdpa_ref.unsqueeze_q({0});
+ sdpa_ref.unsqueeze_k({0});
+ sdpa_ref.unsqueeze_v({0});
+ sdpa_ref.create_reference_sdpa();
+ sdpa_ref.squeeze_sdpa({0});
+ sdpa_ref.transpose_sdpa({1, 0});
+ model_ref = sdpa_ref.build_model();
+ }
+
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
+}
+
+INSTANTIATE_TEST_SUITE_P(SDPAFusion,
+ SDPASqueezeOutput,
+ Values(PartialShape{55, 128}, // Static 2D
+ PartialShape{-1, 128}, // Dynamic batch
+ PartialShape{55, -1}, // Dynamic embedding
+ PartialShape{-1, -1})); // Fully dynamic
+
+class SDPAUnsqueezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};
+
+TEST_P(SDPAUnsqueezeOutput, SDPAFusionTest_UnsqueezeOutput) {
+ // Parametrization
+ const auto& shape = GetParam();
+
+ SDPA sdpa(f16, shape, shape, shape);
+ SDPA sdpa_ref(f16, shape, shape, shape);
+
+ // SDPA model.
+ {
+ sdpa.set_mask({1, 1, shape[1], shape[1]});
+ sdpa.create_pattern_sdpa(true);
+ sdpa.transpose_sdpa({0, 1, 3, 2});
+ model = sdpa.build_model();
+
+ manager.register_pass<ov::pass::SDPAFusion>();
+ }
+
+ // SDPA reference model.
+ {
+ sdpa_ref.set_mask({1, 1, shape[1], shape[1]});
+ sdpa_ref.create_reference_sdpa();
+ sdpa_ref.unsqueeze_sdpa({0});
+ sdpa_ref.transpose_sdpa({0, 1, 3, 2});
+ model_ref = sdpa_ref.build_model();
+ }
+
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
+}
+
+INSTANTIATE_TEST_SUITE_P(SDPAFusion,
+ SDPAUnsqueezeOutput,
+ Values(PartialShape{1, 55, 128}, // Static
+ PartialShape{1, -1, 128}, // Dynamic batch
+ PartialShape{1, 55, -1}, // Dynamic embedding
+ PartialShape{1, -1, -1})); // Fully dynamic
+
TEST_F(TransformationTestsF, SDPAFusionTest_4dAttentionMaskWithBatch2) {
// Init.
int64_t batch = 2;