Skip to main content

TensorDict is a pytorch dedicated tensor container.

Project description

Docs Discord Python version GitHub license pypi version Downloads Conda (channel only)

TensorDict

TensorDict is a dictionary-like class that inherits properties from tensors, such as indexing, shape operations, casting to device or storage and many more. The code-base consists of two main components: TensorDict, a specialized dictionary for PyTorch tensors, and tensorclass, a dataclass for tensors.

from tensordict import TensorDict

data = TensorDict(
    obs=torch.randn(128, 84),
    action=torch.randn(128, 4),
    reward=torch.randn(128, 1),
    batch_size=[128],
)

data_gpu = data.to("cuda")      # all tensors move together
sub = data_gpu[:64]              # all tensors are sliced
stacked = torch.stack([data, data])  # works like a tensor

Key Features | Examples | Installation | Ecosystem | Citation | License

Key Features

TensorDict makes your code-bases more readable, compact, modular and fast. It abstracts away tailored operations, dispatching them on the leaves for you.

  • Composability: TensorDict generalizes torch.Tensor operations to collections of tensors. [tutorial]
  • Speed: asynchronous transfer to device, fast node-to-node communication through consolidate, compatible with torch.compile. [tutorial]
  • Shape operations: indexing, slicing, concatenation, reshaping -- everything you can do with a tensor. [tutorial]
  • Distributed / multiprocessed: distribute TensorDict instances across workers, devices and machines. [doc]
  • Serialization and memory-mapping for efficient checkpointing. [doc]
  • Functional programming and compatibility with torch.vmap. [tutorial]
  • Nesting: nest TensorDict instances to create hierarchical structures. [tutorial]
  • Lazy preallocation: preallocate memory without initializing tensors. [tutorial]
  • @tensorclass: a specialized dataclass for torch.Tensor. [tutorial]

Examples

Check our Getting Started guide for a full overview of TensorDict's features.

Before / after

Working with groups of tensors is common in ML. Without a shared structure, every operation must be repeated for each tensor:

# Without TensorDict
obs = obs.to("cuda")
action = action.to("cuda")
reward = reward.to("cuda")
next_obs = next_obs.to("cuda")

obs_batch = obs[:32]
action_batch = action[:32]
reward_batch = reward[:32]
next_obs_batch = next_obs[:32]

With TensorDict, all of that collapses to:

# With TensorDict
data = data.to("cuda")
data_batch = data[:32]

This holds for any operation: reshape, unsqueeze, permute, to, indexing, torch.stack, torch.cat, and many more.

Generic training loops

Using TensorDict primitives, most supervised training loops can be rewritten in a generic way:

for i, data in enumerate(dataset):
    data = model(data)
    loss = loss_module(data)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

Each step of the training loop -- data loading, model prediction, loss computation -- can be swapped independently without touching the rest. The same loop works across classification, segmentation, RL, and more.

Fast copy on device

By default, device transfers are asynchronous and synchronized only when needed:

td_cuda = TensorDict(**dict_of_tensors, device="cuda")
td_cpu = td_cuda.to("cpu")
td_cpu = td_cuda.to("cpu", non_blocking=False)  # force synchronous

Coding an optimizer

Using TensorDict you can code the Adam optimizer as you would for a single tensor and apply it to a collection of parameters. On CUDA, these operations use fused kernels:

class Adam:
    def __init__(self, weights: TensorDict, alpha: float=1e-3,
                 beta1: float=0.9, beta2: float=0.999,
                 eps: float = 1e-6):
        weights = weights.lock_()
        self.weights = weights
        self.t = 0

        self._mu = weights.data.clone()
        self._sigma = weights.data.mul(0.0)
        self.beta1 = beta1
        self.beta2 = beta2
        self.alpha = alpha
        self.eps = eps

    def step(self):
        self._mu.mul_(self.beta1).add_(self.weights.grad, 1 - self.beta1)
        self._sigma.mul_(self.beta2).add_(self.weights.grad.pow(2), 1 - self.beta2)
        self.t += 1
        mu = self._mu.div_(1-self.beta1**self.t)
        sigma = self._sigma.div_(1 - self.beta2 ** self.t)
        self.weights.data.add_(mu.div_(sigma.sqrt_().add_(self.eps)).mul_(-self.alpha))

Ecosystem

TensorDict is used across a range of domains:

Domain Projects
Reinforcement Learning TorchRL (PyTorch), DreamerV3-torch, Dreamer4, SkyRL
LLM Post-Training verl, ROLL (Alibaba), LMFlow, LoongFlow (Baidu)
Robotics & Simulation MuJoCo Playground (Google DeepMind), ProtoMotions (NVIDIA), holosoma (Amazon)
Physics & Scientific ML PhysicsNeMo (NVIDIA)
Genomics Medaka (Oxford Nanopore)

Installation

With pip:

pip install tensordict

For the latest features:

pip install tensordict-nightly

With conda:

conda install -c conda-forge tensordict

With uv + PyTorch nightlies:

If you're using a PyTorch nightly, install tensordict with --no-deps to prevent uv from re-resolving torch from PyPI:

uv pip install -e . --no-deps

Or explicitly point uv at the PyTorch nightly wheel index:

uv pip install -e . --prerelease=allow -f "https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html"

Citation

If you're using TensorDict, please refer to this BibTeX entry to cite this work:

@misc{bou2023torchrl,
      title={TorchRL: A data-driven decision-making library for PyTorch},
      author={Albert Bou and Matteo Bettini and Sebastian Dittert and Vikash Kumar and Shagun Sodhani and Xiaomeng Yang and Gianni De Fabritiis and Vincent Moens},
      year={2023},
      eprint={2306.00577},
      archivePrefix={arXiv},
      primaryClass={cs.LG}
}

License

TensorDict is licensed under the MIT License. See LICENSE for details.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tensordict_nightly-2026.5.29-cp314-cp314-win_amd64.whl (612.6 kB view details)

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.29-cp314-cp314-macosx_15_0_universal2.whl (540.3 kB view details)

Uploaded CPython 3.14macOS 15.0+ universal2 (ARM64, x86-64)

tensordict_nightly-2026.5.29-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.29-cp313-cp313-macosx_15_0_universal2.whl (540.1 kB view details)

Uploaded CPython 3.13macOS 15.0+ universal2 (ARM64, x86-64)

tensordict_nightly-2026.5.29-cp312-cp312-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.29-cp312-cp312-macosx_15_0_universal2.whl (540.1 kB view details)

Uploaded CPython 3.12macOS 15.0+ universal2 (ARM64, x86-64)

tensordict_nightly-2026.5.29-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.29-cp311-cp311-macosx_15_0_universal2.whl (539.2 kB view details)

Uploaded CPython 3.11macOS 15.0+ universal2 (ARM64, x86-64)

tensordict_nightly-2026.5.29-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.29-cp310-cp310-macosx_15_0_universal2.whl (537.9 kB view details)

Uploaded CPython 3.10macOS 15.0+ universal2 (ARM64, x86-64)

File details

Details for the file tensordict_nightly-2026.5.29-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 f0a1e44ac870ef66b07af102ab2c809cdbafc9747addfdc13c8e59865271857f
MD5 85525fdeb74b3c8e7e0e4f118f5fe248
BLAKE2b-256 e01640ab5ab76186248145fda06d14c6efe4b9948d76e5dacf74c3eab53625c5

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp314-cp314-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e6fb4543591428309c29c37cd78c9544d92b7a16ea0aafc68e9a93cc19cb0c5d
MD5 cdf1c7aff6d6e389f973254c2bc6f365
BLAKE2b-256 d82955e13c17cd9e6610987e33042ab64096673054fe7464b0cb0ec1f8751e62

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp314-cp314-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b1e2f12b00ef91d0248a4bbae79634d3e9f8128e809adbeb1f0951f5b8784bd5
MD5 c9aebee8408860ecdde37e66b3a1e1f4
BLAKE2b-256 4dba1f2b0dab6fbb8baed62889d2ae39e4f50bd02f4e9a9ca1b2512171fe97d8

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 cdd7dd6f2070c9ccbacdf82818ade63dc7bc6f6427e714276578e187ecece14b
MD5 9ebe9754e6879fed3207ffad18d359fe
BLAKE2b-256 d2535c6192463ebe9e79a004864ba522d1a463ab75efce41f497c9c0d16ab6c5

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp313-cp313-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a89e102290f85b6403dbcdf6394ebd833b8a7b9dcc338e296b31788b927879f6
MD5 ffa68464b1270eb36f5ed7871d1b8c89
BLAKE2b-256 db8d5c580df0d39335cf3655d9fa58d8896353fe9a7cbd0e127c4ed3ccbb74b7

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp313-cp313-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 32622ef3f1eedd4027f734a1ac7cefcbc55db34e504ec66ec994932a177a6f59
MD5 3ce8f30189d6fb53574fd549e87ee713
BLAKE2b-256 a737377fa2dbc22d096a8756b18b6960e0e3296b4e42a562919a84d2ea73090a

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0141c8dfa14b4f11de9c35110cb9dc6e55d347b01c3c9e6b82bce32c8382fe0d
MD5 e7c3efed404e9d29fdfa4bdf385b1968
BLAKE2b-256 ad91711e35bb7c5558aa174b574e896f5be616edb2221c15f320186b1cd36b49

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp312-cp312-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7c2e2ba5ebf0e02acc9c9d7f221605acef6773ee46fb8846bd660670ed36529e
MD5 5a46673e22b9e48d147cf63f340709f1
BLAKE2b-256 57b2fbd73332cf953c0773058fdf647edf8707381945d7bcb70595fe2103c749

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp312-cp312-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1aaf84d867b090c9a44b2edb802ea4d7860636c0fc46076a4f24f69d6e018793
MD5 12bda1a15e9bc8874139094de5347ac3
BLAKE2b-256 558fe7f67f23ff5cb2f2b443d26ebb3f496223354a7ccbe1326b08124f558288

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a45743fe2b8820dd388002e4becaa8215a59da7ac727bca06d3615b92b189692
MD5 d4d912104c96a47a0ee209f28ef681d9
BLAKE2b-256 93c049cfcbc10fbde2db232cb75a85d3e4f77f38e57843b9c2a8207bebcfe519

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp311-cp311-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 aed5dcd712629f62db37cb7da35d23cc606440a867600ae947aedd8f1e6c81d1
MD5 fd6e146b5e1c6b02349aa65db0b611c2
BLAKE2b-256 b4317cf97ba3df40b95abe069b3274eb9b27cd1e4c7c818c74081a821f31afed

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp311-cp311-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8627bf8b025f9d5a6c49da4bcb5649ea329bb7c82c6ba49fa03fe3f74528c62b
MD5 741eb238126098a1c3c0c48a68882a9a
BLAKE2b-256 2b513f4794df02478d68d9f957fcc0ac29de0a5e7437211c0429ee9836dd8c78

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5b648e2e4ebfeab9fc8d2a09fc02b50e7ecd53f509c7d727b5f526f428b9e828
MD5 9c4f7d3b66b13bf195617beb4834906b
BLAKE2b-256 cd7c87bb76d8af7b6e5020039a505e88eabc619e8ca7ce9a9fecbb1f1e7b4ab9

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp310-cp310-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 acf700128a96d7b5e9d39ac65db87beb9262aca91d7812cdc5d1880fe8b1743c
MD5 54066f599bee4d2e03e20b0596ac086a
BLAKE2b-256 ac2fa5e7bfe0ef66dfe48d0df7b095a0d9aeb7ff710586bd83666f500688aa03

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.5.29-cp310-cp310-macosx_15_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.29-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f65f9e60351081b93bd75e68d519adae6dd943840cd488bb2ac71c895334e5c7
MD5 3c0b7832f4598705120ac1d4bb78b97f
BLAKE2b-256 75894a8f2e914517a542372fc69051f29f7273953ed1a7b7478e2c9dc79bc6be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page