PackLayout#
- class tensordict.PackLayout(keys: tuple[NestedKey, ...], shapes: tuple[Size, ...], dim: int = -1, nested: tuple[tuple[NestedKey, Size, type | None], ...] = (), device: device | None = None, cls: type | None = None)#
Bases:
objectThe layout of a tensordict packed by
pack().It records the column of each leaf in the packed tensor, and the nesting, batch dimensions and classes needed to rebuild the tensordict with
unpack(). It is frozen, hashable and compares by value, so a module can cache it andtorch.compilecan treat it as a constant.Only tensorclasses are rebuilt with their class. Other subclasses of
TensorDictBase, such asTypedTensorDictorTensorDictParams, come back as aTensorDict.- Variables:
shapes (tuple of Size) – the feature shape of each leaf, that is its shape after the batch dimensions of the packed tensordict. Lists and tuples of ints are converted to
torch.Size.dim (int) – the position of the packed dimension in the packed tensor.
pack()stores it counted from the end (a negative int), sotd.pack(1)andtd.pack(-1)give equal layouts whentdhas one batch dimension, andflatmay have extra leading batch dimensions.nested (tuple) – one
(key, batch_shape, cls)entry per nested tensor collection, wherebatch_shapeis its batch shape after the batch dimensions of the root andclsis its tensorclass, orNonefor a tensordict. Batch shapes given as lists or tuples of ints are converted totorch.Size.device (device or None) – the device of the packed tensordict.
cls (type or None) – the tensorclass of the packed tensordict, or
None.numel (int) – the number of columns of the packed tensor.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(p=torch.zeros(5), v=torch.ones(5, 3), batch_size=[5]) >>> flat, layout = td.pack() >>> layout.keys, layout.offsets, layout.numel (('p', 'v'), (0, 1), 4) >>> out = layout.unpack(flat[:2]) >>> out["v"].shape torch.Size([2, 3])
- unpack(flat: Tensor) TensorDictBase#
Rebuilds a tensordict from a packed tensor.
The leaves of the result are views of
flat. Its batch size is the shape offlatwithout the packed dimension, soflatmay have other batch dimensions than the packed tensordict, for instance after anindex_select.- Parameters:
flat (Tensor) – a tensor with
numelentries along dimensiondim.- Returns:
a
TensorDict, or an instance ofcls, on the device offlatif the packed tensordict had a device.
Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(p=torch.zeros(5), v=torch.ones(5, 3), batch_size=[5]) >>> flat, layout = td.pack() >>> out = layout.unpack(flat) >>> assert (out == td).all() >>> out["p"].data_ptr() == flat.data_ptr() True