ml.trainers.mixins.data_parallel
Defines a trainer mixin for data and model parallelism.
This defines how to wrap the model when launching multi-GPU or multi-node jobs. There are two wrappers:
DistributedDataParallel
(DDP)FullyShardedDataParallel
(FSDP)
DDP is the default wrapper unless conf.parallel.use_fsdp
is set to True
.
DDO runs each model replica on a single GPU processing a subset of the batch,
and then synchronizes gradients across all GPUs. FSDP supports more complex
sharding of the model across GPUs and nodes, and also supports CPU offloading.
- class ml.trainers.mixins.data_parallel.TaskModel(task: TaskT, model: ModelT)[source]
Bases:
Module
,Generic
[ModelT
,TaskT
,Batch
,Loss
]Initializes internal Module state, shared by both nn.Module and ScriptModule.
- forward(batch: Batch, state: State) Loss [source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class ml.trainers.mixins.data_parallel.ParallelConfig(use_fsdp: bool = False, cpu_offload: bool = False, sharding_strategy: torch.distributed.fsdp.api.ShardingStrategy = <ShardingStrategy.HYBRID_SHARD: 4>, sync_module_states: bool = True)[source]
Bases:
object
- use_fsdp: bool = False
- cpu_offload: bool = False
- sharding_strategy: ShardingStrategy = 4
- sync_module_states: bool = True
- ml.trainers.mixins.data_parallel.ddp(model: Module, cfg: ParallelConfig) DistributedDataParallel [source]
- ml.trainers.mixins.data_parallel.fsdp(model: Module, cfg: ParallelConfig) FullyShardedDataParallel [source]
- ml.trainers.mixins.data_parallel.dp(model: T, cfg: ParallelConfig) T | DistributedDataParallel | FullyShardedDataParallel [source]
Wraps a model for data parallel training, if necessary.
- Parameters:
model – The model to wrap.
cfg – The model configuration.
- Returns:
The wrapped model.
- class ml.trainers.mixins.data_parallel.TrainerParallelConfig(name: str = '???', exp_name: str = '${ml.exp_name:null}', exp_dir: str = '???', log_dir_name: str = 'logs', use_double_weight_precision: bool = False, checkpoint: ml.trainers.base.CheckpointConfig = <factory>, parallel: ml.trainers.mixins.data_parallel.ParallelConfig = <factory>)[source]
Bases:
BaseTrainerConfig
- parallel: ParallelConfig
- class ml.trainers.mixins.data_parallel.ParallelMixin(config: TrainerConfigT)[source]
Bases:
BaseTrainer
[ParallelConfigT
,ModelT
,TaskT
]Defines a trainer mixin for fully sharded data parallel models.