Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

[fix] Fix Conv2d_BN fuse bug when groups > 1 #22

Merged
merged 1 commit into from
Mar 25, 2022

Conversation

wkcn
Copy link
Contributor

@wkcn wkcn commented Mar 25, 2022

Hi there, thanks for your great work : )

I found there is a bug when fusing Conv2d_BN with groups > 1. The reason is that the input channel should be w.size(1) * self.c.groups rather than w.size(1) in the function Conv2d_BN.fuse.

Reproduce Code:

from levit import Conv2d_BN
from levit_c import Conv2d_BN as Conv2d_BN_c
import torch
import numpy as np
from itertools import product
import utils

@torch.no_grad()
def test():
    for layer_t, a, b, ks, groups in product(
        [Conv2d_BN, Conv2d_BN_c],
        [8, 16, 32, 64],
        [8, 16, 32, 64],
        [1, 3, 5, 7],
        [1, 2, 4],
            ):
        layer = layer_t(a, b, ks, pad=ks//2, groups=groups)
        layer.eval()

        x = torch.randn((1, a, 16, 16))
        y1 = layer(x)
        utils.replace_batchnorm(layer)
        y2 = layer(x)

        np.testing.assert_almost_equal(y1.detach().numpy(), y2.detach().numpy(), decimal=4)

if __name__ == '__main__':
    test()
    print("Test Over")

Error:

  File "test_conv.py", line 21, in test
    layer.fuse()
  File "/home/wkcn/miniconda3/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/home/wkcn/proj/LeViT-1/levit_c.py", line 99, in fuse
    m.weight.data.copy_(w)
RuntimeError: The size of tensor a (2) must match the size of tensor b (4) at non-singleton dimension 1

@facebook-github-bot
Copy link

Hi @wkcn!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 25, 2022
@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Contributor

@btgraham btgraham left a comment

Choose a reason for hiding this comment

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

Thanks for the fix.

@btgraham btgraham merged commit d000b74 into facebookresearch:main Mar 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants