Skip to content

[GPU] Enable post-op fusion for eltwise with FC/MatMul/Transpose when reshape place in middle - #36879

Open
clee30 wants to merge 4 commits into
openvinotoolkit:masterfrom
clee30:eltwise_fusion
Open

[GPU] Enable post-op fusion for eltwise with FC/MatMul/Transpose when reshape place in middle#36879
clee30 wants to merge 4 commits into
openvinotoolkit:masterfrom
clee30:eltwise_fusion

Conversation

@clee30

@clee30 clee30 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

A rank-expanding reshape (a unit-dimension unsqueeze) placed on one input of a binary elementwise Add blocks the producer + Add post-op fusion: the eltwise fusing pass only inspects the Add direct parents and skips rank-changing reshapes, so the Add stays a standalone kernel that re-reads both operands.

This pass rewrites

Eltwise(R, Unsqueeze(P, axis))   ->   Unsqueeze(Eltwise(Squeeze(R, axis), P), axis)

so the producer P feeds the Add directly in the lower rank and the Add can be fused as a sum post-op on P. The introduced Squeeze is the inverse of the unsqueeze emitted by the preceding op in the chain and is normally removed as a redundant reshape. Only genuine unit-dimension unsqueezes are matched; reshapes that also split or merge non-unit dimensions are left untouched.

The sink is applied when the producer P is a primitive whose GPU kernel can absorb the Add as a sum post-op: FullyConnected / MatMul (or its fused bias Add), and Transpose (Permute). The Transpose case covers the residual add on a transposed attention output, where the add can be fused into the permute kernel instead of running as a separate eltwise; without this the add is emitted as a standalone eltwise_simple_vload8 primitive per block.

Graph before transformation (For MatMul)
Screenshot 2026-08-04 at 5 33 00 PM

Graph after transformation (For MatMul)
Screenshot 2026-08-04 at 5 36 17 PM

Graph after transformation (For Transpose)
Screenshot 2026-08-04 at 5 41 39 PM

Tickets:

AI Assistance:

  • *AI assistance used: yes
  • AI helps to identify this transformation

@clee30
clee30 requested review from a team as code owners July 14, 2026 13:12
@github-actions github-actions Bot added the category: GPU OpenVINO GPU plugin label Jul 14, 2026
@maxnick maxnick added this to the 2026.4 milestone Jul 20, 2026
@maxnick
maxnick requested a review from Copilot July 20, 2026 12:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new GPU graph transformation that sinks a unit-dimension Unsqueeze/unsqueeze-equivalent Reshape through a binary eltwise op so the producer (FC/MatMul/Transpose) can fuse the eltwise as a post-op, and integrates the pass into the Intel GPU transformations pipeline.

Changes:

  • Implement SinkUnsqueezeThroughEltwise model pass to rewrite Eltwise(R, Unsqueeze(P)) -> Unsqueeze(Eltwise(Squeeze(R), P)).
  • Register the new pass in the Intel GPU TransformationsPipeline.
  • Add unit tests covering FC/FC+bias/Transpose producers, plus a negative case.

Reviewed changes

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

File Description
src/plugins/intel_gpu/tests/unit/transformations/sink_unsqueeze_through_eltwise_test.cpp Adds unit tests validating the new sinking rewrite and a no-transform case.
src/plugins/intel_gpu/src/plugin/transformations/sink_unsqueeze_through_eltwise.hpp Declares the new SinkUnsqueezeThroughEltwise pass and documents intended behavior.
src/plugins/intel_gpu/src/plugin/transformations/sink_unsqueeze_through_eltwise.cpp Implements pattern detection and graph rewrite to sink unsqueeze-like reshapes below eltwise.
src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp Registers the new pass in the GPU transformations pipeline.

@v-Golubev v-Golubev self-assigned this Jul 23, 2026
@maxnick
maxnick requested a review from v-Golubev July 23, 2026 09:19
@maxnick

maxnick commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

@v-Golubev , could you please review?

@clee30
clee30 requested a review from a team as a code owner July 24, 2026 09:43
@github-actions github-actions Bot added the category: transformations OpenVINO Runtime library - Transformations label Jul 24, 2026
@maxnick
maxnick requested a review from v-Golubev July 28, 2026 12:41
@CuriousPanCake

Copy link
Copy Markdown
Contributor

Please, do not create 2 matchers inside a single MatcherPass class. This is not how we normally write transformations and it complicates the view.

@clee30 clee30 closed this Aug 3, 2026
@clee30 clee30 reopened this Aug 3, 2026
@CuriousPanCake

Copy link
Copy Markdown
Contributor

Please do work on writing the transformation. Take other transformations for inspiration.

…e post-op fusion

Add a new MatcherPass (common transformations) that handles binary elementwise
ops where one input passes through a unit-dimension Unsqueeze/Reshape whose
producer is a "fusable" op (one that can absorb the eltwise as a post-op):

    Eltwise(R, Unsqueeze(P, axis))
      => Unsqueeze(Eltwise(Squeeze(R, axis), P), axis)

so the eltwise operates in the lower rank and can be fused as a post-op on P.
This complements the existing MoveEltwiseUpThroughDataMovPerChannel pass, which
only handles the per-channel-constant case:

    Eltwise(Reshape(P), Const_per_channel)
      => Reshape(Eltwise(P, Const_lower_rank))

The pass is configurable via constructor parameters:
- fusable_producer_types: list of DiscreteTypeInfo for fusable op types
- check_bias_add: whether to look through one level of bias Add

It is registered in the GPU transformations pipeline for FullyConnected,
MatMul and Transpose producers so the eltwise can be fused as a post-op of
those kernels, which was previously blocked by the rank-changing reshape.
@clee30
clee30 requested a review from CuriousPanCake August 5, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: GPU OpenVINO GPU plugin category: transformations OpenVINO Runtime library - Transformations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants