memmap#
- tensordict.memmap(data: TensorCollection, prefix: str | None = None, copy_existing: bool = False, *, num_threads: int = 0, return_early: bool = False, share_non_tensor: bool = False, existsok: bool = True, robust_key: bool | None = True, archive: bool | None = None, compression: str | int | None = None, atomic: bool = False) TensorCollection#
Writes every leaf to a memory-mapped file and returns a new tensordict of them.
Same as
save(), and asmemmap(), which holds the full text of every argument. Use it to share a tensordict between processes without copies, or to keep data that is larger than RAM on disk. To convert the tensordict in place, usememmap_(). See Save and load tensordicts and Load data and share it between processes.- Parameters:
data (TensorDictBase or tensorclass instance) – the tensordict to write.
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.existsok (bool, optional) – if
False, a tensor file that already exists underprefixraises an error. Defaults toTrue.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.
Examples
>>> import torch >>> import tensordict >>> from tensordict import TensorDict >>> td = TensorDict(a=torch.zeros(3), batch_size=[3]) >>> td_memmap = tensordict.memmap(td) >>> type(td_memmap["a"]).__name__ 'MemoryMappedTensor' >>> td_memmap.is_locked True