TensorDictBase.all_reduce#
- TensorDictBase.all_reduce(op=None, *, group: 'torch.distributed.ProcessGroup' | None = None, async_op: bool = False, bucket_bytes: int | None = None) None#
Reduces each leaf over all the ranks of the group, in place, so that every rank holds the result.
Every rank of the group must call it, with a tensordict of the same structure. Each leaf is reduced with
torch.distributed.all_reduce(), for example to sum or average gradients. See Collectives.- Parameters:
op (ReduceOp, optional) – the reduction. Defaults to
None, which istorch.distributed.ReduceOp.SUM.- Keyword Arguments:
group (torch.distributed.ProcessGroup, optional) – the process group. Defaults to
None, the default process group.async_op (bool, optional) – if
True, returns the list ofWorkobjects of the reductions without waiting for them: callwait()on each one before you use the tensordict. Defaults toFalse.bucket_bytes (int, optional) – if given, packs the leaves of each device and dtype into buckets of at most
bucket_bytesbytes (a larger leaf is a bucket of its own), and runs one reduction per bucket instead of one per leaf, which is faster with many small leaves. The results are copied back into the leaves once each reduction completes, and withasync_op=True, the list holds one entry per bucket: aWork, or atorch.futures.Futurethat also copies the results back. Packing copies the leaves, so abucket_bytesbelow the size of the large leaves keeps them out of the buckets: withglooon CPU, packing leaves of 4 MB made the call several times slower. Non-contiguous and sparse leaves are never packed: each is reduced on its own. Every rank must hold the same keys, shapes and dtypes, as the buckets are built from the local leaves; if the layouts differ, the call can hang. Defaults toNone, one reduction per leaf.
- Returns:
With
async_op=True, a list ofWorkobjects (withbucket_bytes,WorkorFutureobjects, one per bucket). Otherwise,None.
Examples
This example runs in two processes: add it to the runner script of the distributed guide and pass it to
run.def all_reduce(rank, world_size): td = TensorDict(a=torch.ones(3) * rank, batch_size=[3]) td.all_reduce(op=dist.ReduceOp.SUM) print(rank, td["a"]) # with 2 ranks: tensor([1., 1., 1.]) on every rank
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.