API reference¶
trainloop ¶
| CLASS | DESCRIPTION |
|---|---|
BaseHook |
Lifecycle hooks for |
BaseTrainer |
Minimal training loop that orchestrates builds, accumulation, retries, and hooks. |
CUDAMaxMemoryHook |
Record peak CUDA memory per step into |
CheckpointingHook |
Save and optionally restore checkpoints at regular intervals. |
EMAHook |
Maintain an exponential moving average of model weights. |
ImageFileLoggerHook |
Persist logged images to |
LossNoneWarning |
Warning raised when |
ProgressHook |
Log progress to stdout with optional metrics, ETA, and memory. |
StatsHook |
Aggregate training stats and pass them to a callback. |
TensorBoardHook |
Log metrics and images to TensorBoard (rank 0 only). |
WandbHook |
Log metrics and images to Weights & Biases (rank 0 only). |
| FUNCTION | DESCRIPTION |
|---|---|
map_nested_tensor |
Apply |
BaseHook ¶
Lifecycle hooks for BaseTrainer.
BaseTrainer ¶
Minimal training loop that orchestrates builds, accumulation, retries, and hooks.
Subclasses provide component factories and a forward pass; the base class handles sequencing, mixed precision, accumulation, state management, and hook dispatch.
| PARAMETER | DESCRIPTION |
|---|---|
max_steps
|
Number of training steps to run.
TYPE:
|
grad_clip
|
Max gradient norm; if set, gradients are clipped before stepping.
TYPE:
|
max_non_finite_grad_retries
|
Number of retries when encountering non-finite gradients (scaler disabled).
TYPE:
|
mixed_precision
|
TYPE:
|
gradient_accumulation_steps
|
Number of microsteps to accumulate before stepping.
TYPE:
|
workspace
|
Optional working directory used by hooks (e.g., checkpoints, logs). |
device
|
Device for the model and tensors. |
no_sync_accumulate
|
Whether to call
TYPE:
|
state_dict_options
|
Torch distributed checkpoint options.
TYPE:
|
logger
|
Logger instance; a default logger is created when omitted.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
build_data_loader |
Return the training data iterator. |
build_model |
Construct and return the model. |
build_optimizer |
Create the optimizer for the model. |
build_lr_scheduler |
Optionally create a learning-rate scheduler. |
build_hooks |
Return hooks to run during training. |
build_grad_scaler |
Create the gradient scaler used for mixed precision. |
train |
Run the training loop until |
forward |
Perform a forward pass and return loss plus records for logging. |
log_scalars |
Dispatch nested scalar records to logging hooks. |
log_images |
Dispatch image records to hooks. |
build_lr_scheduler ¶
Optionally create a learning-rate scheduler.
build_grad_scaler ¶
Create the gradient scaler used for mixed precision.
forward ¶
Perform a forward pass and return loss plus records for logging.
| PARAMETER | DESCRIPTION |
|---|---|
input
|
Batch yielded by the data loader.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor | None
|
The loss ( |
Records
|
A nested dict of numeric metrics that will be averaged and emitted to hooks. |
log_scalars ¶
Dispatch nested scalar records to logging hooks.
Dictionary keys are treated as path components. Logging hooks may join
nested components with backend-specific namespace separators, such as
/. Avoid those separators within individual keys because distinct
nested paths may otherwise map to the same backend metric name.
| PARAMETER | DESCRIPTION |
|---|---|
records
|
Nested dict of scalar metrics to log. |
dry_run
|
If True, hooks should avoid side effects and only report intent.
TYPE:
|
CheckpointingHook ¶
Bases: BaseHook
Save and optionally restore checkpoints at regular intervals.
| PARAMETER | DESCRIPTION |
|---|---|
interval
|
Save every
TYPE:
|
keep_previous
|
Keep the last N checkpoints in addition to the latest.
TYPE:
|
keep_interval
|
Save and keep checkpoints every
TYPE:
|
keep_steps
|
Save and keep checkpoints at these explicit step numbers. |
path
|
Directory (relative to workspace unless absolute) for checkpoints. |
load
|
Path to load at startup or
TYPE:
|
exit_signals
|
Signals that trigger a checkpoint then exit. |
exit_code
|
Exit code after handling an exit signal. |
exit_wait
|
Optional sleep before exit (useful for schedulers). |
EMAHook ¶
Bases: BaseHook
Maintain an exponential moving average of model weights.
| PARAMETER | DESCRIPTION |
|---|---|
decay
|
EMA decay rate.
TYPE:
|
use_buffers
|
Whether to include model buffers in the EMA.
TYPE:
|
name
|
Name used for the EMA state in the trainer state dict.
TYPE:
|
ImageFileLoggerHook ¶
LossNoneWarning ¶
Bases: UserWarning
Warning raised when forward returns None in distributed contexts.
ProgressHook ¶
Bases: StatsHook
Log progress to stdout with optional metrics, ETA, and memory.
| PARAMETER | DESCRIPTION |
|---|---|
interval
|
Log every N steps.
TYPE:
|
with_records
|
Include per-step records in the log line.
TYPE:
|
sync
|
If True, aggregate across distributed ranks.
TYPE:
|
eta_warmup
|
Steps to warm up ETA calculation.
TYPE:
|
show_units
|
Whether to print units (s, GiB) alongside values.
TYPE:
|
StatsHook ¶
Bases: BaseHook
Aggregate training stats and pass them to a callback.
| PARAMETER | DESCRIPTION |
|---|---|
interval
|
Emit stats every N steps.
TYPE:
|
sync
|
If True, aggregate stats across distributed ranks.
TYPE:
|
callback
|
Function called with the trainer and aggregated stats.
TYPE:
|
TensorBoardHook ¶
Bases: BaseHook
Log metrics and images to TensorBoard (rank 0 only).
Nested namespace components are joined with namespace_separator and
the final name is separated with /. Avoid these separators within
individual keys because TensorBoard uses them to organize tags.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Optional text values to log when training starts. |
namespace_separator
|
Separator for nested tag prefixes; the final TensorBoard name boundary is always "/".
TYPE:
|
WandbHook ¶
Bases: BaseHook
Log metrics and images to Weights & Biases (rank 0 only).
Nested scalar key components are joined with /. Avoid / within an
individual component because it is also W&B's panel namespace separator.
| PARAMETER | DESCRIPTION |
|---|---|
project
|
W&B project name.
TYPE:
|
config
|
Optional config dict or JSON file path to log. |
tags
|
Optional tag list. |
image_format
|
File format for images or a callable taking the flattened image key and returning the format.
TYPE:
|
**wandb_kwargs
|
Extra arguments forwarded to
DEFAULT:
|
| METHOD | DESCRIPTION |
|---|---|
on_log_images |
Note that the final component of each flattened key becomes the image caption, |
on_log_images ¶
Note that the final component of each flattened key becomes the image caption,
and the remaining components become the W&B key. For example,
foo/bar/name1 and foo/bar/name2 are logged as two images under
foo/bar, so W&B displays them together in one panel.
The list is replaced rather than extended when the same W&B key is logged again at the same step. Callers should therefore collect all images for a panel and pass them in a single call per step.
map_nested_tensor ¶
Apply f to every tensor contained in a nested structure.