TensorDictBase.isend#
- TensorDictBase.isend(dst: int | None = None, *, group: 'torch.distributed.ProcessGroup' | None = None, group_dst: int | None = None, init_tag: int = 0, pseudo_rand: bool = False, return_early: bool = False, consolidated: bool = False) int | List['Work'] | None#
Sends the leaves of the tensordict to another rank, one asynchronous transfer per leaf.
As
send(), but the transfers of all the leaves run at the same time. By default, the call waits until they are done. Withreturn_early=True, it returns at once: callwait()on each returnedWorkobject before you change the tensordict. The receiver callsirecv()orrecv(). See Distributed tensordicts.- Parameters:
dst (int, optional) – the global rank of the receiver. Exclusive with
group_dst: pass exactly one of them. Defaults toNone.- Keyword Arguments:
group (torch.distributed.ProcessGroup, optional) – the process group. Defaults to
None, the default process group.group_dst (int, optional) – the rank of the receiver in
group, which must be given. Exclusive withdst. As insend(),groupcan then be a standalone process group. Defaults toNone.init_tag (int, optional) – the tag of the first leaf. The next leaves use
init_tag + 1,init_tag + 2, and so on. The receiver must pass the same value. Defaults to0.pseudo_rand (bool, optional) – if
True, the tags are pseudo-random numbers seeded byinit_tag. The receiver must pass the same value. Defaults toFalse.return_early (bool, optional) – if
True, returns the list ofWorkobjects of the transfers without waiting for them. Defaults toFalse.consolidated (bool, optional) – if
True, sends all the leaves as one message, assend(consolidated=True)does. A tensordict that is not consolidated is consolidated on each call, which copies every leaf: keep a consolidated copy and refresh it withupdate_()to avoid the copy. The receiver must callirecv(consolidated=True)orrecv(consolidated=True).init_tagandpseudo_randare ignored. Defaults toFalse.
- Returns:
With
return_early=True, a list ofWorkobjects. Otherwise, the tag of the last leaf sent, orNonewithconsolidated=True.- Raises:
ValueError – if both or none of
dstandgroup_dstare given, or ifgroup_dstis given withoutgroup.
Examples
This example runs in two processes: add it to the runner script of the distributed guide and pass it to
run.def isend_irecv(rank, world_size): if rank == 0: td = TensorDict(a=torch.ones(4), b=torch.ones(4, 3), batch_size=[4]) works = td.isend(dst=1, return_early=True) # ... other work, which does not change td ... for work in works: work.wait() elif rank == 1: td = TensorDict(a=torch.zeros(4), b=torch.zeros(4, 3), batch_size=[4]) works = td.irecv(src=0, return_premature=True) # ... other work, which does not read td ... for work in works: work.wait() print(td["b"].sum()) # tensor(12.)
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.