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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.14-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.14-cp313-cp313-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.14-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.14-cp312-cp312-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.14-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.14-cp311-cp311-win_amd64.whl (600.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.14-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.14-cp310-cp310-win_amd64.whl (598.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.14-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.14-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 cb7d4b517b07d9a2acada57230ba53ea9a0d2b1aa9b6f4e16b8dba17dd23e0bd
MD5 dec1d77ac05f1624bcee554eff1ccaba
BLAKE2b-256 148917a17e252b809dbaf9ae29e0be6c8ef737211ba20b449324e6869ef50ab2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 211e0e1d342abddef2b8885aae2a806dd74f3c18b285c8cf1798c08fe527ef0f
MD5 7a0be731f327edb5dca5992d0f99057f
BLAKE2b-256 cfd3ce3278040341bc8c101fcf1635eb594ac4a5c9b04c5451e459fa634cda44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6e5ac71003e70d666856c3caa17a790cd19c251b04fc49de9ee8901bce0e5240
MD5 dc212890f4cc172756a0ef0bbaf028ec
BLAKE2b-256 f6a8637f07ae21e980d22a3b195fb738afa354e8599bd1df923d3aba0c661414

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6bab6f9a4040dd0407110d9f555bbf67e497b59b2b7783033e658e65ea57f815
MD5 15b1c7c7ae7abb95baf870880798d6cc
BLAKE2b-256 f91f1557f210f2994912fb95d743ec55e09a3d9ff472ccc70b139a42ebd6c6c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2c5a2d9e0c58b999db7be5675806f048aefc3ec50590431ecfe8c062e83ce62c
MD5 59edab5a5155dea16fb73b8e4622e655
BLAKE2b-256 016bb98721ae2cdc0a3157cbb60d9d6a4e8d55f9532d636b2caa679bcd245ed9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 66f01ecc6231c5e05ab629e4fe449500173c680dbe96c7c7eb70db027a7e4260
MD5 048d15fb23b461c91372b4d3dd2d5f22
BLAKE2b-256 2b98d5b7b30a01beb8b671c3a55346abdeba4fdaf0cf2d0297575bca79307960

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 29dfc1c5c991c072e692a3ae1e90828ead706994eb1f408e34f8af721d606390
MD5 3ba024e79d026d2e16066de45ebb78ee
BLAKE2b-256 42229f53f6b076f4149e72a38c476170bce3f031457461d5d9884a12ebd4bc93

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8d07992377f1d44c66c040a9ed74a8b6ba61296581ab3ef2e0354787098d00a2
MD5 5dd60fd78e4e66cebc3edfb859424682
BLAKE2b-256 07322b8d625a43e2cd7d03084da83123d0ef90891cc27f8850789afc035fffb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 cd512e461a76eeb21617ffc972eb076a61fa3ab2db16eb49622d6127fde47666
MD5 afc2d45f0eb4237b75800a04b6fbc35f
BLAKE2b-256 9b871957125ecf9f77dc01be6088d2dc17dfb09fd8d7f541a52818079b66f6dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 68738aec2e32f33d7256556d12708bdd0504533c5e61aa2aeee9a62e31c59a9f
MD5 ceab67e28faf0442de2291e4a1802c3f
BLAKE2b-256 64fe637dc24136c457b8bf0f35fdc08068b4d9f82bfb7ee0fec5dd5f23355bc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d28ccd5fcda217b127d9749a77df2a93d83d72cbf9edde160bafbf49b32b313e
MD5 4bb8930809dd0a515cf4520364f0e9b7
BLAKE2b-256 9a97ce85ccef28ae20316d2ac032c76e9db343ab041fa9c227a8abf4f0a9008d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 077bf1220296b94ed0e3c8ed437846d8ee0ffdbf060b4f444621c6fe8e2c6c3a
MD5 2b50b7a59bc80ff6f22545869e4df266
BLAKE2b-256 5b3573db92581f18e8ceff7b6a7ea1d568fbd746ad7636721f29306bc5fb5eb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a5ed7a752fc6dd4925cb5911b5881e453c2e2391d937b9bd75e9e34f47c81fb0
MD5 c6e540d3b89c157eb1499d5d337b1b19
BLAKE2b-256 29dcf957407bd2883e356e01fedf4702e4b5b0f12b8646d22bf452c4edd75d14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ec8ac806fce33d38154f24bee6bb35a3b4278479bc8617a2ecbeaee00226c2eb
MD5 de8ed9f4f1e580d5215dfc8bc23c30f7
BLAKE2b-256 308b27836abcbddc8688e3cdc2640a571a438d48d9a91fa0152889834d372c9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.14-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 554f8bbbb45772a71a9410a70bf2756e800f1452e402d34f5c60c613cedcb572
MD5 a138c609952df8e8289fe992542c7d52
BLAKE2b-256 e74788e81509e345c9018984481115718be9810940780a50a66973660a774c6a

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