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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.24-cp314-cp314-macosx_15_0_universal2.whl (538.6 kB view details)

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

tensordict_nightly-2026.5.24-cp313-cp313-win_amd64.whl (609.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.24-cp313-cp313-macosx_15_0_universal2.whl (538.4 kB view details)

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

tensordict_nightly-2026.5.24-cp312-cp312-win_amd64.whl (609.1 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.24-cp312-cp312-macosx_15_0_universal2.whl (538.3 kB view details)

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

tensordict_nightly-2026.5.24-cp311-cp311-win_amd64.whl (607.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.24-cp311-cp311-macosx_15_0_universal2.whl (537.5 kB view details)

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

tensordict_nightly-2026.5.24-cp310-cp310-win_amd64.whl (605.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.24-cp310-cp310-macosx_15_0_universal2.whl (536.2 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a847d2845c5b3125ea9355f1ac8ecb46ca608f6ea148d7f69685b28e5df81ccd
MD5 acdfdbc8bec1d809c628cc21714578f4
BLAKE2b-256 a8e5e77ccec8e4b5c0ddee449b01d70e166001f0637bb560574d39dc9ed05175

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 568484691a5e4bf5ddcef40d9466a8ebb67036aa1d55eda6e949712e46bdeced
MD5 154e0e0f1de4e094ed69b45f6ad92858
BLAKE2b-256 b38dd3451831547a4525de2d4124c04a2860a3f727924762fa2e09ec099a3eb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f67111f58542f9f4337d897001dc7f78c702ea3b5cce51bb85a3176e55b18448
MD5 056295b600f77fd2a34b955c6dce16f5
BLAKE2b-256 e4b6f8b6d114450626318ebc1ede43f3405a99bddde605ed3588cb43f9808956

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fc7e85a8fdae95e6784e54ff16a0035c173b79a19fbd719f9a9bbf2540fe6be2
MD5 76975f08701cc5afa7f8599b2d1c8ae9
BLAKE2b-256 6d80fc0ef90c8ab466aed61b48500ce099bea8f1330ef93676a4bca17ffd69c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 43a892add7af0845549e2258fa3f0e2fe112b3adc50701119c095e7379efd6d5
MD5 e687f28b5b10af3f244e2cd55a1bce3e
BLAKE2b-256 99fd3d271800ea21495b15daecac3d0f2f2ea505afce5bcc7c8042f2c6824428

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ca20744a008ef60bb5df8f2cc14c4b37851aa185c35e38308b6826c17b23bcb1
MD5 290f4c95a94335ee9fdab36981d11ff6
BLAKE2b-256 b1d79acd9db782a230fe634f6f3a2d888066b484b051f6a997f5eb6eb0e10202

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 12086b657707f1eccb87e6a06e9e61d745ec9edac1d6ba76f2b71be9c66da907
MD5 a9baa81a5ba365bb4fde06b8c3b338a2
BLAKE2b-256 14fc255bce41c2a3f7b4dd52748ed878b0809c7b953a65861ddb60a43e38c050

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4b1d6557b3b9523cd7fc6db8d7566ed5ea22d6156d5cf208ff42debf2552cc68
MD5 12ac5f43d44208ec6e753d78f7d83655
BLAKE2b-256 bf7c76d27755256f89b09dfd60ae9e2fc779e9dc7554425b719557ad162824d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8212306af925a1b37e4d0564780775ff92a430c7cb6a07f1cbfcb8b0ee6ae257
MD5 e1e4f3d0e4f3a6ef7c32e51f80c9c84f
BLAKE2b-256 2c166f81971e40c14b02b48bd5e808e491f0b82795a0ed240e95d5572bdb9db7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 eedda4e8916c4628bc6ff23236e799453f1272031605a53bfea46c90487edcb1
MD5 c5ba6d08d95be9a9400d5f4929b1aa7b
BLAKE2b-256 6a93145ea0978cbf90967b987219938e9e3ed852bcdd4c615514cc2be35ab3da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 42186e766318e89237c683eb8e1cec358ae3bec1169b443ab33f25abce22b7c4
MD5 b1cacbb486c2e530eede1ec39377d91e
BLAKE2b-256 22f91dd599f5c3579331a84ce412a9b82f5868f74830f26fd29499caf2953c1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c77135b1692d54f0354017830aac9a22e3ebb9c69424cca6b7e7b2c565bd2860
MD5 6d9940258dbc5ecad177f5155c1aa458
BLAKE2b-256 d32db17a1fd8af9d6b6a83119e912befecfc290cd668ceee8ee11df1f0c778e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5a192f4bb91ce41a0c2ddc9ef3668848650335d2d7626234b67ad32c9f4f8afd
MD5 91b1abb32445bf9802ddff2d17660bd4
BLAKE2b-256 2073686da8d613b204b12f69261f345620f821e4ab9b5d16fdedd905641388b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6c5566def365e0e75f8ea39c2b65112789706ef73e439e61bd510646448499bf
MD5 0e32c5780e5a20f0aaa45cef05472239
BLAKE2b-256 d4368b04032b55be659091286ebc5a644a817280f753999989feef8b7a1e713e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.24-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b91ab7f1ca8cfb60e073ebbae46d11159c8715b5fb730120a812d0c375a14396
MD5 9649b843d165e39fe45aec48a2108341
BLAKE2b-256 0baa9229a7d7105dc84fb54e739cd9a0ef7e3b98ac3c15d1d116aa30e8469a26

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