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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.27-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.27-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0fc9c1eba60552ff3c846881d774be511769bb5cb1e422a320ada25c18f76fc0
MD5 0a8f71e5ee7f99105fb470034efc55b3
BLAKE2b-256 7e902f81c2d81c1915d4c11d17c471ad20622a9d1888030fc76a37ad0094b12c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ac5a64682317f43162db9c4fbab19a38bdd40612a90c691a9b3f09aa7b93f588
MD5 ca23f0ae1caf850b8aa11734d707ab04
BLAKE2b-256 73cbe73883cb7d4ee0dfcb8ccf020db5b4d29df0e48a90a415fd4233012501a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3bba6ff3f8ba0de047070a45fd447f79bcaa700b1f2d525127d2e203b4c6b314
MD5 7415ea84bcdbffe7f40eb9acfccfd4c2
BLAKE2b-256 45d983c5414cbf22e034b7c8f5adf4441b643aff48180c93e186c75a7fcdc0d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 383b437d99a6ee58fa57d97de5ddf12a5125ea0cfa5fbe567cf52b3bb48d5eca
MD5 12803cef482033070630098c06524aa2
BLAKE2b-256 de71fa45566b5f3d0ac1722974c4c88d82445c629d4cda1e3f00b7621acc0fe0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6a81ed3e6d0747bf380209c116211f0d67c4aaeed15962806b21a6f65fabd416
MD5 79d392f7463143fee6b3479ff09996fc
BLAKE2b-256 d997768a4db67e9089714fa93eb7d05ff64b53a91d74cb6197b3f48e8da38124

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 24c3115502bd129763d864aafa70577fce70651c5abef9ffe8145961c4f10a1f
MD5 b8eda030cdd4398a35c9af1b8b4c37da
BLAKE2b-256 a3e81b1dbc601fb1dea98e60c24ed4157d5b50967215d5aceea8edab0f3d711e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d97b7ff74b7bea4ab71a5180ee973995f9362582052b74ea7a4cdceee55ff11a
MD5 e463a47b417cfb85d408050018420016
BLAKE2b-256 e7d3d2cb6e1ed305bc0999bfec0143ffe19a2b90704fe26d3eee8a5a767f9469

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ed808a38b142b48978c99941394d94ae8582967c0a7ee1416fd131a069a9cff1
MD5 34de613a68e99bc5b8f7a9499daef97e
BLAKE2b-256 2efd8b624f36eb214a6c0130abbe47ad52296fd79a0f16a310429b87f6cb8b13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6ef9ba6204f1968a0d38a83610e81bbed08014d69937abaf0064f3e186056683
MD5 f05b75b0a4f99c97a30e16ef9677af23
BLAKE2b-256 31e97169a116847ef92ef628b4e00fca23f27498db4f0c85b836458519ac82d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 cde45ee839612056dca2b8d5630fec24eaa6fae3608356e7c80ea8790595eb79
MD5 fdb0eb109580191a9b6abf7eee9e400e
BLAKE2b-256 9939404bc8500a605dfc65f489e8030bddee1b29a82fcbded41647ed5734d5f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6665afb491ac4369a54d4efa0588cc681a184e3694fdb1eca58925d4df17a35c
MD5 f96a9a93ad15d589d279167b5bf7019b
BLAKE2b-256 be972d749e6aa06783c3969494836d6362d8d716be80f0b9cb2e2ee6783d6f56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 da4520028394cda0971a4428bfaf839f7d80b59c644fbf10e76a8eec39adade5
MD5 b4361b08d35c6c1c74cae2985170aa94
BLAKE2b-256 e1cc7262fc8cbc5a64eeb1a7fb91e1d88bb0ff84163452f3f45c0fcff6cbe21c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 885dcc67784752f43711e22d5e5b0a8076bec6bf5622aeaf703ce6e7aa6bf112
MD5 16f717a105830623bbbbea4d6da89028
BLAKE2b-256 076fe85af7e04d2f23ebc30f4821ac4ac15a89dc95fa766e4e41fe53b042f22e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3592342179cc2bcdd473a82b549d0c85ede667b3662a6ce72bda53390cf491f6
MD5 bf12ba9b8b0598893ab0e9a39e3da1f9
BLAKE2b-256 b8fedee2f659b7f98414294fdf29661e900d243af958bd103c5f21c164ddc623

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.27-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 85f34579cffda1f7e7422a5be674df8b64bdc9bec2e5bd3700ba43a997ad8368
MD5 c0f2070811afe1be2586533f0cbf194d
BLAKE2b-256 5bf3faa80420423b5c0d624b7633078073d362907e325e2d79105d521474099f

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