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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.14-cp314-cp314-macosx_15_0_universal2.whl (520.2 kB view details)

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

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.14-cp310-cp310-macosx_15_0_universal2.whl (517.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 555347e20cd4223557777913c09f1937c2ca17c5781739343728defa3356f076
MD5 25ebc811e98c2265a3390dd52b6c7d71
BLAKE2b-256 30b500fc568021ade782ce37c2e5688851ecf46089449c24fa0293266483ce0c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 95a3b995e3551f960eb5b4ba5c7adb1e0195debc2c04917d6c8f2d4d00ed37d5
MD5 72b22a9575c5b8307bc85bb2ef5315d0
BLAKE2b-256 169312d0ee7504a65de3434a5317118cb17b31783c24bb9a8aa46519b5946bee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6f020dda0ef5e304571e880302f7c8aeb6cbe9c48961c8b27ebbabc4c48258a8
MD5 1a5c0b129f178577690a4ef4b70d3338
BLAKE2b-256 8f442d9c4c271d7fd70415b16b3f453343c1e91b8f02572737a137cd221834db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6cccc386ae74661c4cf22849abe990b38d4d83c7f40bf1a57ecbf1ac859c9aa6
MD5 7d0fee554f67a3d2b3f548d1e9e76c46
BLAKE2b-256 1136a29297e39a750e680ee81c5a5bdec452fe12be55980b983b44669b420913

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2ca7e6192955a3803a0d81e4408e50b1f7e83c9f0c9b2ed5e92f8a24bdddb3ef
MD5 1f2818984def651860c130796ee57ac3
BLAKE2b-256 1be96cc74a85e32fb31ddf13415bccf523106d0690cada8eeed9fd6a65dfe7b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5f3eaf34d8d2164b9ff78a6d3a9bb78dcd5caf48ea6e3890d3d52ea10af312c8
MD5 c63ffb077139793c0d8bb5c18420bc94
BLAKE2b-256 d488b51f7b8463dfdd169f89682f8aa6328a298659bf11eb9e9f99342f683a17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c2a760727e747686bfea5cfed84860ac46ec85cc2946f9e5f7f35bc748898a0c
MD5 ee695184449b18a732eeea7e0a89f1ca
BLAKE2b-256 bca471eb707f0bf9bde928938976752ad6b1c6ec0394bd44b709bbce918b8c80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 38a457584503b44f98bb80913ab32df4843e687ded0b73f5bfb168cc1626d2e0
MD5 f02b316f5e6bf53983b9295c737c9ff1
BLAKE2b-256 f2f1b5768c47e2319e3cc029277b090ed5e0dbe572680990811a6978ae0b0599

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 0584f8fd2de490976d80335bfb827ff9396aed61442850dc1101d67713172d2a
MD5 62dbf00677e597fc54700b12cd2f9c79
BLAKE2b-256 bcec98a794291ee125eab9e966219815992fd63217091b18b27b4d51239d642e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c4b75af56d8e2591ea0c1b1497fc0f2a031e3bcdca0a94189c05e0102967cac5
MD5 64618e156b8450ba627cf3c2876a8864
BLAKE2b-256 7dcee7276f94c9bbb9fb5de68d7b15e52644fe205d68f81da0243c19f6923bc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c458196c9622e0fe8bdd4b87794b9624c17242d8e9e0dba930e5256a203f2beb
MD5 3e55d33be9d5368e89578fe1b073bc87
BLAKE2b-256 1db56203a23b370e35e442350bc563f58985089fd59a62310dd8db19f3083f42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9cd2e691123609fed01acabda6eb1c5536178edcb844157ff63d8c3a803f2a5b
MD5 d5773e7ab715ce1963fc85f366ca28d4
BLAKE2b-256 999bdc1f8640d386f41865142505ea89e7bbf55deaf6a4d8d629a8f4f05342f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 dd2df5ca359eeac7f474d3152ed210ee9d5a4d0f8a07b2719d460892ccdd00a9
MD5 5aaba6f9460b3478ba3050cd6f742b86
BLAKE2b-256 1787891a19ea27b6e503d3a912b1ece60be18fc981d037e4a6096fab8beae180

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 290e5a9f5465069d53ea7c122f96f48ce6818efd134c00c6794a2ba1bda2f764
MD5 714219a6634b033f675de31bf8fef328
BLAKE2b-256 7cb1bd7c5009e665951316036ec1d4b24d7216ef9d3fac0826ccabaf9e90632a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.14-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b55930b9dcabc8d490e1b3e3af519ea6de8c896a5dca1a4dd2d74d05c3196b82
MD5 3639c6e28290bd258b318126923a23ec
BLAKE2b-256 ccf7b7fb61a4d069fcf16640db866ad91561cb3bae3f07efd56d6d08a75ccdc4

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