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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.10-cp314-cp314-macosx_15_0_universal2.whl (520.2 kB view details)

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

tensordict_nightly-2026.4.10-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.10-cp310-cp310-macosx_15_0_universal2.whl (517.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 285151fd7b322e0c168e569940065000621cdef356e209c77a7d9c051de4c48e
MD5 c7bb29320e9b9ef6b2380b48b488f301
BLAKE2b-256 5496c1fa84653a62c2c4062fef7d2e641e19111625833db576151f6eac698f23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 012e2d9fa47fbe327261a080defe2350bcc7c436a3f060dc1940bb2119513740
MD5 53a97ee6fb50e37c4a6b9c67a5e55aae
BLAKE2b-256 3e1734d113573cee0e963a1c7b5c8b88f6e45737988cbf283b265b07aeaa0667

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f2c0423983843f40e29949bba93bebbbf8c674d4af7c374ddc4b8729966fd9d9
MD5 cef4b4ef69fe03d3ea9e44f0064d34b4
BLAKE2b-256 6c2b839ea1986050f12213574a1a4be59f2ea1d9b6490fd61abb3643c5c13a14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 abe0dc3b40c247fff8638da4c8c43ea2fdbc11c50ea53ae8f6cdc104fc04f188
MD5 3ed9199457aecb0bb467a51ea887c4da
BLAKE2b-256 ee5183cffd490649da84b5128617a680b5f1f455b0e6f6029f51d83698f4c78b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 50ae33c3f5c3c4ed4a132f83989488f8c0d358debd665a325e3bd17711f0c2ac
MD5 c14b7cb74c79db5f22d9c2d6e6f2b3b3
BLAKE2b-256 4361324d6b5f2ed32c9dedcbae9b301c01eaffb52bc8cf56b17f960f211a6029

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 90955147e30c40ea26ae2118f9cca9577dcc063fc7ed5253a1d2e2879bda9a95
MD5 d0fbba6c5a0d95fa2d970a35eaa7d2db
BLAKE2b-256 8673f5e3ca94f3c9a03671f61cf315faca10c1672d79eb486793937bc42f7c52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 691cc77b22a74536e0187774b1a643415f3e356d570a1730a5b80cf780d923d6
MD5 f4c7ee5e9b3fc40a74cac73ed63e823f
BLAKE2b-256 9fa4235c8705566a181a6c54136244742bed30bc7c400eeddd417d325347b1eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e18dd6ca32ca51f220bb2214baf2ea47b681dfeb1d3e65ae061d7af04ec26f4e
MD5 8c8f9e59c6ae07cbd9e55fe073611e81
BLAKE2b-256 6d25b6f42a6cbf84e6ed88069146f0b9ac3fe54689987e7f41b7491af44bcd00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5dcc2cabcbbe319884b742c503ed3b9d1fa04bf28a51d44b1167d1a75ff81bc7
MD5 c15047cd6a678bf216e9cd3cf234f7e9
BLAKE2b-256 c9b122ced87256b3de0b6416382d292a8e11edf7bfdc6057c54fb9623c7334d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d20c18feff50c9df2705fa0ef3b15b7ef04f442b191101ab0e26467fa68f72b0
MD5 d63facbf8c1f604a882d03058b1ae81d
BLAKE2b-256 b153da7c7de60d9e195618d3147f884c67ebfd9fff52464160e7296426fca1ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 731f680182ccfab3b83030dea4a72e96e2e6d4ac673d004b4b0681b3c0548681
MD5 bade94b244ed7c06a477996b234abd1e
BLAKE2b-256 e7830926b2a2d4d06154be92a529c5011cd832598ffbe768006dd5ae13af25ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8cc45dfcb55d4c510e809f63954e34b696b43180faa53c8889d8d0de6d4558ef
MD5 a313071f9de6dd98f27e4ed6af1bd2e0
BLAKE2b-256 761551d6b8cf8dd491a77be548741bd70ce10f8af8fa200e6e809706b9d2b28c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4a5e07eb970f82cedb721c75143348f830bc61d58b3b7afcba025d9b29634d57
MD5 7c72dd717834ca48e3069074d91b6dc4
BLAKE2b-256 7b7a995b8b9be669e965bf66bf36cc509adc8ade016a5e99ebae0d4948e73795

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a8498e995a1688f0dd7c6786965be68086efd95423f27a776f75d723562ff02f
MD5 ed8a5f364ff57278ea46069b5c9a4df0
BLAKE2b-256 11782d004d7066e0ea4a8be7c884854a036adcfe2f2acf73b84744539ce99baf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.10-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ec04caf73e1425769b30d3ebfeadde0eaff69b72dc0d170912ffe412ae2dd421
MD5 e9f1664e64491804bbbe80e88948832d
BLAKE2b-256 05e3cf30f69a17f17fa2fbcbb6d5d09a3e01d53e70e338cfdaa4b7a46640cfc8

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