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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.23-cp313-cp313-win_amd64.whl (609.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.23-cp313-cp313-macosx_15_0_universal2.whl (538.4 kB view details)

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

tensordict_nightly-2026.5.23-cp312-cp312-win_amd64.whl (609.1 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.23-cp312-cp312-macosx_15_0_universal2.whl (538.3 kB view details)

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

tensordict_nightly-2026.5.23-cp311-cp311-win_amd64.whl (607.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.23-cp311-cp311-macosx_15_0_universal2.whl (537.5 kB view details)

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

tensordict_nightly-2026.5.23-cp310-cp310-win_amd64.whl (605.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.23-cp310-cp310-macosx_15_0_universal2.whl (536.2 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 4e2aac73701a47b2dbcd6bdf63d4fb780afdc2ceb4e6ba12a4b710c2a432cd0c
MD5 7518d4fab43a8e13077c824435f836ea
BLAKE2b-256 bdc0e806dd88d801847d821b1c2fb537da5cf9213295f6e274ab8acbd89306ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 46677ccbe61b67e4c6d8393dcae009750da6fffb656d2a10dcae27db63684943
MD5 47ee2fcad4474d0107c19b293239a193
BLAKE2b-256 1a52f79533e87946a4889a696a0966a6157a2e3277e9d5a872ef796313e63b2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6de7520dbee1791a78cca174ac8c0cd2b3a4b76f913c757464f6ed2ad1d92100
MD5 05a96daef848fd6be48c1df7f8250286
BLAKE2b-256 211b0365e4b103527b2ae7687528f4626ac614cca81ad2505875d8a458135a33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d2296d77b5e457450938286059164788624b3f2efc89387304e28eec0da3216d
MD5 8acbdc05142331c8744e685e07789eed
BLAKE2b-256 c8e7ad5c27abd0094db5d385df39ac8bc3ab789639d09fe148278446173fb4dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3719978f9a47e70f48edefa6c8c9808c8eed7b69d3e835c3c23bd920f3a73156
MD5 8aad433fad75996675ea512395bd42b1
BLAKE2b-256 e70a30e5aa848af87e555315b6f594f1f45c68fb0ce5b677f1a9dca45f72af80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 22959587b71b8e5e74a8d04c92b065d4b873a59d3f16f248c2c30b83b2a0e2eb
MD5 2bd8d5234b7aca7050f09720ebef8dfb
BLAKE2b-256 1f1d80dbf71976959c88c08e6229e64908afbb14dca9622b88d698ac96dc03a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f57e0074fd85b5a78ca51bda948b12f47a15828b265504fd4470b2fbdc0884ba
MD5 6898a3f7b8db3faaa50981dddf3f562e
BLAKE2b-256 035e25cace4e376086108847ce883ba86e798a989c90f57c25f6c5e4746a88bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 7fdfe221ffa930661114ed78e6df3a78f4380cb7b204ea2c86a2a09b1e183d4c
MD5 5525ea0b8cc544d9f0b2f9654486b6a6
BLAKE2b-256 dc44684e9581b5bb641fe8a7318bc942526457e8448f120b9cca49bc6cd3184c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 59ded803b635383a576be0f943a42c80f00248d9c10282ee4cf6d32508b256e9
MD5 7ac0bfc30d803bceecd617dd6f4363ac
BLAKE2b-256 bc0389f44d7bbf08d7e353923259ebbf8848ad8b887f7e75ff931479b76bdc5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8df92dd388f49c90b6a4f696ee9e27826ca2faabc40d9d21182c7445332c2d71
MD5 2657316553d4e312aa6c50c8fe9480ec
BLAKE2b-256 eff87d9bbf55d21f8530feed40c22d7b106f506e0d5a845ecf6d8b265aeb71f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 35980bf32ec7599d9bf425c831189d16c803053818b7264cdd6028932b946f95
MD5 f23769ec69120630a834aff24754b4d1
BLAKE2b-256 be18e9f0c4f569b9b366cd101311e5c7e3234fb899cb51cc41f1eb28de9262f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a4325bcbebd56c1831e7c37735d1188f570f5ee9c7d4dcfe2fa861584c4cb2c7
MD5 b1d2dc7417b0482cf3f4658267f49318
BLAKE2b-256 d1bfb6d23073a37d14e3dc290731ca10c5bfb5b4efb51f5df206f16626197385

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fe796b007a5a4c0e99bc49fa8a019fadc05d8b1b589b6e0623182d12655d95eb
MD5 82781fc8d0dfe97f021418b35ab09b8d
BLAKE2b-256 fada99fe010ae920a1ac86de1b43c0739ff0ae3478fdb01e23cb84c4a6cdf48a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.23-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e1fc09f2269f301e01b63f327872ef30dc35a22245ca1c3b61409d8b5381878b
MD5 f5b15972d2bed35895d44cfd43f8c508
BLAKE2b-256 2a798824ca0c0b2d59118d52f718cd8931a0198a7c2e394d4d0aa4ee2a7f891f

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