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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.19-cp314-cp314-macosx_15_0_universal2.whl (538.3 kB view details)

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

tensordict_nightly-2026.5.19-cp313-cp313-win_amd64.whl (608.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.19-cp313-cp313-macosx_15_0_universal2.whl (538.1 kB view details)

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

tensordict_nightly-2026.5.19-cp312-cp312-win_amd64.whl (608.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.19-cp312-cp312-macosx_15_0_universal2.whl (538.1 kB view details)

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

tensordict_nightly-2026.5.19-cp311-cp311-win_amd64.whl (607.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.19-cp311-cp311-macosx_15_0_universal2.whl (537.2 kB view details)

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

tensordict_nightly-2026.5.19-cp310-cp310-win_amd64.whl (605.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.19-cp310-cp310-macosx_15_0_universal2.whl (535.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 567ee966a8b88784af55b6ce7a9d30090815f60708f923c7e19e850002a62768
MD5 8ef066e5cd6e00bff00a398e9adfbbad
BLAKE2b-256 f97f89f39247a034169faf7746efbccb8c5026f202d3a6e419d70fe79f4c4197

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 678185332696d730947d2a052021363a251544d8f5d4eaa04f24f75f4f522d70
MD5 acd76275dd46c301408322c9b14dc2ee
BLAKE2b-256 f1b8da0bef472afe691047e120e7c464f8e99dd6a0b9f1ed939b118649f78eb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f392be276ea7cf124c3a6338d176f91ed1eed6e066c7e24dbbadfa143f570bfa
MD5 64d9e19a6c17d04d82d222eae10c8da3
BLAKE2b-256 56347a5b098cd0e623c42e4b0a49e64eff88734ce9e3a8bff612a7142b4283f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1dadc258e37ca96f2adb654a02200d1bb88ef31d255fb62f328365ce9287f7f8
MD5 ee4e4296d6ef7a4d5b1be6ef1b9a31d8
BLAKE2b-256 051ac1b186cca101de3056004ae957a6179a31d2d55ff761813096c598ec4637

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 68bf3db2273d209b2613fc56b430437aeee64dbf7340ec0ece2251f003489232
MD5 f4b727040ee987c5a333a439cd926e26
BLAKE2b-256 9567fb5aba743c7e80d1b194fee26e7510555228010471d59f14f586599b3a64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f6add4064e52362388c5137d756ec1aa38a085fd01d9f86a16c5d105928a1532
MD5 d2d66d8b7ba065c2398cfda37d58674b
BLAKE2b-256 d08790180f96a721b1c75461e62aa426fff615d50a16be3a7215af23b01e17f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 86363acdcc36375318bbd0b0d28cfd6c4b39fe26d57b1be9a468ec725983f5c0
MD5 24e5be5cc09fd580f36ca4c68bc737a0
BLAKE2b-256 00d5fe2731402de4e8135283ab42cd1c814fa13c9eca946b6def718e4371cd1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1a6dbf8f6e99934834a71eb5a6058d084dbfb3f02f143986ceaf68fc847b0ba4
MD5 1b4f80c33c63417bc45ddc61ab0f4bc6
BLAKE2b-256 cc5e476dfbeba46b6ae1eaa19f18d11de54bd5a91e2a1fd41be204a5e629ba91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ef161eaee9ef261e40f3eb98e8483f0dfcc2b0255b297ee2454eaf657cbe44cf
MD5 58306194b6217863f66a5bb4edbb9105
BLAKE2b-256 05a08bef2fb3692c9955d4ce24d20ad334ec81363d7c32977c33026f03a2d594

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b339e64bf582e223487181c96853dbbe6b17df49ec8be531ab2c31e5f8ce0e73
MD5 a220a1a3709975a14bb2dcf39a757382
BLAKE2b-256 29ac575187ad3d5f0b4f4c1b6c8aad82f2139d69a46e410acd7e453801a0b458

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dd18556fc3585897ca7522bb9afe9e6044445b08e4595d5d27d222de5abd1345
MD5 ea1e91130272575eed4f0c5a9959b20c
BLAKE2b-256 e3d1985279af61a9fe85e1e4420023fb332d01a1ad109e289ee3e7db88f7b5e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d2ccffb489c84116bbacda61dbf91b5cf1ddabd31a0329ec51f81b96070a75c3
MD5 45d376303089db0c569307f183e89d01
BLAKE2b-256 c48e2b75fe7b7762954fa76f5044534e0a36d0b68f31a5948c8e44270b8c0b4e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 dcb5361ce29c0fe685f0ed9c5d5dfa40105d11ac6901f8966e136fac8a95db76
MD5 ceedb6b432c412f6823023c604bd0f2e
BLAKE2b-256 556a0bfd3bad39f96276418492d1472b7dde1d15ad7eb122cf951d02f0ac8d13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b2f0d025fd4dcc094f3b1d05203de36cfbfa1dea64a350a0715ee3cf978b4dc4
MD5 367f91d0e6eaa8da56e566b76b12abe8
BLAKE2b-256 8461f102fbc223a0ef390891a009645f153ba967f9a73d9b215846059c432619

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.19-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b1c16de82cceb8d5232564704cb85137c232f97b4e0766891496ca7999dbd7da
MD5 3880667cf591ff95cec05a28428771c6
BLAKE2b-256 fb4c1d1bd7fe182a601e824e6669ebebd09160a0791859e0bdafed13cdd31399

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