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

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

Built distribution (wheel)

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

Total release size: 346.9 kB

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

Download URL reax-0.7.0.dev3.tar.gz
Size 152.0 kB
Tags Source
SHA-256 checksum
How to use checksums
761c17f7c2f4fc905d92ed3db0bc7668ae0df222e3d60b80131e1bcb6b5cd2d8
BLAKE2b-256 checksum
How to use checksums
e615bdc9e050e9a0e61a02782781333631dcbb20713581e20525c568385d339f
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.dev3-py3-none-any.whl

Download URL reax-0.7.0.dev3-py3-none-any.whl
Size 194.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f51d7a354c5f952b372c847432d0aa7c06046a3b22e626685bf5a75d76860fe8
BLAKE2b-256 checksum
How to use checksums
51ebdbba637b6d9ab2918514bdfa7971d25b3c431aa4031b617419f6ec2b7adc
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.dev3

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