Training strategy API#
Core training-loop classes and helpers.
See also
Fine-tuning guide: Fine-Tuning Pretrained Models
Fine-tuning API: Fine-tuning API
Losses guide: Losses
Training update hooks: Training update hooks
Strategies#
Pydantic-driven supervised training loop for MLIP models. |
|
Run a forward pass and prefix output keys with |
Field |
Type |
Description |
|---|---|---|
|
|
Hooks to run at training stages. Accepts |
|
|
Runtime optimizer-step counter, excluded from specs. Batches whose optimizer step is skipped by update hooks do not advance this counter. |
|
|
Named models visible to |
|
|
Optimizer/scheduler configs keyed by model name. Keys may target a subset of |
|
|
Epoch count; mutually exclusive with |
|
|
Target step count; mutually exclusive with |
|
|
Positive multiplier applied when converting |
|
|
Explicit forward-pass callable. Single-model strategies call |
|
|
Composed loss whose components drive target collection. Leaf losses are accepted and normalized to one-component composed losses. |
|
|
Callable that assembles loss targets from the loss function, training predictions, current batch, and optional workflow. |
|
|
One device shared by all models, or one entry per model naming that same device; named-model |
|
|
Optional external distributed manager. The strategy passes this through hook contexts for distributed-aware hooks. |
|
|
Runtime optimizer-step counter across all data-parallel workers, excluded from specs. This advances by the distributed world size when an optimizer step runs, so checkpoint restarts can recover sampler progress without assuming the same world size. |
|
|
Runtime batch counter, excluded from specs. This advances for every completed batch, including batches whose optimizer step is skipped. |
|
|
Runtime epoch counter, excluded from specs. |
|
|
Runtime counter for batches consumed within the current epoch, excluded from specs. |
|
|
Runtime flag recording whether a single model was supplied (stored under |
|
|
Most recent validation summary dict, or |
|
|
Optional inference-time model (e.g. EMA weights) used in place of the live training model for validation when |
|
|
Validation configuration controlling when and how validation runs. |
devices has length 1 or len(models). A named-model run stages one
batch on devices[0] and hands it to every model. A per-model list must
therefore name the same device in every entry, and a list naming more than one
distinct device is refused at run time. The per-model form lets a caller list
a device for each model it places; it is not a way to span devices. Entries are
compared after resolve_device() fills in the index of an
index-less cuda, which means whichever device the process has made current,
so cuda and cuda:0 count as one device on the rank whose current device
is 0 and as two on every other rank. A single-model run is unaffected. A DDPHook on the NCCL backend
collapses devices to this rank’s own device before the check runs.
Optimizer helpers#
Declarative optimizer and optional LR-scheduler bundle. |
|
Build optimizers and schedulers for configured model names. |
|
Call |
|
Call |
|
Call |
Field |
Type |
Description |
|---|---|---|
|
|
Optimizer class; |
|
|
Keyword arguments forwarded to the optimizer constructor; validated against its |
|
|
Optional LR scheduler. Time-based schedulers ( |
|
|
Must be empty unless |
|
|
How a metric-driven scheduler ( |
Serialization and checkpoints#
load_checkpoint() and
load_checkpoint() rebuild a saved
strategy from its metadata and restore the weights into models built from the
saved specs. models= hands the loader live models to restore into instead,
one per saved name, for a caller whose other objects already hold them. Extra
keyword arguments are runtime overrides forwarded to the strategy class’s
from_spec_dict. Both are refused on the loads that rebuild no strategy.
Base class for JSON-serializable, no-pickle hyperparameter specs. |
|
Build a |
|
Rebuild a |
|
Register JSON (de)serializers for a custom type. |
|
Unified checkpoint manifest and runtime container. |
|
Save a checkpoint with a manifest. |
|
Load a multi-component checkpoint written by |
Runtime helpers#
Thin wrapper around |
|
Configure model parallelism, preserving input shape. |
|
Move one model or named models to device(s), preserving input shape. |
|
Move an optimizer's per-parameter state onto its parameters' devices. |
|
Temporarily eval/freeze models omitted from optimizer configs. |
|
Temporarily put optimizer-configured models in evaluation mode. |
|
Temporarily put a module tree in evaluation mode. |
Parallelism helpers#
A parallelism wrapper takes the place of the model a strategy holds. Code that
needs the model’s own surface, such as its model_config, a
BaseModelMixin method, or an unwrapped
state_dict, has to reach through the wrapper first. unwrap_model()
returns the module behind the wrapper, or the model itself when nothing wraps
it. It recognizes a wrapper by the module attribute the wrapper publishes,
not by its class, so a hand-rolled wrapper unwraps exactly as
DistributedDataParallel does.
Return the module a parallelism wrapper owns, or the model itself. |
Distributed helpers#
all_reduce_flags() collects one flag per
rank into a world-sized vector that reads the same everywhere: each rank raises
its own entry and a MAX all-reduce merges them, on the device
collective_device() picks for the backend. A
strategy uses it to turn a verdict only one rank can see, such as an empty data
shard, into a refusal every rank raises together, before any rank reaches a
collective its peers would block on.
all_gather_rows() stacks every rank’s
rows of a tensor in rank order, padding unequal shards and trimming them back,
and returns the slice this rank contributed; by default the gather is
differentiable, so a loss over the world tensor sends gradients back to each
rank’s rows. all_gather_objects()
collects one picklable object per rank. All three return without a collective
on a single process.
Return every rank's flag as one tensor, this rank's at its own index. |
|
Return every rank's tensor stacked along the leading dim, and this rank's rows. |
|
Return every rank's obj, in rank order. |