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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.25-cp314-cp314-macosx_15_0_universal2.whl (540.3 kB view details)

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

tensordict_nightly-2026.5.25-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.25-cp313-cp313-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.5.25-cp312-cp312-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.25-cp312-cp312-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.5.25-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.25-cp311-cp311-macosx_15_0_universal2.whl (539.2 kB view details)

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

tensordict_nightly-2026.5.25-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.25-cp310-cp310-macosx_15_0_universal2.whl (537.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a21ec92e2eaa78fa9790d901be58f30b88710a6127818a1297bd21a718ac56d8
MD5 bb24760a83a27ac320194f12a3d97d6a
BLAKE2b-256 19ce315ee4a6a8b35f9df89a8b4ff5cbeaed188aa749da6ecf92df81e1d10363

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f8ef0a4d871a264a63707873d938fcd986460f15a9192f02f88ea450170456c6
MD5 5dc28133a72ff7f7dd76b5bf725c0195
BLAKE2b-256 962232dd9f8ca39378381adc63b310cfffb0cc16e6785b6cf9ad7491640d65a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5d4ce79f3b447035a9fb651c84c7a23c57daec2c38127a0c09dc2c57e3fc7c3e
MD5 0f43c6c5ae52ccb410ef12d608859bda
BLAKE2b-256 0c1bfaf9097d8e94854c8434419861d0d291788a50169403b10cd469f086ef80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 119c59ba0a92cb9caad0ce92e095e4ab033ec163f95b9b661e7174488c046a83
MD5 0728ea70c40528eb0b4fcd788a7e680b
BLAKE2b-256 11a4c3e3f74043f557968f05356d0f9dffcf96e79057e6e0dc4248251c81be7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e17d7c23b3c71e775866fce7b5ddf67b38597e3fb8267bfb0da4ccade7757eb1
MD5 012f2b1a3b7c9558204c9ce36bd3cada
BLAKE2b-256 30a2248b4fd6985e33659156ec6c21a4c2f279d5959a8e906f20c8f5fb4ddfc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e9aeff555f1031de05d3525a59455b1200b2e6f6976b7d88bb9171289bde030d
MD5 e4f1938d5b22406cf495153b21b60b3a
BLAKE2b-256 b9ed1bed08626aed162fac984332b09577fd2d21fa82dfe90a42a6d1fdd850d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0bf18fbad45172837dc9299068487ef9ee3df2e0435e5f88b96fb2b686f96b9b
MD5 e52ce57d4ce549948f03c9c440180a44
BLAKE2b-256 345db85eab20372ed227f4dc3ae9f61bc687756e24edbe74f956f07a122e6431

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1c994fb66d43a2df55303e947428ec874bbb969d5f2621d2077cf7d26ae345d1
MD5 300b40c93b695a2f3a978a28b437fe3f
BLAKE2b-256 87761f9e886d5c9f464b339205355d84f5a8ff549c9299dfe1726a24ed77fbd9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 43f55dddf38477fd33b8c7bfbd2fa7979c67d2acdaa8241599d62c278bd1a6bb
MD5 d33b9d3afa9e15dfad432ccf1b10c8b8
BLAKE2b-256 0aec8c533904e6f70245f2a1ce07fced83197317b0a745a94011b94e503d18f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 91a3cd96d07b149829a4c7b0b64b7bc6a4b715d66efe47fef26188fd1d32add7
MD5 4166d01cc21637ffa964a7be58c6954e
BLAKE2b-256 58ff2099bd560d72c29c863c43521fb41f332305c245fcafd75b1c010ea9a866

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b644d24f8bb392aba57d8be3303635b12d127230117426395648cebef3603528
MD5 88e0fd1112fa40336672f559ae61302c
BLAKE2b-256 309adbe739f9cd62cbc9fce7c1492b62a2c5485698d7668f32f46b2f4019faa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2ed3caffb7218148ba782418a9b22e22c04aa29f71c4933958e12e5b926aad93
MD5 cca9d61cc45ab610d195bbb1023f4975
BLAKE2b-256 997dbf1cd50994733f82d7fd200204beeebf975dbc3428b61d8cfe10815ff225

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 90f3a667c3e289437ca1a1c89d136dfc9e6562a8aeb1a7a4829cb9d4d45e7b95
MD5 e2d91291148d891c718e22a0f4ca5228
BLAKE2b-256 d59c000a7025d45e381a7283d7da75aebdce3da77b5777b6158b4b5a8242f23a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e769d37c178ae7c783e8ffd1cb45224e6fb6712f296ba368986f427047620f35
MD5 dc62eae7d62c2bbf014ca9b851728fd1
BLAKE2b-256 e4f3f6df1dda05c27045d2587165019c9d69e208ce732db0a53ddac200355cff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.25-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3ff67c8741a5719229b1976bd883c7b16d17e31abc8dbfbb7c74e612f052398a
MD5 39bb8e8fc42c0414c581da4a8a4fbdf8
BLAKE2b-256 2f49e546755227b4628a12713a4d63164a5b6c1a4ccb57ed4b65247af2af8b20

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