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.4.23-cp314-cp314-win_amd64.whl (595.8 kB view details)

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.23-cp314-cp314-macosx_15_0_universal2.whl (523.8 kB view details)

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

tensordict_nightly-2026.4.23-cp313-cp313-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.23-cp313-cp313-macosx_15_0_universal2.whl (523.7 kB view details)

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

tensordict_nightly-2026.4.23-cp312-cp312-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.23-cp312-cp312-macosx_15_0_universal2.whl (523.6 kB view details)

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

tensordict_nightly-2026.4.23-cp311-cp311-win_amd64.whl (592.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.23-cp311-cp311-macosx_15_0_universal2.whl (522.8 kB view details)

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

tensordict_nightly-2026.4.23-cp310-cp310-win_amd64.whl (591.0 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.23-cp310-cp310-macosx_15_0_universal2.whl (521.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 92c41a85e2c6733f99a1cc9f444fb65ecfbcb4d7228a6214acb15cd8e73e8655
MD5 fa380496dfaa359765d9136d149b9ae0
BLAKE2b-256 93f17a40f0bb590f26f844d85e0d86d5e3004f33fa3361d2d32bdd0264fb441a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d8352955e501eed3f88926e79a222cb98449fcc927dfe3cea0ce6e80280cb94f
MD5 b081a8e7d27794c4cc6ca46edc141b59
BLAKE2b-256 2d66e52fa206165bd76dcc891fd15d22efca2a9435193c982f8a883a3bfed072

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 59c6f81a2dd8fe8983a7215f02207fbd45e0ee0de6c6bedd63ace2d41fb99568
MD5 e1d08a078501dac0db7765d8388d0cee
BLAKE2b-256 1e3afacf567ab3162b020aa0b6c77b6876c210811419c301485f12d2aa7b0c96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ac731e30c4317b26fc9b226888663623cc473889bd5f832f55268c5eeb70e940
MD5 e959543fdd9ceb30c042f17d0f6db165
BLAKE2b-256 713ace3f2b61f3fa13a1ab5075bbf197c9c9f076d8b1c5e307be32e72eeb5c47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2accaef0ffa4f4bffe0ae883fb148aee09d46b4e6e65a6868ae513502df8bfc8
MD5 8e49e918a5911d224dc551588fec15db
BLAKE2b-256 7459caa8688db172a6aff2782469e28c8ee0ba494e31d1475e8641d69be02168

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b2ef23df8e7662c54ed5320fa1e8620e11e598817859937f4452f3e13923d0de
MD5 4770f5cd762b598dd15968862c4a3886
BLAKE2b-256 50fbfb6567db6f6ec615a4ba1d4e4fb666e72b1279fdaf8872a6ce5e3279f205

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6e8ff2c670a7c6657ef8776a033ae0af7907664e1d8d0bb62609989fb0d2fabc
MD5 0c96f33fff94b9588dbb5c099bb4ed18
BLAKE2b-256 e7f1c949806123bf36cdf86543a973767b87e65a1094712890cbb8d090f093b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3dbe51fb5b786cf8831aab4fdc1deb6b2be8b56b1355a3aa779334f8546f505e
MD5 f0a5b6f6fa9cf383c22733a886f2b1b1
BLAKE2b-256 1b1066da0937d54e9c083c7487e8670991364694fbc087d74298007bea49ed52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 fee1436e38e0a5740d848ba16c89b1285ff212851881bb022fd2c46154eaa578
MD5 778a5781f3da8a3d3736b8d07cd229b5
BLAKE2b-256 fa767a29e805aec39c20765e84dea43c281c25009d6600d87d820362ffc66f34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a9ececfdf6b144b025db903e97e5840a8bc3d1453762958225d680622f86dad7
MD5 930463d04ce996362196ba1c747fe7d6
BLAKE2b-256 3d55d84a610f9cb10a61649bd666695316c3b06eb1b2982dbbb771bd16830dfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cd53e1ef0e7e5ee9ffef5779ee733f112b3600907803c7ffa298f7ac172620b0
MD5 8804bc53b6c2252be28897da43641cf4
BLAKE2b-256 9c1c4baeb8a7ddb3c7e2c3fbe5811ee1cda5efaff171878d5601f2f72a96850a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 62c93d93bbcba6b0910e4d9f8225742eaffe4469a6c38f57d07f7f84aabe4e54
MD5 25fce24309739543f67fe93cd5e2ac9b
BLAKE2b-256 2d39db55824c735a75da6a47941054e8bc2affe5c61e5506bd6cc6539689528c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 df3eddb9255711f46a61554b73992832f32b849bde84334b5d27161ef9f02705
MD5 ebe58c3bae65bbc3639227bc4d2d2c67
BLAKE2b-256 b46db9401ca3b84b20ae9edc6cb101da80de47100dc834abc368c5ea2b7f4f7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c8861a74ddfb22a4bcd692757d55420724b183d17653f9f6f4e0de56581f44d0
MD5 085df7f015dd8de133d4b89f8fd5f1b4
BLAKE2b-256 fd28f5b8750e22a33740a1384898a9f056113afb6bd7498fecfe951166b69799

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.23-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c4841f2ab656b9702935e5594f2d3cd1b313a6af68518f1c2d84626ab7310c51
MD5 182f66dc40d2eed938fbd617e9ec1c09
BLAKE2b-256 08cf5c40e92bcca77fccdd8db46590854b65fa2ee680b38623f0f12767f90c11

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