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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.13-cp314-cp314-macosx_15_0_universal2.whl (531.6 kB view details)

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

tensordict_nightly-2026.5.13-cp313-cp313-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.13-cp313-cp313-macosx_15_0_universal2.whl (531.5 kB view details)

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

tensordict_nightly-2026.5.13-cp312-cp312-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.13-cp312-cp312-macosx_15_0_universal2.whl (531.4 kB view details)

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

tensordict_nightly-2026.5.13-cp311-cp311-win_amd64.whl (600.8 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.13-cp311-cp311-macosx_15_0_universal2.whl (530.6 kB view details)

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

tensordict_nightly-2026.5.13-cp310-cp310-win_amd64.whl (598.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.13-cp310-cp310-macosx_15_0_universal2.whl (529.2 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 cc4f00010e523f2e2701954b01ca87418cd213b88f7c8acfd8c72f4cbb7dea85
MD5 ffb4d67e1302bb58bab955222b6bc352
BLAKE2b-256 70aec9519df61d4cd041b0b4857ce83265d3dcee0d28883ac113a5a8e0451dbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bd14a5d3b5843eb37d67d5532cde8aebabcb71c4ceab6a9b85dc04d40b3343f6
MD5 b587c4f301684e683655072ca0bc9898
BLAKE2b-256 a1e5dd5747c619b6051ba1c54ce3673d6e06c9ab6753fcb609a756fcdf5f51da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3e471ddc2c3c6c588da3573493f77def9daf90c12f23ec2eb9f0500a3b219efe
MD5 6f5861e383b5cd6562e597f57e00af08
BLAKE2b-256 0314b4bc3fcc22f1ea13f608a0413356bc434704d11c3516a9e075e850003024

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e612e50a79944cb17f2923d32415e4a5dcf10bcfc53ea230c3e9846f28f47079
MD5 f2cf795d9f1336cde5f1601f97f8bc3a
BLAKE2b-256 15f9efd4ad5dc362bdd072002b65323c37526793027881eeeac4a5d984a7d7e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8561af8715abe3889da0520e2be7fa9749a6dbffd8f4affc422f70b8556d860c
MD5 455b88a085565e9faa2dff99369332a0
BLAKE2b-256 002e37db547f79e6f909c32d29e08ef9e5d69629d737eec47bf67c148cc8f9b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6177c5b0a1ef3a4a08538939d1225c8789d155c13583d3365cf683d705ebf833
MD5 e5ec35a72b0230b302381598c2c16088
BLAKE2b-256 bfdbbae21a831caefc687e8b9e3403a0ee0383e83ff4fc135a02eeb25c3867b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 33711f735176ffba11f8949b3fd604e63bd31011cc9feeefa4a04889223d445c
MD5 2bfcb512a0f2dce35f4f6e869bb117fc
BLAKE2b-256 a75c88a8127153c7e252a162d2163952529d802385132b83857292430e6ed63c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8cdaf8e6d66fe994475fe876ad515767c38ce0c332eaea1c522faec05f049cdf
MD5 6172ec9e955e6e36f77074ea44f80560
BLAKE2b-256 8fed62a35a062fba2ea3ec61b5ddf6d7e45e29cf90b88f99a1bf20d18502fb2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 20746e0e30ffe8f7a59bfa7b35a8f9f03146358178914163cb65e1eb1ba483b5
MD5 9b955c1625410a7a44ccdf7482ec422d
BLAKE2b-256 8c31894f2bb4fd1b0892c5906ae55401be46efef03bfecbb4baec77a8da682fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ff7774d2390ca0fab27d9b919683748b7405069db2f02246cf3417572ffcfaac
MD5 8c32673e9efa58c30a3696a216ffd7e4
BLAKE2b-256 fc7436d46a49a0966d8c3af6c128226629cab189afaac2003b557fdf8e944339

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 34cc6fa7fb6f0e71794414d387c4a2770be5a3195e6996534ca0a97e9d82ddde
MD5 aa29badc0a54f054afb8021a7eb1e958
BLAKE2b-256 b6f766e0492c15821be0c538c1afff34c88465adfc30a7ad6233e75ec7ede848

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 58ca34fbeaa0bedd6f27f5e9ee1cdf8a2878c27e144a1eee8df14c2ab33ac88d
MD5 45c19a944d9089d8bbc09aa581aa5282
BLAKE2b-256 ae07eac13a2c8cfba90fa08a9e466a23b14e722d8e044ff6b1e86b324a78dee4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e1ee7d07a6c7dff8812b2029c5f0514ad06d2c33a79deb1b26ed06e069b05ebb
MD5 e28e283676d6eb518d996da6f2d7e1c0
BLAKE2b-256 19f686ab0b4f45e90de6fc05ef30c4b71798a050123d12749f56712aff12bd57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 06142543c54bc3e5233cf44b2018cb7a6c8ec768db62a8ed7bb99c83d75f632d
MD5 ec73c4dc22b181cef40a47eb8bc1df52
BLAKE2b-256 4e86987be7eb57b5d3493b34e8f0a9d4e474d5b7cf6b61db9775315906f22ed1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.13-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 29b26f9f46edfb26e085f48aa7b083d0299526d5b64d518622598577efe2e2f5
MD5 c90ab854791ae0e7642774953a0dfc7f
BLAKE2b-256 1f3574b00964f5a326441f8ae81cb569e7094cddc83b9cae821f2df2b4a1dbf5

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