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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.19-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.19-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.19-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.19-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 7e4cd779c0c9d4cc426010327ba8519052c17729a3ed5c5401da81669e45e97c
MD5 b9597c42201e01257a6ee7377e48f73e
BLAKE2b-256 0a511e6e6a32af36d174907d124b451c29946410aa0ade695c2815ac0b00eb4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2a49870ae29ed233c72bc1afb253e5f0edf60ae64ce5841625e5ea9c598bc07d
MD5 b6a62a09b065609ffee402cd9d37b66f
BLAKE2b-256 12671412bc8b4d1204abf066286c6d308aba44c2ee622f63c98b315e74a3f8d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 0dc412543076a0d598d3e5b0713e6b13e653dda10caaf3ebcecc4d08f1d37f74
MD5 ddf24e8f45e54bcd87315931f9b35bbc
BLAKE2b-256 0579e14c4b67b1d3d8658bc8e40b17ba158d2a77d4d82d8f982f0838cbdd8bb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 64e3e9610d35916b5d2472a9e6d3e647a0f7f6a6fcbf83bdd5a34a7e951d9b33
MD5 b8b5f9b2a7776935cd79cbafb43c4e9c
BLAKE2b-256 1a6012b4b99e8def489f2ecf37b54a846e868d172b2e63549914a29e807df800

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e9d211328d970fff10783e22a2ad4538b609771255d68d4652ebe2afe4770c1e
MD5 8875727a7b3dd5f16b9bd249de736dd4
BLAKE2b-256 9f547da04b1f44ef21136e14f18c3afcb940236f38eff3e0abf59ff30c965031

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 af7360101312ce103667e599b6a262f466d0f88fde3855462acde9756ff5eae4
MD5 6a7d5f447523c9d9dc6edb1c67ffb7ca
BLAKE2b-256 b228613f63959459602a5019a5af4bc34a6817838972aef3a67ac9b9fa97f8a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f9c1c04d601c1ae23e97f3146069aa9c663c5cad54f2fa02110e2c9fa2294062
MD5 d5f634ab8bd87a65f2c3e225b10c0ad4
BLAKE2b-256 95d8c61fa5baa8f0db35d1208a31e78e0dc8ccb5594197e27cdb5a8d9bbf7982

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9ecbe001d0039a0544244b3a74f43f1bd6d217d36cbb98d434f53887ad35a80d
MD5 2deb844aab03769e2605c65964e12b0a
BLAKE2b-256 08eae8670fe0d1c7f1ec3121ee9142509028fe5ce44dd530c4c760d4efe4ac8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 84373de1f6e4e440c3757d0c2447acfb66a9e6e3016163a3577a44c79ce5051c
MD5 ae8cf51bd664392ddaf6a6fce6690649
BLAKE2b-256 ac42b86dee5ba0f1c11302c1d299d9c01ab3b69cbd9b35e19f456c651ba2c5fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f1f832cd9f9b7f41690920fc58481017b825b744e4b085ceb9f1c4fa06d87c5f
MD5 226d03b4ce00d311b02471935d38872b
BLAKE2b-256 b28ad8276013a61735b300f5cd2db611361b15b2e1060cbc60e6768a23f7da40

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c960dc49686342c42ad1c7c9fe135b3393d1c6782a6470242371af16561d3877
MD5 48fc8185458b4b7414f27ff694737b15
BLAKE2b-256 70663e5379e64b62dbab68794b9ad93d2f050b2d975cb437e1a28b945939aa84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b2d1b18ad03dfbff06e6c91d5e045268cca4af48df7d174a71f6610dc1b2c0d3
MD5 7fbf8ce0d8aada738b28a99e8b3cf25b
BLAKE2b-256 2b441ad9e59cf6cd9181a58833bda68b15a0c07fcb01c44b16350b0d0a1ee5c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 96616451720c0f0dc27d732fcc53480879f494227a7dc190ac7870c20d0b39eb
MD5 b5d8894a2e8c4713f267831b906bc404
BLAKE2b-256 bd49c78379da26b37c444d5a87d6a6f536c08a0c23665fe5d1c371582439ec15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0abe7d59994348e141e37c0b3fc1be8a9377ede16e1e573986617928184136a4
MD5 ae00180c1f119ea0e50c021b1f23a02a
BLAKE2b-256 9575da07c194369bcc13760f9f12127fea7a5f87e032a4d500eb251a5208a957

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.19-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1aa50b345c151e9e4ea6402b779c6338c1f25fcac805c30fc7a20efd315408ba
MD5 19dc5e87f629507d7107dbdcd5c19678
BLAKE2b-256 41e11131221fd3e5d75076eced69e6420c49ab58dba196575bbd480f44134f61

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