Patching Batch Norm
What’s happening?
Batch Norm requires in-place updates to running_mean and running_var of the same size as the input.
Functorch does not support inplace update to a regular tensor that takes in a batched tensor (i.e.
regular.add_(batched) is not allowed). So when vmapping over a batch of inputs to a single module,
we end up with this error
How to fix
One of the best supported ways is to switch BatchNorm for GroupNorm. Options 1 and 2 support this
All of these options assume that you don’t need running stats. If you’re using a module this means that it’s assumed you won’t use batch norm in evaluation mode. If you have a use case that involves running batch norm with vmap in evaluation mode, please file an issue
Option 1: Change the BatchNorm
If you want to change for GroupNorm, anywhere that you have BatchNorm, replace it with: