Repository navigation
Add Muon as an optimizer option and compare it with AdamW on the small preset (#21) - #72
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 21, 2026 22:43
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.
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.pyadds 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,--resumeand the search's slot resets work without changes.--lr, the WSD schedule and weight decay.make_optimizer(model, lr, wd, kind="adamw"), plus--optimizer {adamw,muon}inexealm.train.pretrainandexperiments/tokenizer_lm.py. AdamW is still the default and builds the same optimizer as before. Result rows now record the optimizer.experiments/muon_table.pyprints the learning-rate sweep and the paired difference between the two optimizers at their best rates. I didn't reusefunding_table.pybecause it only pairs runs against the hybrid.experiments/tokenizer_lm.py --accum Nruns 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 2does. Each step sees the same tokens.--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.--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 ofresults/muon/README.md.--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/muonhas the 10 result files,summary.json, the 4 runs of the batch-16 check inaccum_check/, and a README with both tables and the commands.experiments/leave_one_out_data.pybuilds public stand-ins for the private data packs, which these runs read.--fetchdownloads the four sources and the 32k tokenizer.Why not torch.optim.Muon
The project allows torch>=2.3, and
torch.optim.Muonis only in recent versions. Where it exists, a test checks that our Muon steps match it withadjust_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.pyandtests/test_muon_table.pycheck that:torch.optim.AdamWnn.Linearweights other than the embedding and head go on Muon, and an untied head stays on AdamW--optimizer muonlearns, and a run stopped and resumed mid-search ends with exactly the same weightsmuon_table.pypicks each optimizer's rate on seed 0 onlytests/test_tokenizer_lm.pychecks that--accumgives 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 missingzero_gradshows 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_corewithHYBRID_13Mfromexperiments/gpu_session/make_jobs.py). That is thesmallpreset 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: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.
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.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 inresults/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)./ accum, or movingzero_gradinto the micro-batch loop each makes a test fail.Notes
make_optimizer. I didn't change them.--optimizer muon. The two processes end with identical weights through 4 circuit replacements and match one process's architectures, eval is within the test's 0.01 bits, and a run stopped at step 6 resumes exactly. So the two PRs should work together in either merge order. If Train in several processes with DDP (#17) #68 lands first, I'll merge main and reruntests/test_ddp.py.git merge-treeon the head.