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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.11-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.11-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ed2fa5e468d30df9766c01435487944c913cd6e2fdf0ebbd0346d5f88e612e78
MD5 518a159dc92a69e10fb1b222057d3184
BLAKE2b-256 6ac8a20c031cf643cccd92cf2119f6d2c11cf293c70ae4f5a208db50a4660886

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 af7ac4f34045071f96b89c66f57053172ff62db3adcfacbb87cc0f30e66f6ea8
MD5 8e391beb049c179db57c0b7fb03ac21c
BLAKE2b-256 985b12029dce871aed16a36013f50bd333a759ecb6765f7f36b8f773ed229f46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 41da886b5ed850f804b8b4f26cb2884414244f1c2bd3e18604117f3c54d0da24
MD5 3f5699b8b8c80b47ee3606d193fd1cfe
BLAKE2b-256 43233ed3f6b12b7bce59bf3ce5fdf84c510606dd8b5927c8c1b411408f5f40b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ed177792d8301ced2d85b6a3fbe44ba64eccaf2308e9ade8cc3a994c7410ef32
MD5 f59163f0395ee8832aac358a50fdf258
BLAKE2b-256 13296cec5013d640c748d4c41224b54fce9d3b63b98885dc05d3a74128a4b9b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8ae3542ae39e5bb38ffd08c3c0b525700a0e8b69e64900c063f1a217cd388ad1
MD5 5286a2043e50642774701ea600105209
BLAKE2b-256 6849406f535ddbe35eaa1e05f56ef77ea0624ba495a8084c8f872584de4d3cf6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c7854458c65b340ca75a6620e9e0be501db34e488f5b440ba40338fd7e67bc71
MD5 c1ec92d9613ac42d183e9bbc0f6994c6
BLAKE2b-256 3af48fa60ced1c747ca560a9e0ef910d6bfd509a4f18c8d8deba8d4644aad7e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1ce11cdbfcfd2a94faa0c520418632e7a1b831fdea3a5f9aadb3696fd508566b
MD5 be2e0f7f3d83a854444c8128b8dd247f
BLAKE2b-256 653755b700aeb823a9e369985cde225f6195fad02c7df98950b03c31e78bb853

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ffb6966ad60eaf3d3bcb7d0472c69df6276e7666b1335c0bdcc3e8a0a2545405
MD5 483ff89aac258bba6c199d3c5aa06e6f
BLAKE2b-256 f12b58132a5d363e43b6852ecb279c4317dff2db029f89b9c0eb368632eb3ac7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3bc769af32e412705014f914b51043029d621d57cc685ef4b4e564501537eca0
MD5 7df9fdf29086c08094ef675dc3bf493a
BLAKE2b-256 05c34c5ef37200753a82c552f97be79d86c808f4cec66c4835dd9da28fab8d7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 11e34ac5c4e2eb31817996c5e194bea2c5aa9ce15b8cabb1eab309801ed1bc0b
MD5 c2eafd90fb32abd837cd61669041ea6c
BLAKE2b-256 6f84db038693e6d0360465e8366f19b79866105006d73ba6952eb5f676251f44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4df32ae048d51882850921c1f216d7f6ef690490c133a49ac4ab21d6b7e80268
MD5 dacf858578a7468a443c26271a7724e7
BLAKE2b-256 e09b69e2b4ed0c08f3cfd25993e85fd35bd2b27f17522976a49c0738933b8df3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f0898b2aa42da50a687e407d7dc0ca1e5b8c41ee6e0f4752e5149167373b3ff7
MD5 19ce362fc4f5a9d4ea12c8c0c27e003c
BLAKE2b-256 805d406bd6a750933a6ab6c09a12c5d737030697ca0d59ca1f6e7b18e4f5a2b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 854eaf2fa67906c0cd9372c3015f340aa2eff5d4c0c50046e7e1bcd88b68766f
MD5 9785c3fe47eb2706f904c2db71fc9db3
BLAKE2b-256 dd5426d0160b9357371e39b86364a68eb6f5ca5c114f3f6668d2e794b665c570

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c41ea269402c2cf047a5110bfb6bb8b0327f9eeb5fd04c3fd0252e182366da27
MD5 b4835ffd77fff634f77dff9c55b7d688
BLAKE2b-256 511796e6a96bd8bc65bca5f1b18397982de1e9256f649708d86f76ebf2276e20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.11-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 65a3650851160ddeb37edbadb8a8aa4cd7006d2b4a6bc859553c909aeae90f81
MD5 5c0f2ef90f637d3d3c3f97bcc411439d
BLAKE2b-256 8b02d1409f93014760347734dc6f4a04d01ad8f1b09d75c42dda7be49b962df9

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