Skip to content

[GlobalOptimization] Make im2col a generic convolution rewrite - #25011

Merged
ziereis merged 7 commits into
mainfrom
users/ziereis/generic-im2col
Oct 19, 2026
Merged

ziereis merged 7 commits into
mainfrom
users/ziereis/generic-im2col

Conversation

@ziereis

@ziereis ziereis commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Replaces the three layout-specific im2col patterns (NHWC, NCHW, depthwise NHWC) with one generic rewrite that handles any convolution, named or generic, including grouped, dilated, 1-D/3-D, dynamic and integer ones.

The pattern gathers the convolved input with an all-parallel linalg.generic, rewires the op to read the gather, keeps the original payload, and collapses the result into a matmul using DispatchCreation::getCollapsibleLoops. Add a basic heuristic via a control function that skips convolutions without an output channel dimension (depthwise, pooling), where the gather is never amortized.

The pass is renamed to iree-global-opt-convert-conv-to-im2col; the pipeline flag is unchanged.

🤖 Generated with Claude Code

@ziereis
ziereis force-pushed the users/ziereis/generic-im2col branch 2 times, most recently from bf66ddf to b7a5976 Compare October 8, 2026 06:31
@ziereis
ziereis marked this pull request as ready for review October 8, 2026 06:36

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

I think in general, such a rewrite should live in upstream MLIR and not here in IREE. Is there anything speaking against that? If not, I think we should go ahead and extend the ConvertConv2DToImg2Col over there, preferrably keeping the default patterns to 2d-only.

Furthermore, this currently turns on im2col for 3d convolutions as well, which could be quite expensive (~27x input buffer for 3x3x3 conv3d). Did we do any benchmarks over here and do we really want to enable this instead of a direct convolution? Or am I missing something here and was this already enabled by some other im2col pass?

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.

I think we're missing some positive and negative cases over here, e.g. no conv3d is tested, 1d is only tested for one layout etc. also, I think it might also be useful to have the tests a bit more verbose and at least test the indexing maps.

@ziereis

ziereis commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@egebeysel I don't have any opinion on where this should live i just wanted to refactor the pass here. My main grudge with the current pass is that i think the implementation is needlessly complex and it can't perform the conversion on generics.

Regarding what to rewrite and what not, my core idea was that the pattern should be able to rewrite everything and what to rewrite is a pretty complicated question either way. For now i only excluded stuff that is guaranteed to be bad not matter for what hardware.

I did some rough benchmarking for conv shapes i shaw in a handful of model and even the current pass as on main rewrites stuff that regresses hard (something like 20x on some depthwise convs) which this already improves on. However this for sure not always a net gain, i assume thats also the reason why its opt-in?

@egebeysel

Copy link
Copy Markdown
Contributor

If you don't have bandwidth to upstream this, add a TODO and we can do it sometime later. Though I believe it's more suitable for upstream MLIR.

Regarding what to rewrite and what not, my core idea was that the pattern should be able to rewrite everything and what to rewrite is a pretty complicated question either way. For now i only excluded stuff that is guaranteed to be bad not matter for what hardware.

+1 on pattern being able to rewrite everything and +1 on having a control function as you did, I was just wondering how this would play out generally for 3D convolutions, I'm also not super opinionated but maybe we could do a few benchmarks with 3D convolutions and eventually block the application of this pattern to them with the controlFn.

@ziereis

ziereis commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@egebeysel got claude to run some benchmarks:

https://gist.github.com/ziereis/3bf7b43c7af666bc5bcdb5afe50896ce

i think based on this the heuristic should be fine as is for now.

However generally this is pretty biased right now since IREE is just poorly optimized for running direct convs in comparison to running matmuls so this will probably change if conv handling improves.

@egebeysel

Copy link
Copy Markdown
Contributor

Nice! Thanks, looks super nice, regardless of:

However generally this is pretty biased right now since IREE is just poorly optimized for running direct convs in comparison to running matmuls so this will probably change if conv handling improves.

We can revisit this if that were to be the case, LGTM in general. I'll take another look at the code soon.

@ziereis

ziereis commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Regarding upstreaming, the old pass actually was upstreamed in 2023:

https://github.com/llvm/llvm-project/commits/b4badb65df4ea15fc6ecabd8f9366a96df11dcb5/mlir/lib/Dialect/Linalg/Transforms/ConvertConv2DToImg2Col.cpp

But somehow never replaced in IREE? :D

What currently would block upstreaming this is that i tried to reuse:

static SmallVector<ReassociationIndices> getCollapsibleLoops(Operation *op) {

Which is also not in upstream but IMO would also be a good candidate for upstreaming.

ziereis and others added 7 commits October 19, 2026 12:09
Move the declaration of getCollapsibleLoops, which finds the runs of
loops that every indexing map of an op keeps contiguous and that share
an iterator type, into a CollapseDimensions.h header so that passes
outside dispatch dimension collapsing can reuse the same collapsing
legality. The function is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
The im2col pass matched three named convolutions (conv_2d_nhwc_hwcf,
conv_2d_nchw_fchw, depthwise_conv_2d_nhwc_hwc) and built each matmul by
hand. Generic convolutions, other layouts, groups, dilations, and 1-D or
3-D convolutions were left alone. That includes the integer convolutions
produced by the QDQ rewrite and every convolution after unit dimension
folding.

The convolved input access is the only part of a convolution that a
contraction cannot express. The new pattern materializes that access
with an all-parallel gather indexed by the loops it uses and rewires the
op to read the gather through a projected permutation, keeping the
payload unchanged. The gather lays out depth loops first, then batch
and output image loops, then input channel and filter window loops in
filter order, so the filter needs no transpose. The contraction's loops
are then collapsed with the dispatch dimension-collapse legality
(DispatchCreation::getCollapsibleLoops), which turns standard layouts
into plain or batched matmuls; the reshapes it introduces also keep
producer fusion from folding the gather back into a convolution.
Strides and dilations only appear in the gather map. Unit filter window
loops are substituted first, so an unstrided 1x1 convolution reads its
input in place. Pooling ops are left unchanged.

The pass is renamed to iree-global-opt-convert-conv-to-im2col; the
--iree-global-opt-use-im2col-for-convs pipeline flag is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
im2col copies every input element once per filter window position it
falls into, and the resulting contraction only amortizes that copy by
reusing each gathered element across output channels. Depthwise
convolutions with a unit multiplier have no output channel dimension,
so the rewrite only adds memory traffic: on znver5 a 3x3 NHWC depthwise
convolution went from 0.07 ms to 1.5 ms single-threaded.

The pattern now only checks that the rewrite is valid and takes a
control function deciding whether it is applied. The pass supplies a
target-independent policy that requires an output channel dimension.
Pooling ops fall under the same policy, replacing the separate check
for an unused filter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
Cover the layouts, ranks and element types the generic rewrite claims
to handle: channels-last filters (nhwc_fhwc), 1-D ncw, 3-D 3x3x3 in
both channels-first and channels-last layouts, 3-D with unit spatial
window loops, channels-last groups, depthwise with a channel
multiplier, and mixed-precision bf16 -> f32 named convolutions.

Every rewritten case now checks the gather read map and the
contraction indexing maps, not only the shapes. Quantized
convolutions join the existing depthwise and pooling negative cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
The generic im2col pattern and getCollapsibleLoops only depend on
upstream dialects and would generalize the upstream 2-D im2col
patterns, so mark them for upstreaming to MLIR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
A preset compilation info or lowering config refers to the loops of the
original op. The im2col rewrite replaces that op with a gather and a
contraction over different, possibly collapsed loops, so it would drop
the configuration. Leave such convolutions alone, as
CollapseContractionDimensionsPass does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
The im2col control function rejected only convolutions without an
output channel loop, so a depthwise convolution with a unit channel
multiplier, whose output channel loop has extent one, was still
rewritten even though nothing reuses its gathered input. Require an
output channel loop whose extent is not statically one; a dynamic
extent may exceed one and counts as reuse.

Pass the already inferred convolution dimensions to the control
function, so selection uses the same dimensions as the rewrite instead
of inferring them again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Thomas Ziereis <[email protected]>
@ziereis
ziereis force-pushed the users/ziereis/generic-im2col branch from 22cca20 to 43e6c6e Compare October 19, 2026 12:39
@ziereis
ziereis merged commit e4387cb into main Oct 19, 2026
60 checks passed
@ziereis
ziereis deleted the users/ziereis/generic-im2col branch October 19, 2026 16:41
@devtbi devtbi mentioned this pull request Nov 16, 2026
8 tasks done
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.

3 participants