Rate this Page
★ ★ ★ ★ ★

TensorDictBase.to_store#

TensorDictBase.to_store(*, backend: str = 'redis', host: str = 'localhost', port: int = 6379, db: int = 0, unix_socket_path: str | None = None, prefix: str = 'tensordict', device=None, **kwargs) → Any#

Upload this TensorDict to a key-value store (Redis, Dragonfly, etc.).

Returns a TensorDictStore (or LazyStackedTensorDictStore for lazy stacks) backed by the uploaded data.

For LazyStackedTensorDict inputs, data is streamed in chunks to avoid materialising the full stack in memory.

Keyword Arguments:
  • backend (STORE_BACKENDS) – Store backend — "redis" (default) or "dragonfly".

  • host (str) – Server hostname. Defaults to "localhost".

  • port (int) – Server port. Defaults to 6379.

  • db (int) – Database number. Defaults to 0.

  • unix_socket_path (str, optional) – Unix domain socket path.

  • prefix (str) – Key namespace. Defaults to "tensordict".

  • device (device, optional) – Device override for retrieved tensors. If None, uses this TensorDict’s device.

  • **kwargs – Extra connection keyword arguments.

Returns:

A store-backed TensorDict instance.

Examples

The example needs a Redis server on localhost:6379, and the last line a Dragonfly server on the host dragonfly-host, so it is not run when the documentation is tested.

>>> import torch
>>> from tensordict import TensorDict
>>> td = TensorDict({"obs": torch.randn(10, 84)}, [10])
>>> store_td = td.to_store(host="localhost")  
>>> store_td["obs"].shape  
torch.Size([10, 84])
>>>
>>> # Using Dragonfly instead of Redis
>>> store_td = td.to_store(backend="dragonfly", host="dragonfly-host")  

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.