Repository navigation
Prevent overflow while clipping finite MLX gradients - #2178
Open
sylvesterkaczmarek wants to merge 1 commit into
Open
sylvesterkaczmarek wants to merge 1 commit into
sylvesterkaczmarek wants to merge 1 commit into
Conversation
Signed-off-by: Sylvester Kaczmarek <[email protected]>
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.
Problem
MLX gradient clipping squares float32 gradients before summing in float64. Large but finite gradients overflow during the square, producing an infinite norm and a zero scale. The update disappears instead of being clipped to the requested norm.
Change
Perform the square in float64, matching the precision already intended for accumulation. Keep the threshold, denominator offset, tensor dtypes, tree structure, and disabled-clipping behavior unchanged. The baseline script remains below its line limit.
NumPy's square reference documents the dtype argument used for the intermediate calculation.
Validation
git diff --checkpass.python -m pytest tests/test_mlx_gradient_norm.pywith MLX, the baseline dependencies, and pytest installed.Python 3.12.14, MLX 0.32.3, and NumPy 2.5.3 on macOS arm64. MLX operations run on its CPU stream. No pretrained weights, datasets, Metal/CUDA execution, or full training was used. Already non-finite inputs and other optimizers are outside this change. No dependency changes.
Reproduced against
f5c079314c4877fbb0af378c0abade5a8ca33d3a.Maintenance validation on 6 November 2026
Revalidated large finite gradients, unchanged input trees, dtype preservation and disabled-clipping controls on the MLX CPU stream. No further source changes were needed.
Focused validation at
643cba98b282: 16 CPU tests passed. The combined six-PR checkout passed 121 tests, including the actual PyTorch and MLX CPU implementations. The two baseline scripts remain below 1,500 lines; archived record scripts were not modified.Python 3.12.14, PyTorch 2.15.0 and SentencePiece 0.2.3 on macOS arm64. Tests used synthetic inputs with external sockets disabled. No pretrained checkpoint, external dataset, GPU training, full validation benchmark or leaderboard measurement was used. There are no hosted test runs on these PR heads. Earlier validation counts above are historical.