Stabilize reduced-precision InstanceNorm - #4230
Open
ternaus wants to merge 2 commits into
Open
Conversation
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
Fixes #4228.
This PR stabilizes reduced-precision
InstanceNormwithout changing its public(N, ..., C)layout or output dtype:(N, C, spatial_voxels)and use the stable fusedmx.fast.layer_normreduction;Why
The current FP16 path computes
mx.varin FP16. A channel alternating between-512and512therefore getsvar=inf, andInstanceNormcollapses the channel to zero instead of returning values near-1and1.mx.fast.layer_normalready performs the same reduction needed byInstanceNormafter the spatial dimensions are flattened. It avoids a full FP32 input cast and uses the optimized normalization kernel.Performance
Apple M4, MLX 0.32.0 core, FP16 input, five warmups and 20 measured calls:
1 x 32 x 64 x 64 x 300.785 ms0.789 ms0.196 ms1 x 128 x 96 x 96 x 302.357 ms5.145 ms1.098 msThe larger shape is the first-stage feature map from the 3D segmentation route that exposed the overflow.
Validation
PYTHONPATH=python:python/tests python -m unittest test_nn.TestLayersPYTHONPATH=python:python/tests python -m unittest test_nnpre-commit run --all-filesChecklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes