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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.28-cp314-cp314-macosx_15_0_universal2.whl (540.3 kB view details)

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

tensordict_nightly-2026.5.28-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.28-cp313-cp313-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.5.28-cp312-cp312-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.28-cp312-cp312-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.5.28-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.28-cp311-cp311-macosx_15_0_universal2.whl (539.2 kB view details)

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

tensordict_nightly-2026.5.28-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.28-cp310-cp310-macosx_15_0_universal2.whl (537.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 4c5a91ce31276d8d012539c69a8dc5e23f27635514b8d214f98f8456845b8208
MD5 ac7e344d1b44ae28899ab8cda652b6b3
BLAKE2b-256 b3798f3bdf23b24deb724f356b12c6e0947785c201a8fb3030996a7293b5b31f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 05a4f9c5c95d2bde9eef0a903be4d79323acde7a5ef2c17ce94abc846c55bd25
MD5 5202aae2a1d44610115d093794a3014a
BLAKE2b-256 fb93883a52d8ba73986a98657efe32b66664230df00ff586c5d0695915dc6636

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 977172180683fcfecaa650cd4c25e6e225cb44d8c20326cf7ad88e1d774bc65d
MD5 e04cf641ecdd0eba784cab8eca66d58f
BLAKE2b-256 edcaef57b53eb80818c1be2fd9b8a9ae9085cc04d28ecab0cf0c67374925d0d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 41c005d9121648ebb85f08889494db2d71bbc70a1fa62c5c1e79678432e792ec
MD5 d034613c5e132796f4214e7b209ba0fe
BLAKE2b-256 7bb8b34ee26db5adc3a1fbafddd175a43e0a1c417656263d8c4ea77f729adb4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0aac457e95e63a2ef6b31ecda709d4ce6dbb55a68fffc0b986b93a748dbd4324
MD5 00694f9a44c9863b6f659f43432c9347
BLAKE2b-256 eb9f5f94662df11c2d5db8dc61658c72e743dc1d2d7a9b83735d3ffb961b4fd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d44f155b9634b92038a9e6b12618443e29bbfb44a4160a1a3ae4a3613f498f07
MD5 67463d7f4c588426f9e29a2fe4424161
BLAKE2b-256 5fc018bc018609572a9906dd01ab6594495ec4255b0e1d22cd2104416558be47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8ee7f9361d6cd4e2994ec2878a4138d02a2f7991ba65cdfd23541554bfc6f1c5
MD5 6ccc1c8fd5d1d3f74090bb8f0ea415e6
BLAKE2b-256 c693271b49902b8d54dd5f415823f4c321590558d6c35e613b854657a6b28444

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e0659364d051d58e413b5ec2a5b921676bf72b78d7fa5a599df6575ed993dc49
MD5 49d6ea53b8fb31b74155bb03326918bf
BLAKE2b-256 21f4a0ebcead3b7f142063b13eedb86fb9add9ebe3c0e665dce78cc43060e7a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 235dd507f45f482287f7ed128d106b7f54ee4cbebc922a7a0dd81daacc4cbb5e
MD5 5e26309cfa618de5e11bcfe513a51c99
BLAKE2b-256 a0bb7459bb640b582a3e9908475b92c99c35a167344fe2d70b5d6140f81a6a2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 db2d6bb439b683c76d934874b97e13b8cfc9c6db36105b2ad1ac3cfce3eb8f73
MD5 954c151171b4666b6bb0d040c00ec157
BLAKE2b-256 3517864aef6591998bbd6499812065e185e8ba3f765606c158a189e2d6cf4e29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9c4e063d4011961cdfb6ff4061915858420530fe4da72d897fc83f2bec947f2a
MD5 bc17ddbd30995c6e1d88f24ffe41a459
BLAKE2b-256 b95da90f6ccab883256e27f91093bc75a55a16c238739eae27fe00e5d7344d8f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 66024373f0c3217923af1ba43b9a11bb1c9b2754a5b3b091a13222d1958bf7d9
MD5 ed6dfe764883372f263b9c8e6627ac7b
BLAKE2b-256 1821cb31f7fa108ad7179391f351b19c51d40e7731053d2cda018f211b2728ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 57401dc211c229ebd1dd9f6a09a48f2709f79e1ecafe21d5f161021f7764bcd2
MD5 1febbfd41e5ed4f156489f3fff33f98e
BLAKE2b-256 a842114343ca5facf3ce7e9ce106caec93ce71d4b707606fcffe38de14d5080e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 422abf7f78b629b61e9cb8aa0a7d3717946a486b5c2b72f3b103266a1149e264
MD5 77c3b8cdada66b8ab18474eb1250af65
BLAKE2b-256 b2863adeea3d7f7521760ec4ac0b24b6e24a97f80faf85c4e9d483031d370540

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.28-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d8fc152f7a9d1f7f8dda80de0767f84dd6429d59e973cf66b95f0be7ef2c0878
MD5 1dfe36b839eb7a00d999ef2c39b050f6
BLAKE2b-256 70767f2f97cdef5679066be45a8bc92cb1647c36c6a15a1b6d542b5d355f0e3b

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