Skip to content

Use accumulated momentum for non-Nesterov Muon updates - #2174

Open
sylvesterkaczmarek wants to merge 1 commit into
openai:mainfrom
sylvesterkaczmarek:fix/use-non-nesterov-momentum
Open

sylvesterkaczmarek wants to merge 1 commit into
openai:mainfrom
sylvesterkaczmarek:fix/use-non-nesterov-momentum

Conversation

@sylvesterkaczmarek

@sylvesterkaczmarek sylvesterkaczmarek commented Oct 5, 2026 •

Copy link
Copy Markdown

Problem

With nesterov=False, Muon updates its momentum buffer but then orthogonalizes the raw gradient. Accumulated momentum therefore has no effect on the parameter update. The orthogonalizer also mutates its input when that input is already bfloat16, because the dtype conversion can alias the original tensor.

Changes

Use the momentum buffer for the non-Nesterov direction. Normalize out of place so the orthogonalization does not overwrite that buffer or a caller's bfloat16 gradient. Preserve the default Nesterov formula, parameter groups, serialization, and distributed update layout.

Why both changes are required

The non-Nesterov direction points directly to the stored momentum buffer. With bfloat16 gradients, G.bfloat16() can return the same tensor, so the old in-place X /= X.norm() + eps would overwrite that buffer. That destroys the multi-step momentum history just as the new branch starts using it. Out-of-place normalization is therefore part of the correctness fix, not an unrelated refactor. The tests explicitly check buffer preservation and bfloat16 input nonmutation over several updates.

Scope: the reference training path uses Muon's default nesterov=True and float32 matrix parameters, so this patch does not change the submitted baseline or establish any new leaderboard result. It corrects the optional nesterov=False path and protects bfloat16 callers.

Validation

  • 29 CPU tests passed. On unchanged production code, 14 fail and 15 controls pass.
  • Independent update calculations cover three successive steps, both momentum modes, float32/bfloat16, tall/wide/square matrices, and zero/three Newton-Schulz iterations.
  • Tests also check the retained momentum buffer, unchanged input gradients, direct helper nonmutation, and parameters without gradients.
  • Ruff syntax/error checks and git diff --check passed. The baseline remains below its 1,500-line limit.

Python 3.12.14, PyTorch 2.14.1 on macOS arm64. No CUDA/distributed execution, compilation benchmark, dataset, model checkpoint, or end-to-end training run was used. No leaderboard improvement is claimed. This branch is independent of the byte-counting correction and the existing rotary fix.

Reproduced against f5c079314c4877fbb0af378c0abade5a8ca33d3a.

Maintenance validation on 6 November 2026

Revalidated both momentum modes, input nonmutation and multi-step buffer preservation. The existing description already explains why out-of-place normalization is required and why the default baseline remains unchanged.

Focused validation at 3f02b3642537: 29 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.

@Evreu1pro

Copy link
Copy Markdown

Checked out 3f02b36: tests/test_muon_momentum.py gives 29 passed here too (torch 2.14.1, Linux CPU).

One thing that might be worth spelling out in the description: the two changes aren't really independent. I tried the momentum fix alone (keeping X /= X.norm() + eps) with a bf16 parameter and nesterov=False. g is then buf itself, G.bfloat16() returns the same tensor, and the in-place divide normalizes the momentum buffer. After one step the buffer norm is ~1.0 instead of the gradient's ~3.66. With fp32 it's fine because .bfloat16() copies. So the out-of-place divide is what makes the new else buf branch safe, not just a cleanup.

On impact, if I'm reading main right this doesn't change the baseline or any record: Muon(...) in train_gpt.py never passes nesterov, so it stays at the default True, and every explicit nesterov= I could grep in records/ is True or passed through. The matrix params are also fp32 (CastedLinear gets .float()), so the aliasing never triggered there either. It only bites someone who flips Nesterov off, which is still a real trap for anyone ablating it.

@sylvesterkaczmarek

Copy link
Copy Markdown
Author

Thanks for checking this. Agreed — the out-of-place normalization is necessary for the nesterov=False change because the direction aliases the momentum buffer, and bfloat16 conversion can leave that alias intact. The multi-step tests check the buffer remains unchanged by orthogonalization. I've made that coupling and the baseline's unchanged default-Nesterov behavior explicit in the PR description.

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.

2 participants