Skip to content

Add Muon as an optimizer option and compare it with AdamW on the small preset (#21) - #72

Merged
AvnehSBhatia merged 13 commits into
exea-labs:mainfrom
Aadi-Tiwari:aadi/muon
Nov 22, 2026
Merged

AvnehSBhatia merged 13 commits into
exea-labs:mainfrom
Aadi-Tiwari:aadi/muon

Conversation

@Aadi-Tiwari

@Aadi-Tiwari Aadi-Tiwari commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #21.

This adds Muon as an optimizer option and compares it with AdamW on the 13M hybrid, two seeds each, with each optimizer at its best learning rate. Both were best at 4e-3. There, Muon's mean bits per byte was lower than AdamW's by 0.017 on seed 0 and 0.030 on seed 1, and lower on all four kinds of text on both seeds. That is two seeds at one size on public stand-in data, so AdamW stays the default. The table and the commands are in results/muon/README.md.

What changed

  • exealm/train/muon.py adds Muon. Weight matrices get momentum, then 5 Newton-Schulz steps that turn the update into a nearly orthogonal matrix. Every other parameter (the embedding and tied head, norms, biases, the per-slot scalars) takes plain AdamW steps inside the same optimizer, so checkpoints, --resume and the search's slot resets work without changes.
  • The update is scaled by 0.2 * sqrt(max(rows, cols)), as in "Muon is Scalable for LLM Training" (Liu et al. 2025, arXiv 2502.16982). That keeps it about the size of an AdamW update, so both share --lr, the WSD schedule and weight decay.
  • make_optimizer(model, lr, wd, kind="adamw"), plus --optimizer {adamw,muon} in exealm.train.pretrain and experiments/tokenizer_lm.py. AdamW is still the default and builds the same optimizer as before. Result rows now record the optimizer.
  • experiments/muon_table.py prints the learning-rate sweep and the paired difference between the two optimizers at their best rates. I didn't reuse funding_table.py because it only pairs runs against the hybrid.
  • experiments/tokenizer_lm.py --accum N runs each batch as N equal micro-batches. On the 13M hybrid a batch of 32 x 512 needs about 9 GB of GPU memory, so it doesn't fit an 8 GB card, and --accum 2 does. Each step sees the same tokens.
  • One catch with --accum: TONN's running slot scale updates once per micro-batch, so the gradient isn't bit-identical to one whole batch. On the first step the second micro-batch is divided by the first one's measured size instead of 1, and after that the running average moves twice per step. Both optimizers in the comparison use --accum 2, so they're treated the same.
  • To see how much it moves a result, I ran AdamW at 4e-3 with batch 16, which fits whole, with and without --accum 2. On seed 0 the split scored 0.018 bits per byte worse, and worse on all four kinds of text. Seed 1 against seed 0 without the split was 0.013, so the split moved the score about as much as a new seed did, maybe a little more. It could come from the slot scale updating on every micro-batch, but I haven't tested that. Muon at batch 16 without the split was still 0.035 ahead of AdamW on seed 0, so its lead doesn't come from --accum. The numbers are in the last section of results/muon/README.md.
  • With --models tonn_core_search, the search's selection batches are cut the same way, since the worth pass still builds a whole batch of logits (about 2 GB at 32 x 512 with the 32k vocabulary). The worth it measures doesn't change.
  • results/muon has the 10 result files, summary.json, the 4 runs of the batch-16 check in accum_check/, and a README with both tables and the commands.
  • docs/training.md and docs/ideas.md say how to turn it on and what the 13M runs found, and the README to-do for Muon vs AdamW on the small preset #21 is checked.
  • experiments/leave_one_out_data.py builds public stand-ins for the private data packs, which these runs read. --fetch downloads the four sources and the 32k tokenizer.

Why not torch.optim.Muon

The project allows torch>=2.3, and torch.optim.Muon is only in recent versions. Where it exists, a test checks that our Muon steps match it with adjust_lr_fn="match_rms_adamw". The largest difference after 5 steps is 1.1e-8, on weights that moved about 6e-3.

Tests

tests/test_muon.py and tests/test_muon_table.py check that:

  • the Newton-Schulz output points the same way as U V^T (cosine above 0.97), with singular values between 0.6 and 1.25 for non-square matrices
  • one Muon step has an update RMS between 0.15 and 0.22
  • the AdamW groups take the same steps as torch.optim.AdamW
  • only nn.Linear weights other than the embedding and head go on Muon, and an untied head stays on AdamW
  • the search's slot reset clears that slot's Muon momentum
  • a tiny pretraining run with --optimizer muon learns, and a run stopped and resumed mid-search ends with exactly the same weights
  • muon_table.py picks each optimizer's rate on seed 0 only

tests/test_tokenizer_lm.py checks that --accum gives the same loss and gradients as the whole batch, for Llama in training mode and for TONN in eval mode, where the slot scale doesn't update (starting from stale gradients, so a missing zero_grad shows up). No test covers TONN in training mode; the batch-16 runs above measure that case, and that cut selection batches hold the same tokens and give the same worth.

Experiment

The 13M hybrid that results/funding uses (--models tonn_core with HYBRID_13M from experiments/gpu_session/make_jobs.py). That is the small preset without its two memory circuits: RG and AG are not pinned and not in the menu. It trains for 3,000 steps (about 49M tokens), batch 32 x 512 run as 2 micro-batches (--accum 2), 32-bit, on an RTX 4060 Laptop. Seed 0 at lr 1e-3, 2e-3 and 4e-3 for each optimizer. Both were best at 4e-3, the top of that range, so each got one more run at 8e-3, which was worse for both. Then seed 1 at 4e-3 for both.

The data is public stand-ins for the private packs (FineWeb-Edu, Japanese Wikipedia, FineWeb-2 Japanese, codeparrot-clean Python), so the absolute numbers won't match results/funding. Only the comparison inside this study counts.

Mean bits per byte over English, Japanese Wikipedia, Japanese web text and code (lower is better), from python experiments/muon_table.py results/muon:

Optimizer Learning rate Seed 0 Seed 1
AdamW 1e-3 1.3721
AdamW 2e-3 1.3390
AdamW 4e-3 1.3209 1.3307
AdamW 8e-3 1.3377
Muon 1e-3 1.3448
Muon 2e-3 1.3102
Muon 4e-3 1.3041 1.3006
Muon 8e-3 1.3075

Seed 0 also picked the rates, so seed 1 is the cleaner check. For scale, this model's 10 seeds in results/funding have a standard deviation of 0.007 bits per byte. This doesn't show what happens at 0.1B, on the real packs, or with one whole batch of 32.

AdamW also did better at 4e-3 than at the 2e-3 that results/funding uses for this model, by 0.018 on seed 0 with this data.

How I tested it

Windows 11, Python 3.13, CPU torch 2.15.0.

  • On this branch's head: python -m pytest -q tests/test_muon.py tests/test_muon_table.py tests/test_tokenizer_lm.py tests/test_leave_one_out_data.py: 26 passed.
  • Full suite, PYTHONUTF8=1 python -m pytest -q, on this branch merged locally with Make the tests and data tools work on Windows, and run CI there #71 (the Windows fixes): 262 passed, 1 skipped, 0 failed. That was one commit before the head; the last commit only edits text in results/muon/README.md. Without Make the tests and data tools work on Windows, and run CI there #71, the 5 Windows-only tests that Make the tests and data tools work on Windows, and run CI there #71 fixes fail the same way they do on main (checked on an earlier head of this branch).
  • I broke the code on purpose to make sure the tests catch it: changing the 0.2 scale, removing AdamW's bias correction, dropping the / accum, or moving zero_grad into the micro-batch loop each makes a test fail.
  • The training runs used PyTorch 2.14.1 with CUDA on the RTX 4060. CI on Linux is the real check.

Notes

make_optimizer(..., "muon") and pretrain --optimizer muon give the weights of
linear layers Muon steps and keep AdamW for the embedding, the output head,
norms, biases and per-slot scalars. Both live in one optimizer, so checkpoints,
--resume and the search's slot resets work the same way. The Muon update is
scaled to about the size of an AdamW update, so both share --lr and the
schedule. AdamW stays the default.
--optimizer muon runs the same comparison with Muon instead of AdamW, and
every result row records which optimizer made it.
Writing the sums out rounded to bfloat16 twice as often: the result was 2.7%
off a 32-bit run, and is 1.1% off now (256 x 680). Muon steps now match
torch.optim.Muon with adjust_lr_fn="match_rms_adamw" up to float rounding,
and a new test checks that on PyTorch versions that have it.
experiments/muon_table.py reads the tokenizer_lm.py rows in a folder, prints
the learning-rate sweep, picks each optimizer's best rate on seed 0, and
prints the paired difference at those rates on every seed both have.
funding_table.py only pairs runs against the hybrid, so it cannot show this.
The optimizer row in docs/training.md and the Muon entry in docs/ideas.md now
say how to turn it on and what it covers. AdamW stays the starting value
until the comparison in exea-labs#21 is done.
On the 13M hybrid a batch of 32 x 512 needs about 9 GB of GPU memory, so it
does not fit an 8 GB card. --accum 2 runs it as two halves with the same
tokens and gradient. TONN's running slot scale updates once per half, so the
result is not bit-identical to one batch. The default stays 1.
…rs (exea-labs#21)

The test now fills every gradient with stale values before the step and
compares with a plain backward pass, so a missing or misplaced zero_grad
fails it. train_step rejects a batch that does not split evenly, and its
docstring and the --accum help say the first step differs most, since only
the first micro-batch is divided by an unmeasured slot scale.
The private packs behind results/funding are not public, so the Muon
runs read packs built from public text with
experiments/leave_one_out_data.py --fetch, which also gets the 32k
tokenizer. Without this file the comparison could not be rebuilt from
this branch alone.

It is the same file, with the same test, as in the leave-one-out work
for exea-labs#59. The copies are identical, so the two merge in either order.
With --models tonn_core_search, the worth pass scored the 4 selection batches whole. It runs without gradients, but each batch still makes logits for every token: 2.1 GB at 32 x 512 with the 32k vocabulary, and the loss makes a second tensor that size. So --accum capped the training step's memory but not this pass.

selection_batches() now cuts each batch into --accum equal pieces. group_worth averages the per-piece losses, so the worth does not change. With --accum 1 the list is the same as before.

The new test checks that the pieces hold the same tokens and give the same base loss and slot worth on a tiny TONN model. It fails if the cut is removed or if group_worth divides by a fixed count. A tiny CPU run with --accum 2 finished its search round at step 250 with 8 pieces of 4 rows.
Ten 3,000-step runs of the 13M hybrid on the public stand-in packs, each
with --accum 2 on an 8 GB laptop GPU. Seed 0 swept the learning rate for
both optimizers, and both were best at 4e-3. Seed 1 then ran at that rate.

At 4e-3 Muon's mean bits per byte was lower by 0.017 on seed 0 and 0.030
on seed 1, and lower on all four kinds of text. That is two seeds at one
size, so the README says what it does not show. It also has the table and
the commands to rebuild it.
)

The README to-do for exea-labs#21, the optimizer row in docs/training.md and the
Muon entry in docs/ideas.md now give the 13M result and link
results/muon: both optimizers did best at 4e-3, and Muon was lower by
0.017 and 0.030 bits per byte on the two seeds. Each also says it has
not been tried at 0.1B, so AdamW stays the default.
A batch of 16 fits the 4060 whole, so AdamW at 4e-3 ran three times at
batch 16: one batch on seeds 0 and 1, and two halves on seed 0. The
split scored 0.018 bits per byte worse on seed 0, worse on all four
kinds of text, against 0.013 between the two seeds. So the README no
longer says the split changes results only a little.

Muon also ran with one batch of 16 on seed 0 and was 0.035 lower than
AdamW, so its lead in the main table does not come from the split. The
README has the table, the commands and the part of the code the gap
could come from.
…-labs#21)

The README called the Muon gap about three times the seed spread. That
compared a difference of two runs with the spread of one run, measured
on other data at another rate. It now gives the ratio per seed (2.3 and
4.2) and says the spread is only a rough yardstick.

The --accum paragraph also said the first step differs most, which the
runs never measured. It now says what the code does: on the first step
the second half is divided by the first half's measured size instead
of 1, and after that the running average moves twice per step.
@Aadi-Tiwari
Aadi-Tiwari marked this pull request as ready for review November 21, 2026 22:43
@AvnehSBhatia
AvnehSBhatia merged commit fd70f9f into exea-labs:main Nov 22, 2026
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.

Muon vs AdamW on the small preset

2 participants