TensorDictBase.pad#
- TensorDictBase.pad(pad_size: Sequence[int], value: float = 0.0, inplace: bool = False, safe: bool = True) Self#
Pads this tensordict along the batch dimensions with a constant value.
- Parameters:
pad_size (Sequence[int]) – The padding size, applied to the batch dimensions starting from the first. For a tensordict of
ndimbatch dimensions,pad_sizeis a sequence of even length up to2 * ndimformatted as(dim0_left, dim0_right, dim1_left, dim1_right, ...).value (float, optional) – The fill value. Defaults to
0.0.inplace (bool, optional) –
If
True, this tensordict’s object identity and key set are preserved; each leaf storage is replaced by its padded counterpart one leaf at a time, keeping peak memory close to1xinstead of2x. The leaf tensors themselves are still freshly allocated because padding necessarily grows shapes. Defaults toFalse.Warning
If
inplace=Trueand a later leaf’s pad fails (e.g. OOM), the tensordict is left in an inconsistent state.safe=True(the default) catches the user-error class of failures before any mutation occurs.safe (bool, optional) – If
True, validate that the operation would succeed for every leaf before any mutation occurs. Set toFalseto skip the pre-flight walk when the inputs are known to be valid. Defaults toTrue.
- Returns:
The padded tensordict. When
inplace=Truethis isself.
Note
On a lazy stack whose tensordicts have no names, the result of
inplace=Falsehas no names, even when the stack dimension has a name. Seepad().Examples
>>> import torch >>> from tensordict import TensorDict >>> td = TensorDict({"a": torch.zeros(3, 4)}, batch_size=[3, 4]) >>> td.pad([0, 0, 0, 1]).batch_size torch.Size([3, 5]) >>> td.pad([0, 0, 0, 1], inplace=True) is td True >>> td.batch_size torch.Size([3, 5])
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.