xwm.training¶
One family-agnostic Trainer, optimiser schedules, TrainState, and a trajectory ReplayBuffer that refuses to cross an episode boundary.
Training: one loop for every world model in xwm.
Trainer is family-agnostic -- it only needs loss, prepare_batch
and trainable. What differs is where batches come from:
xwm.data.iter_batches for a fixed dataset (JEPA), and
ReplayBuffer for the reward-driven families, whose losses need
contiguous slices of a single episode.
Modules:
| Name | Description |
|---|---|
replay |
A trajectory replay buffer for the reward-driven families. |
schedules |
Learning-rate, weight-decay and EMA-momentum schedules. |
state |
Training state. |
trainer |
The training loop. |
Classes:
| Name | Description |
|---|---|
ReplayBuffer |
Fixed-capacity ring buffer over trajectory steps. |
TrainState |
Everything needed to resume training. |
Trainer |
Trains any |
Functions:
| Name | Description |
|---|---|
adamw |
AdamW with the defaults xwm uses for JEPA training. |
cosine_warmup |
Linear warmup then cosine decay -- the standard ViT recipe. |
ema_momentum |
Teacher momentum, increasing toward |
weight_decay_schedule |
Weight decay increasing over training, as in the DINO/I-JEPA recipes. |
print_metrics |
A callback that prints selected metrics. |
ReplayBuffer
¶
ReplayBuffer(capacity: int, observation_shape: tuple[int, ...], action_shape: tuple[int, ...] = (), *, extra: dict[str, tuple[int, ...]] | None = None, seed: int = 0)
Fixed-capacity ring buffer over trajectory steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
number of steps to retain. |
required |
observation_shape
|
tuple[int, ...]
|
shape of a single observation. |
required |
action_shape
|
tuple[int, ...]
|
shape of a single action ( |
()
|
extra
|
dict[str, tuple[int, ...]] | None
|
additional per-step fields to store, as |
None
|
seed
|
int
|
RNG seed for sampling. |
0
|
NumPy rather than JAX: this is host-side mutable storage with random writes, which is exactly what JAX arrays are bad at. Batches are handed over as NumPy and converted at the jit boundary.
Methods:
| Name | Description |
|---|---|
add_episode |
Append one episode. |
sample |
Sample |
Source code in xwm/training/replay.py
add_episode
¶
Append one episode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observations
|
ndarray
|
|
required |
actions
|
ndarray
|
|
required |
rewards
|
ndarray
|
|
required |
extra
|
ndarray
|
any fields declared in |
{}
|
Source code in xwm/training/replay.py
sample
¶
Sample batch_size slices of horizon steps.
Returns a dict with observation (B, horizon + 1, ...), action
and reward (B, horizon, ...), plus any extra fields at
(B, horizon + 1, ...).
Raises:
| Type | Description |
|---|---|
ValueError
|
if no slice of that length fits inside a single episode. |
Source code in xwm/training/replay.py
TrainState
¶
TrainState(model: WorldModel, target: WorldModel | None, opt_state: PyTree, step: Array | int = 0)
Bases: Module
Everything needed to resume training.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
WorldModel
|
the world model. |
target |
WorldModel | None
|
EMA teacher, or |
opt_state |
PyTree
|
optimizer state, covering only trainable parameters. |
step |
Array
|
steps completed. |
Source code in xwm/training/state.py
Trainer
¶
Trainer(model: WorldModel, optimizer: GradientTransformation, *, ema_momentum: float | Schedule = 0.996)
Trains any WorldModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
WorldModel
|
the model to train. |
required |
optimizer
|
GradientTransformation
|
an optax transformation (see
|
required |
ema_momentum
|
float | Schedule
|
teacher momentum. A float is constant; a
|
0.996
|
Example
Methods:
| Name | Description |
|---|---|
init |
Fresh training state, with the teacher and optimizer state allocated. |
step |
Prepare the batch, take one optimizer step, update the teacher. |
fit |
Train over an iterable of batches. |
evaluate |
Average the loss over |
Source code in xwm/training/trainer.py
init
¶
init(model: WorldModel | None = None) -> TrainState
Fresh training state, with the teacher and optimizer state allocated.
Source code in xwm/training/trainer.py
step
¶
step(state: TrainState, batch: Batch, key: PRNGKey) -> tuple[TrainState, Metrics]
Prepare the batch, take one optimizer step, update the teacher.
Source code in xwm/training/trainer.py
fit
¶
fit(batches: Iterable[Batch], *, key: PRNGKey | None = None, steps: int | None = None, state: TrainState | None = None, log_every: int = 10, callbacks: Sequence[Callback] = ()) -> tuple[TrainState, list[dict[str, float]]]
Train over an iterable of batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batches
|
Iterable[Batch]
|
any iterable of batch dicts (see |
required |
steps
|
int | None
|
stop after this many steps; |
None
|
state
|
TrainState | None
|
resume from this state instead of a fresh one. |
None
|
log_every
|
int
|
how often to pull metrics back to the host. Metrics stay on device otherwise, so the loop does not block on the step it just dispatched. |
10
|
callbacks
|
Sequence[Callback]
|
called as |
()
|
Returns:
| Type | Description |
|---|---|
tuple[TrainState, list[dict[str, float]]]
|
|
Source code in xwm/training/trainer.py
evaluate
¶
evaluate(state: TrainState, batches: Iterable[Batch], *, key: PRNGKey | None = None) -> dict[str, float]
Average the loss over batches with the model in eval mode.
Source code in xwm/training/trainer.py
adamw
¶
adamw(learning_rate: float | Schedule, *, weight_decay: float | Schedule = 0.05, b1: float = 0.9, b2: float = 0.95, grad_clip: float | None = 1.0) -> GradientTransformation
AdamW with the defaults xwm uses for JEPA training.
b2 = 0.95 rather than 0.999: the loss is a moving target (the teacher
moves, or the regularizer's random projections change every step), so a
shorter second-moment window tracks it better.
Source code in xwm/training/schedules.py
cosine_warmup
¶
cosine_warmup(peak: float, total_steps: int, *, warmup_steps: int = 0, init: float = 0.0, final: float = 0.0) -> Schedule
Linear warmup then cosine decay -- the standard ViT recipe.
Source code in xwm/training/schedules.py
ema_momentum
¶
Teacher momentum, increasing toward final over training.
Starting lower lets the teacher track the student while the representation
is still changing fast; ending near 1.0 freezes it into a stable target
once it is worth imitating. I-JEPA and V-JEPA both ramp it this way.
Source code in xwm/training/schedules.py
weight_decay_schedule
¶
Weight decay increasing over training, as in the DINO/I-JEPA recipes.
Source code in xwm/training/schedules.py
print_metrics
¶
A callback that prints selected metrics.