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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.3.27-cp314-cp314-macosx_15_0_universal2.whl (519.6 kB view details)

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

tensordict_nightly-2026.3.27-cp313-cp313-win_amd64.whl (589.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.3.27-cp313-cp313-macosx_15_0_universal2.whl (519.5 kB view details)

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

tensordict_nightly-2026.3.27-cp312-cp312-win_amd64.whl (590.0 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.3.27-cp312-cp312-macosx_15_0_universal2.whl (519.5 kB view details)

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

tensordict_nightly-2026.3.27-cp311-cp311-win_amd64.whl (588.5 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.3.27-cp311-cp311-macosx_15_0_universal2.whl (518.5 kB view details)

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

tensordict_nightly-2026.3.27-cp310-cp310-win_amd64.whl (586.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.3.27-cp310-cp310-macosx_15_0_universal2.whl (517.3 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a5d25eedeb68be5d6812830e658e3bf0d2c134345e28d53e0517e2513eeb3380
MD5 400b6aa3627c2259bf0faf85e719cd31
BLAKE2b-256 6f03eb0ba78ef9f68dcdbd1561a0d35ef8a4e8a568b95d126099070b1525c069

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 24b3926de421cb78963146bfea12d5558fb77024ea85d243970f3c33c8218e7a
MD5 5e3b9b437422c58569ee3b0d48ff7875
BLAKE2b-256 dfc649ee15517dbddc7993457476cefe3eb3c15f2b0c2f7c72f9cd981ae39869

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d20e3b647df108cf4ae2c31ac5334c48256d256511374c2570e127b24812722a
MD5 ae688eec08f72cdcabb5070870ea08d2
BLAKE2b-256 60f1ac868815339b0fa9807a28465dc25bf4d59a820602512503a7a5737ea7ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 759c8723a6bb6c6a072b077ddbe545341067a193a3fb4e1e3d44fcfab60e1cf1
MD5 c9d00656c809fde5155073c5a0ee3160
BLAKE2b-256 89ee303fda8c2a0ed0e128dd2f9f620db4bedf865b8c0389099e0edf19cdba33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1212c2a6fecbadd75f2a642a7b90349b6b392b8a3e1f317705511d7b63922d5a
MD5 cf2e05907f5e6c2f8c137d3a0c7cad28
BLAKE2b-256 caea52937e085f158f85f80bc6af0617c8d4ac5659792e0f7e1f2cc5ff2ef657

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1744df82f3a2b615b10dcbad37639a27a284f195146de2e9f97af2993fe251e2
MD5 5200fd0cc11aca590365d2f2f2f46d03
BLAKE2b-256 234bd87f0601d32158dd2914ee63c8fbd53dfe3fcbac1c3cbd2011cb96da590c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 739a1538fd76829dcd972ed674e04d6e2ebd516168f3b984341a327ac5d6e182
MD5 defabd5edb37a758cc085f09340ab6b9
BLAKE2b-256 5e3763eed696f7e044688adee1657340384f855003b1c9f385a94b48f449b8a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b17fb34791169076dea08943e40eae049fadfca844f14f834401fce51443914b
MD5 798aef445d3735b518b2a4f4599dd00e
BLAKE2b-256 7a6a369de255030076f760054288f922c56641c9e1463a429c4ff103a015a583

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1222c26252ec2932ed7b7202c3ada16f6d15427e4c5485fdc7df895dba867fd5
MD5 86dc8db1b27a6d140958550ef1f3054e
BLAKE2b-256 211a294392c93320b6f3c6e066c2bd2134590cec7f3062112a572ace49e3a9dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a98ff52898a06840541748848892e4bd0ba7d525a38752b58d1318266b7a92cf
MD5 4125ca0e8e4b9b23b7bd2051bc2edef4
BLAKE2b-256 125887ea0b98d1325c53548228fbc7a359c404294f3b281e444c84a363cfe64a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d4f4d8282cdd5c781038d01f74d0dc7abc7530b518c2c95a3547a8d437c20def
MD5 730844fe9b2eb1904db08b38148289d5
BLAKE2b-256 cd5290ff68ade84a1c880d77ed82c380588445c44e93a2c692d3ae893c661695

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 243ab63d9fd23cc35a07398e33c51d624c1503a7d22924e6189e1b988a039097
MD5 83511be6a931034406753fe37bf71bb7
BLAKE2b-256 b30165cc9759c166972179ec0fa23794687211fdffa1c7fb7b5497b7b90baa4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d70740dcabc3cc8a3d42dcfc53456c21dd9e55d67189433520e2f1a985f19c92
MD5 721f3a9f812aca925d8e0cf6c61b2d07
BLAKE2b-256 9219aa38affbe93c2a3dfbf22998ca1ebbae07ce90946baf304fedb7f245e57a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8364157530fc3ce274857b47a697f1994dbddfdac8bb3d90df07ff3c3b70766c
MD5 e0e773650d451e88da264266d45138b9
BLAKE2b-256 2f96f334bcc4a4fb9bc78a41f73660eea27ec915d6d210df6b1242d04bda5447

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.27-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3f249e17b1899d9f1d514db53491547a534e741ee8ff92a95e06eb9348a721e1
MD5 8fada9441caed13dfff8c6d609ad355c
BLAKE2b-256 4e8c329c83402598e5f07a89d309512c429d283cc6e7a7662f581d811a79513d

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