This page contains the API reference documentation for the optimizers included in timm, and an overview of how
Optimizers
This page contains the API reference documentation for the optimizers included in timm, and an overview of how
they are selected and configured through the optimizer factory.
Usage
Optimizers are created by name with create_optimizer_v2. Given a model, the
factory builds the parameter groups (weight decay exclusions, optional layer-wise learning rate decay) before
constructing the optimizer. Optimizer specific arguments pass through as keyword arguments.
import timm
from timm.optim import create_optimizer_v2, list_optimizers
model = timm.create_model('vit_base_patch16_224')
# AdamW. 1D params (biases, norm scales) and params in model.no_weight_decay() are excluded from weight decay.
optimizer = create_optimizer_v2(model, opt='adamw', lr=1e-3, weight_decay=0.05)
# Layer-wise learning rate decay (fine-tuning), per-layer lr scales from the model's group_matcher().
optimizer = create_optimizer_v2(model, opt='adamw', lr=1e-3, weight_decay=0.05, layer_decay=0.75)
# Optimizer specific arguments are passed through.
optimizer = create_optimizer_v2(model, opt='lamb', lr=5e-3, weight_decay=0.02, trust_clip=True)
# A list of params or param groups can be passed instead of a model, weight decay grouping is then up to the caller.
optimizer = create_optimizer_v2(model.parameters(), opt='sgd', lr=0.1, momentum=0.9)
# All registered names, optionally with a description.
print(list_optimizers(with_description=True))
The training script (train.py) exposes the same through these flags:
--optselects the optimizer.--lrsets the learning rate.--weight-decaysets weight decay.--momentumsets momentum.--opt-epssets epsilon (eps).--opt-betassets the beta coefficients (betas).--layer-decaysets layer-wise learning rate decay.--opt-kwargs key=value ...passes any other optimizer arguments.
Naming conventions
Registered names are lower case, a base optimizer name plus modifiers:
| Modifier | Meaning | Examples |
|---|---|---|
c prefix |
Cautious variant, the update is masked where its sign disagrees with the gradient (Cautious Optimizers) | cadamw, clion, cmars, cadopt |
w suffix |
Decoupled (AdamW style) weight decay, for optimizers that default to coupled L2 decay | sgdw, lambw, adoptw, novogradw, rmsproptfw |
c suffix |
Corrected weight decay, the decay is scaled by lr² / max_lr instead of lr (Why Gradients Rapidly Increase Near the End of Training) |
adamc, nadamc, sgdc, lionc, adoptc, kronc |
n prefix |
Nesterov momentum (SGD, LARS and Muon families) | nesterov, nlars, nmuon, nadamuon |
lookahead_ prefix |
Wrap the optimizer in Lookahead |
lookahead_adamw |
fused prefix |
NVIDIA Apex fused implementation (requires apex) |
fusedadamw, fusedlamb |
bnb prefix |
bitsandbytes implementation, 8bit suffix for 8-bit state (requires bitsandbytes) |
bnbadamw, bnbadamw8bit |
A few names predate the conventions: lambc and larc are LAMB / LARS with trust ratio clipping (the corrected
decay LAMB variant is lambcd), momentum is SGD with classical momentum and sgd is SGD with Nesterov momentum.
The legacy implementations in the table below predate their inclusion in PyTorch.
Optimizer families
表格(24 行)
| Names | Implementation | Notes |
|---|---|---|
adamw, adam, nadam, radam, radamw, adamax, adadelta, adagrad, rmsprop, sgd, momentum, nesterov |
torch.optim |
|
adamwlegacy, adamc, cadamw, cadamc |
AdamWLegacy |
AdamW with caution, corrected decay, update clipping, capturable |
nadamw, nadamc, cnadamw, cnadamc |
NAdamW |
AdamW with Nesterov momentum (MLCommons algorithmic-efficiency form) |
adopt, adoptw, adoptc, cadopt, cadoptw, cadoptc |
Adopt |
Adam variant that converges for any β2 |
adan, adanw, cadan, cadanw |
Adan |
Adaptive Nesterov momentum |
adabelief, radabelief |
AdaBelief |
radabelief adds RAdam style rectification |
adafactor, cadafactor |
Adafactor |
fairseq Adafactor, relative step size when lr=None |
adafactorbv, adafactorbvc, cadafactorbv, cadafactorbvc |
AdafactorBigVision |
Big Vision Adafactor, factored second moments, low precision momentum, update clipping |
adamp, cadamp, sgdp, csgdp |
AdamP, SGDP |
Projection of the update for scale invariant weights |
adahessian |
Adahessian |
Second order, requires a closure (is_second_order) |
kron, kronw, kronc |
Kron |
PSGD with a Kronecker-factored preconditioner |
lamb, lambc, lambw, lambcw, lambcd, clamb, clambw |
Lamb |
Layer-wise adaptive moments, trust_clip variants (lambc, lambcw) |
laprop, lapropc, claprop |
LaProp |
Momentum and adaptivity separated |
lars, larc, nlars, nlarc |
Lars |
Layer-wise adaptive rate scaling, trust_clip variants (larc, nlarc) |
lion, lionc, clion, clionc |
Lion |
Evolved sign momentum |
madgrad, madgradw |
MADGRAD |
Momentum based dual averaging |
mars, cmars |
Mars |
Variance reduction, AdamW or Lion style update |
muon, nmuon, adamuon, nadamuon |
Muon |
Newton-Schulz orthogonalized momentum for 2D+ weights, AdamW / NAdamW fallback for 1D params |
novograd, novogradw, novogradc |
NvNovoGrad |
Layer-wise normalized moments |
rmsproptf, rmsproptfw, rmsproptfc, crmsproptf |
RMSpropTF |
TensorFlow ordering of ops and eps handling, lr folded into the momentum buffer |
sgdw, nesterovw, sgdc, csgdw, csgdc |
SGDW |
SGD with decoupled weight decay |
nadamlegacy, radamlegacy |
NAdamLegacy, RAdamLegacy |
Reference implementations, superseded by torch.optim |
fused*, bnb* |
NVIDIA Apex, bitsandbytes | Registered when the package is installed |
Common options
Most timm optimizers share a set of options beyond the usual lr, weight_decay, betas and eps. Which
optimizers accept which can be checked in the class signatures below, the factory raises on unsupported arguments.
cautionapplies the cautious update mask, equivalent to thecprefixed registry names.corrected_weight_decayscales decoupled weight decay bylr² / max_lrinstead oflr(thecsuffixed names), so the effective decay follows the learning rate schedule.max_lris the learning rate the optimizer was constructed with, construct it with the peak schedule lr (astrain.pydoes). It is kept in each param group (max_lr_snapshot) and saved / restored with the optimizer state.decoupled_decay(ordecoupled) switches optimizers that default to coupled L2 decay to AdamW style decay (thewsuffixed names).foreachselects the multi-tensor implementation. By default (None) it is used on CUDA and other devices with foreach kernels, matchingtorch.optim, unless unsupported by the configuration (a tensorlrwithoutcapturable, orcautionon a PyTorch version without the required_foreach_maximum_overload). The multi-tensor step is run in chunks of parameters sized so each chunk's working set stays in the GPU L2 cache, which is faster than streaming every parameter through memory once per op for large models. The chunk size is derived from the device L2 cache size (PyTorch >= 2.5) and chunking is skipped on devices where the resulting chunk would be below 4M elements.TIMM_FOREACH_CHUNK_SIZE=<elements>overrides the chunk size,0disables chunking.capturable(AdamWLegacy, NAdamW, Adopt, Muon) keeps the step count on the device so the step can be captured in a CUDA graph. All optimizers accept anlrgiven as a 0-dim tensor, updated in place by a schedule, for use withtorch.compileand CUDA graphs.clipping_threshold(AdamWLegacy, NAdamW, AdafactorBigVision) clips the update by its RMS,update / max(1, rms / threshold)withrmsthe RMS of the second-moment normalized gradient, per tensor (StableAdamW, Adafactor).1.0as in the papers. As in Adafactor, the (decoupled) weight decay is not clipped, StableAdamW also scales the decay by the clipped learning rate.maximizemaximizes the objective instead of minimizing.
Low precision parameters (bfloat16, float16) are supported throughout:
- AdaBelief, RAdamLegacy and Adafactor keep their state in
float32for low precision params. - AdafactorBigVision keeps its momentum in a reduced precision (
momentum_dtype). - Kron has separate
mu_dtype/precond_dtypeoptions.
These state dtypes are preserved by load_state_dict (PyTorch's default loader casts state to the param dtype).
The step functions avoid host syncs (data dependent Python branches on device tensors), AdamP / SGDP's projection
check is the exception.
Parameter groups
When given a model, create_optimizer_v2 groups parameters with
param_groups_weight_decay (no decay for 1D params and the model's
no_weight_decay() set) or, with layer_decay set,
param_groups_layer_decay, which assigns a learning rate scale of
layer_decay ** (num_layers - layer_id) per layer using the model's group_matcher(). Models without a
group_matcher() fall back to auto_group_layers, which needs the model's
pretrained_cfg['classifier'] to separate the head. For hybrid optimizers (Muon) the fallback_list and
fallback_no_weight_decay arguments route parameters to the fallback (AdamW) optimizer.
API
Factory functions
[[autodoc]] timm.optim.create_optimizer_v2 [[autodoc]] timm.optim.list_optimizers [[autodoc]] timm.optim.get_optimizer_class [[autodoc]] timm.optim.get_optimizer_info
Parameter group functions
[[autodoc]] timm.optim.param_groups_weight_decay [[autodoc]] timm.optim.param_groups_layer_decay [[autodoc]] timm.optim.auto_group_layers
Optimizer Classes
[[autodoc]] timm.optim.adabelief.AdaBelief [[autodoc]] timm.optim.adafactor.Adafactor [[autodoc]] timm.optim.adafactor_bv.AdafactorBigVision [[autodoc]] timm.optim.adahessian.Adahessian [[autodoc]] timm.optim.adamp.AdamP [[autodoc]] timm.optim.adamw.AdamWLegacy [[autodoc]] timm.optim.adan.Adan [[autodoc]] timm.optim.adopt.Adopt [[autodoc]] timm.optim.kron.Kron [[autodoc]] timm.optim.lamb.Lamb [[autodoc]] timm.optim.laprop.LaProp [[autodoc]] timm.optim.lars.Lars [[autodoc]] timm.optim.lion.Lion [[autodoc]] timm.optim.lookahead.Lookahead [[autodoc]] timm.optim.madgrad.MADGRAD [[autodoc]] timm.optim.mars.Mars [[autodoc]] timm.optim.muon.Muon [[autodoc]] timm.optim.nadam.NAdamLegacy [[autodoc]] timm.optim.nadamw.NAdamW [[autodoc]] timm.optim.nvnovograd.NvNovoGrad [[autodoc]] timm.optim.radam.RAdamLegacy [[autodoc]] timm.optim.rmsprop_tf.RMSpropTF [[autodoc]] timm.optim.sgdp.SGDP [[autodoc]] timm.optim.sgdw.SGDW
Source and license
Source: timm documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.