PyTorch-Compatible Learning-Rate Schedulers #
Schedulers whose phase boundaries and step counters follow the corresponding
torch.optim.lr_scheduler behavior. They remain pure Lean state machines, so a training run can
store, inspect, and reason about the exact scheduler state without calling PyTorch.
Schedulers.Core documents the zero-indexed counter convention, shared scalar operations, and
literature. The Native module provides simpler total schedules when compatibility is not the
contract.
PyTorch-compatible scheduler variants #
The schedulers below use formulas and step-count conventions chosen to match PyTorch's
torch.optim.lr_scheduler.* semantics more directly.
Important convention note (PyTorch last_epoch):
- In modern PyTorch, schedulers effectively start at
last_epoch = 0right after construction (fresh run withlast_epoch = -1in the constructor triggers an initial internal step). - We model that behavior by using a
current_step : Nat := 0counter. Think:current_stepcorresponds to PyTorch'slast_epochafter construction.
These schedulers are LR-only (they do not mutate optimizer momentum/betas). If you need the full PyTorch OneCycle momentum behavior, consider adding a separate momentum schedule and stepping both in lockstep.
StepLR #
PyTorch-compatible StepLR.
Semantics:
current_step = 0yieldsbase_lr.- Every
step_sizesteps, multiply LR bygamma. - When
step_size = 0, this degenerates to a constant schedule (total, no exceptions).
PyTorch reference: torch.optim.lr_scheduler.StepLR.
- baseLr : α
Base learning rate (what PyTorch calls
base_lrs[i]). - stepSize : ℕ
Step interval (
step_size). - gamma : α
Multiplicative decay factor (
gamma). - currentStep : ℕ
Step counter matching PyTorch
last_epochafter construction (0-indexed).
Instances For
CosineAnnealingLR #
PyTorch-compatible CosineAnnealingLR.
Key behavior difference from TorchLean's CosineAnnealingScheduler above:
- PyTorch's
CosineAnnealingLRcontinues the cosine curve pastT_max(it is periodic with period2*T_max), rather than clamping toeta_min.
PyTorch reference: torch.optim.lr_scheduler.CosineAnnealingLR.
- baseLr : α
Base learning rate (
base_lrs[i]). - tMax : ℕ
Maximum number of steps in a half-cycle (
T_max). - etaMin : α
Minimum learning rate (
eta_min). - currentStep : ℕ
Step counter matching PyTorch
last_epochafter construction (0-indexed).
Instances For
Current learning rate for CosineAnnealingLR at current_step.
Instances For
Advance CosineAnnealingLR by one step.
Instances For
Constructor for CosineAnnealingLR starting at current_step = 0.
Instances For
OneCycleLR (LR-only) #
Anneal strategy used by OneCycleLR (matches PyTorch "cos" or "linear").
- cos : OneCycleAnnealStrategy
- linear : OneCycleAnnealStrategy
Instances For
Instances For
PyTorch-compatible OneCycleLR (LR-only).
Notes:
- This mirrors PyTorch's
OneCycleLRlearning-rate schedule only. PyTorch can also cycle momentum (or Adam'sbeta1); TorchLean keeps this scheduler pure and LR-only. - PyTorch defines:
initial_lr = max_lr / div_factormin_lr = initial_lr / final_div_factor(note:min_lris derived frominitial_lr, not directly frommax_lr).
- PyTorch uses "phase end steps" that are floats:
- phase 1 ends at
pct_start * total_steps - 1 - phase 2 ends at
total_steps - 1(andthree_phaseinserts a middle phase). This means the boundary can be fractional; the schedule uses interpolation ratios (pct) computed from these float endpoints. We match that behavior usingαarithmetic.
- phase 1 ends at
PyTorch reference: torch.optim.lr_scheduler.OneCycleLR.
- maxLr : α
Peak learning rate (
max_lr). - totalSteps : ℕ
Total number of steps (
total_steps). - pctStart : α
Fraction of steps spent increasing LR (
pct_start). - divFactor : α
div_factorused to deriveinitial_lr = max_lr / div_factor. - finalDivFactor : α
final_div_factorused to derivemin_lr = initial_lr / final_div_factor. - annealStrategy : OneCycleAnnealStrategy
- threePhase : Bool
Use PyTorch's
three_phasevariant whentrue. - currentStep : ℕ
Step counter matching PyTorch
last_epochafter construction (0-indexed).
Instances For
Derived initial LR (max_lr / div_factor).
Instances For
Derived minimum LR (initial_lr / final_div_factor).
Instances For
PyTorch-compatible anneal helper (no clamping).
Instances For
Current learning rate for OneCycleLR at current_step (LR-only).
Instances For
Advance OneCycleLR by one step.
Instances For
Constructor for OneCycleLR starting at current_step = 0 (LR-only).
This mirrors the PyTorch parameterization:
initial_lr = max_lr / div_factormin_lr = initial_lr / final_div_factor- phase endpoints computed as
pct_start * total_steps - 1andtotal_steps - 1(with the optionalthree_phasemiddle phase).