Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

REAX

Coverage Tests Documentation Latest Version https://img.shields.io/pypi/pyversions/reax.svg https://img.shields.io/pypi/l/reax.svg Ruff

Scalable, flexible training for JAX, inspired by the simplicity of PyTorch Lightning.

REAX is a lightweight framework for training JAX models. It takes care of the training loop, logging, checkpointing and scaling across devices, so that your code can focus on the model and the science. 🚀

Why REAX? 🌟

  • Bring your own model 🔧: REAX works with any JAX library, e.g. Flax Linen, Flax NNX or Equinox. Your model stays plain JAX code.

  • Minimal boilerplate 🧩: write a reax.Module with your training step and optimiser, and let the reax.Trainer run fitting, validation, testing and prediction.

  • Batteries included 💾: checkpointing, early stopping, progress bars, metrics, and loggers for CSV, TensorBoard and MLflow.

  • Scales with JAX ⚡: run on multiple devices and processes with minimal code changes.

  • Stay in control 🎯: hook into every stage of training with module hooks and listeners, or drop down to the reax.Engine to write your own training loop.

Installation 🛠️

With pip:

pip install reax

or with uv, to add REAX to your project:

uv add reax

Example

Train an autoencoder on MNIST, using a Flax Linen model:

from functools import partial

from flax import linen
import jax
import optax

import reax
from reax.demos import mnist


class Autoencoder(linen.Module):
    def setup(self):
        self.encoder = linen.Sequential([linen.Dense(128), linen.relu, linen.Dense(3)])
        self.decoder = linen.Sequential([linen.Dense(128), linen.relu, linen.Dense(28 * 28)])

    def __call__(self, x):
        return self.decoder(self.encoder(x))


# --------------------------------
# Step 1: Define a REAX Module
# --------------------------------
# A reax.Module defines a full *system* (e.g. an LLM, diffusion model, autoencoder or
# image classifier): the model, how to train it and how to optimise it.
class ReaxAutoEncoder(reax.Module):
    def __init__(self):
        super().__init__()
        self.ae = Autoencoder()

    def configure_model(self, stage: reax.Stage, batch, /):
        if self.parameters() is None:
            # Initialise the parameters using an example batch
            x, _ = batch
            params = self.ae.init(self.rngs(), x.reshape(x.shape[0], -1))
            self.set_parameters(params)

    def __call__(self, x):
        return self.ae.apply(self.parameters(), x)

    def training_step(self, batch, batch_idx):
        x, _ = batch
        x = x.reshape(x.shape[0], -1)
        loss, grads = jax.value_and_grad(self.loss_fn)(self.parameters(), x, self.ae.apply)
        self.log("train_loss", loss, on_step=True, prog_bar=True)
        # Return the gradients and REAX takes care of the optimiser update
        return loss, grads

    def validation_step(self, batch, batch_idx):
        x, _ = batch
        x = x.reshape(x.shape[0], -1)
        self.log("val_loss", self.loss_fn(self.parameters(), x, self.ae.apply), prog_bar=True)

    @staticmethod
    @partial(jax.jit, static_argnums=2)
    def loss_fn(params, x, apply_fn):
        predictions = apply_fn(params, x)
        return optax.losses.squared_error(predictions, x).mean()

    def configure_optimizers(self):
        opt = optax.adam(learning_rate=1e-3)
        return opt, opt.init(self.parameters())


# -------------------
# Step 2: Define data
# -------------------
trainer = reax.Trainer()
dataset = mnist.MnistDataset(download=True)
train, val = reax.data.random_split(trainer.rngs, dataset, [55_000, 5_000])

# -------------------
# Step 3: Train
# -------------------
autoencoder = ReaxAutoEncoder()
trainer.fit(
    autoencoder,
    reax.ReaxDataLoader(train, batch_size=64, shuffle=True),
    reax.ReaxDataLoader(val, batch_size=64),
    max_epochs=5,
)

More examples, including Flax NNX and Equinox models, are in the documentation.

Coming from PyTorch Lightning?

REAX ports the abstractions and best practices of PyTorch Lightning to JAX, and parts of its code are adapted directly from Lightning. If you know Lightning, the Trainer, Module, hooks, listeners (Lightning’s callbacks) and loggers will feel familiar. Where it matters, REAX departs from Lightning to fit JAX: parameters and optimiser state are explicit, and training steps return gradients rather than relying on loss.backward().

Metadata

Release files for reax 0.7.0.dev2

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

Source distribution (sdist)

Source distribution for reax 0.7.0.dev2
File Size Uploaded Provenance Actions
reax-0.7.0.dev2.tar.gz 151.1 kB Without provenance Details for reax-0.7.0.dev2.tar.gz

Built distribution (wheel)

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

Total release size: 344.8 kB

Release files / reax-0.7.0.dev2.tar.gz

Download URL reax-0.7.0.dev2.tar.gz
Size 151.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6c8c056749ed6b2943e6ba8bd58891c05c5e61eabd6802b940d1d7b178271af8
BLAKE2b-256 checksum
How to use checksums
b8ed1d6d1e50b555f43c3199054772ad8ee96bd047d4dbcf2346ba9bb3618fc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / reax-0.7.0.dev2-py3-none-any.whl

Download URL reax-0.7.0.dev2-py3-none-any.whl
Size 193.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
14b4911eae1aed357f04f7794f1ed6e6ca9557c4d34215e3c1aab955e6123b0b
BLAKE2b-256 checksum
How to use checksums
3b7ea315f2ec19fb4ef5fb8219674161469941cd89b7f80c2c628605b4f2ca8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Security for reax 0.7.0.dev2

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