TensorDictBase.send#
- TensorDictBase.send(dst: int | 'TensorDictPipe' | None = None, *, group: 'torch.distributed.ProcessGroup' | None = None, group_dst: int | None = None, init_tag: int = 0, pseudo_rand: bool = False, consolidated: bool = False) None#
Sends the leaves of the tensordict to another rank, with
torch.distributed.The receiver calls
recv()on a tensordict with the same keys, shapes and dtypes, which writes the values into its tensors. By default, each leaf is one message, marked with a tag. To send to a rank that does not know the structure, useinit_remote(). See Distributed tensordicts.- Parameters:
dst (int or TensorDictPipe, optional) – the global rank of the receiver, or a
TensorDictPipeto send through with UCXX, which ignores the other arguments. Exclusive withgroup_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. The messages then go through the backend ofgroupdirectly, sogroupcan be a standaloneProcessGroupbuilt on a store, never registered byinit_process_group()ornew_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. Give two transfers that run at the same time tag ranges that do not overlap. Defaults to0.pseudo_rand (bool, optional) – if
True, the tags are pseudo-random numbers seeded byinit_tag, so that transfers from several ranks do not overlap. Generating them costs about 10 microseconds per leaf. The receiver must pass the same value. Defaults toFalse.consolidated (bool, optional) – if
True, sends all the leaves as one message: the tensordict is consolidated first (seeconsolidate()), unless it is already. The receiver must callrecv(consolidated=True)on a consolidated tensordict with the same structure.init_tagandpseudo_randare ignored. Defaults toFalse.
- 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 send_recv(rank, world_size): if rank == 0: td = TensorDict(a=torch.ones(4), b=torch.ones(4, 3), batch_size=[4]) td.send(dst=1) elif rank == 1: td = TensorDict(a=torch.zeros(4), b=torch.zeros(4, 3), batch_size=[4]) td.recv(src=0) print(td["b"].sum()) # tensor(12.)
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.