automuon
Automatically split your PyTorch model's parameters between Muon and Adam.
Installation
Requires Python 3.12+ and PyTorch 2.9+.
pip install automuon
Quickstart
Mark modules or parameters you want to exclude from Muon with without_muon, then pass the model's parameters to get_muon_and_adam. It returns two optimizers with their parameters already assigned: Muon (torch.optim.Muon) and Adam (torch.optim.AdamW).
Parameters that are not 2D always go to Adam, regardless of marks or rules.
import torch.nn as nn
from automuon import without_muon, get_muon_and_adam
class Model(nn.Module):
def __init__(self):
super().__init__()
self.embedding = without_muon(nn.Embedding(...))
self.transformer_encoder = nn.TransformerEncoder(...)
self.head = without_muon(nn.Linear(...))
model = Model()
optimizer_muon, optimizer_adam = get_muon_and_adam(
model.parameters(),
muon_args={"lr": 1e-3},
adam_args={"lr": 1e-3, "betas": (0.9, 0.95)},
)
You can also use a rule to assign parameters without marking them. Pass model.named_parameters() so the rule can inspect parameter names:
class Model(nn.Module):
def __init__(self):
super().__init__()
self.embedding = nn.Embedding(...)
self.transformer_encoder = nn.TransformerEncoder(...)
self.head = nn.Linear(...)
model = Model()
optimizer_muon, optimizer_adam = get_muon_and_adam(
model.named_parameters(),
rule=lambda name, param: ("embed" not in name) and ("head" not in name),
)
API Reference
Marking Functions
Four marking functions are available:
from automuon import with_muon, without_muon, with_adam, without_adam
with_muon and without_adam are equivalent, as are without_muon and with_adam. Use whichever reads more naturally in your code.
These functions mark an nn.Module or nn.Parameter for assignment to Muon or Adam when passed to get_muon_and_adam. They return the original object. Marking a module marks all of its current parameters, including those in its submodules.
For unmarked 2D parameters, rule applies if provided; otherwise, the default argument determines the optimizer ("muon" by default).
If a parameter is marked multiple times, the mark from deeper in the module hierarchy takes precedence. At the same depth, the first mark wins.
In this example, model.layer.fc1.weight goes to Muon, and all other parameters go to Adam:
class Layer(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = with_muon(nn.Linear(...))
self.fc2 = nn.Linear(...)
class Model(nn.Module):
def __init__(self):
super().__init__()
self.layer = without_muon(Layer())
model = Model()
get_muon_and_adam
Splits parameters between torch.optim.Muon and torch.optim.AdamW and returns the two optimizer instances, in that order.
get_muon_and_adam(
params: Params,
muon_args: dict | None = None,
adam_args: dict | None = None,
rule: Rule | None = None,
default: Literal["muon", "adam"] = "muon",
) -> tuple[torch.optim.Muon, torch.optim.AdamW]
params: An iterable ofnn.Parameterobjects, such asmodel.parameters(). Whenruleis provided, pass an iterable of(name, param)tuples instead, such asmodel.named_parameters().muon_args: A dictionary of keyword arguments passed to torch.optim.Muon.adam_args: A dictionary of keyword arguments passed to torch.optim.Adam.rule: A callable invoked asrule(name, param)for unmarked 2D parameters. ReturnTruefor Muon orFalsefor Adam.default: The optimizer for parameters not assigned by the conditions below. Either"muon"(the default) or"adam".
Assignment Priority
Parameters are assigned in the following order of precedence:
- Dimensionality: Parameters that are not 2D always go to Adam.
- Marks: An explicit mark determines the optimizer.
- Rule: The result of
rule(name, param)determines the optimizer. - Default: The
defaultargument is used as the fallback.
Metadata
Release files for automuon 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| automuon-0.1.0.tar.gz | 28.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| automuon-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 34.2 kB
Release files / automuon-0.1.0.tar.gz
| Download URL | automuon-0.1.0.tar.gz |
|---|---|
| Size | 28.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c0207c77bbfa1f39640d17ee1bb4f5f366f3b7d852947f45a5581353cd236611
|
|
BLAKE2b-256 checksum How to use checksums |
57a498db7cbfcb1761d1fa459ef473d117a6b326512110cddf873673637354b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / automuon-0.1.0-py3-none-any.whl
| Download URL | automuon-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
762012fcfe96fbab20ab6d8780d8af0d5836f67e085972ebeb2069701dcfcbd5
|
|
BLAKE2b-256 checksum How to use checksums |
0fc1d853999224df5c1b64654c00d9f9e354a0456274effd70723dab5448375d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|