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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.5-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.5-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.5-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.5-cp312-cp312-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.5-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.5-cp311-cp311-win_amd64.whl (589.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.5-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.5-cp310-cp310-win_amd64.whl (587.3 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.5-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.5-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 efa0b569172d41e204c853922b6219782dc53db949cf0daf546df5ba23db1f08
MD5 273637bf799eb01bb20e9c71871c802e
BLAKE2b-256 6d5381cb9419b56cbb940cad5ccd590c06f612b0a8c52612e291dc273abd96f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 59b1e3f493e492c13bd1cdfd61e31cf5b0507d7fcfd2de5c72ff3c25ba2140e0
MD5 f3703a01d69b30f467c28b9a0d056d5a
BLAKE2b-256 859b9a1801b0c612682b77ff5e111ba49307c58b3beee31d4a189b99462b962c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e9fde75836ed59e90157d0d47dd7eb0b1bcf30f79dbc16bf062fbfe605073e21
MD5 a02656afa7161196d7d46f152d877df8
BLAKE2b-256 dbb11fdb52a6219fc6285e3b1ebed39ce6248b25b47b0369f053fc7fd555cfc0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d086285e1eda45a0c12696aeaee6de3c0f13ef6f0ddd2ac4be9d0afbcd9062f8
MD5 f4979eb81fd165d29f99cee3ab2d5c6f
BLAKE2b-256 36f79a0719915feb1db49723605f56734039993386ee3fb5ee711e1f3f7989e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6347de89f502e28d2bcbd358b08277f1e739e461e770840d7599dc5cb7246a96
MD5 b0e307fcc7b639706ee691f9ebb59b86
BLAKE2b-256 59a9fe06464bb5e43063a085b6f8ae58a2880805128c45d0c04bbb399911d3e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2993620eea2b3facb7ea80e9bbd3f258bdf242b583d27bdecf1989e38013cb5b
MD5 ff67eb7abdf224c87144f9faf9266769
BLAKE2b-256 5bb36fb6718fd3b546f90fd26934dddc76d3acffb61dc4f93f406de6deb85709

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bf1ed996cd1bfe2278d71c30f45be8f352c5018756627e4b98d85d665b3ab415
MD5 868833b23bbc8359da44309d12946a99
BLAKE2b-256 18fd82552276777e881b4e621fe5edce8ab99127c3b6816ed03d17851f109f3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2051d115365e7a6a82128b2605997b580d74b1e1f1c65fcc3c667fdb85eb525c
MD5 a816bc689872a00986630a34ec7a84b8
BLAKE2b-256 bd399013977ced60fb6327d267677d7b7317f69100720c4e264fee22d04acc32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 56e6fc7c4699e55ab4f82f27d80f659dd0923f14aa978fe53658732f59d29b78
MD5 d87e02e27c3be2ef0784c5d257ab8c04
BLAKE2b-256 9c1ad809ebbc3afd91634c471759cffc1cc0b109dec5f28190aa380d0c3dba70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c882afa133baf942241d9a74e17100d8b90ddad66513b54995345c5ebcf11e7d
MD5 a25000785aec55be00790f5bbf4b2c2e
BLAKE2b-256 b4b25beaefec8955f2a6aa971d703a1d6e66f6d1137e3d3f16946a529614c641

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3d350fc3dbc31e6823f9cc1aad16b930260b4017227183d0a6b09a4c842b518d
MD5 85023be4396c3458f58320bfff571fa9
BLAKE2b-256 8fc7c9c5cbbe8813be56a0df66c81d72203b947211579949550504f712abf1eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 78cdbd75018aa26a0ac8046698184d3503fdcb21d3e1d1f86993d7ee5dbe5c75
MD5 65b30990a5749e2299a2e6bcb6560e38
BLAKE2b-256 738c90971cc41bf1a19a3d0e3d8a4a7914740c123bab609414e136863e2bbce4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b77aca0157de7c1df0f5714f19e78aacfd5de77d31262f39bfd458d62d6e3957
MD5 aee5adfbdcf72266173a218ea2d37e42
BLAKE2b-256 cad2dcc00d303432c6325d618d03b378a6e8f3e0a3669f34dbbfef881cef35b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 379c400474e16f9941092034a9bb537eceeb15e51d7a16a5d0be914287fffea9
MD5 3adb76a4629d90f1658be606e06de678
BLAKE2b-256 bbbf2078cf28e62ccfb489b0710b1e92e197eb4a57b91b0e1710203e7a8eb3a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.5-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f79a3785bab0a8ee1ba4ba0d718a78aaab4755126a0653d883f8a054417a5c2e
MD5 fe1c4f692ef811d5dd52ca43e6341ca4
BLAKE2b-256 b456120d6a38485199d8d2af854e3a9d2de0e8f4fbe2beb267feaa6a39582dce

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