Skip to main content

REAX: A simple training framework for JAX-based projects

Project description

REAX

Coverage Tests Documentation Latest Version https://img.shields.io/pypi/pyversions/reax.svg https://img.shields.io/pypi/l/reax.svg https://img.shields.io/badge/code%20style-black-000000.svg

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

REAX - Scalable Training for JAX

REAX is a minimal and high-performance framework for training JAX models, designed to simplify research workflows. Inspired by PyTorch Lightning, it brings similar high-level abstractions and scalability to JAX users, making it easier to scale models across multiple GPUs with minimal boilerplate. 🚀

A Port of PyTorch Lightning to JAX

Much of REAX is built by porting the best practices and abstractions of PyTorch Lightning to the JAX ecosystem. If you’re familiar with PyTorch Lightning, you’ll recognize concepts like:

  • Training loops ⚡

  • Multi-GPU training 🖥️

  • Logging and checkpointing 💾

However, REAX has been designed with JAX-specific optimizations, ensuring high performance without sacrificing flexibility.

Why REAX? 🌟

  • Scalable: Built to leverage JAX’s parallelism and scalability. ⚡

  • Minimal Boilerplate: Simplifies the training process with just enough structure. 🧩

  • Familiar: For users who have experience with frameworks like PyTorch Lightning, the transition to REAX is seamless. 🔄

Installation 🛠️

To install REAX, run the following command:

pip install reax

REAX example

Define the training workflow. Here’s a toy example:

# main.py
from functools import partial
import jax, optax, reax, flax.linen as linen
from reax.demos import mnist


class Autoencoder(linen.Module):
    def setup(self):
        super().__init__()
        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):
        z = self.encoder(x)
        return self.decoder(z)


# --------------------------------
# Step 1: Define a REAX Module
# --------------------------------
# A ReaxModule (nn.Module subclass) defines a full *system*
# (ie: an LLM, diffusion model, autoencoder, or simple image classifier).
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:
            # Prepare example batch for initialization
            x, _ = batch
            x = x.reshape(x.shape[0], -1)
            # Initialize Flax Linen model with RNGs and example input
            params = self.ae.init(self.rngs(), x)
            self.set_parameters(params)

    def __call__(self, x):
        # Use the full autoencoder model for forward pass
        return self.ae.apply(self.parameters(), x)

    def training_step(self, batch, batch_idx):
        x, _ = batch
        x = x.reshape(x.shape[0], -1)
        # Static method receives params, data, and model apply function
        loss, grads = jax.value_and_grad(self.loss_fn, argnums=0)(
            self.parameters(), x, self.ae.apply
        )
        self.log("train_loss", loss, on_step=True, prog_bar=True)
        return loss, grads

    @staticmethod
    @partial(jax.jit, static_argnums=2)
    def loss_fn(params, x, apply_fn):
        """Compute reconstruction loss.

        Static method for JIT compilation. Receives parameters and apply function.
        """
        predictions = apply_fn(params, x)
        return optax.losses.squared_error(predictions, x).mean()

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


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

# -------------------
# Step 3: Train
# -------------------
autoencoder = ReaxAutoEncoder()
trainer.fit(autoencoder, reax.ReaxDataLoader(train), reax.ReaxDataLoader(val))

Here, we reproduce an example from PyTorch Lightning, so we use torch vision to fetch the data, but for real models there’s no need to use this or pytorch at all.

Disclaimer ⚠️

REAX takes inspiration from PyTorch Lightning, and large portions of its core functionality are directly ported from Lightning. If you are already familiar with Lightning, you’ll feel right at home with REAX, but we’ve tailored it to work seamlessly with JAX’s performance optimizations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reax-0.6.10.tar.gz (135.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reax-0.6.10-py3-none-any.whl (175.5 kB view details)

Uploaded Python 3

File details

Details for the file reax-0.6.10.tar.gz.

File metadata

  • Download URL: reax-0.6.10.tar.gz
  • Upload date:
  • Size: 135.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for reax-0.6.10.tar.gz
Algorithm Hash digest
SHA256 0182b91e458443d96a1e47c349f446b2885037d47bc729d6d735980f37c5cd06
MD5 d6165a7fdb9b3e5e0c57e7e9be15d46f
BLAKE2b-256 993f3cdf4436523d1127e76c52e40f4d5031b3e10e665274e4e3e59846a8f181

See more details on using hashes here.

File details

Details for the file reax-0.6.10-py3-none-any.whl.

File metadata

  • Download URL: reax-0.6.10-py3-none-any.whl
  • Upload date:
  • Size: 175.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for reax-0.6.10-py3-none-any.whl
Algorithm Hash digest
SHA256 7293f07276f7bf5af8b74b535ae77bd426f0497ae25e5d15b812d986612724f6
MD5 2c27eb7e73de2674d81b3a3ca77c40f6
BLAKE2b-256 961c7f528593d1ba158fa53eaccff9ca47d08a33d231f4cd9bd274258a7027f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page