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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.3-cp314-cp314-macosx_15_0_universal2.whl (526.9 kB view details)

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

tensordict_nightly-2026.5.3-cp313-cp313-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.3-cp313-cp313-macosx_15_0_universal2.whl (526.8 kB view details)

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

tensordict_nightly-2026.5.3-cp312-cp312-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.3-cp312-cp312-macosx_15_0_universal2.whl (526.7 kB view details)

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

tensordict_nightly-2026.5.3-cp311-cp311-win_amd64.whl (596.1 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.3-cp311-cp311-macosx_15_0_universal2.whl (525.9 kB view details)

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

tensordict_nightly-2026.5.3-cp310-cp310-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.3-cp310-cp310-macosx_15_0_universal2.whl (524.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 698a2031365394ac04bce4a02c410c525e660c600d54059247f3404946c99500
MD5 efa77d09f46a00bba372eb0f2fee491e
BLAKE2b-256 77b7b3bbe4f75c93d7d5953f681c910447e2f4adf5ffb41800549018852647b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 89abe36c2be368631616100eb8c6d3b97dffdc3c8a86dcdc837c85b6d82103dc
MD5 40fedabcadf2be7309876556dc4e61b5
BLAKE2b-256 665c767eaf258be3323c833e7654678566115512812aae5775044c167b4b722e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 92577cb0700028fe4447f23f5cf8b17392e8fa044b52d938f9f619bd062db49e
MD5 8bf105e904caf08b804db0f2059a019d
BLAKE2b-256 9b8870e98a1119c207ec97f779224956a62f85707ee94de23bae5590f62c9629

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 63a63ed5c5a64e6d8de901a729f1aafa96d84ca08460bd362cc448f3852a4a5c
MD5 55dc513947611523df92acce7f1c478b
BLAKE2b-256 6c4fa2844f7456a251d0ce035ecfd35f8a3280b084d104c04a300de01c58856f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 61d4db777faebba64f64b5d05277d3df3553ccd5126e913ef13a70a45a2745f4
MD5 257c17bd64e1e9b4dc0bacd1d386d257
BLAKE2b-256 882e984f3d34121a834fd5b55199df03b9ded60e09222062f9e67bceb8af6790

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8a3f60db1d2ce442c841663caf8e7202c5b21bc5a0d673b6fdd8a0a97382282b
MD5 43fed55b75107e9236ab0108170737db
BLAKE2b-256 8e0aa1cbbcc8bd8bbafd30c3e084e6d81be2a5800f6dd4c0439c46e944eac165

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 486c73ef347c519f12af9d7ce0a1bce1e19558e65c30945738363c663d14201e
MD5 34539c335e0028beefbc66f155300a7a
BLAKE2b-256 35e752347e3c5a17164ae9c3bd722fa36b0eeeb2d6e4913b9351e4904c85e1f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d16a6fa4af89ca03ecb3b3af7c2b32b2ae597a1f560fa80db4ef062a1eb31c0a
MD5 d992778b2ef24ff1e1335a1c298587b7
BLAKE2b-256 91ee9e2275d9cf8af2dde4150e54e7093b765c87c616bb690acc6e7f86987662

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 09b32bb4f7d80222015a92f3c7cc3e48ff566f3b473f8a2efc57da30d6ce5354
MD5 e9ae1189fd23e19cd137960bbdca7ac7
BLAKE2b-256 7a3a1c5b7b5c05984460942af5dd9c748e04d3f401f7cfd9949b5f45e3ffe87f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 24fe9aeb71d9d73f1267517e84ec58c8962877aee09e6065652327aab4253764
MD5 a62d7df691b8b354a783ea9cda4d7b8f
BLAKE2b-256 ec2e14e78cd49026c315a81e419406da118af52f2e9a1868e6d9474605b58803

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 256c6b05abea9ab5ccd2d0c92ff55a71832954a40f2854a6da6e40bffe87eb36
MD5 ed83e29ffc5522e45e7f0e3c3ee553cb
BLAKE2b-256 3d6e0379e47c675d1531478d542e116c94fece48593ed4e4aaf97318318798e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 afae3ba6244bf92badb1406163c633cbdf6f40c853957daa12e34abd4ee24588
MD5 5ce20b8963ee4cd9d55c6793935750c7
BLAKE2b-256 8d63689059980831e577513fb29d15b6bcc8fccf23eacc1423fe65ad40b7bbe0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 208dca3415eb7ae35cc9b354712b91000297a34ac145f392063d509889d1e900
MD5 591797bcac8f47a79b92548f71870ad9
BLAKE2b-256 b8b066bd5808cebce9d1dfda875a03a34a8f0c6ce8c961124cefb4701009656e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3b674f0cbe9031f47ea9d37bf2f0289040a2b34ead3becee9c1600dd0b94dc9e
MD5 512e424b7817febbbc5eaf0d36c11150
BLAKE2b-256 1984853f63d182ff1b87b82d652952718040f173c8f66525f2639e37eeecaf5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.3-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3f42d5c48b733cf63a706e3af5205f2b8c9b12a14cab5341521ab0636b20cab9
MD5 e7cc0bbc3fe0ae0550c39e86b8e0d242
BLAKE2b-256 13e008cb13aeb753acb7d6593f24b93aedfb36e3989be6a54f2dd9700d9679bb

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