Skip to main content

lrsched

lrsched logo

PyPI CI License: MIT

Framework-agnostic learning-rate schedules as pure functions, in Python with zero dependencies. Each schedule maps a step to a learning rate, so it works in any training loop or framework, or none.

Install

pip install lrsched

30-second example

from lrsched import cosine, with_warmup, sample

schedule = with_warmup(
    cosine(base_lr=1e-3, min_lr=1e-5, total_steps=1000),
    warmup_steps=100,
    start_lr=0.0,
)

lr = schedule(250)            # learning rate at step 250
curve = sample(schedule, num_steps=1000)  # the whole curve, for plotting or logging

A schedule is just Callable[[int], float]. Plug schedule(step) into your optimizer however your framework expects, or use it to drive a plain training loop.

Why this exists

Every learning-rate scheduler is tied to a framework: torch.optim.lr_scheduler, timm, transformers, or optax for JAX. If you write a custom loop, use a non-PyTorch stack, or just want to plot a schedule, you end up pasting a LambdaLR snippet. lrsched is a small, dependency-free library where each schedule is a pure function, easy to test, plot, log, and reuse anywhere.

Comparison

lrsched torch / timm optax
Framework none PyTorch JAX
Pure step to lr function yes no (optimizer-bound) partial
Zero dependencies yes no no
Composable warmup and phases yes partial yes

Schedules

  • constant, step_decay, multi_step, exponential
  • linear, polynomial, polynomial_decay
  • exponential_decay: base_lr * decay_rate ** (t / decay_steps) with 0 < decay_rate < 1
  • cosine, cosine_restarts (SGDR)
  • inverse_sqrt (Transformer)
  • one_cycle
  • warmup_stable_decay (WSD / trapezoidal)
  • triangular, triangular2, exp_range (cyclical learning rates)

polynomial_decay(start_lr, end_lr, total_steps, power): (start_lr - end_lr) * (1 - t/T)**p + end_lr, holds end_lr past total_steps. exponential_decay(base_lr, decay_rate, decay_steps): base_lr * decay_rate ** (t / decay_steps). step_decay(base_lr, drop, step_size): base_lr * drop ** floor(t / step_size), with 0 < drop <= 1.

warmup_stable_decay(base_lr, warmup_steps, decay_steps, total_steps, final_lr, warmup_start, decay_shape): the WSD (Warmup-Stable-Decay), or trapezoidal, schedule from the MiniCPM line of work (Hu et al., 2024), now common in LLM pretraining. It linearly warms up from warmup_start to base_lr over warmup_steps, holds base_lr on a stable plateau, then decays to final_lr over the final decay_steps. decay_shape is required and must be "linear" or "1-sqrt"; the 1-sqrt shape (base_lr scaled by 1 - sqrt(progress), interpolated to final_lr) is the decay leg the WSD papers recommend. warmup_steps + decay_steps must not exceed total_steps; set either to zero to drop that phase.

Composition

  • with_warmup(schedule, ...) prepends a linear warmup.
  • sequential(phases) runs schedules back to back.
  • sample(schedule, num_steps=...) evaluates a schedule over a range.

Parameters are required keyword arguments, so every schedule reads explicitly at the call site. Schedules hold their final value past the end rather than erroring, and a negative step raises.

Per-parameter-group learning rates

scale_by_group applies fixed per-group multipliers to any base schedule, returning a function from step to dict[str, float]. Useful for discriminative learning rates where different layers or modules train at different rates:

from lrsched import cosine, scale_by_group

base = cosine(base_lr=1e-3, min_lr=1e-5, total_steps=1000)
group_lr = scale_by_group(base, multipliers={"backbone": 0.1, "head": 1.0})

lrs = group_lr(250)  # {"backbone": ..., "head": ...}
# plug into your optimizer's param_groups, keyed by name

Each multiplier must be a positive finite number. An empty mapping and non-positive or non-finite multipliers all raise ValueError with a descriptive message.

Command line

Sample a schedule from the terminal, one value per step or as a sparkline:

lrsched cosine --base-lr 1e-3 --min-lr 1e-5 --total-steps 1000 --steps 1000
lrsched triangular --min-lr 1e-4 --max-lr 1e-2 --step-size 200 --steps 800 --sparkline

Supports cosine, linear, exponential, step, triangular, wsd, and one-cycle.

Examples

python examples/schedules.py

Testing

pip install -e ".[dev]"
pytest

Tests cover the exact value of each schedule at known steps, schedule-specific shapes (restarts, the one-cycle peak, the warmup handoff), and invariants checked with Hypothesis (cosine stays within bounds, warmup is monotone).

Contributing

Issues and pull requests are welcome. See CONTRIBUTING.md.

License

MIT. See LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lrsched-0.5.0.tar.gz (954.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lrsched-0.5.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file lrsched-0.5.0.tar.gz.

File metadata

  • Download URL: lrsched-0.5.0.tar.gz
  • Upload date:
  • Size: 954.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for lrsched-0.5.0.tar.gz
Algorithm Hash digest
SHA256 40cb3e69459c7d51e6783c2b3a932c3b65345165e40d7f76330080f8076d89ce
MD5 a6bd924d61bdf350c4e6f13302114155
BLAKE2b-256 259c57aa8f199ab87f2f82f7aa959c757ff1a5a476ebf94091121c0e66c25eb1

See more details on using hashes here.

File details

Details for the file lrsched-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: lrsched-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for lrsched-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a2a781e05d262b82329c11353b70ce09679b303275b67bef6f22da011016d5b
MD5 4b47dd847c60d0138d42341dcb2939f9
BLAKE2b-256 2a256676eca14f055752b199be4bae82672842b1fb78460b1f88c5195d154b6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page