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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.24-cp314-cp314-macosx_15_0_universal2.whl (523.8 kB view details)

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

tensordict_nightly-2026.4.24-cp313-cp313-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.24-cp313-cp313-macosx_15_0_universal2.whl (523.7 kB view details)

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

tensordict_nightly-2026.4.24-cp312-cp312-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.24-cp312-cp312-macosx_15_0_universal2.whl (523.6 kB view details)

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

tensordict_nightly-2026.4.24-cp311-cp311-win_amd64.whl (592.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.24-cp311-cp311-macosx_15_0_universal2.whl (522.8 kB view details)

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

tensordict_nightly-2026.4.24-cp310-cp310-win_amd64.whl (591.0 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.24-cp310-cp310-macosx_15_0_universal2.whl (521.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 27bcb5fa146bd6d8725784d321514487966ec767aeaaecf0cea161a449869e25
MD5 c04c49c1c02da38e66043a4372dec059
BLAKE2b-256 0c35be9367fc525ee07f6214736d577cdbc7356769bb4613009483c3e0ae3f18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9d180711403b28f73a796f4f5e1ddcabc6c55c2ab6cac93f7b919448057b87f5
MD5 c9e4d23369b47df2fedfedd80fe1c405
BLAKE2b-256 918b8f66ee1b3f13944feee8f9322d0a318f2de9255c234bd4f3c70b0fd12981

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 125bd1265001e80c91924086a6fd18fce6af1734304fd736edbe5d33ffc6572e
MD5 78dd914c9682fbd4e1a96a1c641caec2
BLAKE2b-256 6ca236fd81d9132ef89666ad1c3a2ff3c088187a5ab9503e3ceefe33b8676143

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 527a2690538ac199c6ccb9ad1e2b2c9bbd7c966ab85af1e17b62818cf0b1f0f1
MD5 2ea2f6da370957dcd1717952faeba6de
BLAKE2b-256 bde67aee0502ef9cc41dd22354da421f28c821c20e24aee415bb661f7b9a6c7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 19af813de6845f5a11bd2de06cedd9311cda54a7b22c8e243a92d0c62c0dfc66
MD5 ebf24fbd023211f1575ed88b59add9ce
BLAKE2b-256 cbdffc43f5c37d9d9faf7ad121fc0a9bb0976859b28532407ca2ec336cc4f985

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 efb1759fd14813af8da58d1a8611ba73a4885cdd30e1775c26d141821eb1933d
MD5 f889614bc53f08b625455f5a805b8498
BLAKE2b-256 082e6d63609ee00c1c1efc1b3667037921560b49bacad6e68f94dd301f3fe835

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9bc101be4ea8ac0428f2b54a44804ce9ff393d537d21d93907e0fbeaeaedf88f
MD5 9719e2b5e15853c95454209c8efba940
BLAKE2b-256 9832737177f868f5df9950ad48e7b693aef30aaca8cfcb43cacc06ac5a5bc349

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7bfa85da7f7a5842914951934f2abce4efdacb50540602511825b5bb39132e91
MD5 19f4c1cd20862d3c014638cc2e0ae139
BLAKE2b-256 dbe5dd92c3e4985e5b179dd9f790c0af7d4573fb8fa371436c0f347b70801b32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 eec70a6ac10eda023611e2aca975990572268db9322695213bd85756ae3834fb
MD5 33258aadd18e04d5e250e1fd3d4f2a0b
BLAKE2b-256 194fce3de0b9c8e2cae93d51959333d758e5420d22d0356ee676c237f5f1ef52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7030b1b86f6f70efe1a02eb81a9ef81b3b2ae3b10db9eb8abc19b340d24d6015
MD5 c971b17e544995302a08b469d63122e6
BLAKE2b-256 3bcacd46ae5cd1a38d2cbe7b6f5a75b40cf7dcca82f5ea187a7a819aa3618824

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 03b286fa22d134fd6e0d477b978687be53df67dd574897873141ff01d8462bcf
MD5 f981aeb661b61611cd3fc587340fc3da
BLAKE2b-256 5082e56550178f50f8fef07294c182807341c3ba6ca372dd4053cc3044fafc04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 26b876f3c40b3e1a7371089aabec049f387096412a156e870aab950814dd9380
MD5 02d27347fc753887c0198ef4dbe6891c
BLAKE2b-256 1edcb59994f038e3434e9a4b20fd253e65d18c92c507e9e94f41aa5b36de9195

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f980874c8ba6c03e168adac0b7c135ceaceb427b2073e1e22ae418ab9953ea0d
MD5 f36163221b6a520586c46c204d0ae148
BLAKE2b-256 6b4689a2e68b1e42ee2a2ed577e0233fcc6315dc4b0c7b2e6af9ca98391d4711

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e50064ce5451d58b53769fbe7037018a9f1e448d679c9ba4a04e0be37b5a0a33
MD5 3aab4ab47bab4404a934d00fb7ff66fa
BLAKE2b-256 e331f409f72e407a88425eed3e4dc5b49dfed8922bdb6959999ea9ea53cff669

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.24-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3463ca0508384a8b9e4dfad1f48e123f76cf1d0a6daaf5fe6baa9933b6e1ff2a
MD5 5d5d6f5ceb2d1fc38b459c5d48049ae4
BLAKE2b-256 247e3b08f4d11e5003ad91260b3a8411dfb5fba4a247fe9557872bc82597aebe

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