Skip to content

Train and predict

This guide explains the normal training workflow and when to use the explicit state API.

Run the common workflow

Create a PhiModule blueprint, PhiDataModule, Optax optimizer, and Trainer. The Trainer binds the model after the DataModule has prepared its input statistics.

trainer = Trainer(max_steps=10_000, accelerator="auto")

result = trainer.fit(
    module,
    datamodule=data_module,
    optimizer=optimizer,
    seed=0,
)
predictions = trainer.predict(result, datamodule=data_module)

Trainer.fit() performs the following work:

  1. Set up the DataModule and read its input statistics.
  2. Initialize and bind the model factory.
  3. Create equal static loss weights unless a balancer is supplied.
  4. Split independent runtime, sampling, and balancer PRNG streams.
  5. Infer objective batch keys and compile one train step.
  6. Initialize or restore TrainState.
  7. Run host-side sampling around one JIT-compiled update per step.

The original module blueprint is unchanged. The returned FitResult provides the bound module through result.module and final functional state through result.state.

Choose an optimizer

Pass an Optax optimizer directly to fit(). See Choose an optimizer for an Adam example and optional SOAP installation, Python/Hydra configuration, and checkpoint requirements.

Choose a loss balancer

Pass a static or adaptive LossBalancer directly to fit(). Adaptive balancers own their update interval and diagnostic sampling settings.

from phijax.balancers import GradNormBalancer

balancer = GradNormBalancer(
    module.loss_names,
    update_every_n_steps=100,
)
result = trainer.fit(
    module,
    datamodule=data_module,
    optimizer=optimizer,
    seed=0,
    balancer=balancer,
)

See Loss balancers for update timing, diagnostic batches, and logged metrics.

Resume or load weights

Pass an explicit checkpoint path to restore a complete training state. Add ckpt_step to select one saved step. With a configured ModelCheckpoint, ckpt_path="last" selects its latest checkpoint. Before the first checkpoint exists, "last" warns and starts from fresh state, as in Lightning, so one configuration both starts a run and resumes it after preemption or a SLURM requeue. An explicit path without checkpoints still raises FileNotFoundError.

result = trainer.fit(
    module,
    datamodule=data_module,
    optimizer=optimizer,
    seed=0,
    ckpt_path="last",
)

Use weights_only=True when the model parameters should be restored into fresh optimizer, balancer, PRNG, and step state. Complete restoration resumes the deterministic sampling sequence.

max_steps is the global step at which training stops, not a per-call budget. To continue a run that stopped at step 10_000 for 5_000 more updates, resume with max_steps=15_000. Learning-rate schedules indexed by the optimizer step, such as one sized with max_steps, then continue where they stopped. A weights_only=True fit starts at step 0.

Checkpoints also store the model's input normalization statistics. A complete restoration and prediction from a checkpoint use the stored statistics, even if the DataModule's data changed. Transfer learning with weights_only=True uses the current DataModule's statistics. To keep the source statistics instead, return load_input_statistics() from the DataModule's input_statistics().

Predict from a checkpoint

A separate prediction script needs only the module blueprint and the checkpoint. The Trainer builds the model with the input statistics and periods stored in the checkpoint, then restores only the model state:

module = PhiModule(model_factory, objective)
predictions = trainer.predict(module, ckpt_path="runs/heat/checkpoints", datamodule=data_module)

No optimizer, balancer, or seed is needed, and the DataModule's fit stage is not set up. Checkpoints written by PhiJAX 0.2.0b5 and earlier store no statistics or periods; for them, the Trainer takes both from the fit stage of datamodule and warns.

Train a custom module

A custom BasePhiModule trains with the same fit() call. Override configure_model() to build the model; the Trainer then creates the state, compiles the train step from training_step(), and runs the same lifecycle.

Understand the execution boundary

Host: sample batch -> place batch -> call compiled update -> hooks -> log/checkpoint
                                      |
Device:                         loss -> gradient -> Optax update

Sampling, hooks, logging, checkpoint scheduling, and signal handling stay in Python. The loss, derivatives, gradient, balancing, and Optax update run inside one JIT-compiled function. The Trainer reads the restored step once before the loop and verifies it once at the end. Converting a device value to a Python or NumPy scalar from a callback introduces an intentional synchronization at that callback's cadence.

Report extra diagnostics

Return TrainingOutput when the compiled objective already has useful fixed-shape diagnostics.

return TrainingOutput(
    losses={"pde/heat": heat_loss},
    diagnostics={
        "residual/mean_abs": jnp.mean(jnp.abs(residuals)),
        "residual/by_region": regional_residuals,
    },
)

Scalar diagnostics are logged automatically. Arrays remain available to callbacks and are not reduced implicitly. Use self.log() from on_train_batch_end() to change where a scalar appears. Run expensive reporting-only calculations in a separate compiled evaluator at the required cadence.

Handle interruption and evaluation

After Ctrl+C, fit() returns the last completed state with result.interrupted=True. The caller may then predict. SIGTERM runs exception hooks and cleanup before exiting with the standard signal status.

Prediction is skipped when predict_batch_source() returns None. PhiJAX does not impose a generic validation loop; use prediction and the evaluation APIs for application-specific reference grids, measurements, or physical checks.

Next steps