Repository navigation
Validate inputs in GroupNorm and InstanceNorm - #3653
Merged
Merged
Conversation
ishtihoss
force-pushed
the
norm-input-validation
branch
from
June 10, 2026 22:06
5fc83d8 to
9521e2f
Compare
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.
Proposed changes
Two cases where the norm layers accept bad inputs and return meaningless results instead of raising:
InstanceNormon an input with no spatial dimensions returns all zeros. For a(N, C)or(C,)input the reduction axes tuple is empty, somx.meanreturns the input itself andmx.varreturns 0, and the normalized output is exactly zero everywhere:GroupNormdoes not check thatnum_groupsdividesdims. With saynum_groups=4, dims=6the reshape can still succeed depending on the spatial size, and the groups end up mixing channel and spatial positions, so the statistics are garbage. Thepytorch_compatible=Truepath floor-divides the group size and silently drops the intended grouping too. PyTorch raises for this at construction.Both now raise a
ValueError, same idea as #3615 did forSinusoidalPositionalEncoding. Added tests to the existing group norm and instance norm tests.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes