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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.17-cp314-cp314-macosx_15_0_universal2.whl (532.8 kB view details)

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

tensordict_nightly-2026.5.17-cp313-cp313-win_amd64.whl (603.3 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.17-cp313-cp313-macosx_15_0_universal2.whl (532.6 kB view details)

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

tensordict_nightly-2026.5.17-cp312-cp312-win_amd64.whl (603.3 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.17-cp312-cp312-macosx_15_0_universal2.whl (532.6 kB view details)

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

tensordict_nightly-2026.5.17-cp311-cp311-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.17-cp311-cp311-macosx_15_0_universal2.whl (531.7 kB view details)

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

tensordict_nightly-2026.5.17-cp310-cp310-win_amd64.whl (600.1 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.17-cp310-cp310-macosx_15_0_universal2.whl (530.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 3bd6292d33215c64f53edadf694c4d88486c3f9bf410e52cb5459c5a0a841301
MD5 6a7c7e488640a5dd5587291ec7776424
BLAKE2b-256 85629399fcafe9cbb445f6dd1f639d75b7327dc5d62897e6bd510aa3f0e3594e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e72af2330ff23ebbbd61c635e5ab3d7d0f90ce12730839e692b495db1a8b1a04
MD5 f494b1e04b46af16d77d937dd60ebbb2
BLAKE2b-256 c480b5b625ea31dac62ae680355a33b09d15c54621853f19d3e5da9428bc7295

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 56a0de66782d22bf273ac526cc39b0c65965dd4b75a5e65768abf386f09099f3
MD5 326edcb377f3bcfb1385bd872d92314e
BLAKE2b-256 83aa3d773c934fdd643da8e8b3f6bd71f3c584c7b0b20285174cf4572b71b8e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7d30c5be20313123622b4c703ed8a9c2d160b79c55ba6a0436db8d7cdf2302a5
MD5 eb80173808706ee6c6370a47e4ea0280
BLAKE2b-256 1438529be33f79df0f099c9e5bbe6fb6f2dc1f11f41f370a87a74acb2a1acd6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4c4f11b74c9627a68eee3deb3907bfa803616cc05a4f7ec2dc45a08cf21b631e
MD5 06cf3d6ac7da55a4192e3cf3c99ac3ae
BLAKE2b-256 017d23a3afbe3cd0d148a5441c076470469a3e7c107a5a2ab13b0ac3d8c509a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ba62ef6e7b897bd21ae3da8e39d9fcd958dfd44f4bffd3c739ebee38e9492040
MD5 d8207fdac0c7902dc5f744547d8cd5d2
BLAKE2b-256 db3cecccc3e72a94680c0a175338f19ab13d4dcd317e20dd73c61a1bb8411f4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 fcabff9d79a598f8af6e9c7f395ae31de15ac69bbd76923eb2a2c805643ef5b8
MD5 70875e6bf0fda04c66141844a30d4f6c
BLAKE2b-256 703d477f538e58849fcc8813174137b79bb69294e8b7b850a9e39005637eb4b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4914e8b65f3b702c6982722a3e333e5b9442d8e15d0a8aa1512300b04cb4c24f
MD5 6667573704453e685a49917119a8ed95
BLAKE2b-256 6bd750e5feb0447ac1e2579a82e19289aecc1eb96d17a72fe2a21efbc724d092

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4a210aeb1732b28d126bd7d311ff5582d45f0fecdf39cd6e7fae731a1afe2893
MD5 bdec2a0bab9697e13dfa8d3b4949b421
BLAKE2b-256 d03e498116b1d363eec6d2d1e8b11f3c91c125bdad2adea5bdfd55b2523f3bb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c276cd60763b1b860516ee4984a7fc09a32c73c3d88f188dd1e2c491ad9c6b91
MD5 d069feae826511983c0e38e4740ff414
BLAKE2b-256 3cc6b0ebec09ffc9f6aceb8ac0af98058acb1ec5acc90956e32210e0789d79d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 aaca684bcdc4a23a53a3e72351b6100a23a72586fbfe3e4568ec153f789ed82b
MD5 e9d361cfc22563090dbb97ec98d8c759
BLAKE2b-256 a7bc43c66d46e29e019d9317a90b3e25fb0d51cf8a7563a39b8e811490ee2c94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 882bc538c25c0715ddd8ddff74a26d5580a5028bd14db673a7eab1696eebe2e1
MD5 95238e4bba304a49e4f5767d68c2a250
BLAKE2b-256 41f93df399413fc02c725ad4baa50817c765acdc4a24d4cbf920883edb472b18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 dfd87fe18ead4034cfbf46177bd80f61a5c5230c16610c188c982114d22b2b00
MD5 0ef7715ec3a38ab5a4774bc5923d9cb5
BLAKE2b-256 2f5daac5a9b50a44fc682a9007b4db4eb007b29d1601cd61363d2c08ddbc05cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7f53935809335c0ec371187cd2c11194e18d9773effa1c483ed6498e2f856870
MD5 e591879f538fdcc33740f038e4f44a0c
BLAKE2b-256 142a8a0e87ddaede0915f12645906815999c9e063bbc4e562ec701df267b932c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.17-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c829800fa327e93ff60e612493b75349a6e24e9a308306880c81276e9e76ecfc
MD5 dc44882756fa5d2ed1a2d7baa596be6e
BLAKE2b-256 10b83d03c19e4ef24e6e039a63d9406de8e586369d01b9ac4632d17caba7d3a5

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