Skip to content

Add Muon optimizer - #607

Merged
fynnsu merged 3 commits into
vllm-project:mainfrom
eldarkurtic:feature-muon
Jun 18, 2026
Merged

fynnsu merged 3 commits into
vllm-project:mainfrom
eldarkurtic:feature-muon

Conversation

@eldarkurtic

Copy link
Copy Markdown
Collaborator

We recently found and validated that Muon optimizer performs much better than Adam in training of speculator models.
Validated on two models: decoder-only (Qwen3-8B) and an MoE (Laguna-XS.2).
Training setup is the standard Magpie dataset, 50k samples for 1 epoch.
Validation setup: an extensive LR sweep for both Adam and Muon.

Test example 1:
a) Qwen3-8B, DFlash, LR sweep, metric=accuracy
image

b) Qwen3-8B, DFlash, LR sweep, metric=loss
image

Test example 2:
a) Laguna-XS.2, DFlash, LR sweep, metric=accuracy
image

b) Laguna-XS.2, DFlash, LR sweep, metric=loss
image

A few notes regarding the implementation:

  • it supports single and multi-gpu training
  • Muon optimizer can be used only for 2D weight matrices, so Adam is used for others
  • this means we need to enable support for "a list of optimizers"

Signed-off-by: Eldar Kurtic <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b7757a53-f0a6-4b14-9098-39857764c9fb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds Muon as a second optimizer choice alongside AdamW. A new optimizers.py module partitions model parameters by tensor rank and name, routes 2D non-embedding parameters to torch.optim.Muon and the rest to torch.optim.AdamW, and returns a list. The trainer, checkpointer, and CLI are all updated to handle lists of optimizers and schedulers.

Changes

Muon Optimizer Integration

Layer / File(s) Summary
Optimizer factory and parameter routing
src/speculators/train/optimizers.py, src/speculators/train/trainer.py
New split_named_params_for_muon partitions trainable parameters into Muon (2D, non-embed/non-lm-head) and AdamW groups. build_optimizers returns a single AdamW or a Muon+AdamW pair based on config.optimizer. TrainerConfig is extended with optimizer, weight_decay, and all Muon-specific fields; build_optimizers is imported.
Trainer multi-optimizer and multi-scheduler wiring
src/speculators/train/trainer.py
setup_optimizer calls build_optimizers to populate self.optimizers; per-optimizer schedulers are built into self.schedulers. New helper methods _zero_grad_all, _optimizers_step, and _schedulers_step replace single-instance calls. Periodic and best-checkpoint saves pass the full optimizer and scheduler collections.
Checkpointer list-capable optimizer and scheduler APIs
src/speculators/train/checkpointer.py
Adds OptimizerOrList, SchedulerOrList type aliases and _as_list normalizer. All abstract and concrete load_optimizer_state_dict, save_checkpoint, and load/save_scheduler_state_dict signatures are widened to accept either a single instance or a list, with backward-compatible single-entry serialization. DistributedCheckpointer iterates the optimizer list to fix step tensor dtypes after restore.
CLI arguments and test fixture update
scripts/train.py, tests/unit/train/test_checkpoint.py
parse_args adds --optimizer, --weight-decay, --muon-lr, --muon-momentum, --muon-weight-decay, --muon-ns-steps, and --muon-adjust-lr-fn; main() threads these into TrainerConfig. Test helper _make_minimal_trainer is updated to use trainer.optimizers (list) and trainer.schedulers (empty list).

Possibly related PRs

  • vllm-project/speculators#411: Modifies DistributedCheckpointer.load_optimizer_state_dict to cast optimizer "step" tensors back to float32 on resume, the same fix this PR extends across a list of optimizers.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add Muon optimizer' is concise and clearly summarizes the primary change across the changeset.
Description check ✅ Passed The description is directly related to the changeset, providing validation results and implementation notes about Muon optimizer support.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@scripts/train.py`:
- Around line 782-815: The numeric hyperparameter arguments (--weight-decay,
--muon-lr, --muon-momentum, --muon-weight-decay, --muon-ns-steps) lack bounds
validation, allowing invalid values like negative learning rates, non-positive
muon-ns-steps, or out-of-range momentum. Add validation constraints to each
parser.add_argument call by using custom type functions or validation logic that
enforces: non-negative values for weight-decay and muon-weight-decay, positive
values for muon-lr and muon-ns-steps, and valid range (typically 0 to 1) for
muon-momentum. Apply these constraints directly in the argument parser
configuration to catch invalid inputs early before training begins.

In `@src/speculators/train/optimizers.py`:
- Around line 63-70: In the AdamW optimizer initialization within the
config.optimizer == "adamw" block, replace `model.named_parameters()` with
`model.parameters()`. The torch.optim.AdamW optimizer expects an iterable of
parameter tensors, but named_parameters() yields (name, param) tuples which will
cause a runtime error during optimization.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9c2175cb-a757-4c08-adb8-9a69ebb7cec4

📥 Commits

Reviewing files that changed from the base of the PR and between d1a3ff3 and d024a63.

📒 Files selected for processing (5)
  • scripts/train.py
  • src/speculators/train/checkpointer.py
  • src/speculators/train/optimizers.py
  • src/speculators/train/trainer.py
  • tests/unit/train/test_checkpoint.py

Comment thread scripts/train.py Resolved
Comment thread src/speculators/train/optimizers.py Resolved
@fynnsu fynnsu added the ready This PR is ready for review label Jun 15, 2026
@mergify mergify Bot added the documentation Improvements or additions to documentation label Jun 15, 2026
fynnsu added 2 commits June 15, 2026 16:38
… multi-optimizer LR logging

- Use strict=True in zip when loading optimizer/scheduler state dicts to
  fail loudly on count mismatch instead of silently dropping state
- Return named parameter tuples from split_named_params_for_muon for
  consistency with the adamw path
- Log per-optimizer LRs as a dict when multiple optimizers are active

Co-Authored-By: Claude Opus 4.6 <[email protected]>


Signed-off-by: Fynn Schmitt-Ulms <[email protected]>
Signed-off-by: Fynn Schmitt-Ulms <[email protected]>
@fynnsu
fynnsu requested a review from shanjiaz June 17, 2026 15:00

@rahul-tuli rahul-tuli left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@fynnsu fynnsu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking till e2e is done:
https://github.com/neuralmagic/llm-compressor-testing/actions/runs/27772187815

Just want to make sure the switch to multi-optimizers doesn't break anything.

@fynnsu fynnsu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E2E passed

@fynnsu
fynnsu merged commit f7ce702 into vllm-project:main Jun 18, 2026
12 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ready This PR is ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants