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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.2-cp314-cp314-macosx_15_0_universal2.whl (520.1 kB view details)

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

tensordict_nightly-2026.4.2-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.2-cp310-cp310-macosx_15_0_universal2.whl (517.7 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 6a7410adfb71658db6a9f05d550b0ddd68c6f5b8b0781e9611dd189dff6e6437
MD5 aa45bb0d50fdb726b87ddb3236690036
BLAKE2b-256 23e8ad7d1d967e0441760e2e1f4a9ca279cc20cd6fe4fddc178ed1342e3a3b4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7b02908bf90f1465a5f5ab44ecde109761f3184a494cab93a56dd82eb3bf945a
MD5 fbea1c088dd353d8f9d29e34482b39ad
BLAKE2b-256 bdcb6edf3ed52541d81c1fc6b4cc8fac92bd7bc759c061b8030857f2f98c3cd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 fa6075e40a5f33751dcc825b0d3f7f24b236db8dad5f2c19bf7688f385e00cbb
MD5 c6c5b7230438ad5307a20c7c0bb5e893
BLAKE2b-256 142c03ea9191b596d8602169cf016decab4b96a30dafe4b2476e1506163a7fb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2f7c599b6234929161420ede409b919e49a7d05dca93808459d232bbece48766
MD5 f7d6ed9d0f08c00bf0aef8e50ad27937
BLAKE2b-256 9f077986ac5b2704e882846380d8211a1cfab6b2c6e90682b85a28478d42a4b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 578209574ee418f74c8c24a611d966f2b982446b4bad8fbf355dd5e063d37d2b
MD5 7dbfffe1f93196c7dceb9dda10f549a3
BLAKE2b-256 db42a2de7e579688909cfe9dfa1f9e6839284dd67a630fb9bafa7f0032fc5fde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 abe20a3262cd454e51890df037a2e6b00238cb8ec6ff86331636025a15ef9290
MD5 f4894baf5a7a33ed2a851587ce227a64
BLAKE2b-256 b0af045dd74f845a82ea71dccc836518a9a540a47c814bcb583b9eaf177a9fd9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a017c7a0799a27fa2521922b32b8deec61ffb94b4724646339a56313329e36de
MD5 2a5db6dd5d0bcba74be51a9ef7683e48
BLAKE2b-256 4c0317dc0806928c4944e05a1f2a1cd597a8f03a43e7289cdf3961abadea25d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f95eae2541df6f8c70681c54c8e879edc7edb16f9a80717a7a3b12e91c828042
MD5 06ce39542c1ed7963253f1e95df9733b
BLAKE2b-256 a350e1b14005a2b00e241da91d1b16e789a93cfffa36c8b6908a4d1f3edaec48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 0854fd9341094d4f0e2205cb0ce29c9b2bcac2cfaa14b703a1c6e81b0f3f54a4
MD5 f60298f3d0ed53d93fe78b7f6edbf3cb
BLAKE2b-256 1b5c5ee3c7d388bc3b65f076d61edee8ef34a0920b4953dd094121fc8d97f2a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b0085acda4a767699818de1784932e9c4dd84aad7ba3beb42e9fa4e412fc4647
MD5 f03ba7fd3f8be9060a208ccb91452ddf
BLAKE2b-256 26e9a1b8fa9f4d9fa26da855920e8ff7ed03d2aeb790acff6e09a93faadad788

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 187c8a1c881c85743d251272cbee016a111ed376f6550c8413cc59b802b89167
MD5 7f260d73459fbd31e6bc2c6ad1b5aeb3
BLAKE2b-256 f14d889d9c4e50b8e4ee02325323a8491ea367f50867c9adbb9377f9e05fb5e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 baef4486af9038dfbe58f05cda383295f8e1ffa535c78f5576d33f9614e3e0d9
MD5 949a32788a7bcbd52b4635722cf7261b
BLAKE2b-256 86dbb09baca3915dc0293bc30ec371f8e1b940c24cde67bde47941cd18f19930

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 877b9d2ba4bbe0245102f43871ae2f9f9547a62bf3178cc7d2157b463138e261
MD5 a8cf221196915dbbce5dcdd6859a0cdf
BLAKE2b-256 1fcbc4d27dca760ada3b84501874d83bacc7d8b7137ec3354e79c125f3ee35c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c8d31c7d0779d24bd31821a169f8b7a85913094f4ec4ab0e91a1476a8396d6f7
MD5 2215f3787df06efdcb4874564f91f886
BLAKE2b-256 aacf14abe3a4748b75baf3c8474d9320d3d134cbf0b819dc2fee06cd7f1867ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.2-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8e20818d2190ff4f361256cad654e15bacba9c2c2e9dc6bcb5bf7bb4865dd401
MD5 88d0acc77531f8efe0f4ad7ba9c3fefd
BLAKE2b-256 b70b621339ab3769c70865b8b845d05edc31c1e73a3a0f40c689870db9f4ec36

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