TensorDictBase.init_remote#
- TensorDictBase.init_remote(dst: int | None = None, group: 'ProcessGroup' | None = None, device: device | None = None, use_broadcast: bool = False, _tensorclass_type: str | None = None) None#
Sends the structure and the content of the tensordict to a rank that does not know them.
The receiver calls
from_remote_init(), which builds a new consolidated tensordict. The tensordict is consolidated (seeconsolidate()), then its metadata is sent withtorch.distributed.send_object_list(), which pickles it, and its storage as one tensor. Send the next batches withsend(consolidated=True), into the tensordict thatfrom_remote_init()returned. See Distributed tensordicts.With
use_broadcast=True, it callsbroadcast()instead, and every rank of the group must take part: the others callfrom_remote_init(src=..., use_broadcast=True).- Parameters:
dst (int, optional) – the rank of the receiver. Required unless
use_broadcast=True, which ignores it. Defaults toNone.group (torch.distributed.ProcessGroup, optional) – the process group. Defaults to
None, the default process group.device (device, str or int, optional) – point-to-point, the device that the metadata goes through, as in
torch.distributed.send_object_list()(withnccl, a CUDA device). Withuse_broadcast=True, the device of the consolidated storage, as inbroadcast(). Defaults toNone.use_broadcast (bool, optional) – if
True, broadcasts the tensordict to every rank of the group. Defaults toFalse.
Examples
This example runs in two processes: add it to the runner script of the distributed guide and pass it to
run.def cold_start(rank, world_size): if rank == 0: td = TensorDict(obs=torch.ones(2, 84), reward=torch.ones(2, 1), batch_size=[2]) td.init_remote(dst=1) td["obs"].add_(1) td.send(dst=1, consolidated=True) elif rank == 1: td = TensorDict.from_remote_init(src=0) print(td.batch_size, td.is_consolidated()) # torch.Size([2]) True td.recv(src=0, consolidated=True) print(td["obs"][0, 0]) # tensor(2.)
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.