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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.29-cp314-cp314-macosx_15_0_universal2.whl (528.9 kB view details)

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

tensordict_nightly-2026.4.29-cp313-cp313-win_amd64.whl (599.3 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.29-cp313-cp313-macosx_15_0_universal2.whl (528.7 kB view details)

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

tensordict_nightly-2026.4.29-cp312-cp312-win_amd64.whl (599.3 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.29-cp312-cp312-macosx_15_0_universal2.whl (528.6 kB view details)

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

tensordict_nightly-2026.4.29-cp311-cp311-win_amd64.whl (598.0 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.29-cp311-cp311-macosx_15_0_universal2.whl (527.8 kB view details)

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

tensordict_nightly-2026.4.29-cp310-cp310-win_amd64.whl (596.1 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.29-cp310-cp310-macosx_15_0_universal2.whl (526.5 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 69322af41e7db1c1230e6c9d9499c25b19c09c73bd69b7f2640da676df32320a
MD5 1a6028256b7693ff26bbd19cbc5bd12d
BLAKE2b-256 f718ada2261eccaf6952ecf1047333105bb280b074d49ad4e0a322be001372ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 679e3dd351cd76fbd01dddcdfcad1d3a89345a027a48b3c30002a4fb77d5e211
MD5 1934a6cfedd4050ec5f5c767cb32a869
BLAKE2b-256 413bf11f7284482059ae305a7be57cdd8abfab0d2b8de209ae2c6f81cb740655

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2ac6d8b26e916dd588e657c82d4270ca2727ea22905e7f57bffcbc1c40139a98
MD5 71581f6cd9ecd0673c20db88d592eb65
BLAKE2b-256 a81bb547e57397a6f28a09703eb0cf33689d1692087896ea019969dc1292618c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8a8b3599e19bd57380925cee627016e82dfb04b4dd26adfd1f1beb5c121a1722
MD5 df9dbca31a34762bd687d93a68a59152
BLAKE2b-256 2dcb47f751ecc83830262a532813ca1e4f8902b00f4007e1a9b15d867681d7e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 40cc1d2af0c378e1a1f039a7d1dbfd0844147af3044d0e321569b029d6d2e8c7
MD5 c4e77b055d8433c76cc2986212a99999
BLAKE2b-256 e766db79455c80a6619be89a6329ec5b39c24c96abee6852aa01e5d00a484feb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d9a7688864a641355529abaabc822147b45b39e3dd3b293842f756e2ee01ceb4
MD5 b5cd93e6f8f9cb9dde2a292a3d77e9bb
BLAKE2b-256 2fee8288f2073b44e3cd85d368c1b716b792719e5bedc22ee2510973299c8996

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bfca6c2bdb8ec8c896ad0df7c2d369806a49e35295084b36e64cc0a56c76d932
MD5 926952806b14bbcbbd9d3a7b76950faa
BLAKE2b-256 ae6b6f4816e3960878bd2cdf92fcc6720251fa74c59adf3c4ec050788016f9af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 272a544b28b9584a33651a3994bd0f3698e97c4db8db82e2d26e09568ebec648
MD5 807fbfb77d1917c8a58d88a2ce7ffc58
BLAKE2b-256 59f187ebc7d294df1d5c4f22175e921d83008f3058c92c700e0a8560ef04f08e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9f80578c3cd526179d1a02d3eeff2ae02ca9454f9ee84e78245e12a9bd33747c
MD5 9bbbf8ba7084e5b6752052179048ba13
BLAKE2b-256 93c98b25130e754834fa76a22faa86ec4602d672cd8fa82a30bc4c910b5edb47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7b408950992a78bf4ff2308c7d89a203de8c7a502b51209556ff6ff0e3462f0e
MD5 14b56c4bea635437b3a485f1d3a0f55b
BLAKE2b-256 a25b9157e92bc1ef5b41bc009af26a39d81a758a2824c08b5c06d94bdef9f7ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f14ffce688aeb1a7706cf5fb590965aa497219a702f28037460123fbd8ca2403
MD5 63a1e21e5d7141f4a3b3adce705567ac
BLAKE2b-256 a6413c0beeb6886d5bebeb9be76f6f56b39f590a9f8194b0408ddf6c76f2a4a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ad332820a8e2a0307c6a9ce52490d2f21e4c118cb414fd8a3b5c864347d50c24
MD5 a03d0302326cf62dce3d9f5d55d847f9
BLAKE2b-256 74d2e1e3e37e5933c2c7b9434905dd1490a51385355773997f584201a1a9f5fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8e2b57f3f7403df5e41b303317d11461f5c6d76ed6537a7f64088c9ea69584f3
MD5 841516872e7b619270baa2eb81a17c92
BLAKE2b-256 569279e8e51b9fd34d50ad51077f9150753d152c40f2ad8c1e2c015c886652c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5248d1a82b3d3465280f3026d95014346deafecc17ce91d684e508d277832b4d
MD5 912cb39b9d90d33b25c37280fe00afa6
BLAKE2b-256 b09a78ddde30f7ade9d7995d908680a4f1be54ef11da4b199248a376d37ecd7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.29-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ab79e92a75e7ac8f9fa37d3bb227f52712e92d4382ee37b729707aa80060bc96
MD5 caa78f506917e60c9e507ed17431923f
BLAKE2b-256 26c0f72c59499a7158a9c926c30e413b241460f3f22ad5504da46fac2114af27

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