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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.31-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.31-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.31-cp313-cp313-macosx_15_0_universal2.whl (540.2 kB view details)

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

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

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.31-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.31-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.31-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.31-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.31-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.31-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 05804fc97bfb71920a5f779b3273f5a073eddf4fd436c519f211943559e1e1f5
MD5 9c7272e9d691137b16fd630b210d6f8c
BLAKE2b-256 f5cb00e686a6440dcb70edd902d1348689b18e8fcbc2719203a2671497c1e745

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 820e225edf15cc377454e788cb00a3458c16c9a713045d13f5c9f553b61c1a41
MD5 e235d20505825e7f67144ecd713bd266
BLAKE2b-256 b2127943af812edf0a037b68bfc998f2d9814369c372b8e5b5315b1f056399d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b743becafc55b50fe01cc9a12e074f000ed51dc7620a5814b43fad02aa8ff987
MD5 d1e17da2cb8e470c7e2cdd9f315a366f
BLAKE2b-256 d0fab03f11d9afa3d44584abe3a6f571eaa56a9bbdceccb1cd847f0c91a256ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a9cd463bcf8dd567d54ad99e6b32fa3dbb5168ad6fcf73e146f96f38927f5a08
MD5 8db0ff54762266b8643da3300286e196
BLAKE2b-256 32551426169fe1dfd25e2711f034dddbd72ac7f85ba7206f55dbb73121d8efba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 04347ce9b50843df9de3ffbb75359dff1c8140e2f816267bf143eb8c7bee8b33
MD5 5f70fd1478de8ecd10cd86736d53f1d6
BLAKE2b-256 8900d87bbf525aa0ae5b1477200877b28d1656d82a2225d232114acbd4e00e70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 89c7beb44b5aecabc6f299b9d4c2410326a5de35ddf53d1659373d7849761aff
MD5 05455f0e5e76ccee10fb5862f6e15d4f
BLAKE2b-256 50a64ddcd5d0f21242e833599e185f7bd029d5feb1585fea75088717ddaa7b3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 77421d088705e86e7fbdc35efbbff0dfdb6ddd33bd886aee167886825deb53af
MD5 4d319e05c4c3ef8b9bc09c3e63a8ed4c
BLAKE2b-256 8d06ae7d8134ffdcfa338064f5094ca42d6fc580dd5af39caa8388215c4bb224

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9b54cd83d5b3df53be2efb48b4eceb245cca8ad497115d396761d01e479ff081
MD5 8991ec0f787e90081b62b8f5c2922b51
BLAKE2b-256 57d416991e9538201cee83a3fcda4aa252c3428b23d8bd283b376350ff5e18ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e83c475386fa2677b5b71dc380b992b4f1e066c03e4a0febfd1e5ea4789af784
MD5 6187944ad9fdf405ed34ff334def8f6e
BLAKE2b-256 cede024f202555cb3e952b7b454f4fd896593dbf96e19a855a6a6330e8e51540

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 93fa812dfc82370c18857f94eed449e8c73eec540389cec1ee68045c3dcd6f56
MD5 22a2fd86d357ec034f9205b13b6246f0
BLAKE2b-256 a4755d8d53aaeff03e4aa4716fc0528d8a88ea4c434d6ede5c926e4e7be174c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1669733d3e2fade7d7387b9ff5a78d28eeaa629556dbd5728fa27bdd486f76c4
MD5 0f485bf675be39a093d33e2c3d6b96d6
BLAKE2b-256 ada992f58392832c58bc5e41a4aeac4a79629708395346847f7f09546d399114

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 98049e411af87857568b6c089178e6089e605b514ad2d1a71bb715e34e74c535
MD5 5d5cc1779c7142a1b5a3ef1c68254307
BLAKE2b-256 151b02e3e28b0f7775231efdf3102ea2f5638107096a65761698abe856cfb17c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e4eff8a097d3b6dc1333b1f682cdc02899b0ede87b8edf51c60ebe2714175cf2
MD5 b68582074c253087795596650817f94a
BLAKE2b-256 264907e781967f8938fe63d0d615416829ec405708fb89497eb9e0ea8a55148f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 64aa8db6d0763824535b3d0e7e7d254fcbd803e92c546b97d2a43d3a06857add
MD5 21b1b7a4fb45e6699426cacbc93e181b
BLAKE2b-256 5d1046d98f57d7a6451bf66a2745539bb1c7e43686f0cb10cf964e5d01640bf9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.31-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 345b0bc38071ad78f3dd53fd06de87a4d75084d53de0f6e81242fefd0c4efefd
MD5 cce1a582c9023f0b74e9acd6f3f3d515
BLAKE2b-256 48e54cd5616779c26109d378e1a121cd04c285ee840e071afea094d2533de5fe

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