Metadata-Version: 2.4
Name: lrsched
Version: 0.1.0
Summary: Framework-agnostic learning-rate schedules as pure functions, in Python with zero dependencies.
Project-URL: Homepage, https://github.com/amaar-mc/lrsched
Project-URL: Repository, https://github.com/amaar-mc/lrsched
Project-URL: Issues, https://github.com/amaar-mc/lrsched/issues
Project-URL: Changelog, https://github.com/amaar-mc/lrsched/blob/main/CHANGELOG.md
Author: Amaar Chughtai
License: MIT License
        
        Copyright (c) 2026 Amaar Chughtai
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: cosine-annealing,deep-learning,learning-rate,lr-schedule,machine-learning,one-cycle,scheduler,sgdr,training,warmup
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: hypothesis>=6; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Description-Content-Type: text/markdown

# lrsched

<p align="center">
  <img src="assets/logo.png" alt="lrsched logo" width="160">
</p>

[![PyPI](https://img.shields.io/pypi/v/lrsched)](https://pypi.org/project/lrsched/)
[![CI](https://github.com/amaar-mc/lrsched/actions/workflows/ci.yml/badge.svg)](https://github.com/amaar-mc/lrsched/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)

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

```sh
pip install lrsched
```

## 30-second example

```python
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`
- `cosine`, `cosine_restarts` (SGDR)
- `inverse_sqrt` (Transformer)
- `one_cycle`

## 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.

## Examples

```sh
python examples/schedules.py
```

## Testing

```sh
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](./CONTRIBUTING.md).

## License

MIT. See [LICENSE](./LICENSE).
