Skip to content

Add batchnorm folding transformations (fx) - #348

Merged
fxmarty merged 23 commits into
huggingface:mainfrom
fxmarty:add-batchnorm-folding
Sep 20, 2022
Merged

fxmarty merged 23 commits into
huggingface:mainfrom
fxmarty:add-batchnorm-folding

Conversation

@fxmarty

@fxmarty fxmarty commented Aug 10, 2022 •

Copy link
Copy Markdown
Contributor

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
@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Aug 11, 2022 •

Copy link
Copy Markdown

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

@fxmarty
fxmarty force-pushed the add-batchnorm-folding branch from 7ed5ba7 to c70f023 Compare September 8, 2022 09:04
@fxmarty
fxmarty force-pushed the add-batchnorm-folding branch 3 times, most recently from 81d8caf to d29ba45 Compare September 19, 2022 09:31

@michaelbenayoun michaelbenayoun left a comment

Copy link
Copy Markdown
Member

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!!!

working-directory: tests
run: |
python -m unittest discover -s fx -p 'test_*.py'
python -m pytest fx/optimization/test_transformations.py --exitfirst

Copy link
Copy Markdown
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
Copy Markdown
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 )

Comment thread setup.py Resolved
@fxmarty
fxmarty force-pushed the add-batchnorm-folding branch from e3d9a44 to 2aa167f Compare September 20, 2022 08:01
@fxmarty
fxmarty merged commit 48f2b79 into huggingface:main Sep 20, 2022
@jogepari

jogepari commented Mar 1, 2023

Copy link
Copy Markdown

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

fxmarty commented Mar 2, 2023

Copy link
Copy Markdown
Contributor Author

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