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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.28-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.28-cp313-cp313-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.28-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.28-cp312-cp312-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.28-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.28-cp311-cp311-win_amd64.whl (592.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.28-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.28-cp310-cp310-win_amd64.whl (591.0 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.28-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.28-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 b7d15fbe1692b787ca1a6b2ed08eeefabdd0a617d82a586ab2b1d7e3c3278209
MD5 58d6a53bdfe1f8605c8ca6186bae93a1
BLAKE2b-256 855d37c5b23a7c3c237a04d76ea01388ed6d3e8fa264e43dae190555febf8353

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 602f626a901f7e14ea30baec5dbb380a0e93055e2ed8b5de6cfec572824a2905
MD5 576aeec00a9154853b8e876af3fe1129
BLAKE2b-256 a3c502df8f9c7f047c88ee99505cbda0eb78fca7af343eced2b75293a792d9e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e0938575cf07334c600a1a7b7a2ed754fb1b300c5e1e67384ff3f3f56c3265e1
MD5 1befb14c87e6508b3173cf7f1e0dd006
BLAKE2b-256 c3ec5583513bec6beef46acc6f38fe6b8d77ddb57a0d22253b827746ef6f558b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6d4ded7c446f4bcd0600c9cdc24335dab81089d98e8366a1e3512d06f0058c9c
MD5 15fd7c27f381ba150da0ad60c4bad964
BLAKE2b-256 3f23c4de0a75dbf693dfe2ccb85483bc7f78a5c15374bdd86324b15b94bd17e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 923220ff2964c95fed00ff3ec6cf7117a122933d7df790b68ab254b4127dbfb0
MD5 1f8e980e1a511aa33db3e3f8a334faa4
BLAKE2b-256 3183c390ebf1351d54fc09484ed24a06b4ea3b4a9f195848f7ff2edc4ef14be1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a97ad37a0d9256cc6105aca6da34aed504496f5001958f97d126c60adbbee78a
MD5 0c6d8cb3acdb27a64b1d8142da925a9b
BLAKE2b-256 6404fa232c3c6e30de2c3afe9867c416e690311330ca925c47133808aacc0c4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 cf86dac255a1bff5f9f8500b2ac3fa408211e959c9c3fcb659e485df38310655
MD5 46859396284c4416cf2ce095218ed41a
BLAKE2b-256 c4ae6a29dbe175456392126a2ac4d110249bddacdd29e11a974cd6eb53d1219d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 94050e24f6f55bed8531fbe3ab553c0a1c1e30fd00e3e281981213c4b04ca487
MD5 a3d1dbf33e1fada63eed00719f05f70c
BLAKE2b-256 b7004969b2aae24c3a85dfbe6f7de864a9dd82be4d7df283e118565877b2dc29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 85b78e8fcbb39c88860efc7b8f9cae919928256c2d47e0fd3ca3013731518388
MD5 13119699d7856dfa0998451adf85fb37
BLAKE2b-256 62a79d6f9f263a93690379ad2774e4bff3854351e8f9d224589f20216e3e9d5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 45fb86bd9316319ecc9dccbbe2959e836d7a1ed94368131427558c9ae6298cb4
MD5 48c46068e2699bbf642579426dbb7cae
BLAKE2b-256 b96c5e10e10e9363aa8ef9b69cd5ea15b6b14ead49db3f89238b4b280b0331f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c458f0c7da264b87310242c961eb43873415a30fb9a1ead5c5324a42d484d005
MD5 e06d22661d9faf91d1cf87ea7c249719
BLAKE2b-256 43b2a4e5cd761cbff016ae8a2065a57643157d8ebde661dcd234751f552ba412

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 cbef3e686927ac1957f4a29c0cf466220a701aa6dab93cea0df5db4123808f8e
MD5 91c4a3c8fc564d9910389ba094d9ef58
BLAKE2b-256 4854ab57f1437214f591cee30e149c1146d03b49c01f678f5de461b7509c511a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b82fcf196ed1d372b5948bbafafcc1c145a7afe4e440481489353a0682195dfc
MD5 ee07dc6a197f5f4cd56757733ee6a4ec
BLAKE2b-256 fdd511fbaaa621bce43b64d0bf7644c18ae60011831c16278427108bcfaa2930

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3b5ca457a571d78b6a227b5b5feb73402b4a68ce875893e7e268b82bace0019c
MD5 9534f1d9de353ae6a96370a95c0ff84b
BLAKE2b-256 4e710d778b104e58ad2f62f8bdbf20d18f108d05c1827563073270da9bc8c300

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.28-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3e27d7cabe5ef2c1a6919c01611f810a8741f30ca518582b0d3905560405bafa
MD5 765ca313c1db56e9d32efc07394f1b20
BLAKE2b-256 c23b11046dda6857c4db12e997640cd324f2ecce68b00cb71fbf261793ba09ea

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