Skip to main content

automuon

PyPI Python PyTorch

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 of nn.Parameter objects, such as model.parameters(). When rule is provided, pass an iterable of (name, param) tuples instead, such as model.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 as rule(name, param) for unmarked 2D parameters. Return True for Muon or False for 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:

  1. Dimensionality: Parameters that are not 2D always go to Adam.
  2. Marks: An explicit mark determines the optimizer.
  3. Rule: The result of rule(name, param) determines the optimizer.
  4. Default: The default argument 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)

Source distribution for automuon 0.1.0
File Size Uploaded
automuon-0.1.0.tar.gz 28.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for automuon 0.1.0
File Interpreter ABI Platform
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}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page