TensorDictBase.save#
- TensorDictBase.save(prefix: str | Path | 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) Self#
Saves the tensordict to disk, and returns a memory-mapped copy of it.
save(and its aliasdumps) callsmemmap()withexistsok=True.memmap()documents the arguments in full. The tensordict is not changed. Read the files back withload(). See Save and load tensordicts.- Parameters:
prefix (str or Path, optional) – the directory where the tensordict is written, one file per leaf. If
prefixends with".tdz"orarchive=True, a single archive file is written instead. Defaults toNone, which writes into the directory of the tensordict if it is already memory-mapped there, and otherwise into anonymous shared memory, which has no file name and lasts as long as the process.copy_existing (bool, optional) – if
False, a leaf that is already a memory-mapped tensor with a file at another path raises aRuntimeError. IfTrue, the file is copied to the new path. Defaults toFalse.
- Keyword Arguments:
num_threads (int, optional) – the number of threads that write the files. Defaults to
0, which writes them in the calling thread.return_early (bool, optional) – if
Trueandnum_threads > 1, returns aTensorDictFuturewhile the threads write the files. Defaults toFalse.share_non_tensor (bool, optional) – if
True, non-tensor data is shared between the processes of the node. Defaults toFalse.robust_key (bool, optional) – if
True, keys are percent-encoded in file names, so that every file stays insideprefix. Defaults toTrue.archive (bool, optional) – if
True, writes a single archive file. Defaults toNone, which writes an archive ifprefixends with".tdz".compression (str or int, optional) – the compression of the archive entries. Defaults to
None, which stores them uncompressed.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. An archive is always written this way.memmap()gives the details. Defaults toFalse.
- Returns:
A new memory-mapped tensordict, as
memmap()returns.
Examples
>>> import tempfile >>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(a=torch.zeros(3), nested=TensorDict(b=torch.ones(3), batch_size=[3]), batch_size=[3]) >>> with tempfile.TemporaryDirectory() as path: ... _ = td.save(path) ... loaded = TensorDict.load(path) ... print((loaded == td).all()) True
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.