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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.6.2-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.6.2-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.6.2-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.6.2-cp312-cp312-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.6.2-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.6.2-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.6.2-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.6.2-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.6.2-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.6.2-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 9e9358f3a122f10410909634824005bb567dcbbc80a080969695543dec602c0b
MD5 11b78aeff2afe6d1142a9f492fc02aa5
BLAKE2b-256 c5e3a7624845fbcf4a67885540c45be5591b24cd1d3b312ed08f66ff662f14f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 69ced1278a58fddab758176b2cdbadab9341e43d494be97da5c747b7c3de43f9
MD5 1c25a3bb82fcddf758fe29297555335c
BLAKE2b-256 70d82351b5a805bb7dcbf58f64a8dd43b09dbfbfb828019aa0dff8e6facd5f4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 66e8d18bd38a41e56a1b4bbb4f5370e1dc10f5cfef30c6386f068d13b66b50a6
MD5 51df9164cfd3f8dd15eb29c4d15e5ffc
BLAKE2b-256 c73ceaaea969421b349d97d8249b0ec634bd059b30f46dc0d97f43962cbf226d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 19c73998f77908ab7b7834cf7a7539c8547bb92502da6c60e8bd75f269d2777a
MD5 033c77d0ab7796aadb2ca4d5413a232b
BLAKE2b-256 ba3ffb92eb9392a1b5ddf0fa9357b0a46ed12eda28242b04a8bcaeb7c323189a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e57de006bc18e205be332cf12bb44b62d307dcfefd5007441b62916fc5e049c6
MD5 03b4a0ea599a9097f9a87ba21381ef84
BLAKE2b-256 1715813c1b4d4fb004b7296a19a87639d0c41197887395f2745c5a1e7af8e636

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f06e0524053b9e7b38ac213e21166a38869d397c2260b9a6771c6a663f044fd0
MD5 f523a34b4f945b42c850907a37853d13
BLAKE2b-256 f654ffe8e94db2b57a7a64b88262295a662b65c863b5e061ef35937ebb3af5cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8dc5ebaa68fb6685903e6a7ae975bd729b684e87325623a6faf7ba5da4b61f7e
MD5 7f0a87e3de16e57e2c6d6c3b3b35ceae
BLAKE2b-256 8da873ffd01d546d6b2fa99f1f638aa1c13e5ec2237069902eeb448e2a1cf4f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 45094bcacbe239357c168c72a9c537c2c1c846c3045c6a97cef2fab14276240e
MD5 6b705143ef7c050faf718d198ad05638
BLAKE2b-256 9654032842a611238f53a4c23c8a5c6a55d7826345adaca0ebd33a22a3a3c3a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 125a6f307d7f1afe7b0cfbec9e7c95318772a88b232764061b1bde9efb5de543
MD5 29c6fb93daa9a27d06c1472beb37ed82
BLAKE2b-256 7b84f9f80688e14f15bcf682e85c922a1be1e4c04c28c4c767da22747f88deef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8749ec59608394b5324c3e359a8566b956cc3550b94b688d872b9950e2d6549a
MD5 e74d177d3384ae35ae768d4bbd3395d4
BLAKE2b-256 f2ff92812f60ec3dc8febf7a51ab808ddfa8f3bc0d03d14d48a6f1e2d4b2357e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4e22080bd0f23bc46662cbb785584eaa288ef2704975788634c61562367b9617
MD5 a188232479e5edc084bfd5af90ee03e3
BLAKE2b-256 66375c63eaa66dfc41ed407d2e22577b06bbdd36c48cea5568a7210e0e69336d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 56034be1499b0e5527470acc0a23fee4bc1ea5f62df9d3bceac2ff6e6f90b7e3
MD5 2329097b1b33d2f87daa76e33f6b11b4
BLAKE2b-256 09b848ddf4e6532c415dc748dfbded1aee7a7c5a3daa1cc0df23a83b4a82fa2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0189243b21913041b34af12cfe43006e05ae041a2e19e086c8d4a3218d2b4c01
MD5 4fa2ae407e7ed43b95761f8c427db3e3
BLAKE2b-256 57cba12760e7910d511b941f169a2b612519a8e9bb169072082b507cc84a03da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b06d2f211394d3ab5a9d1188fbfdfd70d7ca581a632220d4a94ebd3866770885
MD5 80bad8d018304c92c150f7bcf13e8863
BLAKE2b-256 bf839b264cb2da202a9ebd83fccfdf3cfb3a1506d2b207bfc6bcc5c6ced01ef3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.2-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 40553ee59c9b824490c360f09677a3024f7c66807f968ab935cd2860398c15f6
MD5 da0f0ed775a016b0cea4af2c2a2f5a57
BLAKE2b-256 be53bef55f4b8b64963ec1c79d1d27894ac654869c164dc43e3aa5d8ce98e41d

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