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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.16-cp314-cp314-macosx_15_0_universal2.whl (520.2 kB view details)

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

tensordict_nightly-2026.4.16-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.16-cp313-cp313-macosx_15_0_universal2.whl (520.0 kB view details)

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

tensordict_nightly-2026.4.16-cp312-cp312-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.16-cp312-cp312-macosx_15_0_universal2.whl (519.9 kB view details)

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

tensordict_nightly-2026.4.16-cp311-cp311-win_amd64.whl (589.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.16-cp311-cp311-macosx_15_0_universal2.whl (519.1 kB view details)

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

tensordict_nightly-2026.4.16-cp310-cp310-win_amd64.whl (587.3 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.16-cp310-cp310-macosx_15_0_universal2.whl (517.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 12ab182356ff22f5328f052fec2c4cff4d279a9910aeed344ec4ce4131a14559
MD5 09d43686217f2294d418a7edc28d7b36
BLAKE2b-256 24804578ff8fc26b447791fa3705fcc9673ee41f8c00cfd5f41a333a1908a3cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2c0dd0f2e68290e9bbbfb1afe94e71616ad9c809f776473864f94613c6705b8a
MD5 16c3d179912eda02ad8036feb3cb2c86
BLAKE2b-256 7fc0fbe429d403ed9003cdfb80a950435a2b2f195376eacfcc0ca024c72a9046

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e2cdd200022cc0963faf8fbdd421cfb5251625244e01cf8918e828c6a869e27a
MD5 6bcf73a0e35eb7d6e95a331d495ff1ce
BLAKE2b-256 c0f26937c2c52fc61666636a50fcd0d7eebad4f412dcdbc0c414a58aca6d80f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3bbfc8f43953f83b2fcb5c69dc583b376224dba3ae7a1f242a468f34e9e0fe82
MD5 55713573f3816692c767fe6e6b6de9a0
BLAKE2b-256 ad1e0fc2db9528f276d1a0a187f3edad7d2d9cd8a8491eeccc7f4b015edac82e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 942abb1f75f371bdfbfb0f93683c287ea9ccc7d3e622a4bcf6b7c40a77e77db8
MD5 20a73d886fa0f01715681cc3a8bb5110
BLAKE2b-256 42dfb7eb73f01562c4ffa79fbfc05a9055345b4a4810e1873fc6752b13daa078

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8898d2335d36ce3057a2fc4db47133893a24e10c69e3d07bbfaa1a988eb1b908
MD5 7b7d659e3e8562dc4e2400603b693934
BLAKE2b-256 13c0fc27fc49db17a0763a399261a94b7fa15c28622a369aaae7d6db13727b9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a081860a1a3a47e8c85fe7775131f77b4cf01ab95baf52a140b1c669c2702a69
MD5 bb20f9f400207600f17d959926e5a322
BLAKE2b-256 4eac9d967bd6bfe37b0e0897c82016de1b2a0072436ff311c032f06515fef2de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dbf12161306bab35d02ec9c4c91d47b0cdeaddf1e198b8210a553a38eb29304b
MD5 1694fc348b8224e3c5c85d86c1c0a34c
BLAKE2b-256 4d2c9cc0731012f9c4be444cc6f243d97b37626312c7df984ab37eb0ad5aed3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 acbd429d6b3ff947c1e0e7a252009e09a4483c7194d425ea65b0b2637828b44f
MD5 b3e6529857055dd09d5b2fb2fdb0f8f4
BLAKE2b-256 56711fff1b4bc18bf5120f97314c5a79cc0a63d9e58e719a4a3775b1a688af19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 de23ecf06bc0e40baeb5ff1f0072491d4b65060a392b470786f103fa6266a9e7
MD5 c72dea916da2c397cd006032d9669554
BLAKE2b-256 b9a9c26354c9745b8751f00b89efc8199363a50672627783d0a3ab2811a010f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 547c052e87c7e0a5d49790736d45975a93aeb24839baf6c03700d651111039ab
MD5 826cb617f79c910de92b03646d58d034
BLAKE2b-256 36f8297416703011f7a4afd4c2ce3b455941429d5b849f73eedb5442d72e4b71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4d96dc61d73a96316ac4011547ec227716879f7d066a0854669f507f08fa5c70
MD5 8b61363e967428308067820d6ec8669a
BLAKE2b-256 5afe9364a3380f3f3a1c9ce1b5987154c1cc0814f173d0e448300415ddf4c950

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5e0211400b8ad4eadfe19dacb4971259ab513b2d8cadd1629ef988496fa084da
MD5 d44566d25bf5c72d1c67b992a250eb54
BLAKE2b-256 c3eaaa7ccb6ce164b7a55c46b8c3728bfd6d4daaf7f86417b0ed1d4ea85a76c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b8cd67ceb595924be2a4e504638c4aedd7b51877e39a26a5bf26ff5817e77c04
MD5 25a4e6c180df2ed2b691e38295866ad0
BLAKE2b-256 47a2eb0d869921b083b8580cf81ca9c858829986466e413d57552ec04a9972fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.16-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f52b0541876304e851d8db506c7c325502d64c9c5f65573f8a14a4956248c99d
MD5 cb71b0b6aedda13b775849e960ca725d
BLAKE2b-256 56b622c4ee84f8075368b17a3878e9437337a1b90f66a8f3ac4f3ad502964731

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