Skip to content

[DispatchCreation] Fuse packed contractions with epilogues and unpack - #24967

Merged
maxbartel merged 5 commits into
mainfrom
users/maxbartel/layout/packed-contraction-fusion
Oct 13, 2026
Merged

maxbartel merged 5 commits into
mainfrom
users/maxbartel/layout/packed-contraction-fusion

Conversation

@maxbartel

@maxbartel maxbartel commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Packed contractions lose their fused epilogues when they arrive at dispatch formation as mmt4d or a data-tiled convolution. The result unpack can also become a separate dispatch.

Add opt-in fusion (fuse-mmt4d, fuse-data-tiled-convolution) of layout-preserving elementwise consumers and the result unpack. Notes for review:

  • Epilogues must read the contraction result and write their own result through identity maps. Epilogues that update the result through their init operand are not matched.
  • Only the unpack layouts that data-tiling materialization produces are matched. For mmt4d that is the row-major form and the narrow-N transposed form. For convolutions it is NHWC, NCHW and batchless HWC; batchless CHW is not matched.
  • The result unpack is the last op of the group, so a consumer of a claimed unpack gets its own dispatch instead of fusing with the unpack.
  • An unpack that its root does not claim becomes a root at its own position in program order, as without the options, so it fuses with its own consumers as before.
  • A group holds pack-like ops (linalg.pack, set_encoding) or the result unpack, but not both, whichever joins first. A unit-batch collapse only joins together with the unpack that consumes it, so a group never ends in the collapse.
  • The matchers hardcode the CPU layouts; a TODO describes moving the layout decision into its own component that dispatch formation queries.

No pipeline enables the options until #24969.

This is one of five PRs adding an experimental CPU path that materializes data-tiling layouts before dispatch creation, so that linalg.mmt4d and data-tiled convolutions fuse with their elementwise epilogues and result unpacks.

Assisted by AI.

@maxbartel
maxbartel force-pushed the users/maxbartel/layout/packed-contraction-fusion branch from 7eba7d0 to 2c4d75b Compare September 28, 2026 15:35
@maxbartel
maxbartel added this pull request to stack #24970 September 28, 2026 16:31
@maxbartel
maxbartel marked this pull request as ready for review September 28, 2026 17:04
@maxbartel
maxbartel force-pushed the users/maxbartel/layout/packed-contraction-fusion branch 2 times, most recently from be68083 to d5205cc Compare October 5, 2026 12:39
Base automatically changed from users/maxbartel/layout/propagate-tensor-encodings to main October 6, 2026 17:52
@maxbartel
maxbartel requested review from chrsmcgrr and jschuhmacher and a balanced review from Copilot and removed request for Copilot October 9, 2026 11:21
maxbartel and others added 2 commits October 9, 2026 16:58
When contractions reach dispatch formation already packed, mmt4d ops
are not fused with their elementwise epilogues, and the result unpack
of mmt4d ops and data-tiled convolutions becomes a separate dispatch.

Add opt-in fusion of layout-preserving mmt4d epilogues and of the
result unpack. Epilogues must read the contraction result and write
their own result through identity maps. Only the unpack layouts that
data-tiling materialization produces are matched: row-major mmt4d
results, the transposed narrow-N form, and NHWC, NCHW and unit-batch
HWC convolution results. A group holds pack-like ops or the result
unpack, but not both.

The unpack is the last op of its group, so a consumer of a claimed
unpack gets its own dispatch instead of fusing with the unpack. An
unpack that its root does not claim becomes a root at its own position,
as without the options. Other fusion decisions are unchanged.

No pipeline enables the options yet. A follow-up enables them for
modules whose layouts are materialized before dispatch formation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cher

isDataTiledRoot returned false for an mmt4d or a data-tiled convolution
whose fusion option was off, so its name hid an option check. Make it
match only the op, and check the options in isDataTiledFusionEnabled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@maxbartel
maxbartel force-pushed the users/maxbartel/layout/packed-contraction-fusion branch from d5205cc to 819f773 Compare October 9, 2026 15:42

@phemashekar phemashekar 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.

Thanks Max! Generally lgtm, but I have some questions/concerns

maxbartel and others added 2 commits October 13, 2026 09:13
Between chained packed contractions the result unpack is packed again for
the next contraction, and that pack got its own dispatch. Let a static pack
of the claimed unpack join and end the data-tiled group, as
unset_encoding -> set_encoding fuses in dispatch-time data tiling. Drop the
rule that kept pack-like ops and the result unpack apart, which only
existed to be conservative.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The collapse could join while its unpack was later rejected, because the
unpack was in a nested region, depended on the group through its
destination, or was already a root. The dispatch then returned the
collapse. Check the unpack with the collapse as a pending group member
before admitting the collapse.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@phemashekar phemashekar 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.

thanks, looks good from my side!

// Only a pack that relayouts the result unpack for the next packed
// contraction follows it, as unset_encoding -> set_encoding fuses in
// dispatch-time data tiling, and the pack ends the group. Dynamic inner
// tiles are excluded as in producer fusion (#12746).

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.

nit: this is an old issue, we might have to check if this still reproduces

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do in a follow up!

@maxbartel
maxbartel merged commit 6daf6ea into main Oct 13, 2026
85 of 89 checks passed
@maxbartel
maxbartel deleted the users/maxbartel/layout/packed-contraction-fusion branch October 13, 2026 14:38
@devtbi devtbi mentioned this pull request Oct 19, 2026
7 of 8 tasks
maxbartel added a commit that referenced this pull request Nov 6, 2026
Dispatch-time data tiling assigns layouts inside dispatches, so the
phases before dispatch creation cannot see or propagate them.
Materializing layouts during global optimization makes that phase target
aware on purpose: layouts propagate through the surrounding tensor
graph, and the packed contractions still fuse with their epilogues and
result unpacks.

Add `--iree-opt-experimental-early-data-tiling`. For a module with a
single target that supports host encoding materialization
(`isHostEncodingMaterializationSupported`: llvm-cpu on x86_64 or AArch64
with the CPU encoding resolver and without inner-tiled layouts), and
with the default encoding strategy, the new pass:

1. normalizes contraction dimensions,
2. assigns and propagates encodings,
3. materializes them on the host,
4. records the target (#24968).

Other modules, and the padding encoding strategy, keep dispatch-time
data tiling. The route takes its operation families and encoding
strategy from the dispatch-creation data-tiling flags, passed down from
`Pipelines.cpp`, because unsupported modules fall back to that route.

**Behavior changes that are easy to miss.**
- The flag takes precedence over the other data-tiling placement flags,
so conflicting data-tiling flags are not an error when it is set.
- The target is recorded even when nothing was data-tiled, so
dispatch-time assignment stays off for that module.
- The early pipeline also runs `BubbleUpExpandShapes` and `SinkReshapes`
on eligible modules.
- For modules with the attribute, dispatch creation always enables
`fuse-mmt4d` and `fuse-data-tiled-convolution` (#24967), even if they
are set to false, and keeps the loop order of data-tiled convolutions.

**Relation to existing paths.** `--iree-global-opt-data-tiling` with
early materialization also materializes on the host, but it has no
contraction normalization or propagation, and its mmt4d ops are not
fused with their epilogues. Like that path, this one relies on
`MaterializeHostEncodingPass` and is restricted to single-target
modules. The layout decision is split between this pass,
`isHostEncodingMaterializationSupported` and the dispatch-creation
passes that read the attribute, and it adds HAL and Codegen dependencies
to the global phases. TODOs (#25031) describe moving it into its own
component that decides per affinity.

**Performance** on an Apple M3 Max (local-task, f32,
`--iree-llvmcpu-target-cpu=host`, median of 10). HAL dispatches in
parentheses. x86_64 has not been measured.

| workload | dispatch-time data tiling | this PR |
`--iree-global-opt-data-tiling` |
|---|---|---|---|
| matmul 512x1024x1024 + bias + ReLU | 1.49 ms (4) | 1.50 ms (3) | 1.43
ms (4) |
| batch_matmul 16x256x512x256 + ReLU | 1.69 ms (3) | 1.68 ms (3) | 1.75
ms (4) |
| conv NHWC 1x58x58x64, 3x3x64x64 + ReLU | 10.5 ms (3) | 10.5 ms (3) |
5.68 ms (1)* |

On these workloads the new path is within noise of dispatch-time data
tiling, which already fuses epilogues inside encoded dispatches. *The
global-opt row does not data-tile the convolution, because
`--iree-dispatch-creation-experimental-set-data-tiling-ops` only affects
dispatch creation. On this machine the existing experimental convolution
data tiling is slower than direct convolution codegen, independent of
this PR.

This is one of five PRs adding an experimental CPU path that
materializes data-tiling layouts before dispatch creation, so that
`linalg.mmt4d` and data-tiled convolutions fuse with their elementwise
epilogues and result unpacks.

Assisted by AI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants