load#
- tensordict.load(prefix: str | Path, device: device | None = None, non_blocking: bool = False, *, out: TensorCollection | None = None, robust_key: bool | None = True, subpath: NestedKey | None = None, mode: str | None = None, num_threads: int = 0, allow_pickle: bool | None = None) TensorCollection#
Loads a tensordict that
save()wrote.Same as
load_memmap(), which takes the same arguments. Seetensordict.TensorDict.load_memmap()for every argument, and Save and load tensordicts for the ways to save and load a tensordict.- Parameters:
prefix (str or Path) – the directory, or the
.tdzarchive file, that holds the saved tensordict.device (device, str or int, optional) – if given, the data is cast to that device. With
"meta", no file is read. Defaults toNone.non_blocking (bool, optional) – if
True, the cast todeviceis not followed by a synchronization. Defaults toFalse.
- Keyword Arguments:
out (TensorDictBase, optional) – a tensordict to load the data into. Defaults to
None.robust_key (bool, optional) – if
True, reads the percent-encoded file names thatsave()writes by default, and falls back to the legacy names of keys that are safe file names. IfFalse, reads the legacy layout only. Defaults toTrue.subpath (NestedKey or str, optional) – the nested tensordict to load, as a nested key (
("module", "0")) or a"/"-separated path ("module/0"). Only that subtree is read. Defaults toNone, which loads the whole tensordict.mode (str, optional) –
"r"maps the files copy-on-write (in-place writes stay in memory),"r+"maps them shared (in-place writes reach the files). Seetensordict.TensorDict.load_memmap()for the default.num_threads (int, optional) – the number of threads that decompress the leaves of a compressed archive. It has no effect on other data. Defaults to
0.allow_pickle (bool, optional) – if
True, non-tensor entries that were pickled are unpickled. Only load pickles from trusted sources. IfFalse, pickled entries raise aRuntimeError. Defaults toNone, which unpickles them with aFutureWarning.
- Returns:
The saved tensordict, of the class it had when it was saved. Its leaves are
MemoryMappedTensorinstances.
Examples
>>> import tempfile >>> import torch >>> import tensordict >>> from tensordict import TensorDict >>> td = TensorDict(a=torch.zeros(3), b={"c": torch.ones(3, 2)}, batch_size=[3]) >>> with tempfile.TemporaryDirectory() as tmpdir: ... _ = tensordict.save(td, tmpdir) ... loaded = tensordict.load(tmpdir) ... print(loaded["b", "c"].shape) torch.Size([3, 2])