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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.30-cp314-cp314-macosx_15_0_universal2.whl (529.3 kB view details)

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

tensordict_nightly-2026.4.30-cp313-cp313-win_amd64.whl (599.7 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.30-cp313-cp313-macosx_15_0_universal2.whl (529.1 kB view details)

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

tensordict_nightly-2026.4.30-cp312-cp312-win_amd64.whl (599.7 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.30-cp312-cp312-macosx_15_0_universal2.whl (529.1 kB view details)

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

tensordict_nightly-2026.4.30-cp311-cp311-win_amd64.whl (598.4 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.30-cp311-cp311-macosx_15_0_universal2.whl (528.2 kB view details)

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

tensordict_nightly-2026.4.30-cp310-cp310-win_amd64.whl (596.5 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.30-cp310-cp310-macosx_15_0_universal2.whl (526.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 786f4ba5f4d91952c486d756443340924e1d266bafd5f0fab3202be0994fb4ec
MD5 b6824e77e8ebf4e602f8ba9920752894
BLAKE2b-256 bb57cb5665955fb1abf880ae48fa12b6a579b810b966f62cf19e24c37ad8b46a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cd7ce03e2a5db887c2e09a42156651717d56b6f9c1ec15cda7d6ff5035d1e0b8
MD5 1037a5505f9f679c6219e3e4bc667fb1
BLAKE2b-256 4055d96c081658535266dd0cf86d987154d22139a1ed027e91cadc10f95afd54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 05e3eeca0b111c18a76706d1b6299525657ed43de7807d8b9a21de39752cac11
MD5 fc597256e41c67d5ff80f79b66081e44
BLAKE2b-256 e0fee6f7f2e035e25bd3cffc343503db48593552f5ea697880a7ee9ebfcad902

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2fb952cbc7bc43b58c44b47f2f707acedfeaefcfbe764a753172e9739845deeb
MD5 803171cc3cd78efa67a5ed5fcdcefdcc
BLAKE2b-256 81680167ff5303dd1f6ac82d3cbc97af4e33f7475dfe6007f09507e0853b5503

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 840c482089dd3780cbcd79f41ac9c053b8648f613af449235b4bd4555082566e
MD5 4726fa83cae060a48a0872ea1f0d011e
BLAKE2b-256 20214afdf2786adc53384fd2e88f947d2cb1393192dc106d0a877b9c6017e1d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5fa3ac593922edefc0385a1ef54703c62b8a036a480c4cc20292d8eaa334b77c
MD5 87cfa5922d408045aaa92c28efc956fa
BLAKE2b-256 450533e6a4390ec3340dab9dd71d52b8b5d6c2c6f358cc171b136cf6d8fbce14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 81715599c5b47251b8af574492202880fcb8c8793b95cae3d99e1dc640230f66
MD5 e67f856e43628bd923e39dda17155d70
BLAKE2b-256 9f93d03bc0dbb820a1f4d1a64c3224accbd100f0d1a9a3b98a8a88ac9b81aa81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f0db856dc18d55f95da0bc8ddbd2fa3a61e1e67fd41ec1dac973a8f9bb3208a0
MD5 db8f8080ff36a0e19a7a2b9055dc1a98
BLAKE2b-256 3ddb354c0102bfefa289873214cc4069adf28cd2b50388a78f8489389cafb0c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 73eda891e91501a90f305f96b93df1bf00cdf8a23037e895dcf7c28a2fb589d1
MD5 eaee2c8b2f67c8b1539e348f628f3053
BLAKE2b-256 62eea9d17feae8ce326ac1a19a63e6ecfd146249608193a381e6abbe8607e2e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 73d7ecbb965471e992bda58f2dfe83e4b5420dd23e24d57a9d30b6f1b0be32cd
MD5 8c015410b101b5b211ca7120e53b0e8f
BLAKE2b-256 2265aee528740cd01b200a1648d55e68af4e285a47f6deea3944b55799e59609

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b6b36851bb565ad6afd67ab2c255ae381ff17030187ddc12c6ad573ebbf669b9
MD5 b8f7affcdf34ce2f82bd9421292d47f8
BLAKE2b-256 e630f2a4ee76eb3c5e8af821f903412a0c102c677bbb6b6257e1475d377d34f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 77d4f67c438e1fc0625c44fb8fe31b4f1002e1bec0d613bc9efa3c7a37d381e9
MD5 898e91f22cd04da6e8de8b58ab414d0c
BLAKE2b-256 f9e21e16c312d63ef80f2acdfc3b255c4a1765bb3598f47f78171889d057d1aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2cb74b780cc3f33d67844ca8671ecb2071b8f5e9c4095158af29e8512ccf4f90
MD5 a309dccd63fb910ebe84adf869b8f766
BLAKE2b-256 c866e6a7bfe7d39ebfce83498977a2a1a7c529c3bde6f281fe1a4339e51abc30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 db8a3aea9bdd3bef1fa74fbb8eac552d0f24edb8ea755f2a272b13cef719c72a
MD5 5e2f29f406c7d9b10ee815eee1ee2155
BLAKE2b-256 4509b184c03f400b51232d8fd21460ed714225aa9bd9ec3843bcf606140998ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.30-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ce616572305603e34b808a9cce097b933c315d4d3edec1a2592561c8f743cc09
MD5 d8dafe4de628491982d23970aa788017
BLAKE2b-256 5c1eb778ee5345ad873d758f5bf27e0880a5bd694b9fe09005285dfa563d6ad2

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