TensorDictCache

class hybrid_learning.datasets.caching.TensorDictCache(sparse=False, thread_safe=None)[source]

Bases: DictCache

In-memory cache specifically for torch tensors. Other than a normal DictCache it takes care to move a torch.Tensor to CPU before saving it to the shared memory, since at the time being sharing of CUDA-tensors between sub-processes is not supported.

Note

Do not expect speed improvements if CUDA based tensors are to be cached: Copying tensors from and to CPU is quite costly and comparable if not less efficient than loading from file. Consider using a PTCache in such cases.

Public Methods:

put(descriptor, obj)

Store torch obj under key descriptor in a in-memory cache.

load(descriptor)

Load and densify tensors from in-memory cache.

Inherited from : py: class:DictCache

put(descriptor, obj)

Store torch obj under key descriptor in a in-memory cache.

load(descriptor)

Load and densify tensors from in-memory cache.

clear()

Empty cache dict.

descriptors()

Return the keys (descriptors) of the cache dict.

Inherited from : py: class:Cache

put(descriptor, obj)

Store torch obj under key descriptor in a in-memory cache.

load(descriptor)

Load and densify tensors from in-memory cache.

put_batch(descriptors, objs)

Store a batch of objs in this cache using according descriptors.

load_batch(descriptors[, return_none_if])

Load a batch of objects.

clear()

Empty cache dict.

descriptors()

Return the keys (descriptors) of the cache dict.

as_dict()

Return a dict with all cached descriptors and objects.

wrap(getitem[, descriptor_map])

Add this cache to the deterministic function getitem (which should have no side effects).

Special Methods:

__init__([sparse, thread_safe])

Init.

Inherited from : py: class:DictCache

__init__([sparse, thread_safe])

Init.

Inherited from : py: class:Cache

__repr__()

Return repr(self).

__add__(other)

Return a (cascaded) cache which will first lookup self then other with default sync mode.

__radd__(other)

Return a (cascaded) cache which will first lookup other then self with default sync mode.


Parameters
  • sparse (bool) –

  • thread_safe (bool) –

__init__(sparse=False, thread_safe=None)[source]

Init.

Parameters
  • sparse (bool) – whether tensors should be sparsified before put (and de-sparsified afterwards)

  • thread_safe (Optional[bool]) – whether to use a multiprocessing-capable dict; only available if sparse is not activated

load(descriptor)[source]

Load and densify tensors from in-memory cache.

Parameters

descriptor (Hashable) –

Return type

Optional[Any]

put(descriptor, obj)[source]

Store torch obj under key descriptor in a in-memory cache. In case it already exists, the existing object is overwritten. If the tensor device is CPU, the tensor is cached, else a CPU copy of it.

Parameters
  • descriptor (Hashable) – the descriptor key under which to store the object; used to access the object later

  • obj (Tensor) – the tensor to put into cache; must not be None