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

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.dev5
File Size Uploaded Provenance Actions
reax-0.7.0.dev5.tar.gz 157.2 kB Without provenance Details for reax-0.7.0.dev5.tar.gz

Built distribution (wheel)

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

Total release size: 358.4 kB

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

Download URL reax-0.7.0.dev5.tar.gz
Size 157.2 kB
Tags Source
SHA-256 checksum
How to use checksums
41b8555ed5fff862c1af29c200242fe6531c57836ba92b6dd3004f8c000a80a5
BLAKE2b-256 checksum
How to use checksums
340f6e7199202d7ef2873f4455896d4a79357a390afdfd6076fa606bc0e263ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

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

Download URL reax-0.7.0.dev5-py3-none-any.whl
Size 201.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0d0525e2f71b498faed5abb30e362327435927da9fc47c2185e8e5f5a4e145c
BLAKE2b-256 checksum
How to use checksums
e7b854997dd088cadd00a0d682547d2bc5fb60a0783faeb96ccf1c537ebf1769
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for reax 0.7.0.dev5

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