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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.8-cp314-cp314-macosx_15_0_universal2.whl (520.1 kB view details)

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

tensordict_nightly-2026.4.8-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.8-cp313-cp313-macosx_15_0_universal2.whl (520.0 kB view details)

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

tensordict_nightly-2026.4.8-cp312-cp312-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.8-cp312-cp312-macosx_15_0_universal2.whl (519.9 kB view details)

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

tensordict_nightly-2026.4.8-cp311-cp311-win_amd64.whl (589.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.8-cp311-cp311-macosx_15_0_universal2.whl (519.1 kB view details)

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

tensordict_nightly-2026.4.8-cp310-cp310-win_amd64.whl (587.3 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.8-cp310-cp310-macosx_15_0_universal2.whl (517.7 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 1661c18e09255144c63eab797a8bf92062fc17c5415380ccb567edced717ba6c
MD5 c694c8b0be98515176d27ecaeefaffaa
BLAKE2b-256 5921b3ae7d0f0f54e90a290c4ec7af2e1e1e66a3471e6c92184fdcb0e3dc3ea4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 45fd0568de19a3fcb23c5f8032b53bce2fc91e8543f266a2454a13d1410eeabb
MD5 402c224eca2be7b9cc04be601124d642
BLAKE2b-256 85cef22ad2385ddec0481152b1da42891eaf69c27d38fd16cc7e521170d92117

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e37bb15d120f9046330086d9146c130d60f52072c77686eca4546ce29171a8ad
MD5 d6132fe57c3419a3ec51484c373925ec
BLAKE2b-256 fcfd9b9241aa31c78a2af9a34c1d78859b5e1d6ac0a2be748b579a7a33619d73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 60dbf8be7253c7cce10fb9f7297561d439aafe22867ffb8bac8028a2ccb4c599
MD5 2eeed1b61a969a3fa1079cde190859ee
BLAKE2b-256 fbb881ea35f39ca7b6a2a9cb52aae2dba76cf34aa7487619ff586e9b17294f8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3e75dd90ff1056ad09108a3e1f66b9a3f1ff2fe6caee6a44fa4f11c62bd1f2a1
MD5 3d0e8b61a34ff27cf8c6aeebf8366873
BLAKE2b-256 7735f017b881c2c9ed21467ef9038765bfa18e71cc58e9c665ca7408985fa144

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8f1b03b560fc672a74417ca135e9789cc7dee98d3aa6311cf6dcec3659ac4ef2
MD5 c587b1d7514d96d511261ce7ad832754
BLAKE2b-256 8d58ba9f5f6a55d46679cf7918309f65ac869ed581d0fd80ce2bb529e0b8fa96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 863259375fb6cedfca2148cbaecb0b9b84734a40d07823ada18177e9ebf9b994
MD5 3d99c25172dd66eaa4b460c3728c8e45
BLAKE2b-256 3662bc65d269e0f77894e7dfc3f1f1040d39091d6c1b81e4a039eb51b75d2278

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 058c85bda993a577a5ae0f86a862a7073185d770b0222642731e8eb47f55986e
MD5 b254a6818d1642a4149b3b991b37889f
BLAKE2b-256 f5ea96971dedaefb556a91e3b37dfccc422b2f333369b5066c33f520c9f4c9d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 bb3eac7230fcfeb0b6c75b9dd083f71cffd2cf6e781aeb9b9ca7dedd73a21599
MD5 fb0b8be9492881b318500f241b77e4e5
BLAKE2b-256 71b8a2c5ef8bc0351ec2411d8cdaafc437b1474c3cef2b13d97012691b7c7de9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8610527cacca4c5e59691872c7b014c7f6ee1ae4a81bf4ec833bfcb7f86f53a5
MD5 2a9332cc12191b38d627940355f07e16
BLAKE2b-256 16477aa9e31d3a163b6a65f64451c09d63f65f16486146c3e311a3d2b6f5f9cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6bfd8850cce877b2e8f7a491856af9acbbdabe7e10c85053222d0696a9811e0d
MD5 8d5376fb55a341e329e6b253ff954b82
BLAKE2b-256 f9d33e88b0d12fd3dc27b49a3a8fbb5e72f1c812ab61c7395225c00378aa21b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 88404ee71f318ef3d382ac74f59ae75b965160a43c84f3c7affce3cd6db6a880
MD5 0dbf30068c300dafa0a2306cd5f22654
BLAKE2b-256 24520482fdb06e76f583d8ca9b0e69ff2c2d6290926d92475786023ec65449eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 69e472a7628fd8e1d6c81c77bd988d2ea6ae5a70423bcccdd3d4b741a372a714
MD5 0a05e46cd186e0ce90538bc1658adb48
BLAKE2b-256 4d6ebcd32d442a9aacf4850186c8f64dd15dce4555cd77227fcc008111bbe7ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 aa4ac8912c660aa04d00c5ac392ad07aaf028db9f7b06776db8797b15d259795
MD5 2b69c98bb01b9f4c7139d79499f0ae1b
BLAKE2b-256 0b02e0d0932892717a512ec29b22d2e81848cc72c73d795af0415ee5bbed1822

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.8-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6f1c0754b7ae4c67da0b402b2154fecb01aa0885e2d9818d8ad87f3ebb591fe3
MD5 7504af461418c6ee934d4ddd56f961d8
BLAKE2b-256 13d00fe63ce8520e12a46f237bcbbfe848831c35c21fbad06e8e4a4662e9882e

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