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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.6.1-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.6.1-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.6.1-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.6.1-cp312-cp312-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.6.1-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.6.1-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.6.1-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.6.1-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.6.1-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.6.1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 01ecb1e10eb106fa6e3c471cd4266dd780a8b79475d59f335f11b9150db979d7
MD5 fc0b920acbfde981633dbded74aada8e
BLAKE2b-256 da2550fa7f0d1300eefc5bf952f8a2993c948677f810928e25d42cf9edcb5742

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d3f8ec407658c279aadddb2d6750f87f881b94bf92d29072523bcdafa44ffc5f
MD5 88bec86e425a541c502459e7e29abb13
BLAKE2b-256 1dbc2c6197876ee5594f503a8759f7b5cd48eaebdf68ff97420a0bbc1b87c471

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 08eb88e18672a21aed40e4ed48c28667d210f6832df5ed7d8653519dd2c81dd0
MD5 6159adf4a7db9084b868e5c3ae4d9063
BLAKE2b-256 b4ab018c0fa1db2d78319f3e487d7b2e43d5bfe699e8ca81326e1f1541f371a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 567c1eb4ed2229b75a4f1f5fc86436b38b2da407439527d24d1d1d5383d32989
MD5 5f08ba0dec223d573940d258ebfc471d
BLAKE2b-256 3bacbae64f680837bc77689c865082a2f36732ac8ab62cb0b11eb9ffdb33b324

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 180a19a2d2e061adaefaf6c1af244cb5904ba5d808947f505f08a6fba3f251e3
MD5 6ee259735f2ca88f4186b585d8a7f977
BLAKE2b-256 648df14b14dcde39c2fa8dfff47e52d7b1d57a968102d2045ab48cf73b7bf3e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a90dfd89a6f2884f7bc2d2f49b6734d06980473f1f7234a3c065becfcaa05a9a
MD5 a0f8505b05d17290ede4a40817c6e103
BLAKE2b-256 324fb2eac43fb8fc3655935d1e45688fb5ff8e463f0b1a095da6236758d8f3fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fe41139b7baf9a609cf8656ecb549dc48c8a854f9e858897c2e5b71a9af42738
MD5 6f81d8033ede9919496d72655c6e9a09
BLAKE2b-256 035da83ec7040d2c1f01e3ffcb3291d8c4ee43744a8cb8d6e50e3db1af6bac66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 efa4d83cfc4df66f7c75d7b60564ae38eb6f9bba636830507eb335c67fe6e49b
MD5 a4e22796dc2ad274d900a1e2c52e935d
BLAKE2b-256 55e42f13d6109729cffc9455ede097808ac1659c29a74120776dcd52c0accec1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 385971cdde75fa6eea8c5b2ef8f7e6b88365fa47db2070b3205eebd0cb15e1da
MD5 a3f19f3d5e046d436a517dc38a588c12
BLAKE2b-256 0d3f23bdfeefaa7f955076b2342fb7f65bb4c2812481e0d99e6520dd3698fb66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3b62172d37b35ee1e07fcea89d0b67a04b6602d3363bf3d0b22bdedbf9e73e4e
MD5 7efa63e2680435db1f2c446d5e83cfeb
BLAKE2b-256 cb8119219c078b5b960eaed3d32cc4e8ba9e542148cc46fb469fb1d776b51b9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e423d1af2babe500bc9288f485535342a3127655ca87085d9bb87c10e0beb260
MD5 fada1216ced004d5bf54c70699f70e4d
BLAKE2b-256 b06c79f800d65d6a94f9010220ad5cbefe6b1d75076cc53c8e78b58ff790e6db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 31c52067a6ef594764ce5a67d36c3c25238721ff2c2c968fb9bc4f175258db2e
MD5 eacb6b6dd144c7186dc4aee59bd65071
BLAKE2b-256 4513d07e43b3b5341cd5f050a8dbce027164282f3aa0f46fe81ff36230e94b78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e9bd05f20c697b19990cc4ac59cf758ccc02e80652eccb5d2de4ef5de423dfed
MD5 88c27324204e34fda0c1f2130b1262d7
BLAKE2b-256 5df7f8ae00aba533acc75430cdac97335483af118626e527439f92a3ff0092e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 223bbfe8e2a39f0a590ed5c612e454f7f44657e3b4225a4fd91cbb3b7f6ad776
MD5 019477b6a33048640af91fba58846d1f
BLAKE2b-256 3c6c94090ed505ab6a275f91fa63123be27be763523a44a6e020d89ee0c29071

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.1-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 fa7a01afd2213e49fea347697889c85fd45f108803e7964bcfebab077f40d6cd
MD5 452026ab32d9cca33f0e2bdbaec58984
BLAKE2b-256 71de88d6b53b8f8746c130e306369bdf20d4f30e2a038f9cff2943d1a1f8da77

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