Repository navigation
Scale the right output channel when fusing batch_norm / scale into a grouped conv_transpose - #2938
Open
chaitanya0305 wants to merge 1 commit into
Open
chaitanya0305 wants to merge 1 commit into
chaitanya0305 wants to merge 1 commit into
Conversation
fuse_conv_batchnorm and fuse_conv_scale fold a per-channel factor into the conv weight. For conv_transpose the weight is [Cin, Cout / groups, *D_in]; both passes transposed it to [Cout / groups, Cin, *D_in] and reshaped to [Cout, Cin / groups, *D_in], assuming row c of the result is output channel c. With groups > 1 and Cout / groups > 1, row j * groups + g holds output channel g * (Cout / groups) + j, so the factors were applied to the wrong kernels. A torch ConvTranspose1d (groups=2) followed by BatchNorm1d converted to a model whose output differs from torch by as much as the output itself. Depthwise and groups=1 layers were not affected. Move the per-channel weight scaling into one helper that reshapes the transposed-conv weight to [groups, Cin / groups, Cout / groups, *D_in] and broadcasts the factors per group, and use it in both passes. The existing tests only checked output shapes. Add prediction tests against torch for grouped conv and conv_transpose in both passes.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
common::fuse_conv_batchnormandcommon::fuse_conv_scaleapply the per-channel factor to the wrong output channels of a groupedconv_transpose(groups > 1andCout / groups > 1). Both passes are in the default pipeline. As a result, a torchConvTranspose{1,2,3}d(groups=g)followed byBatchNorm, or by a per-channelmul/div, converts without any warning to a model with wrong outputs. Depthwise transposed convs (Cout / groups == 1) andgroups == 1are not affected. Neither is regularconv.Repro (coremltools main, macOS, CPU_ONLY, FLOAT32)
The conversion without BN is correct. The error comes from the fusion.
Root cause
A
conv_transposeweight has shape[Cin, Cout / groups, *D_in]. Output channelg * (Cout / groups) + jreadsweight[g * Cin/groups : (g + 1) * Cin/groups, j].Both passes transpose the weight to
[Cout / groups, Cin, *D_in], reshape it to[Cout, Cin / groups, *D_in]and scale rowcbyscale[c]. After that reshape, rowrholds output channel(r % groups) * (Cout / groups) + r // groups, not channelr. So whenevergroups > 1andCout / groups > 1, kernels get another channel's factor. The bias update is indexed by output channel, so it is correct. The result is that kernels and biases no longer agree.Fix
_scale_conv_weight_per_output_channel(). Forconvit broadcasts the scale over axis 0. Forconv_transposeit reshapes the weight to[groups, Cin / groups, Cout / groups, *D_in]and broadcastsscale.reshape(groups, 1, Cout / groups, ...).fuse_conv_batchnormandfuse_conv_scale. This replaces the per-channel Python loops and the transpose/reshape round trip. The BN bias update is now vectorized with the same formula.Cin,deconv_weight_transpose_axes,is_conv_1d). This also drops the hard-coded[1, 0, 2, 3]transpose infuse_conv_scale.Tests
The existing
test_conv_transposecases for both passes only check output shapes, so they pass on main. New tests:TestConvBatchNormFusion::test_grouped_conv_numerics: rank 3/4/5, groups 2/3, conv and conv_transpose.TestConvScaleFusion::test_grouped_conv_numerics: rank 3/4, groups 2/3, conv and conv_transpose, mul and real_div.Both run the fused program and compare the prediction with torch (
conv*/conv_transpose*+batch_normor scale). On main, all 14 conv_transpose cases fail and all conv cases pass. With this branch, all pass.The rest of
test_passes.pygives the same results as on main. Locally,TestReplaceStackReshape(numpyreshape(newshape=)) andTestRandomizeWeights(needs torchvision) fail on main as well.