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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.16-cp314-cp314-macosx_15_0_universal2.whl (532.3 kB view details)

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

tensordict_nightly-2026.5.16-cp313-cp313-win_amd64.whl (602.8 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.16-cp313-cp313-macosx_15_0_universal2.whl (532.1 kB view details)

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

tensordict_nightly-2026.5.16-cp312-cp312-win_amd64.whl (602.8 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.16-cp312-cp312-macosx_15_0_universal2.whl (532.1 kB view details)

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

tensordict_nightly-2026.5.16-cp311-cp311-win_amd64.whl (601.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.16-cp311-cp311-macosx_15_0_universal2.whl (531.2 kB view details)

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

tensordict_nightly-2026.5.16-cp310-cp310-win_amd64.whl (599.6 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.16-cp310-cp310-macosx_15_0_universal2.whl (529.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 6ffb83a1564e912716e870d9e7c56c01e98936bd7569ea7f4d24ccd74efca758
MD5 462ff862818b57dbdd111683e9b8b5ef
BLAKE2b-256 9618545a513e3c9ea801cb73b00b7f6aaa0ed54cd3c67a9d084b4c91a6ba57da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9b2341c6647acf9dddf911b0e00ecde3cd780b909c63f56673a88daae573a2da
MD5 535c677d1d10210f18b0d2dce295c103
BLAKE2b-256 4e2d7c95573c627489845e66e92f94fdfe2b4cbf9eee237078a73f8baae9cbb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4af8b483d3dfbf8beed8f51d64cc5ea3870e46317a64c4c6e5387f4ded04f429
MD5 9406f22606a248a734785a9839abd68b
BLAKE2b-256 d6eb5a318beee73dd1fcb5abd728de4f4ef8beba2775d9bdd58f119b220542fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 69738271fcdf21fea9ec0343ef4c7b57cb53876dc836f458ff0104544aeb59d5
MD5 40edeff29e42533d4f746efc9c8a9275
BLAKE2b-256 cdfae1e055b151e398fcb2e9dbb3fd7195906f22e9e02786e99c0f90f70f7f31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 80e497db213297ace34ed4deb1d7c4e69679197be710f9c2766eeabda7a07918
MD5 ca34896626bb4b1df85e34d5328c3b24
BLAKE2b-256 cadd2749c91ea083072daf16d5e2983fa2577633aa248a8a6b49be0026ac3088

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 857de977e9d7ffa550e6940bfb9f33de826243e49768b0acdeaa3297f0792399
MD5 c0f5d71eb4253baaaf75548921b0dc7f
BLAKE2b-256 2a0456cd85dba5def993ba6b4c3e815b4ca4d6d305a2ecd1d87fd70858dfeae5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1a619543ce17efa03273e67367f3eb856a5a31f52334c09470e2c948c81b1430
MD5 3af1083d7eaa8205f488e5fdd29d3e0b
BLAKE2b-256 972dc3efe6a1715307d5e5ea52a770613ced2a58c37ab8a5ebe289e6d1ef665a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0b71c3841e85f1105b4f8094971c48612c26d67d56f76ccbdf60dcb00a4d4528
MD5 c05cc3eb51fc2a2d219704d88bc7a572
BLAKE2b-256 013df364d69b172608e8a957634b3dd17239f512453717536a68ae8d039da83f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f74019565f385307e2f7b11a609c00315d0eca5b0f8c52346817201f60a5a404
MD5 32bf8f19eff226e6cea3935494fea65f
BLAKE2b-256 1b12164fdf9fa4a56436225512ae3a774bf91064bebba89bbe4518991e34a975

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4982ce2b5ed2531917ab6b9774b1667a8705f629f08c5eda7b022b9e9ebd4f1a
MD5 98adc65184df6e4dc6cee66960760601
BLAKE2b-256 b486c5bf2026755a14cf03174c2a271c0a7dfd3bd24aa9081c0ee737bb3c8cd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 db01ea8c334f0673a4ae6e1402618e3314fd8773d8e5a3b8f5a146fce2746e6c
MD5 1f9f8595c5596c872fabbd5b12955768
BLAKE2b-256 0b96bc71fe4461ff774f095ef800435c28cec8a2b10b52717eda470786c0d58b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f1fae8c8a1ae8575ee1cd725c6a4ebc57f4f3222e32f078f5812ee17284e1b7d
MD5 0575ac015d36a91c513faa6c1742f34e
BLAKE2b-256 1f2406de95b7af193bea450acd6444fd0d4ba68914164e0dba63535b8eb1f847

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3f5fbf9dfa6d5349ec01c26e4385d242a7c464f412edbf17d0424951f6306108
MD5 a10bb25636a3864452d6b18ac22b91cd
BLAKE2b-256 c32fb26323baa16ab2f4ef15fdd5e6feb6086024c74105c707d33ec08d1f9230

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c0be081181b7abdeb2c74c253c418f2140b811681ddfeadb3ebe93b2225ed79c
MD5 c001fb405a6bc6c08141403ac69b4988
BLAKE2b-256 3ec14bb67afb6740f661b9e32d070c48c5eebdf44114cde70fba0e7ce4c2645d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.16-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 34074d5e9a1e9245846a142b25f3d74df7476961b7e2a8e49e2d68ef28b71014
MD5 d7964da74b1891354c430a44b61da962
BLAKE2b-256 f3e017adab0b6e18b2ef3106ca83c1feb9aa15fbe57dd9c509e2342dba77c415

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