Skip to content

Prevent overflow while clipping finite MLX gradients - #2178

Open
sylvesterkaczmarek wants to merge 1 commit into
openai:mainfrom
sylvesterkaczmarek:fix/avoid-overflow-in-mlx-gradient-clipping
Open

sylvesterkaczmarek wants to merge 1 commit into
openai:mainfrom
sylvesterkaczmarek:fix/avoid-overflow-in-mlx-gradient-clipping

Conversation

@sylvesterkaczmarek

@sylvesterkaczmarek sylvesterkaczmarek commented Oct 6, 2026 •

Copy link
Copy Markdown

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

  • 16 CPU tests passed. Eight large-gradient regressions fail on unchanged code; eight controls pass before and after.
  • Tests cover float32 and bfloat16 gradients, multiple tensor leaves, two thresholds, and magnitudes from 1e10 to 1e25.
  • Results match an independent float64 norm/scaling reference. Checks cover finite nonzero updates, shapes/dtypes, unchanged input gradients, empty trees, and disabled or unnecessary clipping.
  • Syntax/error checks and git diff --check pass.
  • Run python -m pytest tests/test_mlx_gradient_norm.py with 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.

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.

1 participant