Trainer#

class Trainer(cfg)[source]#

Bases: TrainerBase

Default single-dataset supervised/SSL trainer.

Builds everything from one cfg (model, train/val/test loaders, optimizer, scheduler, AMP scaler, hooks, writer, and the parallel context) and runs the standard AMP forward/backward optimization step in run_step(). The train loop is resume-aware: it restores start_epoch/start_iter and the global step from checkpoint hooks, re-aligns metric writers and the dataloader cursor, and treats cfg.epoch as the absolute horizon so a run can be extended without changing schedules. Registered as DefaultTrainer – select via train = dict(type="DefaultTrainer") (the default).

Parameters:

cfg – Fully-resolved run config providing save_path, epoch, resume, the model/data/optimizer/scheduler/hooks sub-configs, and the distributed/AMP settings.

after_epoch()[source]#

Run epoch-end hooks, clear histories, and optionally empty CUDA cache.

build_model()[source]#

Construct the model and wrap it for the configured parallel strategy.

build_optimizer()[source]#

Build the optimizer from config.

build_scaler()[source]#

Build an AMP gradient scaler when mixed precision is enabled.

build_scheduler()[source]#

Build a scheduler sized to all optimizer steps in training.

build_test_loader()[source]#

Build the optional test loader used by evaluation hooks.

build_train_loader()[source]#

Build the stateful training loader used for mid-epoch resume.

build_val_loader()[source]#

Build the optional validation loader.

build_writer()[source]#

Create a main-rank summary writer for TensorBoard or W&B.

run_step()[source]#

Move one batch to device, run forward/backward, and update LR.

train()[source]#

Run training from the configured or restored epoch/iteration.