kempnerforge.training.optimizer¶
Optimizer construction for KempnerForge.
- Builds optimizers with per-parameter-group settings:
AdamW: standard Adam with decoupled weight decay
Muon: momentum with orthogonalized updates via Newton-Schulz iteration. Applies Muon to hidden nn.Linear weights, AdamW to the rest (embeddings, output head, norms, biases).
Lion: sign-based momentum update (half the optimizer memory of AdamW)
Schedule-Free AdamW: eliminates LR schedule by averaging iterates
- All optimizers:
Weight decay follows optimizer.weight_decay_scope: by default every parameter with ndim > 1 and no “bias” in its name is decayed, and “all” decays every parameter
optimizer.lr_multipliers gives each listed module its own groups, at lr times the module’s multiplier
Fused kernel when available (PyTorch 2.x, AdamW only)
Functions
|
Construct an optimizer with per-parameter-group weight decay settings. |
|
Hold the parameters at |
|
Return the rate each module |
Classes
Lion optimizer (Chen et al., 2023): Evolved Sign Momentum. |
|
Muon optimizer: Momentum with Orthogonalized Updates. |
|
Schedule-Free AdamW (Defazio & Mishchenko, 2024). |
- class kempnerforge.training.optimizer.Lion[source]¶
Bases:
OptimizerLion optimizer (Chen et al., 2023): Evolved Sign Momentum.
Uses sign-based updates with momentum interpolation. Only maintains one momentum buffer (vs two for AdamW), halving optimizer memory.
Update rule:
update = sign(beta1 * m + (1 - beta1) * grad) m = beta2 * m + (1 - beta2) * grad p = p * (1 - lr * wd) - lr * update
- Parameters:
params – Parameters or parameter groups.
lr – Learning rate (typically 3-10x smaller than AdamW).
betas –
(beta1, beta2)for update interpolation and momentum.weight_decay – Decoupled weight decay coefficient.
- step(closure=None)¶
- class kempnerforge.training.optimizer.ScheduleFreeAdamW[source]¶
Bases:
OptimizerSchedule-Free AdamW (Defazio & Mishchenko, 2024).
Eliminates the need for an LR schedule by maintaining an iterate
zand a running averagex. Parameters are set to the interpolated pointy = (1 - beta1) * z + beta1 * xfor gradient computation.Each parameter’s
stepcounts the steps it had a gradient in, and its warmup, second-moment bias correction and average all run on that count, so a parameter whose first gradient comes late starts as a fresh one does. The count is optimizer state, whichget_optimizer_state_dictkeeps, so a checkpoint resumes it.Use with
scheduler.name = "none"— no LR schedule is needed.- Parameters:
params – Parameters or parameter groups.
lr – Learning rate (constant — no schedule needed).
betas –
(beta1, beta2)for interpolation and second moment.eps – Denominator term for numerical stability.
weight_decay – Decoupled weight decay.
warmup_steps – Linear warmup steps (internal to the optimizer).
- __init__(params, lr=0.025, betas=(0.9, 0.999), eps=1e-08, weight_decay=0.0, warmup_steps=0)[source]¶
- load_state_dict(state_dict)[source]¶
Load
state_dict, also one saved before each parameter kept its ownstep.Such a state carries one optimizer-wide count as
_k, which every parameter with state resumes from, and its warmup as_warmup_steps.- Parameters:
state_dict (dict)
- Return type:
None
- step(closure=None)¶
- eval_params()[source]¶
Set parameters to the evaluation point (running average).
Call before validation/inference for best results. Call
train_params()afterward to resume training.- Return type:
None
- train_params()[source]¶
Restore parameters to the training point (interpolated y), bit for bit.
Call after
eval_params()to resume training. A parameter without state stays as it is, and no state is created for it.- Return type:
None
- kempnerforge.training.optimizer.evaluation_point(optimizer, model=None)[source]¶
Hold the parameters at
optimizer’s evaluation point inside the block.An optimizer that evaluates at another point than it trains at, as
ScheduleFreeAdamWdoes, haseval_paramsandtrain_params: the block runs between them, and the parameters leave it at the training point again, bit for bit, also when the swap or the block raises. Any other optimizer leaves them as they are.The swap writes the sharded parameters. An FSDP2 unit that does not reshard after its forward (
reshard_after_forwardFalse or an int, or the root under the default None) stays gathered after a forward in the block, holdingx, and the next training forward would reuse it. So passmodelwhenever the block runs a forward: every FSDP2 unit in it is resharded before the swap in and after the swap back, and each forward gathers the point it should see. Resharding a resharded unit does nothing.- Parameters:
optimizer (torch.optim.Optimizer)
model (torch.nn.Module | None)
- Return type:
Iterator[None]
- class kempnerforge.training.optimizer.Muon[source]¶
Bases:
OptimizerMuon optimizer: Momentum with Orthogonalized Updates.
For 2D+ weight matrices: maintains momentum, then orthogonalizes the update direction via Newton-Schulz iteration. This keeps update directions independent of parameter scale.
For the rest (embeddings, the output head, norms, biases): uses standard AdamW.
FSDP2 note: with
orthogonalize="local", the default, Newton-Schulz runs on each rank’s shard of a weight on its own. That approximates the full-matrix update, and the gap changes with the shard count and the matrix. Split by rows into 2, 4 and 8 shards, as FSDP2 splits a weight, 1024x512, 512x1024 and 1024x1024 Gaussian matrices get local updates with a cosine similarity of 0.84 to 0.94 to the full-matrix update and 0.80 to 1.05 times its RMS. With singular values that fall geometrically over three decades, the cosine similarity drops to 0.62 to 0.91, at 0.97 to 1.21 times the RMS.orthogonalize="exact"gives the full-matrix update.The AdamW fallback follows Muon’s schedule: each step sets every AdamW group’s rate to its base rate, its own
lror elseadam_lr, times the ratio of Muon’s first group’s current rate to that group’s base rate. Withoutadam_lrthe fallback’s base rates are Muon’s, and that group’s base rate is the one it was built with. Withadam_lrit is the peak a scheduler runs the group from, itsinitial_lr, when one set it, so a load that restores a schedule saved under anotherlrkeepsadam_lr.- Parameters:
muon_params – Parameter groups for Muon (the hidden
nn.Linearweights).adam_params – Parameter groups for AdamW fallback (every other parameter).
lr – Learning rate for Muon.
momentum – Momentum coefficient (default 0.95).
weight_decay – Decoupled weight decay of every param group, Muon’s or the AdamW fallback’s, that sets no
weight_decayof its own.adam_betas – Betas for the AdamW fallback.
adam_eps – Epsilon for the AdamW fallback.
ns_steps – Newton-Schulz iteration steps (default 5).
adam_lr – Learning rate for AdamW fallback. None = same as lr.
nesterov – Feed Newton-Schulz the Nesterov look-ahead instead of the momentum buffer.
adjust_lr – Update scale: “original” sqrt(max(1, rows/cols)), “match_rms_adamw” 0.2 * sqrt(max(rows, cols)), or “spectral_unclamped” sqrt(rows/cols).
orthogonalize – “local” runs Newton-Schulz on each rank’s shard, “exact” on the full matrix.
ns_dtype – “fp32” or “bf16”, the dtype Newton-Schulz iterates in. With “bf16” each update is cast to bf16 before it is orthogonalized, so “exact” also gathers and scatters bf16 shards, and the result is cast back to the update’s dtype before it is scaled and applied.
- LEGACY_GROUP_DEFAULTS: ClassVar[dict[str, Any]] = {'adjust_lr': 'original', 'nesterov': False, 'ns_dtype': 'fp32', 'orthogonalize': 'local'}¶
- __init__(muon_params, adam_params, lr=0.02, momentum=0.95, weight_decay=0.0, adam_betas=(0.9, 0.95), adam_eps=1e-08, ns_steps=5, adam_lr=None, nesterov=False, adjust_lr='original', orthogonalize='local', ns_dtype='fp32')[source]¶
- property inner_optimizers: tuple[torch.optim.Optimizer, ...]¶
Optimizers holding parameters that Muon’s own
param_groupsdo not list.
- state_dict()[source]¶
Include internal AdamW state in the plain
state_dict(), and the base rates the fallback follows Muon’s schedule from when a param group has a rate of its own.- Return type:
- load_state_dict(state_dict)[source]¶
Restore internal AdamW state from checkpoint.
A state without the fallback’s base rates keeps this optimizer’s own, and so does one whose rates do not number one per AdamW fallback group, with a warning.
- Parameters:
state_dict (dict)
- Return type:
None
- zero_grad(set_to_none=True)[source]¶
Zero both Muon’s own parameters and the inner AdamW’s.
Only the Muon half is registered with the base class, so the inherited
zero_gradwould leave every delegated parameter accumulating.- Parameters:
set_to_none (bool)
- Return type:
None
- step(closure=None)¶
- kempnerforge.training.optimizer.module_learning_rates(optimizer)[source]¶
Return the rate each module
build_optimizergave its own groups steps at next.Keyed by the metric it is logged as,
lr/<module>. Under Muon that is the rate of the module’s orthogonalized matrices, andlr_adamw/<module>is that of its AdamW fallback, each present when the module has such parameters. No key nests under another metric’s name, which a file-based tracking store cannot hold. Empty whenoptimizer.lr_multiplierswas empty.- Parameters:
optimizer (torch.optim.Optimizer)
- Return type:
- kempnerforge.training.optimizer.build_optimizer(model, config, module_patterns=None)[source]¶
Construct an optimizer with per-parameter-group weight decay settings.
With
config.lr_multipliersset, each listed module’s parameters get their own groups atconfig.lrtimes the module’s multiplier, named by"module"; the scheduler then scales every group’s own rate by the same factor. A module nested in another listed one keeps its own parameters.- Parameters:
model (torch.nn.Module) – Model whose parameters to optimize.
config (OptimizerConfig) – Optimizer configuration.
module_patterns (Mapping[str, Sequence[str]] | None) – Module name to the fnmatch patterns of its parameter names, such as
VLMConfig.module_patterns. Required whenconfig.lr_multipliersis set.
- Returns:
Configured optimizer instance.
- Raises:
ValueError –
config.lr_multipliersnames a modulemodule_patternslacks or one that matches no parameter, or two of its modules match the same parameters or parameters that partly overlap.- Return type: