save#
- tensordict.save(data: TensorCollection, prefix: str | None = None, copy_existing: bool = False, *, num_threads: int = 0, return_early: bool = False, share_non_tensor: bool = False, robust_key: bool | None = True, archive: bool | None = None, compression: str | int | None = None, atomic: bool = False) TensorCollection#
Saves a tensordict to disk, and returns a memory-mapped copy of it.
Same as
memmap(), and asmemmap(), which holds the full text of every argument. The directory mirrors the key structure of the tensordict: one file per tensor and one metadata file per nested tensordict. Ifprefixends with".tdz", a single archive file is written instead. Load the result withload(). See Save and load tensordicts.- Parameters:
data (TensorDictBase or tensorclass instance) – the tensordict to save.
prefix (str, optional) – the directory to write to, or a file name that ends with
".tdz". Defaults toNone, which writes the tensors to anonymous shared memory and saves nothing to disk.copy_existing (bool, optional) – if
False, an entry that is already memory-mapped to a file outsideprefixraises an error. IfTrue, it is copied. Defaults toFalse.
- Keyword Arguments:
num_threads (int, optional) – the number of threads that write the files. Defaults to
0.return_early (bool, optional) – if
Trueandnum_threads > 1, returns aTensorDictFutureat once, while the threads write the files. Defaults toFalse.share_non_tensor (bool, optional) – if
True, non-tensor entries are shared between the processes of a node. Defaults toFalse.robust_key (bool, optional) – if
True, keys that hold path separators or special characters are encoded in the file names. Defaults toTrue.archive (bool, optional) – if
True,prefixis a file and the tensordict is written as a single zip archive thatload()can memory-map. IfNone, an archive is written whenprefixends with".tdz". Defaults toNone.compression (str or int, optional) – the compression of the archive entries:
"stored","deflate","bzip2","lzma"or azipfileconstant. Only valid for an archive. Defaults toNone, which stores them uncompressed. Only uncompressed entries load without a copy.atomic (bool, optional) – if
True, the directory is written next toprefixand then renamed to it, so that an error or a crash during the write leavesprefixas it was. Not supported on Windows for a directory.memmap()gives the details. Defaults toFalse.
- Returns:
A new, locked tensordict whose leaves are
MemoryMappedTensorinstances backed by the saved files.dataitself is not changed.
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: ... saved = tensordict.save(td, tmpdir) ... print(saved.is_locked, type(saved["a"]).__name__) ... loaded = tensordict.load(tmpdir) True MemoryMappedTensor