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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.21-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.21-cp313-cp313-win_amd64.whl (608.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.21-cp313-cp313-macosx_15_0_universal2.whl (538.2 kB view details)

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

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

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.21-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.21-cp311-cp311-win_amd64.whl (607.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.21-cp311-cp311-macosx_15_0_universal2.whl (537.3 kB view details)

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

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.21-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.21-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 70e826ee8a86bf89f81b3fdcaab62a70442c00c1f13dfe8feb6c66ff10aecc25
MD5 043c4e10750917e835b1884cc9adb1b7
BLAKE2b-256 b505852a6102b08976d1ae75f9452399646e79756256cb05b9de680d3d42c7d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0dd8a2d4242f777d193d7b208d476e71de79e5fe73ea1acd698c1b3e4a9c6b95
MD5 dae3d1427f5dfea8313fee91331a09a4
BLAKE2b-256 4b10cc7a9e9b298b90bc321bc3a313609333211e6d5b334c1eac38b19c7a781d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 16812458a5e12d49394187b34d1530ad596d8b2198e24789fb9394b97703d040
MD5 f984fa2b3dea4bad0fb306a09be032d5
BLAKE2b-256 846c8d29ac96c7ac31b8a12cbfaa0df35d05c1ddf8f74e0b668f13a401e6bd3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f2afe54497fbf16c7fb5503f9d0e23c45f298d53f7317bcfaebaa4c23f9857f0
MD5 9e0615017acb65ac751a1f1d7cb7acd1
BLAKE2b-256 601ae1ec51eaa5552728e99b60120497c78330bfa5a9bac5290f81a58d0cc19c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 96601729078f18209d597ceaf005d843666f5480d2d21c7e2b5b50e7ade91c9c
MD5 209dca14ad9851c0b71f43903a76a794
BLAKE2b-256 61c0bb0e984e563b8dad0986484883abc2201483166d85cca851dfc13d9ad3c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 aa9acd794ae121fcd9004fa82a3b6c59cce6faeae7343187a264ee2bc8ab7dc0
MD5 0a767bcfd1569e735845aec2ef3c29ad
BLAKE2b-256 aa345078a1250ba8f245db171af9dedee1f82ae1ee12d6cf7baec715341ee5c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 cd8fc62ea8cf158a3db7bf32b3fa6650a25b6baed88f7da990630f27e2e7333f
MD5 ad6aa0215cb05d7c523d0caff4988029
BLAKE2b-256 104ea5763a10a1679d2f6162f932a5a877a4d6d68abe2b3ae062513e7c1ce44b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0e6d20f0b09b7e998be3ffb9720da43806a96c6902f6fab6f5fe5a2f3a2d3deb
MD5 da1d1f1d94fd4b8da0579e6847bbb6e9
BLAKE2b-256 11bf2f3085da8457f7996dcdb660456a74c5d14b8d4c9c5c88dc19f61189cb97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5fe6856409f406bf74d010d4f2ef1d1d6526dd3753e1c13e2f0b8951108b46c2
MD5 aa3e3eacd4298f12cfb22eaafae36d07
BLAKE2b-256 477b876d098416779d1301f5f6faad1f8abf6ee314c2d7060465f918f0848e53

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4a0befe59c7344bffb5dd6928af74746d945d3133f64ea26e20cbcabc7c7baeb
MD5 dbdeef3e85218fab5def7c8c35b647fa
BLAKE2b-256 8b2812a8b7170c51604c25f7141db25949643716b274ea02e9c1c25d9b1b62ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0d8a905e46cfd965d63bf44051ac0ef59b45195e347df0819d121bcea18144e4
MD5 6ce01f6be20135e84a119577b8b6ae5e
BLAKE2b-256 cfec872ee76c69eb7e21a4c0dcec9091fd778b580275265d3ef950570e8f6591

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2641b65e1bea35a7349b6b97a4c468bc47c0b89104ace6b019065c1d10aff51f
MD5 edd8ce34706b9d86a1a9c29800ba68e9
BLAKE2b-256 7b57c8fb4448f7173f9bd4739378b85191112f09834335c219efddfa1e1e363e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 77017f524dd66cc36600fb88698506785b9dfdc75e08a97fcbe045b518adcc58
MD5 aa6f873266ce31f1b6736694bd887aa9
BLAKE2b-256 21aabb8793708f2f43c06499f49619ee084aa59ac638c97c4a9b130a0d2752b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 67663b38d94d7d87efad9130b05668320fa5574eda1a529684669138c0e804ce
MD5 840ec6e7c6c93a08c3bc2e69eac5e0b7
BLAKE2b-256 b2f0b0e0fe8c9e9378a1b00e45b88a0aed258c270148d5ca25cdb504425bc6d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.21-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a8cf13ff20cdb9f4d0865219b34a571951ec0e0674779ddfe1a431d36c74259c
MD5 4a727d284f4a1f3e5bb7476d59301e9d
BLAKE2b-256 7f735c1d5ac5e281b7fc6c1ee9fa497d9c91855ef61d321fffd1d44fcd5de5b8

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