latest (dev)
Copy
Latest development documentation · Updated 2026-10-08
tensorplay.distributed.optim API
Functions 1
as_functional_optim
functionFull reference ↗Classes 3
DistributedOptimizer
classFull reference ↗- class tensorplay.distributed.optim.DistributedOptimizer(optimizer_class, params_rref, *args, **kwargs)[source]
DistributedOptimizer takes remote references to parameters and runs the
This class requires the RPC framework.
PostLocalSGDOptimizer
classFull reference ↗- class tensorplay.distributed.optim.PostLocalSGDOptimizer(optim: Optimizer, averager: ModelAverager)[source]
Wraps an arbitrary
tensorplay.optim.Optimizerand runs post-local SGD, This optimizer runs local optimizer at every step. After the warm-up stage, it averages parameters periodically after the local optimizer is applied.- Parameters:
optim – The local optimizer.
averager – A model averager instance to run post-localSGD algorithm.
Example:
>>> # xdoctest: +SKIP("undefined variables") >>> local_optim = tp.optim.SGD(params=model.parameters(), lr=0.01) >>> opt = PostLocalSGDOptimizer( >>> optim=local_optim, >>> averager=averagers.PeriodicModelAverager(period=4, warmup_steps=100) >>> ) >>> for step in range(0, 200): >>> opt.zero_grad() >>> loss = loss_fn(output, labels) >>> loss.backward() >>> opt.step()- load_state_dict(state_dict)[source]
This is the same as
tensorplay.optim.Optimizerload_state_dict(), but also restores model averager’s step value to the one saved in the providedstate_dict.If there is no
"step"entry instate_dict, it will raise a warning and initialize the model averager’s step to 0.
- state_dict()[source]
This is the same as
tensorplay.optim.Optimizerstate_dict(), but adds an extra entry to record model averager’s step to the checkpoint to ensure reload does not cause unnecessary warm up again.
- step()[source]
Performs a single optimization step (parameter update).
ZeroRedundancyOptimizer
classFull reference ↗- class tensorplay.distributed.optim.ZeroRedundancyOptimizer(params, optimizer_class: type[Optimizer], process_group=None, parameters_as_bucket_view: bool = False, overlap_with_ddp: bool = False, **defaults: Any)[source]
Wrap an arbitrary
optim.Optimizerand shards its states across ranks in the group.The sharing is done as described by ZeRO.
The local optimizer instance in each rank is only responsible for updating approximately
1 / world_sizeparameters and hence only needs to keep1 / world_sizeoptimizer states. After parameters are updated locally, each rank will broadcast its parameters to all other peers to keep all model replicas in the same state.ZeroRedundancyOptimizeruses a sorted-greedy algorithm to pack a number of parameters at each rank. Each parameter belongs to a single rank and is not divided among ranks.- Parameters:
params – an
Iterableof tensors or dicts giving all parameters.- Keyword Arguments:
optimizer_class – the class of the local optimizer.
process_group –
ProcessGroup(default:dist.GroupMember.WORLD).parameters_as_bucket_view (bool, optional) – if
True, parameters are packed into buckets to speed up communication.overlap_with_ddp (bool, optional) – if
True, requires a functional optimizer and registering one of the DDP communication hooks fromddp_zero_hook.py.**defaults – forwarded to the local optimizer.
Warning
Currently,
ZeroRedundancyOptimizerrequires that all of the passed-in parameters are the same dense type.Warning
ZeroRedundancyOptimizer is experimental and subject to change.
- add_param_group(param_group: dict[str, Any]) None[source]
Add a parameter group to the
Optimizer’sparam_groups.Warning
This method handles updating the shards on all partitions but needs to be called on all ranks.
- consolidate_state_dict(to: int = 0) None[source]
Consolidate a list of
state_dicts (one per rank) on the target rank.- Parameters:
to (int) – the rank that receives the optimizer states (default: 0).
Warning
This needs to be called on all ranks.
- property join_device
Return default device.
- join_hook(**_kwargs: Any) JoinHook[source]
Return the ZeRO join hook.
It enables training on uneven inputs by shadowing the collective communications in the optimizer step.
Gradients must be properly set before this hook is called.
- property join_process_group: Any
Return process group.
- load_state_dict(state_dict: dict[str, Any]) None[source]
Load the state pertaining to the given rank from the input
state_dict, updating the local optimizer as needed.
- state_dict() dict[str, Any][source]
Return the last global optimizer state known to this rank.
- Raises:
RuntimeError – if this method is called without a preceding call to
consolidate_state_dict().
- step(closure=None, **kwargs: Any) float | None[source]
Perform a single optimizer step and syncs parameters across all ranks.
- Parameters:
closure (Callable) – a closure that re-evaluates the model and returns the loss; optional for most optimizers.
- Returns:
Optional loss depending on the underlying local optimizer.
Note
Any extra parameters are passed to the base optimizer as-is.
Help improve this page
Found an error, an unclear step, or a missing example?
tensorplay.distributed.nn.functional API
Complete API reference for tensorplay.distributed.nn.functional, including signatures, parameters, examples and members.
tensorplay.distributed.pipelining API
Complete API reference for tensorplay.distributed.pipelining, including signatures, parameters, examples and members.

