Learning-Rate Schedules #
Pure learning-rate schedules used by TorchLean training loops and examples. A Config describes a
schedule, and Config.rate evaluates it at an optimizer step.
References #
- PyTorch schedulers:
torch.optim.lr_scheduler.*(https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate)
Instances For
Small learning-rate scheduler surface for higher-level training code.
This file keeps the interface compact: a Config is just a description of a schedule,
and config.rate stepIndex computes the learning rate after that many optimizer updates.
PyTorch mapping #
Config.step and Config.exponential correspond to the schedule math of:
torch.optim.lr_scheduler.StepLRtorch.optim.lr_scheduler.ExponentialLR
Config.warmupCosine is the schedule commonly used for Transformer pretraining: a short linear
warm-up followed by cosine decay to a nonzero floor.
All indices count completed optimizer updates, starting at zero. oneCycle uses explicit integer
phase lengths; it does not reproduce PyTorch's fractional pct_start * total_steps - 1 boundaries.
- constant (lr : Float) : Config
- step (baseLr : Float) (stepSize : Nat) (decayFactor : Float := 0.1) : Config
- exponential (baseLr decayFactor : Float) : Config
- linearWarmup
(startLr peakLr : Float)
(warmupSteps : Nat)
: Config
Increase from
startLrat step zero topeakLratwarmupSteps, then hold the peak. Zero warm-up steps select the peak immediately. - cosine
(baseLr minLr : Float)
(halfCycleSteps : Nat)
(periodic : Bool := false)
: Config
Decay from
baseLrtominLroverhalfCycleSteps, then hold the minimum. Withperiodic := true, continue back to the base rate and repeat every2 * halfCycleSteps. A zero half-cycle selects the minimum immediately. - cyclic
(baseLr peakLr : Float)
(halfCycleSteps : Nat)
(cycleDecay : Float := 1.0)
: Config
Repeated linear rises and falls, each lasting
halfCycleSteps. The first cycle runs frombaseLrtopeakLrand back.cycleDecayscales the amplitude once per completed cycle:1gives fixed triangles and0.5halves each successive peak's distance above the base. A zero half-cycle is invalid. - oneCycle
(initialLr peakLr finalLr : Float)
(riseSteps totalSteps : Nat)
(annealing : Annealing := Annealing.cosine)
: Config
Rise from
initialLrtopeakLratriseSteps, then fall tofinalLrattotalStepsand hold it. Both phases useannealing; only the learning rate changes. Validation requires0 < riseSteps < totalStepsand both endpoints at or below the peak. - warmupCosine
(peakLr minLr : Float)
(warmupSteps totalSteps : Nat)
: Config
Linearly warm up to
peakLr, then follow a cosine curve down tominLr.warmupStepscounts optimizer updates. The first update usespeakLr / warmupSteps, and the last warm-up update reachespeakLr. OncetotalStepsupdates have been scheduled, the learning rate remains atminLr. A warm-up longer than the run is clamped tototalSteps. WhentotalSteps = 0, no update belongs to the schedule andConfig.ratereturnsminLr.
Instances For
Interpolate inside a phase, returning its exact endpoints at the boundaries. Roundoff near an
endpoint must not exceed the range checked by Config.validate, even near overflow.
Instances For
Validate a learning-rate schedule before it is attached to optimizer state.
The checks keep every scheduled rate finite and nonnegative. Step and exponential schedules use
decay factors in [0, 1]; a zero step size is rejected instead of silently changing the schedule
to a constant rate.
Instances For
Validate a schedule, optionally checking its largest rate after scalar conversion.
round reports the converted value in binary64. Its default leaves rates unchanged; a runtime
supplies its scalar conversion to catch overflow before training. Schedule arithmetic remains
binary64; each resulting rate is converted when the optimizer state is updated.
Instances For
Learning rate after the given number of completed optimizer updates.