latest (dev)
Copy
Latest development documentation · Updated 2026-10-08
tensorplay.distributed.algorithms.model_averaging API
Classes 3
HierarchicalModelAverager
classFull reference ↗- class tensorplay.distributed.algorithms.model_averaging.HierarchicalModelAverager(period_group_size_dict=None, warmup_steps=0, process_group=None)[source]
Runs hierarchical model averaging (hierarchical SGD).
Process groups of different sizes are organized in a hierarchy, and they average parameters by using different periods concurrently after the warm-up stage. This is an extension of
PeriodicModelAveragerthat supports post-local SGD, which essentially only supports a two-level hierarchy: the intra-machine level and the global level, where the intra-machine level is usually embedded inpost_localSGD_hook(). Similarly, the process groups within this class do not have such an intra-machine process subgroup, which should be embedded by the post-local SGD communication hook instead.- Parameters:
period_group_size_dict – An ordered dict mapping keys of model averaging period to process group size, used for initializing process groups of different sizes in a hierarchy to average parameters concurrently.
warmup_steps (int) – The number of warm-up steps. During this stage, model averaging is skipped.
process_group (ProcessGroup, optional) – The overall process group containing all the processes that runs model averaging.
Warning
The last group size in the dict must be the size of the provided
process_group, which indicates model averaging at the highest level of the hierarchy. Ifprocess_groupis not provided, then the last group size should be equal to the world size.Warning
HierarchicalModelAverager is experimental and subject to change.
- average_parameters(params: Iterable)[source]
Averages parameters or parameter groups of an optimizer.
Averaging only occurs if
stepis no less thanwarmup_stepsand it can be divided by a period in the keys ofperiod_process_group_dict, wherestepis increased by 1 at each iteration in the training loop. Ifstepcan be divided by multiple periods in the keys ofperiod_process_group_dict, only the largest period is used, and the corresponding process group is used for averaging parameters. :param params: The parameters of a model or parameter groups of an optimizer.
ModelAverager
classFull reference ↗- class tensorplay.distributed.algorithms.model_averaging.ModelAverager(process_group: ProcessGroup | None = None)[source]
Base class for all model averagers.
- Parameters:
process_group – The process group to be used for all-reduce. If
None, the default process group, which is created bytensorplay.distributed.init_process_group(), will be used. (default:None)
PeriodicModelAverager
classFull reference ↗- class tensorplay.distributed.algorithms.model_averaging.PeriodicModelAverager(period, warmup_steps=0, process_group: ProcessGroup | None = None)[source]
Averages parameters periodically after the warm-up stage.
This can be used for running post-local SGD, by running
DistributedDataParallel(DDP) using the subgroups created bynew_subgroups().- Parameters:
period (int) – The number of steps per model averaging. Usually the period should be greater than
1to reduce the communication cost. Otherwise, only DDP needs to be used.warmup_steps (int) – The number of warm-up steps. During this stage, model averaging is skipped.
process_group – The process group to be used for all-reduce. If
None, the default process group, which is created bytensorplay.distributed.init_process_group(), will be used. (default:None)
Example:
>>> # xdoctest: +SKIP("undefined variables") >>> averager = averagers.PeriodicModelAverager(period=4, warmup_steps=100) >>> for step in range(0, 200): >>> optimizer.zero_grad() >>> loss = loss_fn(output, labels) >>> loss.backward() >>> optimizer.step() >>> # Will average model parameters globally every 4 steps. >>> averager.average_parameters(model.parameters())- average_parameters(params: Iterable[Parameter] | Iterable[dict[str, Parameter]])[source]
Averages parameters or parameter groups of an optimizer if
stepis no less thanwarmup_steps.Can be divided by
period, wherestepis increased by 1 at each iteration in the training loop. :param params: The parameters of a model or parameter groups of an optimizer.
Help improve this page
Found an error, an unclear step, or a missing example?
tensorplay.distributed.algorithms.ddp_comm_hooks.quantization_hooks API
Complete API reference for tensorplay.distributed.algorithms.ddp_comm_hooks.quantization_hooks, including signatures, parameters, examples and members.
tensorplay.distributed.autograd API
Complete API reference for tensorplay.distributed.autograd, including signatures, parameters, examples and members.

