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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.15-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.15-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.15-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.15-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 902843c3bb1d7f5888090156303558f76fc8b567ceb8be3b5e6a6a11360f52ba
MD5 ace0671567dfaf9d72e75b47c86f6ef3
BLAKE2b-256 6e83e0bda79757127f40aab6ee6196895818f46334e6d8905bbf1fd4b92c67a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1fbd320cb7f4d0660da611cc74a106d72314c264f70201c39b335b0cb460c619
MD5 a6911b3056d00dded777ab1ca6d1538c
BLAKE2b-256 c47782c1500510834b8ba8b2e36acec11836a68db9683e0c4f8adad3b3001a7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 0c6d00b318de36bf912665f38f1bff4f144607c9d01d1102eafa54d3fa822159
MD5 1821d02d41f2674f43ccd865216374b8
BLAKE2b-256 5d48f35a8f58c9be6fdb1c03b023c0c3abedfd60332c816a8bb9db27406cbc5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 158f712ce2140cec9a9aa2bfda37552e92856b119e18da1c3836a4b6d9d74a44
MD5 3e0e1f6e752d2489c9c69c422cb5f4df
BLAKE2b-256 cad23ef2005c99577463d018c35e03c64c7858f57ae1ea5ee1eaa84193cf2b21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bd038b63e11899446492cccba178076c9a8cc8ece3c708421d8c0e2268a4dcbe
MD5 120b89faf15634587403b44082678eaa
BLAKE2b-256 84ef0ebc1df4b74f590d6a01bc427265468654bb857afdae1a0758d221dd7bd1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 08a0acbf458e7dc8f3604b195900ecf4417ebaa56142ea4dbd0a61f4f0777822
MD5 4e2ea09ae5b7de6872952a42b70f99d1
BLAKE2b-256 d8a59e7e3992caa4b896a3d92caf7981d57c656a4e224b9d83bb008c16880a38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 dfa3ffb19c310ed609523a8dabf6df79982a429cf1ac808d74b97e51483c1943
MD5 089f5c2fd800eabe6edf502c2c4e34f6
BLAKE2b-256 c108c650ecea39398534ed992e66ab44ae33f468e113d9398ecc5c72043bcb5a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b7d59e33003109511b02ef0d15f061ae37a99ca8107831e22c7478bbb2e31e51
MD5 dd5cec6f7b9bdf7fa0b3100f6accd118
BLAKE2b-256 fedb16f1654a6704e676b53ff718da26e556753861fac14f333bcaa48846118b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 0606ba381d130ee7d40b4e9bb16787b4c781100aac7765abbaea648dc548fcfa
MD5 77022b900911099744645193f819e23c
BLAKE2b-256 e668b2d66cbd8a7308486c249f0164ba3e2d2c2d1739ad9225062f54d41f010f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0bc1d64fa3d778499a3732d7a5f8689ce946e5a2c1af314c847dcd0829b585dc
MD5 c423c1df99858a0d88167816dc605f17
BLAKE2b-256 cee25fbd69bfd05321c4a8dc05236b20ebc90823a894245fc19e1ede4c9d76b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7a7a79e5754c72292ad26a57f3a7d4421a6fc8b6675e712a1a849563f94ee76f
MD5 45e9f6f55b05d6f1c16bea6b6f5298a1
BLAKE2b-256 f50ba02afa3893efd233d68b5c27948db6da22b09394996866e2df9716de7c3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 535d7fd736360614afe7eab3bc35bf2336954b4b88f2d32761adb3f584ef6944
MD5 ed0586eb960f87c3add652136f90ca4b
BLAKE2b-256 f3f66ced73a5575c3def8c601296d712c467f4af1808cf7f0cd2c9fc6a5c762e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 10a03917bc3c710e93b41c3668630d0fb87ab755c373751659f99ddea628d9e9
MD5 d433a917e4d3065a2a66b547ea4628d4
BLAKE2b-256 36f62b3e5edee5a8d62e7f174a1857c0406eebd3f16971e143e1890bc2f7c70a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 35b194c80f22359a7d9e45869ec88da6da555a057fd5d521b21526b2369f2b68
MD5 2f95d0b76c33ae13982036058911b937
BLAKE2b-256 23412dbd01a7738af53e8abf742626e7dce5dca3d3a6b24309be9e4646d90396

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.15-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4cf3536f056e7f93d413f31ccbd5dab6e3987352d9df4b0b485df79665c510df
MD5 c4ab8e445eca9d94d4ee2dd2ef711b29
BLAKE2b-256 d874ad7340f27bc12ca32b325ce28d3916cc8f1499b57e208caa16b53b65a9d7

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