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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.25-cp314-cp314-macosx_15_0_universal2.whl (523.8 kB view details)

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

tensordict_nightly-2026.4.25-cp313-cp313-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.25-cp313-cp313-macosx_15_0_universal2.whl (523.7 kB view details)

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

tensordict_nightly-2026.4.25-cp312-cp312-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.25-cp312-cp312-macosx_15_0_universal2.whl (523.6 kB view details)

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

tensordict_nightly-2026.4.25-cp311-cp311-win_amd64.whl (592.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.25-cp311-cp311-macosx_15_0_universal2.whl (522.8 kB view details)

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

tensordict_nightly-2026.4.25-cp310-cp310-win_amd64.whl (591.0 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.25-cp310-cp310-macosx_15_0_universal2.whl (521.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 edd04396e5cc026e2936644da07aa88a8e53dac971dba2ab55f865dc966027d0
MD5 620bc49893b8ff3ec1006f07567b2354
BLAKE2b-256 1399a62a7bfc6bf441829e24930a52e3f0bf0a01e0dd54553ec4df1d4bceecdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f4bd5d17b6d72d8a1d4b822a128067957cb18817149d0f142cd1c33e01593d02
MD5 60c6a7bd586ca03018618917b995989f
BLAKE2b-256 292efd635fbaf32d53174029ac29a0c5f5d1e40406af1eddbde5870c5bee42ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 937ee5a2ec03c2690484cc97ffa821ba3555d29b5501da94b95ebb2d38195bfe
MD5 b85e4fc8f16d1426c3c2e9d560363a91
BLAKE2b-256 989e4ec8cff8837f2b5ab366e04d4416c3dc99a819e14b128ac71427b5570907

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 737c951a9ec4c1e9de4ae730560f5bc68e104683df24c71d44ba5667b71ad933
MD5 dbb48b431b836de3f692f4b7af86703d
BLAKE2b-256 210131787fa91611d0b555f318deba6fa356db7318979e5624f9a2eeda90c0b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e8d1f01e33ac47b0417bced7ee912ab810c0ba7f508d0e34ccccd2b0d4e6062a
MD5 886821761063702384987342b19db1a2
BLAKE2b-256 10bded42289da6bc2614632a4652f6be239e80578d9a4ba7d513a97392e61317

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 101173f7bdfc2308edc9015c06e3a6c6a78d9cdc9c63afdfa20a39ebe1890a6f
MD5 81ca3a5e94b35c46ded98946a47888df
BLAKE2b-256 5fcbcbbcd86cf8684e021a7abc26418fef265bdf3340504429ad020077c63cb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 790468f0b1110467714dd19e8df3f843bad54a795dc78705ae5711fe6ff80c48
MD5 1bba38b1b8fd10ed0f0b66aa8769be22
BLAKE2b-256 045d402c05ebd5c021f12bf29c5d6cf50c5771e927fddc2c103ca845b84a0f9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b13f7cab4f7c794fe73693c302199f546dc97fc7d85d20a1c2b11c91de2e233a
MD5 034668392549a600a9fbe31d0ba2d84c
BLAKE2b-256 4f6939df6d851c524a99c397b3117c5033ff5b0f032860cedb033de500b5fb6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3c4ef47ebe7eff85344f7dea9944f133a16b032b5e7cd4b008cbff7334b460fd
MD5 1178f1c6442f86e4a147449b5cf31a9c
BLAKE2b-256 b373f8ceadbd406fb62eb02d6bb5127cfa0c65b1fcc034af95124ad9aa40ecc7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6a39f806d8ae1fef0309fdc5b1154d8682dffa09eb1f2ba3f843bfa50e6188cc
MD5 fa7636e07b37f8e460be2b0ceff82991
BLAKE2b-256 0cb776c7e2a2a73d362cb4eccc4bd028096df556248ac01be4287b3c11637294

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8c3b06812f6dcff715c9ed746b3af090f9d05298b0ba9d573dc1863109b957e8
MD5 f835362d4b0db1e4923a54bce9820531
BLAKE2b-256 d36689d410f304af3da3c471b3ac18dc9be063d2ef48bdcb8cb6572df789d712

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 658c23fc998dd313afc6c6979540814099340a13c21957340df6a6f131dc473b
MD5 e78be983ee1eae1aa1c8ce8d4d61eb6e
BLAKE2b-256 17e03429b37a88b0bca7cb9be2e866139f02c1e9f8e3d85026d27c35a8dfc1c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5557d0ba8fe000668ccad3e9daff9fda551f36f39105ab840c73e4ed2087eaf6
MD5 292d61554fd0f48ab5d5449f9681825d
BLAKE2b-256 c987ace6629b9a38fb82ec750b3950abba4dd7e578aab6073563883ca72269a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 20c222d94ae33cfd94754687730ba90b3c66c053d2f94f758233fc685d5efac2
MD5 fb2592a66b19bcdbbe82f39e428739c0
BLAKE2b-256 a4a7e6718132de783e026f566a9eeceefa57ef5abe73b6809c4da8fcf58b71fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.25-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 fab48dfe623a0438fd55c32ac8229f6e979c8d104f34773c8aa7b2f687e7c78b
MD5 88063f16abe55c7b43881a5c44145e5a
BLAKE2b-256 4bf91ad4bd564373b7d18634383740848eb9f018732ff7aec31e1cc871320e85

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