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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.7-cp314-cp314-macosx_15_0_universal2.whl (520.1 kB view details)

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

tensordict_nightly-2026.4.7-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.7-cp313-cp313-macosx_15_0_universal2.whl (520.0 kB view details)

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

tensordict_nightly-2026.4.7-cp312-cp312-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.7-cp312-cp312-macosx_15_0_universal2.whl (519.9 kB view details)

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

tensordict_nightly-2026.4.7-cp311-cp311-win_amd64.whl (589.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.7-cp311-cp311-macosx_15_0_universal2.whl (519.1 kB view details)

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

tensordict_nightly-2026.4.7-cp310-cp310-win_amd64.whl (587.3 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.7-cp310-cp310-macosx_15_0_universal2.whl (517.7 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 64d52569d0c06b507d61acc5be8faa97ea2ad416c587e0c7a8bf59499217dd49
MD5 2686a125c98a8736c178e450592fa7d9
BLAKE2b-256 ea6a454648bd2a586bd9afef75d02bcd9efec87f1d3d209eb095cb7f4451fbfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2cbd375992719a9984bbda55e89931e9eeaa413a8160abf70eda2ddc9b3ffaba
MD5 8f616fec667a1bb55b08674cb942c73b
BLAKE2b-256 3ca8bf756809e23b2dbdcf10a0b3a9959b90be483b86720822b5f790f3954762

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9dec96b468de8f34905ba589d680b3b44e5878dbea4699d543a4f95cdaea5423
MD5 a4d82047e13313b3589983a088391a42
BLAKE2b-256 adba03ebcec86a3090a06227ef0870fab1c7e82e544ebcfd9ce0f7fce74277ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 57f1a7d16f8cafc3c71b99154dbf3fef00a6f8ad57bea6f8282de01cdfe1c3e0
MD5 d3a4fc0c0804930668d78d00f6a9423b
BLAKE2b-256 f1cdfce7a1f053a4ef92a8c41c60591dc577385c310f6879494080a4af441b8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 40b11f4418edb67d36d3c281e4776830d821e4c1ad6c1508a2cd65332a66e5dc
MD5 4935d793f3d8fecf195f7b765537debf
BLAKE2b-256 1194946086204a87d9c0c09e8d0e701bf96e17076422766a4109f0b1c571b781

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8e75f03d6b04d691038daa6c38b6a078ea736e19488e63ee90aaa31554a6898c
MD5 c2179be0f43368cb772e14c5aa66c5d6
BLAKE2b-256 6db961a997bb0da6d41e969806b821e5e2d1a5942541f672700d1140f9efd70d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4f15f55b4b3514ecbb37e41d49c2c690889e985040536fb4ae813f48965f29b4
MD5 0f1572f52d2fd16c6aaee5c068ce32c7
BLAKE2b-256 fddf7275a711fd40f030fa07d12c1e5f29efac320cbda303503390148056a0ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 eaea0a4bb9e865e486b45fe44c78fd428aed77f35c7eb6b38f3a7828f59845d0
MD5 85069d3698c6ed5c5791292e7a8554da
BLAKE2b-256 2567ea95ddc3b2e0449318249624c838fe4e593b6f5ce40f2252ae2ec3ba5b23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 484d9b223d8eea9abf7c656f9641cb8f5cb87c02ee8ecbc949368fa7720e0dc3
MD5 3b6f64dc19f07859309a921c3ec279cb
BLAKE2b-256 7110e39d7dff097b3df04975e4076111293423f6a32abd5225a19afdb8fe904e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 81cf9b72178a480c10de20d6a6bf02635552f6721831836944a361e94aecae89
MD5 6586abd7915fd73c6191ab4a565741c4
BLAKE2b-256 280202ab4d5cd54b7801eded0b153ee62544dedc21d3af94d7a4cc724a53b49e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3a42c0180ca5176ce95a73ee2183e922e2c8a537362bfa7aef51e7db27762d49
MD5 ba41596d26237fc00a859657c62c9894
BLAKE2b-256 e8ff67ebe1a538e067298be2de8067333b90a811e114a542416b3207a0c04693

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 76368e61f881b380c05b0121a69f190064fe6b90e583a2606d254d756c18b9a0
MD5 90c5b0cbe8eee800cc4257bb66968e07
BLAKE2b-256 9232805e1e3fbcf9735d90ef61be3b477c11de87f0047bd50753adcd68a88bdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4d92757d3b27a3d56be853867bed0bd2bbc90faa8e2d0e148350316a012586d4
MD5 e52b99e524361621422539550992ac1d
BLAKE2b-256 2f264d66d5cc30254862a048de3e73239b9ab8407e6d249a868a908b91512e86

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6863d14b259b922381413876f74529462e50d908df2cd75c7d91763f87f11666
MD5 1221444fdff2afe0cf48b3dd99b43897
BLAKE2b-256 00e60c50ebafda9aaea4aeeb923da0e64a45c7a21ed536583b4096bc451ddaee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.7-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 200c6a05879284a0bb9fe065c04c446d608ff93102f109be6af3c361a198087c
MD5 7f28b41870e16d9a41382cb2ea4278dc
BLAKE2b-256 85e29110e90fd08af8df9232284fadfe5374f93a14cd2aee14bfdd2cb6f413ac

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