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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.3.31-cp314-cp314-macosx_15_0_universal2.whl (519.9 kB view details)

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

tensordict_nightly-2026.3.31-cp313-cp313-win_amd64.whl (590.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.3.31-cp313-cp313-macosx_15_0_universal2.whl (519.8 kB view details)

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

tensordict_nightly-2026.3.31-cp312-cp312-win_amd64.whl (590.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.3.31-cp312-cp312-macosx_15_0_universal2.whl (519.8 kB view details)

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

tensordict_nightly-2026.3.31-cp311-cp311-win_amd64.whl (588.8 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.3.31-cp311-cp311-macosx_15_0_universal2.whl (518.8 kB view details)

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

tensordict_nightly-2026.3.31-cp310-cp310-win_amd64.whl (586.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.3.31-cp310-cp310-macosx_15_0_universal2.whl (517.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 cb5ab898b93f7de292d02b3fb30e34db09a5ccee5a46cf981a92bf416dc85dc7
MD5 21a2105134270038d265d54d91d435c5
BLAKE2b-256 c016444c0729455d0fe3af88149cd718108163ca45cc6b6b0c968be349d622c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0381bd7460abecd9d56af6e02ef042f3cc5be23a00e21ab040ecce25ba38a931
MD5 544f29acff9ac82a9a80556b3e593591
BLAKE2b-256 a2660cb36dd258b31d59cfe12cf406c0ec464a86519d174d4c736d3066e5a26e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d8775805a4b10f8148ade33b251fc9668d0466f577be177245bec2da45734786
MD5 2dd82e5fa739bad3b7cc0eae9e0bd6e0
BLAKE2b-256 6f87e3577db22e834d1ee31e32734eaa5b85e7ecb41607c6c45e3a308f1bd088

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 831d77862a0f92868ae0ca4712fbf071a06dacc975285034fb7c2d06e62f1e0d
MD5 7f0027e408b36f4560753721ec7a74aa
BLAKE2b-256 b8738930d95890fd43d3c93e57886d6d324bc9f06a8ad600ee76b8cb6fbc2cc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bcc8c731d51c57f953e4907213dc1a1135a6f0cbfca5430ff5ee4628c37028c5
MD5 826f8ab0da6170e0dd14700de0603da5
BLAKE2b-256 e5a99e6c8b2a5e557876f403e78396fd288df9db92eb5303c4e5826a8b34897f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 edebe0685926c78910425e48b6918ec42f23832497e65d1405d4f58103221165
MD5 5284117ce9146b28a712777114aeb912
BLAKE2b-256 0f8eaa78c0233e6ec2db97db38da157ea45107b91bd6a79a5c6f3da554039223

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4ec364a855f8e29a91cb71c8bf2443c23121e4f3388902c74f528048352c6a5f
MD5 3fed0a10462f2a4dd61f10764b12b238
BLAKE2b-256 1e433714adaa5fd73453167338dda75b1d03038e2c6bb47e28ce0d3f1ed34de4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7e222d551c5e032c60f758c06e42cf2cc1d235a98dfa6a0e2d9dfb39289b5036
MD5 2e0beab3b434d7bcf5f1a85cc2b6ecfd
BLAKE2b-256 49d5fcc72c0858171329fcdf74d87dd999941340f95c3a53675f3d0158e27340

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8781a3ec4b9210dc80360e352c60fe6462d3d126af2c150494af85b1aa3869f6
MD5 d430d6b83ee3e5daed3b8aab7beb4fd2
BLAKE2b-256 63ef83e1e5940ab86f49e1031c29fdfaa449a8826775c5477a7e1453889e82a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3d15900d8044c79b847268c9fef5dc37c34d20b82a304c83adc569ac689f7313
MD5 b77238f03a64581edadf6d0bcbb9e97a
BLAKE2b-256 effca4e62be65118294c9935a439ee5a6e105d2485e88b8c7a600d61c6d8fdcc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0b5b0d0ba7d20b02626739f4dda24bc844471692ce452df163b422f004df51e3
MD5 ffa761b612fad7e6faa347dce7afe360
BLAKE2b-256 e485430d937db370730ae62ca1fc5f84c8476e53ac8945c6d29f4107ca2e2dc0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1062290e41c57be169fbbfd759871b1171cd529898b8c32fd2f01dfbf0ff15dc
MD5 b0ab8da30f3a9c80fd1eafe6637bc230
BLAKE2b-256 fff241a78c5c274bbbd3dec8007064431f123ff1139d3ba352ec1d79b8cb190e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2059efc2a04140346b98dddac63a3449195754d4e4f898e73367a3fcb5b3f6e7
MD5 523ec2889dbd977fd9f1d3e12c405258
BLAKE2b-256 adc88d1d78c8763c48cdee78aff70d1253c8b1b9953177b8ced9cb3dcab4f773

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 caa07f4df703bbfda4433f5cff4c09669ce9713cf0a7991bb99058a38187f965
MD5 00a8568ae9866809a0df809ac4a8ece5
BLAKE2b-256 97731ebd382d652849bed1c7738ebfe234eda0e979e374cce64399431662d15b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.31-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ccffaa1a1a5c5f43ebf176f88c3bd486c2100adf668be98d36ccfa88b6596a0e
MD5 e99c875cdc667ea51c91cc2c5c4856fa
BLAKE2b-256 f0488710ecefc4302de504dc867c298d38f588dfc162107e32d1f487961abd6d

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