Skip to main content

Ravex

Checkpoint and resume for PyTorch training, from one decorator on the function that trains. Inside that function nothing changes: Ravex notices the model, the optimizer, the LR scheduler, the AMP scaler and the dataloader as they are built, counts every optimizer.step(), and writes a checkpoint on the cadence you configured. If the process dies, running the same command again picks up where it stopped.

Install

pip install ravex

Ravex is pure Python and needs Python 3.11 or later. It does not depend on torch, so that it attaches to whatever build you already have (CUDA, ROCm, CPU) instead of pulling another one in.

On Python 3.10 or older pip install ravex stops with "No matching distribution found for ravex": every release that said it ran there is yanked. Check python --version.

torch's CPU wheels do not bring NumPy, and torch says so on import with a UserWarning: Failed to initialize NumPy. It is torch's and harmless, and pip install numpy silences it; with NumPy installed Ravex also restores its RNG on a resume.

Its default checkpoint engine is Moonclip, installed with pip install "ravex[moonclip]". Moonclip publishes wheels for Linux on x86_64 and aarch64 and for macOS on Apple silicon (up to its 0.3.1, Linux x86_64 only); elsewhere it has to be built from source, and where it is not installed Ravex says so and falls back to torch_save.

Use

This runs as it is, on a CPU, in a few seconds. Save it as train.py:

import torch
import ravex

@ravex.train_loop(max_steps=2000)
def train():
    torch.manual_seed(0)
    model = torch.nn.Sequential(torch.nn.Linear(16, 64), torch.nn.ReLU(), torch.nn.Linear(64, 1))
    optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
    x = torch.randn(4096, 16)
    data = torch.utils.data.TensorDataset(x, x.sum(dim=1, keepdim=True))
    loader = torch.utils.data.DataLoader(data, batch_size=32, shuffle=True)
    for epoch in range(100):
        for x, y in loader:
            loss = torch.nn.functional.mse_loss(model(x), y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            if ravex.step() % 200 == 0:
                print(f"step {ravex.step()}  loss {loss.item():.4f}")

train()
python train.py      # press Ctrl-C after a few lines
python train.py      # the first line is past where you stopped, not step 200

Ctrl-C ends in Python's traceback for KeyboardInterrupt, as in any script; the line just above it says the run was not lost:

ravex: stopped; step 600 is saved in ./checkpoints, and the same command resumes from it

The checkpoints are in ./checkpoints; delete it to start over. Ravex says two things on stderr, whatever the log level: where the first checkpoint went, and, on a restart, the step it came back at:

ravex: resumed from step 1400 in ./checkpoints; it stops at step 2000 (max_steps)

announce=False silences them; nothing is sent anywhere.

Set max_steps. It is the length of the whole training across restarts, and it is what makes a resumed run end where the uninterrupted one would have: without it, the resumed script runs its own for epoch in range(100) from the top again, from the step it came back at, and trains more than the run that never stopped. For longer examples, each of which the test suite stops and resumes: examples/train_mlp.py, a DataLoader loop; examples/train_gpt.py, a small GPT with nanoGPT's loop, batches drawn with torch.randint and no DataLoader; and examples/train_ddp.py, two processes with DDP on a CPU under torchrun.

A resumed run continues rather than restarting from a good place, because Ravex restores everything around the weights too: optimizer state, LR scheduler, AMP loss scale, the torch, CUDA, Python and NumPy RNGs, and the position in the dataset. The test suite holds it to the strong form of that: a run killed at step 20 and resumed produces the same losses, step by step, and the same final weights as the run that was never interrupted.

On SIGTERM (a spot preemption, scancel, docker stop) the training stops at the next iteration, writes a final checkpoint and exits with status 143; run the same command again and it resumes there. That is on by default, and preemption_handler=False turns it off. Any other keyword argument to train_loop is a configuration override, and an unknown name raises TypeError rather than being ignored.

The decorator is the only way in: nothing is ever checkpointed by code that does not say so. Importing Ravex does nothing until a decorated function is called, and its patches on PyTorch come off when that function returns or raises.

When "noticed on its own" is a guess, such as two models of which only one trains, or an optimizer built before the function was entered, name it with ravex.track(model, optimizer). The one moment a checkpoint is consistent, the top of an iteration, Ravex takes from the DataLoader iterator or, in a loop without one, from the first forward of the trained model after each step; ravex.batch_boundary() hands it over by hand where neither is seen.

Configuration

ravex.yaml, at or above the working directory:

checkpoint_every: 500        # optimizer steps between checkpoints (default 100); see below for Moonclip
backend: moonclip            # moonclip | torch_save
storage:
  type: local                # local | s3 | r2
  path: ./checkpoints
keep_last: 5
max_steps: null              # stop at this step however many times the process restarted
sharded_checkpoints: gather  # gather | per_rank, for FSDP

Every option is also read from a RAVEX_* environment variable, which wins over the file, and the keyword arguments to train_loop win over both. A resume is the default (resume: true); RAVEX_ENABLED=0 turns Ravex off for one run without touching the code.

How often it saves. With torch_save, a checkpoint every checkpoint_every steps, and keep_last of them are kept. With Moonclip, the default, Ravex saves a small delta against the last full snapshot at every step, so a crash costs at most one step; checkpoint_every is then how often the store is synced to a bucket and the window it keeps (the last that many steps and the full they stand on), and keep_last is not used. A run of 147 steps holds about a hundred snapshots: that is the design, not a leak. delta: false goes back to one full save every checkpoint_every (docs).

Precision. save_dtype (Moonclip only) stores parts of the checkpoint at a lower precision to make it smaller. It is lossy: a run resumed from a bf16 optimizer state is close to, not identical with, the run that never stopped. Leave it unset when a resume must be exact (docs).

Storage. Ravex decides when to save and what, and puts it back into the live objects; Moonclip decides how the bytes are written and where, bucket included. So storage says where the bucket is, and the credentials are never read by Ravex or from the config file: Moonclip reads them from MOONCLIP_S3_ACCESS_KEY / MOONCLIP_S3_SECRET_KEY, the usual AWS_* pair, a profile in ~/.aws/credentials, or the machine's IAM role (Moonclip's README). Restoring unpickles part of a checkpoint, so give write access to the bucket only to what you would let run code on your training machines.

More than one GPU or machine. DDP and FSDP resume on every rank. With FSDP, gather writes one checkpoint that loads at any world size, bounded by what rank 0 can hold; per_rank has each rank write its own shard, which is much cheaper and resumes only at the same world size (reshard_on_resume handles a different one after a restart). Across machines without a bucket or a shared filesystem, replicate_every copies each rank's store to a peer on another machine, so losing one machine costs at most that many checkpoints; with a bucket, a rank that comes up with an empty disk pulls its store back. Ravex also trains across slow links between distant nodes with an outer loop that exchanges parameter differences every outer_inner_steps instead of gradients every step.

The full option list, with the measurements behind the defaults: docs/configuration.md. How the patches, the step count and the resume work: docs/how-it-works.md.

Next to other tools

  • PyTorch Lightning, Accelerate, the Hugging Face Trainer have their own checkpointing, resumed by passing the checkpoint back. Ravex works under them as under a plain loop, since it hooks PyTorch itself, and also puts back the counter their loop stops on; it warns when the framework's own checkpointing would save the same thing twice. Use one of the two.
  • torch DCP is a format and a save call; Ravex decides when to save, what, and how to put it back into a live run, and writes through Moonclip or torch.save (DCP is read, to resume from a checkpoint another framework wrote: convert_foreign).
  • W&B, MLflow keep working as they are: Ravex does not touch their calls, and its own log_metrics is optional. Runs already logged there come over as Ravex stores of their own, scalars only, with ravex import wandb entity/project/run_id --storage DIR (it needs the wandb package), ravex import neptune ./exports/data --storage DIR for the Parquet files neptune-exporter writes (it needs pyarrow; --run picks one run where they hold several), or ravex import table metrics.csv --storage DIR for any CSV or JSONL file of metrics, a row per step or a row per value; ravex ship then sends one to a backend like any other run.

Versioning and compatibility

Ravex is 0.x. A patch release (0.10.x) only fixes. A minor release may change an option's default or remove what was deprecated, each said in the CHANGELOG; what is removed warns for at least one minor release first. A newer Ravex resumes runs an older one wrote. An older one does not resume a newer one's checkpoint: it says so and starts from scratch, so pin a minor version (ravex~=0.12.0) for a run that may restart on another machine. Python 3.11 and later, each for as long as CPython supports it.

Limits

  • Under HuggingFace Trainer and Lightning the state comes back exactly, but with shuffling and dropout on, the resumed run sees a different random draw, because the framework drives the dataloader and the RNG on its own schedule. Plain loops, DDP and FSDP replay exactly.
  • An IterableDataset has no index sampler, so its position cannot be replayed; everything else is still restored.
  • A sharded model gets no final checkpoint at exit, since collecting one is a collective and ranks stop being in lockstep at shutdown; the newest checkpoint is the last periodic one.
  • A failed checkpoint is lost, logged, and tried again at the next one; a failure inside Ravex never stops the training (fallback_on_error).

What changed between versions is in the CHANGELOG.

Ravex and GPU Zero

GPU Zero is a hosted service built on Ravex and Moonclip: it shows a run's metrics and checkpoints, resumes or forks it from any checkpoint, and starts GPUs in your own cloud account for it. Ravex does not need it, and sends it nothing unless a run is given its address.

Development

pip install -e ".[dev]"
pytest tests/unit

tests/unit runs in one process on any OS. tests/distributed and integration/ start several processes, use torchrun or kill a process with SIGKILL, and need Linux. The integration tests run in their own image, with --init so that the SIGKILL they send is not a no-op:

docker build -f integration/Dockerfile -t ravex-integration .
docker run --rm --init ravex-integration

Licence

Apache-2.0 — GPU Zero

Metadata

Release files for ravex 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ravex 0.12.0
File Size Uploaded Provenance Actions
ravex-0.12.0.tar.gz 437.3 kB Without provenance Details for ravex-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ravex 0.12.0
File Interpreter ABI Platform Actions
ravex-0.12.0-py3-none-any.whl Python 3 none any Details for ravex-0.12.0-py3-none-any.whl

Total release size: 838.8 kB

Release files / ravex-0.12.0.tar.gz

Download URL ravex-0.12.0.tar.gz
Size 437.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e6c9dad9e2f8d3b76162f6dba559305bdd073b731474c09176130fbfe1354d10
BLAKE2b-256 checksum
How to use checksums
ab53f517374fd72f41c8122159dd68e7a947710bdf72b2234ae43ea6bdedd407
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / ravex-0.12.0-py3-none-any.whl

Download URL ravex-0.12.0-py3-none-any.whl
Size 401.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7b58f0233fe491ef492d6e02952045b5e744be95c7c1e80fd7009c09ac63dec
BLAKE2b-256 checksum
How to use checksums
baa243a3684d8d33d3926ec5a75b8f3355c261fab0d32cd0d6c5463406b83526
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for ravex 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page