Skip to content

[TRANSFORMATIONS] Align fused SDPA output#34097

Open
mryzhov wants to merge 8 commits intoopenvinotoolkit:masterfrom
mryzhov:sdpa_squeeze
Open

[TRANSFORMATIONS] Align fused SDPA output#34097
mryzhov wants to merge 8 commits intoopenvinotoolkit:masterfrom
mryzhov:sdpa_squeeze

Conversation

@mryzhov
Copy link
Contributor

@mryzhov mryzhov commented Feb 12, 2026

Details:

This pull request enhances the SDPA Fusion transformation by improving shape alignment between the original and fused nodes, particularly for handling cases when the fused SDPA operation output changes the original output shape (2D->3D, 4D-3D)

SDPA Fusion Logic Improvements

  • Added a try_align_outputs helper function to insert a Squeeze node when the fused SDPA node's output shape does not match the original node's output, ensuring correct shape alignment after fusion.
  • Updated the SDPA fusion matcher to use try_align_outputs after creating the fused node, so that 2D inputs are properly handled and output shapes remain consistent.

Testing Enhancements

  • Extended the SDPA test utility class with squeeze and unsqueeze methods for more flexible test graph construction.
  • Added convenience methods to the SDPA class for unsqueezing Q, K, V inputs and squeezing SDPA outputs, supporting more detailed test scenarios.
  • Added new tests for SDPA fusion with 2D static and dynamic input shapes to ensure correct behavior and output shape alignment in these cases.

Tickets:

@mryzhov mryzhov requested a review from a team as a code owner February 12, 2026 15:54
@github-actions github-actions bot added the category: transformations OpenVINO Runtime library - Transformations label Feb 12, 2026
@mryzhov mryzhov requested a review from mitruska February 12, 2026 15:58
@mryzhov mryzhov requested a review from Copilot February 12, 2026 16:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improve SDPA Fusion output shape alignment for 2D inputs by inserting a post-fusion squeeze when needed, and extend tests to cover 2D static/dynamic shapes.

Changes:

  • Added try_align_outputs helper to insert Squeeze when fused SDPA output shape differs from the original.
  • Updated SDPA fusion matcher to use try_align_outputs before replacing the matched subgraph.
  • Extended SDPA test utilities and added parametrized 2D-input SDPA fusion tests (static and dynamic).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/common/transformations/src/transformations/common_optimizations/sdpa_fusion.cpp Adds output-shape alignment logic (optional squeeze) and integrates it into the fusion replacement.
src/common/transformations/tests/common_optimizations/sdpa_fusion_test.cpp Adds squeeze/unsqueeze helpers and new parametrized tests for 2D input fusion scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ion_test.cpp

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
});
};

std::shared_ptr<ov::Node> try_align_outputs(const std::shared_ptr<ov::Node>& src_output,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a comment where we have a case of 2D tensors (Q, K, V)? Which model and why it happens?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was identified in the GeekbenchAI model. It is not related to the SDPA inputs themselves; rather, it occurs during fusion of the SDPA subgraph. When the subgraph is replaced with the SDPA op, the resulting output shape can change in some cases, making it incompatible with the remainder of the model. The simplest way to reproduce the issue is with 2D inputs.
Another solution would be fixing the SDPA shape validation logic, @mitruska fyi

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

INSTANTIATE_TEST_SUITE_P(SDPAFusion,
SDPAUnsquieezeOutput,
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in parameter reference: "Unsquieeze" should be "Unsqueeze".

Copilot uses AI. Check for mistakes.
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
}

class SDPASquieezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in class name: "Squieeze" should be "Squeeze".

Copilot uses AI. Check for mistakes.

class SDPASquieezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};

TEST_P(SDPASquieezeOutput, SDPAFusionTest_SquieezeOutput) {
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in test name: "Squieeze" should be "Squeeze".

Suggested change
TEST_P(SDPASquieezeOutput, SDPAFusionTest_SquieezeOutput) {
TEST_P(SDPASquieezeOutput, SDPAFusionTest_SqueezeOutput) {

Copilot uses AI. Check for mistakes.
}

INSTANTIATE_TEST_SUITE_P(SDPAFusion,
SDPASquieezeOutput,
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in parameter reference: "Squieeze" should be "Squeeze".

Copilot uses AI. Check for mistakes.
Comment on lines +1271 to +1273
class SDPAUnsquieezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};

TEST_P(SDPAUnsquieezeOutput, SDPAFusionTest_UnsquieezeOutput) {
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in class name: "Unsquieeze" should be "Unsqueeze".

Suggested change
class SDPAUnsquieezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};
TEST_P(SDPAUnsquieezeOutput, SDPAFusionTest_UnsquieezeOutput) {
class SDPAUnsqueezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};
TEST_P(SDPAUnsqueezeOutput, SDPAFusionTest_UnsqueezeOutput) {

Copilot uses AI. Check for mistakes.

class SDPAUnsquieezeOutput : public TransformationTestsF, public ::testing::WithParamInterface<PartialShape> {};

TEST_P(SDPAUnsquieezeOutput, SDPAFusionTest_UnsquieezeOutput) {
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in test name: "Unsquieeze" should be "Unsqueeze".

Copilot uses AI. Check for mistakes.
mryzhov and others added 2 commits February 13, 2026 20:15
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: transformations OpenVINO Runtime library - Transformations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants