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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.26-cp314-cp314-macosx_15_0_universal2.whl (523.8 kB view details)

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

tensordict_nightly-2026.4.26-cp313-cp313-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.26-cp313-cp313-macosx_15_0_universal2.whl (523.7 kB view details)

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

tensordict_nightly-2026.4.26-cp312-cp312-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.26-cp312-cp312-macosx_15_0_universal2.whl (523.6 kB view details)

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

tensordict_nightly-2026.4.26-cp311-cp311-win_amd64.whl (592.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.26-cp311-cp311-macosx_15_0_universal2.whl (522.8 kB view details)

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

tensordict_nightly-2026.4.26-cp310-cp310-win_amd64.whl (591.0 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.26-cp310-cp310-macosx_15_0_universal2.whl (521.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 8eb42c5dfa46f9e88cd9b3ffc9c28ef035c962b17d70567b4dab02219089f1ae
MD5 b799f3f1b3b8490f4cb081f263642390
BLAKE2b-256 dd9ea80e8c5ac1d4d0055f598e786bc2985073d1834512a3c6ef927108a33cad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cc8398f53179ba24073415b826ba3b8240b47486c204341cda5d6eb46ff08f8b
MD5 606cbfd3e0cb6f38b4948e3e74479cda
BLAKE2b-256 de41f4462445deaa797cfcf16314213dda75b152a2e395c549e48497610d3c1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 90178b526572e9d19803ad4e4bb2e1653a06b547bc05df54aae80ee91deff327
MD5 b26d0f9140b49dba79def064807b0dbd
BLAKE2b-256 ba61fb9f0bf24700bf0e6c8c1e9e2b136658620c3191579cae9b3e529afed888

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 00e3ee9a07e0aaf306bddbf3f0e6551a61a099280afcba50a709fc5013dabe09
MD5 461e18a01902bbc58122a46dcbd301d3
BLAKE2b-256 236bd6e9afbb0b11d9660f4396125acabc8fcbc180440f74804bba3769039c99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 17dc914a0d53eafb05aa1788af00e7c820e758f954f2bb89c3fd8752fe10cb91
MD5 d87ded3c6be4e0409dcf2c3ad4f4b30d
BLAKE2b-256 5d436042fe29c365875ddb2cd760f0c007426a4c662dfd18398945b85ddca6d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 51eaa2c3c11187f082ec3a70eb58e2a3ece7ef71d244569ca70332f5e980bfad
MD5 5a6830d66d0554f3f7d7a34e6fdb3f8c
BLAKE2b-256 17b8dfbe6f4fc561f27c1b9e478e23aafcc19955ad00b6c23615714416edcff1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 822a765c97ed96cf7959e3c22b293be97198f0e671ddeb1119895131e33a20cd
MD5 bd6538033738592ab4063214d8152878
BLAKE2b-256 845523b2de19655f990a853b0f8653c2efad27d3c2880556156fcced35f8ac62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 05486eab10b26d414e0d5ffde2cecbb9037730fc02441bff2ee3cdb74af5ec04
MD5 408f9efe42f31bc4bd80ffb76bcefd6f
BLAKE2b-256 896df2807c0125cd4692df9eef47bdde693781e95ca65e53717f44e27b960771

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8a56b49c0ee3fb9f547633a1e8718c04b3ae2164873d2a4d99156322e9d4b41b
MD5 2bdde66a5d9831286721998d483b2e50
BLAKE2b-256 eb5a54367a29acf8d781263cc05388af0d6c9c160291c0f5be9f3a2c985d5e7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6c2dabfe68c2e264da9837a064f4f1b9295febe4d13ad82f410943a6a164b1e2
MD5 70d1ef65d4341d9095c82eb4a769a4ca
BLAKE2b-256 58a3b884276dec54b0e92d7fd0325ecbe304d9c6679fe2ab63a99f9faee78bc4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6963e8a9bf9ab8e2fc13ca70825517d4eccd9ae6b0e64d48d003e95263531f2c
MD5 a326a494e49c3a24e8352038957a6f3f
BLAKE2b-256 072e4fec58600cff1131337adfa4cb4a708528f711892259a393caaef24e7c7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 973a8968f5fe3b6faa1991206e91ec21fbd304c3a85527530d815510ed9b5c55
MD5 683a177b010b0aeafb115f4c0a0754b2
BLAKE2b-256 c6f7f15be06dd3cc4a345ab5a633a78bacff73fe7b350e798330124fcceb480e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4bfcf23ae578c1e2dcadc871d5a07fcf28085f5d349c430f0b17dba54e0f4845
MD5 4fe7af572044bfebf500400c185ca912
BLAKE2b-256 c89b6acdee2765314d04e92d5bbbb9d6959a5a1218567180efb32cef4883540e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a928df04bc58c3d78a977e25bd38fb5fb9b01ccf98da67ef28a86495c948d10e
MD5 4dd2b0aae88abc68a5e128f17ef7c5d5
BLAKE2b-256 d431ccf8984ca18a5ec06181433a4feb414a131dc2dbb545eb626b82d10c5204

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.26-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 291c92962b142585bcdcf0ce0b24ed960e2473123dbb308c9d9fc8b6218cdd9a
MD5 170c7671d0db46f976329d787a82a715
BLAKE2b-256 68a82d650eb5308e780aa836188d67121c09904bbd66af7e77954a6cb948b5b5

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