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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.4-cp314-cp314-macosx_15_0_universal2.whl (526.9 kB view details)

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

tensordict_nightly-2026.5.4-cp313-cp313-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.4-cp313-cp313-macosx_15_0_universal2.whl (526.8 kB view details)

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

tensordict_nightly-2026.5.4-cp312-cp312-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.4-cp312-cp312-macosx_15_0_universal2.whl (526.7 kB view details)

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

tensordict_nightly-2026.5.4-cp311-cp311-win_amd64.whl (596.1 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.4-cp311-cp311-macosx_15_0_universal2.whl (525.9 kB view details)

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

tensordict_nightly-2026.5.4-cp310-cp310-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.4-cp310-cp310-macosx_15_0_universal2.whl (524.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 fe5b3b56fa1a9b274505666d2226428851798cd1e4c2b638f5836a733077f98e
MD5 030ac2110c6b27532c54ed843c0b5f99
BLAKE2b-256 ff86e92a742699e9a9a104d03e54c9ab2e7712cda8c9cc2a8c446147dd8aa004

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 28d01ecd854743c5c8eaa81d5ab2bef1d0182f2ac08febaeabad0b927d9a4638
MD5 32c7aec9c773f1303a0c656935435d7e
BLAKE2b-256 7aad4449e5dac1c43a7e6d2193c0b35131ef00dcee553f523200b12574f60c0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 857fe9ca2bc1d8acbb503b21e9ca54a6e5bdae0c7f1804f2095960623d68f11c
MD5 4fd4345362ec3c459aa34ed628524ff0
BLAKE2b-256 4ef51e6a631c2973125ade18d94d0afe902eedbef268e84d46a65ab63fd7c494

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 08c3819a1ed37d86705cb9ea74d5430e4cfb06065c0b6f2eadf156018ba43b60
MD5 7ea6b093dea5ae03e84b2aad95d480b9
BLAKE2b-256 bf6c8510c9d1389fbcfb07154d28cb673a71314dae090de276e8ad29370df3f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5a10112c3b8052589c4b8a0f594803351faec89f46c325c3fa53f35115214c40
MD5 247e985de12070260e09fe0d6bf3087e
BLAKE2b-256 d483867db08b844e6bc987fb89874d57809616fb88fc8e5e11d806f01e142834

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 32bad95c473ec1d09cb8e1789c316425a062e6e2e5f536780b5753194920e2ef
MD5 140be1fa92152e37c6e18031d9b4f508
BLAKE2b-256 4f811f3ec2825091584067966b6cc931c0b075a72d4bd4201e65abb0b22f9fa1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c46f165c2ce731d016c954f081c4434294122b32c1253804bf1212f37e0c8b09
MD5 b81d6a688b7664d471a253c707d97010
BLAKE2b-256 217a1df631f1d8960b7afa88d0c065a0c78fe31e17fc0450c8d1d6ad0463a2a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 00c4bf51eb76dc1483f6729763488000da16e05aa08502427ef9fb3cc77cdb65
MD5 5d1dbcb4550c556151844b3c84618189
BLAKE2b-256 91f1b1375cf232216e9e4d2281200e7737db72ad2df3d1dc2afa24fd9fce14dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 cf3eaed85adeb8d51e52e92209946f7d9396fa0848f217dd9902b305c823260b
MD5 b54d69211fdd13bafa51b17776855ada
BLAKE2b-256 e8a292f311e802abd875249d2691342eb90b86c58f7971724e42fc446ebaa289

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 48a9f35c4ef852ad5b512116aea1169ae79728e380f79bcf589ca0c4e448a2e9
MD5 852c0b4fb0d7425682de05d2f5862eb6
BLAKE2b-256 1d8d576034115c24c6771d69c8ae718c87945901e7e05678fe0619aa6349f708

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0f551cb1693b54d408f28e4415fd6ee3b4a514836f8bdcdfdf85906786fa63cd
MD5 067d4cb4b0e4d17cd9faf3d6b06e7932
BLAKE2b-256 1553dc10eba53cc61039dcab4a6abcc36e8ceeaa9799a3e46bbe234c7674a8b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5a020fe50dd68b0b0b08ebe9babd018313336f68f400ee0f2efb9352052df14b
MD5 4d105a227cd07d61fb39bd261ac3ff54
BLAKE2b-256 a3ea3992ccef3d2f28ede447abda0992cc15a1bc573e09b52fea6ebd6c835783

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 56e06643ea82a0ba0d75bc90f9df9f64b91a3d67d19301f9795c65310f834563
MD5 2ff8168a766d19a56a82be9a31d6266d
BLAKE2b-256 e0154c0e5af95e0d8057105e1728acb15bc617a24ad4053b27fa03c432bd8c2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 beb234abbc23d5075e013be126e1d4a4b1cbeb403f56b966be120cc4f6da212a
MD5 4e3d184b9b40c016fc4b7cdb087b628b
BLAKE2b-256 e6e5c1c610496e70bd805dcd4dfdf90c143bcda98c0da2b4a72c4120f0ffb63b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.4-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 63d6c91c463b5289d691dcd84aa9992e561a43a51c2e0c52c981cbd21896490b
MD5 d46bde63de998f98627164a8b5e709ef
BLAKE2b-256 a0407a2168b1c6cce8d286d540114bb1f9fd0a74b0814a161a149876f7fb2568

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