Skills for writing, porting, and operating ReactantNitro experiments: the hook contract, the three field markers, metrics, optimizers, recompilation, checkpoints, manual training, device boundaries, visualization, export, and Kaimon-driven runs.
Reactant-first training for Lux models: declare the experiment, compile once, train without boilerplate.
Overview
ReactantNitro is a training framework for Julia, built on Reactant.jl
and Lux.jl. The model is an ordinary Lux model, every compiled
program is Reactant, and XLA runs it. You write the experiment as a struct plus a handful of hooks;
the framework supplies the compiled programs, the device transfers, the optimizer, the schedules,
checkpointing, and the run's lifecycle. Training and serving both stay in Julia, from the first
train! to the exported bundle.
The design follows PyTorch Lightning, adapted to Reactant. Beyond a training loop, a Reactant-first
stack needs control over when XLA compiles and what the tracer walks, device memory freed after each
batch instead of when the host GC runs, and binding by name between loaders, hooks, schedules and
optimizer rules. On top of those it supplies what Lightning does: gradient accumulation, phases,
schedules, checkpoint and resume, logging, and export, plus sharpness-aware minimization (SAM).
A Reactant-first stack has pitfalls that are easy to
hit and hard to diagnose: a compile that takes far longer than it should, an edit that reuses a stale
program, a run that runs out of device memory hours in. None of them raises an error on its own; the
framework guards against each one.
The three field markers
Every field on an experiment carries one of three markers. The marker decides what the compiled
program sees and whether changing the value recompiles:
Marker
Reaches traced code as
In the compile key?
Changing the value
GraphConst{T}
a baked literal
yes
recompiles, correctly: a different value is a different program
Device{T}
a device-resident traced input
no, by construction
never recompiles: sweep it, schedule it, rewrite it live
unmarked, i.e. Host{T}
not at all
no
never recompiles: driver-only, invisible to the tracer
Unmarked means Host because most fields are.
@experiment struct MyExp
"Structural: changes the emitted graph, so it bakes and is part of the compile key."
width::GraphConst{Int} = 128
"A traced input: sweep it or schedule it without recompiling."
smoothing::Device{Float32} = 0.05f0
"Unmarked, therefore Host: driver-only and invisible to the tracer."
max_epochs::Int = 20
end
Programs are keyed and stored once per process, not once per Nitro.
Recompilation explains when a
program is reused and what the compile key cannot see.
Quick start
Installation
using Pkg
Pkg.add("ReactantNitro")
MNIST
Four hooks are required. Everything else has a default: the optimizer (RAdam at 1e-3), one
parameter group, no decay, no schedule, prefetching, validation, checkpointing, and a JSON Lines
logger that writes the loss, metrics and schedule values to metrics.jsonl in the run directory.
using ReactantNitro, Lux, Random
using MLDatasets: MNIST
using MLUtils: DataLoader
# Explicit CPU, so the quick start runs anywhere. It must come before the first `Nitro`, where the
# XLA client initializes; the backend is fixed for the process from then on.
setup_devices!(backend = "cpu")
@experiment struct MnistMLP
width::GraphConst{Int} = 128
smoothing::Device{Float32} = 0.05f0
max_epochs::Int = 5
end
ReactantNitro.build_model(e::MnistMLP, rng) = begin
model = Chain(Dense(784 => e.width, relu), Dense(e.width => 10))
(model, Lux.setup(rng, model)...)
end