Repository navigation
[GlobalOptimization] Make im2col a generic convolution rewrite - #25011
Conversation
bf66ddf to
b7a5976
Compare
egebeysel
left a comment
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
@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? |
|
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.
+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. |
|
@egebeysel got claude to run some benchmarks: 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. |
|
Nice! Thanks, looks super nice, regardless of:
We can revisit this if that were to be the case, LGTM in general. I'll take another look at the code soon. |
|
Regarding upstreaming, the old pass actually was upstreamed in 2023: But somehow never replaced in IREE? :D What currently would block upstreaming this is that i tried to reuse: Which is also not in upstream but IMO would also be a good candidate for upstreaming. |
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]>
22cca20 to
43e6c6e
Compare
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 usingDispatchCreation::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