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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.12-cp314-cp314-macosx_15_0_universal2.whl (531.6 kB view details)

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

tensordict_nightly-2026.5.12-cp313-cp313-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.12-cp313-cp313-macosx_15_0_universal2.whl (531.5 kB view details)

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

tensordict_nightly-2026.5.12-cp312-cp312-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.12-cp312-cp312-macosx_15_0_universal2.whl (531.4 kB view details)

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

tensordict_nightly-2026.5.12-cp311-cp311-win_amd64.whl (600.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.12-cp311-cp311-macosx_15_0_universal2.whl (530.6 kB view details)

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

tensordict_nightly-2026.5.12-cp310-cp310-win_amd64.whl (598.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.12-cp310-cp310-macosx_15_0_universal2.whl (529.2 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 bb01906ba05d6946037746823c958238050dbf06634ce09e5c896860a4a19f6c
MD5 77e60209669dccaed7f53b4f4cb9b35d
BLAKE2b-256 e0317820543cfd0aa2511bd1e54537f97372672e46721a542f6fe0ebb22ab151

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fa68082e622e86a07c81a760b999b5e476823815cea4dd19b36e379b106d8ac8
MD5 f9354cc08484007ed1277a00c041971a
BLAKE2b-256 69bf8a4c1d45f9e27b7e4294b7916bc04766cf6dcaa854919ba319f74e14479b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6d8a55a3427bdd0c4cdee62a92da9efb38175e9a2b8722a14fb54dfb736f0bbf
MD5 bcd4e4e3a058bc239c70e4e0202af53b
BLAKE2b-256 592ee61b31e9e3843f7d99a3d4a5cb9b2a3c8f6ba8d382a7ed5d7e7152458ac1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7f5d34c90b0582e011fc0f64152c9ee483e87c0fcf8c67d54cb419d8bcff0e09
MD5 c586a3ed08e07828168c209120f3d35f
BLAKE2b-256 7b25f84eb4049b02b5acc92002ed8fd6bfbceb7c37c27eae2cb4d69be12c61cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0a5811d7b7137028cd6d512f6c0fd6312a661b69c0b269197e6ca076e3e54466
MD5 bbe20e24a7d76c8524635f9b6f970c87
BLAKE2b-256 bf6720fc6806d5d703046401d8e024b179a8af076c5092f270088ca29ba30d63

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 776487f7fa0ae84767e2538433d8d2f33fdb9cb3f1f56b008abd78c7dc152743
MD5 8178b4cf572492a9d5cb69d0adea9de7
BLAKE2b-256 1b7eb9c49dfaa967ea84523c0324cc9395d8bd7cc9fe82eceb317128f0034bae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 992f7770b84bd52d47c1070d3d7c57dc8bb5c2e39e2513e4aba8b57adbde7c4e
MD5 920b99eb2e14b2abd7373ffe489d6136
BLAKE2b-256 c638f8141fe0c2a5e26239156a5daa282a373dffe0f876892bbc35ed98c63684

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0d70df7bdaf530e80ea0a98f42c2b7564c6c9389a517c8e4d68f9c25c0811ece
MD5 aa526ac61b425c1537383d2f2c86621e
BLAKE2b-256 598d863e7283a1a2f22ed42988c74e0e7d25eeec7cdfe469bb77ba99de161b46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9f3553f4b353c9d70308b72aee2f01a20859cc8417a1b1d676b5524fe10632da
MD5 dfdeb37bd7b22ba5bbf47a56882f6296
BLAKE2b-256 eb69822b86f0b3c0cd958286f8b3d454a3a25e7932726ea7b88a39ce0fbb3dc6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 220b26f9ebd8690f2ce9d65464d73806011f31a0fb56f2f9cd159211d181d5b9
MD5 674512561cd7da4ed38b98e6f01b612a
BLAKE2b-256 8b67893ce1dc228cd2161217d0641b91012bd874904d125ab7982ae9a7b0f6fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6ab9ddd343dbdeaf3b62a5dd2c133057911e9b76fbbdb59dd10919b8eef28127
MD5 93956f45b40eb5da73ae3457623cd307
BLAKE2b-256 218d9a6d22e527e127243fac3decd27fc7fe2c68e3df405708f9cbd7ef7b234f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d2573274661bde47658927cb4aeac3a5f74411aaed967d6f9e7467f99841f65f
MD5 ce836fae74a64ca9c6a1aa7941a5c060
BLAKE2b-256 4789142e4274273461b08e51fbba77b90c2edf6a25748100fd8e68153b8d2ffb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b5b6299717e6bce3844cd30c7835bf2363e21e0b0aba235419cf8a458f0b2a27
MD5 0bed7911db17d4cc3c00b3be166e2e0f
BLAKE2b-256 3255e56cebc624b32e4c57e084e79917a097584646c775a17fda0a5f73df7e02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d4406b8c5df321004bb085ec5c5e82bb6daa8e1c9ebb3876d30b61c6a22ea874
MD5 907965340f7b1a57a28819d446785325
BLAKE2b-256 702a8a629b8f48d086ba1ccb83bd561fb2e363efe793c3d5165afd0fcaadc94f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.12-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 051209b69fa78ce5289f9017634bff2541da8a6baf42b87f4cb125470d2f1f4c
MD5 47e30f8756307a30aebeb848d166a3f3
BLAKE2b-256 dda98f1dd584fb004005b19f87cacb7bb6270df676ef6cc9a6c03b2936789e4f

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