Rate this Page
★ ★ ★ ★ ★

unpack_memmap#

tensordict.unpack_memmap(archive_path: str | Path, prefix: str | Path) → Path#

Extracts a memory-mapped tensordict archive into a directory.

The inverse of pack_memmap(), and the same as unzip: the directory has one file per leaf, which load_memmap() reads. Existing files in prefix are overwritten.

Parameters:
  • archive_path (str or Path) – an archive written by pack_memmap() or by save().

  • prefix (str or Path) – the directory to extract into. It is created if it does not exist.

Returns:

The path of the directory, as a Path.

Raises:

ValueError – if archive_path is not an archive (see is_memmap_archive()).

Examples

>>> import tempfile
>>> from pathlib import Path
>>> import torch
>>> from tensordict import TensorDict, unpack_memmap
>>> td = TensorDict(a=torch.zeros(3))
>>> with tempfile.TemporaryDirectory() as path:
...     _ = td.save(Path(path) / "td.tdz")
...     directory = unpack_memmap(Path(path) / "td.tdz", Path(path) / "td")
...     print(sorted(p.name for p in directory.iterdir()))
['a.memmap', 'meta.json']