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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.30-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.30-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ce530fae9ebec17e1eeee8c166d9a30e53aeebea4b121945bd78c5de3e7fe0d8
MD5 3dbd3545ce816c3e1ed25d7933e51f79
BLAKE2b-256 95c2f4efd6d678574543e11e2b526805c9819e81fb190fe8afadb134c0cd8ba3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 60cacaf500faa01e44f17b441e029ad8d33091e78060cbdf8fc48c046482c7ed
MD5 660f2dce89a115e4f76db5f32ff124fb
BLAKE2b-256 63d29b160b903485cb6d9eed8dffc37794c93b0bbc997b32ba0fc40ea3b5e88b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ede2ba86c2e9868e01b38d5aa62b023e2d3eec9b01f55e787081f800430977c8
MD5 7fa6e8ad463bfa3bfd015b3ef5f0f260
BLAKE2b-256 0858a7ac862ef995a29324577b4d9e96fef5ef99c23f4e19c5d35110b770117c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 74edbf4247445ebc841084c706d7dbd249d4ecac68756948657765d5be247f31
MD5 781748da35179106fe1d995f01c51d42
BLAKE2b-256 cdaba4e850cc9a64b44adfcd6362b415cb55d6a26f0b787f03563c0b5ea2cb30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5ad7c61e3f2c4cf07be109551c0ee2114d15f2334db2c06ead60205be05ab411
MD5 2c832d5dcd9384a4297aa0a7368200b5
BLAKE2b-256 1a25edd688bea8e9a4ac49adb2f2ffa7bc88588ea3486f4d0bab3dffbb956f65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 7fea35ec302fdebe8338ba3eb8b27b29abd3c32cecc14a98ea2c131bec073eb1
MD5 b92af4d20de083115490639f6a8d7471
BLAKE2b-256 9a9d0d43b90b457fc01346cc3c25251dbb814d72a0484431ec11983a0582765b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8ace22e68ad2aba5a834f736fb84df6560fcdf21b4eb8436c61bbc4f8fec010c
MD5 9e2496cad4d0d57bfc70846e9b80a55c
BLAKE2b-256 ea9bdb3bbf15daf476a5aadef4e3486177fcfa14255923feb0a4cdef1a1f19ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 195509a1b8920c29e151c96e29b27a426898a264ac70510ef5f17a3d91450ac1
MD5 066f8a956e5f5f6ff62d4b9c451c8454
BLAKE2b-256 201f7e285c9ce44feffc254cc7df41644bf3fefb15a359c0260171d943ac5f9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ca47b474f0572956f7e88e3a95ce716a860f474271df620b44eed8a623ce7914
MD5 e4f3e20da5359d349b80f9c443a38d8d
BLAKE2b-256 2d82b894d3a65a3e8707100ff07dc39d3d4e80db0e7b2f7c6720d799f6eb738e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 13497a8f8abf72f4beb996cf9c1d0d3c508a28780b189c4e692d86717bb1b1b6
MD5 f8d6dc6adb6a27e635eae206c0fdc8bc
BLAKE2b-256 3299eb05fcbae92c28515a5fea31bd208d276fd61b7988eed96767254ae678da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d05196857ee145d8a84c5a1e82f5555b0d9cd64f7f5c1fddd652cb44314a766a
MD5 22fd4960a1aaef671078239952a92c06
BLAKE2b-256 42bc579c500b7d5a0ea95f899033d9f3e4fab817674e712d4961ea9a9403038e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 325e00d4299d460beedfcd23bedd92cf014f8bda4580c17d8bf59ba40e2c7f65
MD5 d1efbab1b8f4ec4a46e168f366173967
BLAKE2b-256 dde849968862a80f4dd95bacfc38627eedb699c94a9150a0479549deabc902e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ec97dee6f2710b54725c65017a5377429e65b164e24c516c798421eb508afcd9
MD5 16a958150562422da8513e3bb50e20b3
BLAKE2b-256 f9ceca1e995e7a8c1d5351aee76ed089c5666d64bd577a7b9e82ac128740ddb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e1cde665ecdf37d63da5a6ab0d8cc7964ced26df8a56f5f22b4e5452eb4ce5ac
MD5 afc37023c33fb7ad372e3c8f07391a4e
BLAKE2b-256 f92e96ab6c04c12190389c83dd190a1302ee2779fe2a5fd722ea3b30bbd90071

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.30-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a235fae856e57ee25045fa92289c2b62d95152a8c8198c00c7232bf0602f0a42
MD5 10a367ea14aa7b71c35610ddf09f670a
BLAKE2b-256 b4fc819369bcd5d2162d87436f9ed4f5fcbb94b855e8b1d2aca1176c99a997e0

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