Strategy
- class lightning.pytorch.strategies.Strategy(accelerator=None, checkpoint_io=None, precision_plugin=None)
Bases:
ABCBase class for all strategies that change the behaviour of the training, validation and test- loop.
- abstract all_gather(tensor, group=None, sync_grads=False)
Perform an all_gather on all processes.
- backward(closure_loss, optimizer, *args, **kwargs)
Forwards backward-calls to the precision plugin.
- Parameters:
closure_loss (
Tensor) – a tensor holding the loss value to backpropagateoptimizer (
Optional[Optimizer]) – An optional optimizer that gets passed down to the precision plugin’s backward*args (
Any) – Positional arguments that get passed down to the precision plugin’s backward, intended as arguments for the actual function that performs the backward, likebackward().**kwargs (
Any) – Keyword arguments for the same purpose as*args.
- Return type:
- abstract barrier(name=None)
Synchronizes all processes which blocks processes until the whole group enters this function.
- batch_to_device(batch, device=None, dataloader_idx=0)
Moves the batch to the correct device.
The returned batch is of the same type as the input batch, just having all tensors on the correct device.
- abstract broadcast(obj, src=0)
Broadcasts an object to all processes.
- connect(model)
Called by the Trainer to connect the strategy with the model.
- Return type:
- model_sharded_context()
Provide hook to create modules in a distributed aware context. This is useful for when we’d like to shard the model instantly, which is useful for extremely large models which can save memory and initialization time.
Returns: Model parallel context.
- abstract model_to_device()
Moves the model to the correct device.
- Return type:
- on_exception(exception)
Called when the trainer execution is interrupted by an exception.
- Return type:
- on_predict_end()
Called when predict ends.
- Return type:
- on_predict_start()
Called when predict begins.
- Return type:
- on_test_end()
Called when test end.
- Return type:
- on_test_start()
Called when test begins.
- Return type:
- on_train_batch_start(batch, batch_idx)
Called in the training loop before anything happens for that batch.
- Return type:
- on_train_end()
Called when train ends.
- Return type:
- on_train_start()
Called when train begins.
- Return type:
- on_validation_end()
Called when validation ends.
- Return type:
- on_validation_start()
Called when validation begins.
- Return type:
- optimizer_state(optimizer)
Returns state of an optimizer.
Allows for syncing/collating optimizer state from processes in custom strategies.
- optimizer_step(optimizer, closure, model=None, **kwargs)
Performs the actual optimizer step.
- Parameters:
- Return type:
- post_backward(closure_loss)
Run after precision plugin executes backward.
- Return type:
- post_training_step()
This hook is deprecated.
Override
training_step()instead.- Return type:
- pre_backward(closure_loss)
Run before precision plugin executes backward.
- Return type:
- predict_step(*args, **kwargs)
The actual predict step.
See
predict_step()for more details- Return type:
- process_dataloader(dataloader)
Wraps the dataloader if necessary.
- Parameters:
dataloader (
object) – iterable. Ideally of type:torch.utils.data.DataLoader- Return type:
- abstract reduce(tensor, group=None, reduce_op='mean')
Reduces the given tensor (e.g. across GPUs/processes).
- reduce_boolean_decision(decision, all=True)
Reduce a boolean decision across all processes.
- Return type:
- remove_checkpoint(filepath)
Remove checkpoint filepath from the filesystem.
- save_checkpoint(checkpoint, filepath, storage_options=None)
Save model/training states as a checkpoint file through state-dump and file-write.
- setup(trainer)
Sets up the accelerator, plugins and initializes the optimizers (if needed).
- setup_environment()
Setup any processes or distributed connections.
This is called before the LightningModule/DataModule setup hook which allows the user to access the accelerator environment before setup is complete.
- Return type:
- setup_optimizers(trainer)
Creates optimizers and schedulers.
- setup_precision_plugin()
Attaches the precision plugin to the strategy.
- Return type:
- teardown()
This method is called to teardown the training process.
It is the right place to release memory and free other resources.
- Return type:
- tensor_init_context(empty_init=None)
Controls how tensors get created (device, dtype).
- test_step(*args, **kwargs)
The actual test step.
See
test_step()for more details
- training_step(*args, **kwargs)
The actual training step.
See
training_step()for more details
- validation_step(*args, **kwargs)
The actual validation step.
See
validation_step()for more details
- property handles_gradient_accumulation: bool
Whether the strategy handles gradient accumulation internally.
- abstract property is_global_zero: bool
Whether the current process is the rank zero process not only on the local node, but for all nodes.
- property lightning_module: Optional[LightningModule]
Returns the pure LightningModule without potential wrappers.
- property lightning_restore_optimizer: bool
Override to disable Lightning restoring optimizers/schedulers.
This is useful for strategies which manage restoring optimizers/schedulers.
- property restore_checkpoint_after_setup: bool
Override to delay restoring from checkpoint till after the setup phase has completed. This is useful when the strategy requires all the setup hooks to run before loading checkpoint.
- Returns:
If
True, restore checkpoint after strategy setup.
- abstract property root_device: device
Returns the root device.
Was this page helpful?
Thanks for the feedback.