TensorDictBase.memmap#
- TensorDictBase.memmap_(prefix: str | Path | 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, atomic: bool = False) Self#
Writes every leaf to a memory-mapped file in place, and returns
self.Writes the same files as
memmap(), then replaces each leaf of the tensordict with itsMemoryMappedTensor, so thedata_ptr()of every leaf changes. The device of the tensordict becomes"cpu". See Save and load tensordicts.The tensordict is then locked (see Locking and temporary changes): you can write into its leaves in place, but you cannot add, remove or replace an entry. Once it is unlocked,
is_memmap()returnsFalse.- Parameters:
prefix (str or Path, optional) – the directory to write to. Defaults to
None, 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, so that data on disk is not duplicated by accident. 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.
0and1write them one after the other in the calling thread. Threads help with a few large leaves and slow down many small ones. Defaults to0.return_early (bool, optional) – if
Trueandnum_threads > 1, returns aTensorDictFutureat once, while the threads write the files:future.result()waits for them and returns the tensordict. Defaults toFalse.share_non_tensor (bool, optional) – if
True, non-tensor data is shared between the processes of the node, so that an in-place write of a non-tensor entry in one process is seen by the others. Many non-tensor leaves (for example a large stack of non-tensor data) can use up the memory. Defaults toFalse.existsok (bool, optional) – if
False, a file that already exists at the path of a leaf raises aRuntimeError. IfTrue, it is overwritten. Defaults toTrue.robust_key (bool, optional) – if
True, keys are percent-encoded in file and directory names ("a/b"is written toa%2Fb.memmap), so that every file stays insideprefix. IfFalse, keys are used as file names unchanged, as in the legacy layout: do not use it with keys you do not trust.Noneis the same asTrue. Defaults toTrue.atomic (bool, optional) – if
True, the files are written to a new directory next toprefix, which is then renamed toprefix, so that an error or a crash during the write leavesprefixas it was. The new directory is removed if the write fails (but not after a crash). An existing directory atprefixis replaced as a whole once the new one is complete, so files that the tensordict does not write are removed: it must be empty or hold a saved tensordict (ameta.jsonfile). Readers see the old or the new directory, except for the short time between two renames, during whichprefixis missing. The write needs about twice the disk space of the tensordict while it runs, and a leaf that is already memory-mapped to a file is copied, which needscopy_existing=True. The files are not synced to the disk, so this guards against a killed process, not against a power failure. If the write fails, the leaves already converted map deleted files, so a retry needscopy_existing=True. Not supported withreturn_early=True, nor on Windows, where a directory that holds open files cannot be renamed. Defaults toFalse.
- Returns:
self, or aTensorDictFutureof it ifreturn_early=Trueandnum_threads > 1.- Raises:
RuntimeError – if a leaf already has a file elsewhere and
copy_existing=False, if a file exists andexistsok=False, if the tensordict is in shared memory (seeshare_memory_()), or if it is aPersistentTensorDict, aTensorDictStore, aLazyStackedTensorDictStoreor one of their elements. Usememmap()to make a memory-mapped copy of those. Withatomic=True, also ifprefixis a non-empty directory andexistsok=False, or if it holds no saved tensordict.NotADirectoryError – if
atomic=Trueandprefixis a file.ValueError – if
atomic=Trueand there is no directory to write to.NotImplementedError – if
return_early=Truewithatomic=True, oratomic=Trueon Windows.
Examples
>>> import tempfile >>> import torch >>> from tensordict import TensorDict >>> td = TensorDict(obs=torch.zeros(3, 4), batch_size=[3]) >>> with tempfile.TemporaryDirectory() as path: ... out = td.memmap_(path) ... print(out is td, td.is_memmap(), td.is_locked) ... print(type(td["obs"]).__name__) True True True MemoryMappedTensor
Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.