Engine: training
from kito import Engine
engine = Engine(module, config)
Engine takes a KitoModule and the KitoModuleConfig; both are required. On construction it
picks the device, moves the module to it, and creates the logger, progress bars and a timestamp
(%d%b%Y-%H%M%S) used to name outputs.
fit
engine.fit(train_loader=None, val_loader=None, data_pipeline=None,
max_epochs=None, callbacks=None)
fit is available only when config.training.train_mode is True. Give it either DataLoaders
or a GenericDataPipeline. With a pipeline, setup() runs if needed, and an empty validation
split is allowed: training then runs without validation and a warning is logged.
What fit does, in order:
Builds the model if
module.is_builtis false, and loads the pretrained weights first ifinitialize_model_with_saved_weightsisTrue.Creates the optimizer (
associate_optimizer()) if none is set.Checks the module and data loaders.
Uses
max_epochsor, ifNone,config.training.n_train_epochs.Wraps the model in
DistributedDataParallelifdistributed_trainingisTrue.Creates the default callbacks (see Callbacks), appends the ones you pass, and calls the callbacks’ setup.
Runs each epoch: training epoch, then validation epoch if a validation loader exists, then
on_epoch_endwithlogs.
logs contains train_loss, and val_loss when validation ran. Callbacks that monitor
val_loss do not work without validation data. Use monitor='train_loss'.
Training stops after the epoch when any callback sets engine.stop_training = True. Ctrl-C
ends training cleanly. on_train_end always runs, and in DDP the model is unwrapped and the
process group destroyed afterwards.
Weights
engine.load_weights(weight_path=None, strict=True) loads from weight_path, or from
config.model.weight_load_path when it is None. Failures are raised as RuntimeError with
the path and the original error.
Default callbacks
engine.get_default_callbacks() returns the callbacks enabled by CallbacksConfig. Add
your own and pass the list to fit. If you pass callbacks to fit without calling
get_default_callbacks(), the defaults are still created and your callbacks are appended after them.
What one epoch does
Training: the model is set to
train(). For each batch:on_train_batch_begin,module.training_step(batch),loss.item(),on_train_batch_end. The epoch loss is the mean of the per-batch losses, averaged across ranks in DDP.First batch:
module._check_data_shape(batch)is called once perfit.Validation:
eval()andtorch.no_grad().on_validation_begin, onevalidation_stepper batch, thenon_validation_endwith the mean loss.Errors: an empty loader raises
ValueError.Cost: each batch calls
.item()on the loss, which synchronizes the device once per batch.