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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.3.29-cp314-cp314-macosx_15_0_universal2.whl (519.9 kB view details)

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

tensordict_nightly-2026.3.29-cp313-cp313-win_amd64.whl (590.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.3.29-cp313-cp313-macosx_15_0_universal2.whl (519.8 kB view details)

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

tensordict_nightly-2026.3.29-cp312-cp312-win_amd64.whl (590.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.3.29-cp312-cp312-macosx_15_0_universal2.whl (519.8 kB view details)

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

tensordict_nightly-2026.3.29-cp311-cp311-win_amd64.whl (588.8 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.3.29-cp311-cp311-macosx_15_0_universal2.whl (518.8 kB view details)

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

tensordict_nightly-2026.3.29-cp310-cp310-win_amd64.whl (586.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.3.29-cp310-cp310-macosx_15_0_universal2.whl (517.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 37da60f433c28b09244ab6f15ba66526ceb7ac8f6253091beab244bab65e465f
MD5 e70af192f8a15fa1c51a11d957879ea9
BLAKE2b-256 0da79a725fcb5e13891ab725faac96df7f086ab88f776823a64a9a3ea9a2ffc4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 46d92b4a2a08d66df73a4e23a9ce8c596d7117d8ed1dcceaa26ae725773437ab
MD5 652497ee64d6a3fbd833c3cd1ff2b206
BLAKE2b-256 0dd4b25907c8ef07cde7b48162bb5c4afc3e728440207c232834053a0a6dd428

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b5925e5ce304a0e36c35d5982cfe1397c8b6b5241a5568760ce8cf3baf7df211
MD5 2de985517b1383ec5ee6f3977e1f98a8
BLAKE2b-256 465e3f54af8b43f15e7221f432733257195593dd8a53e93f4ee011575b30e3a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b8f5ff56d9f4eebf6dea33ec544b63a79c443a42a813a61596e66480d54fd560
MD5 111713591774192b0ca1d32966f4dd19
BLAKE2b-256 5d634da847c2ade5e87c979ffecb7fa2213125276d31a95e9285a479a9523ddc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 34a6074441e04f784a403fa56f580fc1cfd1275a4d43a4d8ee960290b6452371
MD5 0f9b75dd5d2848884506a3b7c1e6b2e8
BLAKE2b-256 0bda5a8e9ca9452cefe6eceecf74c8052c5f61bd7b9cf674879fbb6f81a6b184

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8b79656022bd275ad72fadd0c445758ba30ee21ce7d5e3a5578054ec0b7d9b8f
MD5 b38b9676da9e13a1f101d5442fc7c181
BLAKE2b-256 fc48020dd86492cf458eaff96e67fd133a397dd8abae1644c655522f6c278df5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4912bbe144c749cbc3147e9dce78a7a39f4db0ce4ad1f325eabe720a1c3fa0ad
MD5 453e5f4312d0faf493d2989cefef74b7
BLAKE2b-256 0a94c65623abe49e979b84c640e9d3491b0b1acdc2ab3e036893b092bdece607

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a2383468bd63626bbdc277cfc5d6af8222dc7105a6d4b04b7db72962b4280be1
MD5 aaf200b0a03de3b982545bfb85f0ea61
BLAKE2b-256 e451f4fc36e99d4d997fcf3e3e70e0e29b1b6488655d201471ef590f7c461804

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 61bf4ce5214c31174355da47fcf435128e1d3b1f3b62601a2f8983922e19e25a
MD5 0f35516751a98f1206b042e5422a4dac
BLAKE2b-256 b4f8d41cc0bf02374f60f48e9b270e6607f98b0cd478157cc695b1a3f27bfee8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8d9e7c20219f8c0a64fabc7e05dd11e7fb1dccae9f35995febeba3db6037bed9
MD5 64258014cbcfe61d3d9da5d5b29914b9
BLAKE2b-256 f2080eb863adcd0e017df39a4b69f7b66e65fda95ec632474a31cb35131255a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 451c697d523a4e0adfa1aa78ec2a688bb3f0ffc3050d2e4ba215b9ffb3fb321e
MD5 c0bc71d9631b1054cf3c29d3feaab673
BLAKE2b-256 b908a733f211726eccb011573da6f5f9b4f5321308a3fd902566253012e95136

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 7ed013c61519f19ecad0432a97aeec9004f3c54823e7f32b0473b11b4675a8f1
MD5 eca8fa8a76183445b757b8ae42702ef0
BLAKE2b-256 4c3762a2d07db2156b403ebd2e56cb42933e0f1d83691723072c49521e6dbd2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e65fddc38dd34211b0ea0446e03d12ad30dead2158902c9c58d4b03a5a482db1
MD5 77d8c8637f8380ec8e6d2293f794a776
BLAKE2b-256 c2ab87c808644bd28e8be28382dbfdeb83be4b082698fcbad0ab70f74f8ee028

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 55508c6377c90f61d25d73833d79d1e85d7b574fdc21dcb29c096b2a418daeb7
MD5 b27cf748728bdab651ea4da4a1af21d5
BLAKE2b-256 315ed0510a33c99dd3081486637f35873e45e56d274a7f312f56aa015b3a11ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.29-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 15cd8814c36c64f2e043bb6206375fdf30acc7e7d3b03748bacbaaec72d0698e
MD5 81de79da1c7b8e388f9ec3f434817e2c
BLAKE2b-256 0be6b1092a88c0319f2a26e27299293742dda6934b9fcf4e3188843d63302d1c

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