Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batchnorm folding transformations (fx) #348

Merged
merged 23 commits into from
Sep 20, 2022

Conversation

fxmarty
Copy link
Contributor

@fxmarty fxmarty commented Aug 10, 2022

What does this PR do?

Add the folding of nn.BatchNorm2d into nn.Conv2d, and of nn.BatchNorm1d into nn.Linear.

The former is useful for example for resnet, beit, regnet, dpt, levit, segformer and others.
The latter is useful solely for groupvit, and could be for levit; but there is a flatten inbetween so the current transformation is not directly usable: https://github.com/huggingface/transformers/blob/ab2006e3d6db88654526a4169e65d4bfc52da2e3/src/transformers/models/levit/modeling_levit.py#L153-L156

Fixes microsoft/onnxruntime#12522

Before submitting

  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

@fxmarty fxmarty marked this pull request as draft August 10, 2022 16:43
@fxmarty fxmarty marked this pull request as ready for review August 11, 2022 10:17
optimum/fx/optimization/transformations.py Outdated Show resolved Hide resolved
optimum/fx/optimization/transformations.py Show resolved Hide resolved
optimum/fx/optimization/transformations.py Outdated Show resolved Hide resolved
optimum/fx/optimization/transformations.py Show resolved Hide resolved
optimum/fx/optimization/transformations.py Outdated Show resolved Hide resolved
optimum/fx/optimization/transformations.py Outdated Show resolved Hide resolved
optimum/fx/optimization/transformations.py Outdated Show resolved Hide resolved
optimum/fx/optimization/transformations.py Outdated Show resolved Hide resolved
optimum/fx/optimization/transformations.py Outdated Show resolved Hide resolved
tests/fx/optimization/test_transformations.py Outdated Show resolved Hide resolved
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Aug 11, 2022

The documentation is not available anymore as the PR was closed or merged.

Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

Left a few comments but you should be able to merge after, thanks!!!

pip install git+https://github.com/huggingface/transformers.git
- name: Test with unittest
working-directory: tests
run: |
python -m unittest discover -s fx -p 'test_*.py'
python -m pytest fx/optimization/test_transformations.py --exitfirst
Copy link
Member

Choose a reason for hiding this comment

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

No we need to be able to also discover other files, for instance when quantization will be merged, we will need to discover those tests as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I agree, I did this considering my comment above:

@michaelbenayoun I rolled back to having a subfolder. I use python -m pytest fx/optimization/test_transformations.py --exitfirst in the yml because test_quantization.py is currently not run on main, and the tests don't pass; independenty from this PR (python -m unittest discover -s fx -p 'test_*.py' was not capturing it, see e.g. https://github.com/huggingface/optimum/actions/runs/3068109805/jobs/4955165866 )

setup.py Outdated Show resolved Hide resolved
tests/fx/optimization/test_transformations.py Outdated Show resolved Hide resolved
@fxmarty fxmarty merged commit 48f2b79 into huggingface:main Sep 20, 2022
@jogepari
Copy link

jogepari commented Mar 1, 2023

Hey, @fxmarty! Found this PR following an old ORT issue to grab your folding implementation, thx!

Sorry for digging an old thread, just wanted to comment that there might be an error here and here:
bn_b = bn1d.bias if bn1d.bias is not None else torch.ones_like(bn1d.running_mean)

In case of affine=False, bias should probably be torch.zeros_like(bn1d.running_mean).

@fxmarty
Copy link
Contributor Author

fxmarty commented Mar 2, 2023

Hi @jogepari Thank you for notifying, will fix!

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.

Vanilla LeViT model run with ORT is slower than PyTorch (seems even slower for large batch size)
4 participants