The optimum.neuron.distributed
module provides a set of tools to perform distributed training and inference.
The main task in distributed training / inference is being able to shard things such as the model weights, the gradient, and/or the optimizer state. We built Parallelizer
classes to handle the sharding.
The Parallelizer
class is the base abstract class being derived for every model supporting model parallelism. It provides methods to parallelize the model and save and load sharded checkpoints.
Base abstract class that handles model parallelism.
( model: PreTrainedModel device: Optional = None parallelize_embeddings: bool = True sequence_parallel_enabled: bool = False should_parallelize_layer_predicate_func: Optional = None **parallel_layer_specific_kwargs ) → PreTrainedModel
Parameters
PreTrainedModel
) —
The model to parallelize. Optional[torch.device]
, defaults to None
) —
The device where the new parallel layers should be put. bool
, defaults to True
) —
Whether or not the embeddings should be parallelized.
This can be disabled in the case when the TP size does not divide the vocabulary size. bool
, defaults to False
) —
Whether or not sequence parallelism is enabled. None
) —
A function that takes a layer as input and returns a boolean specifying if the input layer should be
parallelized. This is useful to skip unnecessary parallelization, for pipeline parallelism for instance. Dict[str, Any]
) —
Keyword arguments specific to some parallel layers, they will be ignored by the other parallel layers. Returns
PreTrainedModel
The parallelized model.
Parallelizes the model by transforming regular layer into their parallel counterparts. Each concrete class must implement it.
( model: PreTrainedModel device: Optional = None parallelize_embeddings: bool = True sequence_parallel_enabled: bool = False kv_size_multiplier: Optional = None pipeline_parallel_input_names: Union = None pipeline_parallel_num_microbatches: int = 1 pipeline_parallel_use_zero1_optimizer: bool = False pipeline_parallel_gradient_checkpointing_enabled: bool = False checkpoint_dir: Union = None num_local_ranks_per_step: int = 8 ) → PreTrainedModel
Parameters
PreTrainedModel
) —
The model to parallelize. Optional[torch.device]
, defaults to None
) —
The device where the new parallel layers should be put. bool
, defaults to True
) —
Whether or not the embeddings should be parallelized.
This can be disabled in the case when the TP size does not divide the vocabulary size. bool
, defaults to False
) —
Whether or not sequence parallelism is enabled. Optional[int], defaults to
None`) —
The number of times to replicate the KV heads when the TP size is bigger than the number of KV heads.
If left unspecified, the smallest multiplier that makes the number of KV heads divisible by the TP size
will be used. int
, defaults to 1) —
The number of microbatches used for pipeline execution. bool
, defaults to False
) —
When zero-1 optimizer is used, set this to True, so the PP model will understand that zero-1 optimizer
will handle data parallel gradient averaging. bool
, defaults to False
) —
Whether or not gradient checkpointing should be enabled when doing pipeline parallelism. Optional[Union[str, Path]]
) —
Path to a sharded checkpoint. If specified, the checkpoint weights will be loaded to the parallelized
model. int
, defaults to 8
) —
Corresponds to the number of local ranks that can initialize and load the model weights at the same
time. If the value is inferior to 0, the maximum number of ranks will be used. Returns
PreTrainedModel
The parallelized model.
Parallelizes the model by transforming regular layer into their parallel counterparts using
cls._parallelize()
.
It also makes sure that each parameter has loaded its weights or has been initialized if there is no pre-trained weights associated to it.
( optimizer: torch.optim.Optimizer orig_param_to_parallel_param_on_xla: Mapping ) → torch.optim.Optimizer
Parameters
torch.optim.Optimizer
) —
The original optimizer. Mapping[int, torch.nn.Parameter]
) —
A mapping (e.g. dict-like) that maps the id of a parameter in optimizer
to the id of its
parallelized counterpart on an XLA device. Returns
torch.optim.Optimizer
The tensor parallelism ready optimizer.
Creates an optimizer ready for a parallelized model from an existing optimizer.
There are two cases:
optimum.neuron.distributed.utils.make_optimizer_constructor_lazy
, it which case the exactly intended optimizer is
created for tensor parallelism.( model: Union output_dir: Union optimizer: Optional = None use_xser: bool = True async_save: bool = False num_local_ranks_per_step: int = 8 )
### Selecting Model-Specific Parallelizer Classes[[optimum.neuron.distributed.ParallelizersManager]]
Each model that supports parallelization in optimum-neuron
has its own derived Parallelizer
class. The factory class ParallelizersManager
allows you to retrieve such model-specific Parallelizer
s easily.
Provides the list of supported model types for parallelization.
( model_type_or_model: Union )
Returns a tuple of 3 booleans where:
( model_type_or_model: Union )
Returns the parallelizer class associated to the model.
## Utils
### Lazy Loading[[optimum.neuron.distributed.lazy_load_for_parallelism]]
Distributed training / inference is usually needed when the model is too big to fit in one device. Tools that allow for lazy loading of model weights and optimizer states are thus needed to avoid going out-of-memory before parallelization.
( tensor_parallel_size: int = 1 pipeline_parallel_size: int = 1 )
Context manager that makes the loading of a model lazy for model parallelism:
torch.nn.Linear
is put on the torch.device("meta")
device, meaning that it takes no memory to
instantiate.torch.nn.Embedding
is also put on the torch.device("meta")
device.optimum.neuron.distributed.utils.from_pretrained_for_mp
docstring.If both tensor_parallel_size
and pipeline_parallel_size
are set to 1, no lazy loading is performed.
Transforms an optimizer constructor (optimizer class) to make it lazy by not initializing the parameters. This makes the optimizer lightweight and usable to create a “real” optimizer once the model has been parallelized.