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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.10-cp314-cp314-macosx_15_0_universal2.whl (530.1 kB view details)

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

tensordict_nightly-2026.5.10-cp313-cp313-win_amd64.whl (600.6 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.10-cp313-cp313-macosx_15_0_universal2.whl (530.0 kB view details)

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

tensordict_nightly-2026.5.10-cp312-cp312-win_amd64.whl (600.6 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.10-cp312-cp312-macosx_15_0_universal2.whl (529.9 kB view details)

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

tensordict_nightly-2026.5.10-cp311-cp311-win_amd64.whl (599.3 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.10-cp311-cp311-macosx_15_0_universal2.whl (529.1 kB view details)

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

tensordict_nightly-2026.5.10-cp310-cp310-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.10-cp310-cp310-macosx_15_0_universal2.whl (527.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 dcbe0b8abe73ef8d1909e2a805d00033309b5ce0589fc9269f9727865fdf44cf
MD5 82a4e3bb821857f08cbce6b267a8b43b
BLAKE2b-256 847c47db18370126cea55399ec70fad6151dc49f32c6c0a27c50dc6e87c3f43c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d164f3162e07b363653d947c9a33ce71e6346c727c2376c4c6aa5dd372952b0e
MD5 adce6e6dde09aaae89bb484995d7939b
BLAKE2b-256 d9a6f6008a49e1857fbcfde67f31e16cbc4d3faee7cb229ec0dee7dcac8b972f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 801eb5260f42e06304706538935fd3175458d7e06ad401e25f23a02c59718c30
MD5 5993a5b00dd51833efc38e15c7f1e46e
BLAKE2b-256 d770501f44526a9ce1c472f04f046f49a644198eeab2055e00214c89ea75150a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3a448e99f54a2cdda99139708d84e743265509b5cbb50426839c0be36ad2d6fa
MD5 4827a5151ff4a9e6210467059cce2827
BLAKE2b-256 9315d522c5fa5e6965c05451ab30fc9794a806fff8ce561e7e2c702d418ae053

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 926f56c0613216fd7467aa091b8c23ac3e4233954d41137f4f5d34dee653b80c
MD5 f6c1040597080cee38db4a77c95501a5
BLAKE2b-256 75356c1a58bf2f72b515ccb1479739fdc48e397af2467a3039b3c38f4411fc6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e66ac1d9c67fd036ee19c3fe8d6ff978febe61f20d841a1a39d41a2901b2d44a
MD5 670f50025ecee2076f53d8e6f908679b
BLAKE2b-256 09d1f3eb6528906c03d3656378b7d9645a9eedb6d3e7a2ad22d10a38f30002d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9b0f6478ae9123ea543ebf34495ad7b5399b7b94ccb7d2a3f81e9da1822a2e10
MD5 f2c9f005ff7ff01201defb30dbb8a188
BLAKE2b-256 de8db1f318fed83db339a3c5d6f8826acb182a58616dece26533e48a314740d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7c955f82720bdf3c3c5d543ea3e5c62c505883f09381b6fd46a0cef25033efab
MD5 7aa8b5f722fad8bb67eb7356cb373fd4
BLAKE2b-256 2d181f52c64d5a13b903bf6a5d8ddfcff84ad22274f845feb7436efa5bef29d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 be1257f4e43c2880bef3b624e221803bf3cbbf74368afd4a0b09661021c5640d
MD5 c8843172dc49228d959072883e807587
BLAKE2b-256 897da73341bb76fdc416d0bc7b4d06cdb906892025df0022e560ed58af170c78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c98e46a2aae59d0f4e48932c1563fe971b665c476b055feaad4580654e4996d5
MD5 cf48f76c9d849d7773df3a91ee193c2a
BLAKE2b-256 ea3f5cd6af143774f474387bb1861c482194f7a9327c542e9098e650c2ff1ff0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 15cade867362a547a444a91aad5a6fc832df055a2f66c847a68f046823a883d6
MD5 9f387da4651d69e56f11a3f353e28f15
BLAKE2b-256 ff7de2b1aac8fc6bac0cb05d0503922989c32b62d2ae3a1b2ec9892a7d836f7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e5ee246ec40c31ff3635d5997be4316844c2654cbea265fde707553e37c405af
MD5 c66fa7bc2106b253c63125f1cdcc28fd
BLAKE2b-256 60852b929b2976228d6f05eb84a81fca1f0e38ea8ef83a589a212f5756d85d8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e6014bb20f9148676810ec448135f0bfb7202cfaf0ff17b6eb9d6f74fc6a83d5
MD5 afb3f5a4dd98aef3fde9fba1884b9c3f
BLAKE2b-256 88be77a8373618fa30fe32cb80039a53c396c92d386a891b05c9ca567d3c5985

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d9b34429e3148f8fd63309d0c6af65005b3706d4b8f14acf8bec6b9eba25ea7a
MD5 5b0b03f3f73ac04839fb6b850506a253
BLAKE2b-256 d813c0650f66ffa428df1440c239993a402b8e5e9a5f56818e09fa0e0a272d28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.10-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 667a9acebd579952ddd366f264fd26a4e6dc676b7ad7112dc03d90dc43c55881
MD5 e4e449239cd4b86d168096785cff2ba2
BLAKE2b-256 088ef12dcadc72d2b42d03f07271b19f62b7ccd738107dea106a88fcd0221324

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