TensorDictBase.contiguous#
- TensorDictBase.contiguous(*, canonical: bool = False, inplace: bool = False) Self#
Returns a new tensordict of the same type with contiguous values (or self if values are already contiguous).
- Parameters:
canonical (bool, optional) – if
True, every dense tensor leaf whose strides do not match the canonical C-row-major strides for its shape is rematerialized into a freshly allocated contiguous tensor (usingtorch.contiguous_format), even iftorch.Tensor.is_contiguous()returnsTrue(e.g. because of size-1 dimensions). WhenFalse(the default), the historical behavior oftorch.Tensor.contiguous()is preserved. Defaults toFalse.inplace (bool, optional) – If
True, this tensordict’s identity and key set are preserved; each non-contiguous leaf is replaced by its contiguous counterpart one at a time.Tensor.contiguous()returnsselfwhen a leaf is already contiguous, so for fully-contiguous tensordicts the operation is effectively a no-op aside from the key walk. Defaults toFalse.
Note
On a
LazyStackedTensorDict, aPersistentTensorDict(h5, zarr) or a store, the result is aTensorDict. If the tensordicts of a lazy stack have no names, the result has no names either, even when the stack dimension has a name (fromstack_dim_nameor thenamessetter).densify()andclone()keep that name. To keep it here, name the dimension again withrefine_names().Examples
>>> import torch >>> from tensordict import TensorDict, lazy_stack >>> stack = lazy_stack( ... [TensorDict(a=torch.zeros(3), batch_size=[3]) for _ in range(2)], ... stack_dim_name="s", ... ) >>> stack.names, stack.contiguous().names (['s', None], [None, None]) >>> stack.contiguous().refine_names("s", ...).names ['s', None] >>> stack.densify().names ['s', None]
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.