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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.18-cp314-cp314-macosx_15_0_universal2.whl (532.8 kB view details)

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

tensordict_nightly-2026.5.18-cp313-cp313-win_amd64.whl (603.3 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.18-cp313-cp313-macosx_15_0_universal2.whl (532.6 kB view details)

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

tensordict_nightly-2026.5.18-cp312-cp312-win_amd64.whl (603.3 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.18-cp312-cp312-macosx_15_0_universal2.whl (532.6 kB view details)

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

tensordict_nightly-2026.5.18-cp311-cp311-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.18-cp311-cp311-macosx_15_0_universal2.whl (531.7 kB view details)

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

tensordict_nightly-2026.5.18-cp310-cp310-win_amd64.whl (600.1 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.18-cp310-cp310-macosx_15_0_universal2.whl (530.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 757eeb75110ca6ff85c688beaf8546acfaa7f28b21c221daeb5d5937c94bf032
MD5 0aadaf25d1908fd49f220a4194e605e9
BLAKE2b-256 c5746cb106b54a17c71b6bf8394a8597c2efc3d89ffdc6d0e5537318066ee15b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 70eca17a565787c9ee706dbbfecdba0a8522c8c74c185b6f0b423ba4390085b1
MD5 729b37fe84b4e5c00ba86c283e6063bb
BLAKE2b-256 b16555f31aee7dd4ade6f57c8b5f4b48de708128449d8e1cb6fe3ca11e75bdaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d01f86301a64a2198703f07818f0bec763cb5003c6d056d71ce123be73b9d78f
MD5 6013b32dc3baa0bf298fa5e1fb265c4d
BLAKE2b-256 c1ea119f74e0391a43e36fb8e30c0b5ddbcebd538a6e611ba5309ba272c13ad3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 00651597d74a623c0ec2272851f53962a83cb9061f38a0525e67ce6005c7f8f3
MD5 e8efc635a47e5a86227e05aaec1d76d5
BLAKE2b-256 d16f117f86ac67373e6fc8ec6c66b814b6633cf0ec4a9afd7cabf2b1a02fab33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ba6601c2c57ae2df7d5259751736af311a3a257f132642b50b7cb391f7d829d8
MD5 2bde6da6fd8892dc1b8d4746d115e83d
BLAKE2b-256 8e7620b7529b2891bab461b74100ba687ed9322e945204733f2f2be264df8c5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f7cf0dbf7ec7cd1fd2e134cecdce60bce028ad7bc9c8e44836c18f635599121d
MD5 5d36994076e7c8cfc5b43607d9766a63
BLAKE2b-256 5938c2b71ecb8d18ec80111b06a2feac71ea98b1dce7d0ace91ff6d4e7b80060

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d899cd778bf2fb1b12015880352290a098e7ba4eb552e5c4bd40da8967dbfae2
MD5 938d8bf7e746dcf48576a87a7d8fb6d0
BLAKE2b-256 1cc96322d0c37351ed7e3fa51ddf023e0431508bc19668ca8b223a54a9cee945

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1dee02d5b89cc898f941ac6f46f0efea3215a0e9e3d1950b33a2d86fe8862500
MD5 239e50605e139d6efdf411e7b691a0d2
BLAKE2b-256 07bb770f265d30184d841af6da73455e640cd4b6613ff5a02d7f5773edd96c77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4a6080abcfe81abfcb4288b81bc205b341cb0bf90fcb4c17ec3e6eafa7a2ae79
MD5 fef0b8c1928f1ea5cc6abef8355b122b
BLAKE2b-256 cdbc5b82e4747aa732afdf41bb3509cb75a38d4256960183e7000e518a87f921

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 542345335f46c88a4abfc9b6dfb65844ee75569c3006f195f4bd0779554f9536
MD5 79b329fc57866e904eda21e023e814ac
BLAKE2b-256 3834d6a5334320a788c123229dcc5b3a0fe0a1593a69f14a7ea04491890c4c67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 95dbe817a2e1739117a8bb327211b67479048a5bef3fc32561a6dddd2dab9c1d
MD5 a9b495aab9eab07c1358d1243e21e641
BLAKE2b-256 0d5c16c7fb64f8851d6a03c526e67bcd2892db7b2b4e4a3f3259126b8ef5bd31

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b884594d10766af5578fa6c6bd8fe2d7da44593fa5a50921f3f49ae73dcaa985
MD5 7c86d335f1ac32952806bc2880308186
BLAKE2b-256 41d389c45d4d808aa50ba0327046fda895644eeb6eddf661f1dbff9b83b882fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4a29fb7d8927bf2c91abb10823195d202639db851e741517426439b2debd954b
MD5 a98b739a8f133ae19373a6f6326ff155
BLAKE2b-256 cd2087b520615a3408db51bf3c8480fe76425a0585d3b7a092eff60222619373

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f1fc3b888b43ea2db7c59c44b6be913e535797aee13fbcffcd690d86936a3015
MD5 2a30d94ad9af2acd0e886e03f4ad95ed
BLAKE2b-256 4ce717221cba144e738ffb7930abbf4b9dedbdfe1236bcbcca943194b7605d8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.18-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 33e7872069095b96cc360f6af40f4aae1d1e1dcf0545b406e26f37b50dfa3df7
MD5 c19473cd921985e028026d235a1a7432
BLAKE2b-256 4e47b02f2530e0bc8b6ada5fa54a2e924403fa104f9c609e96c21965c1ea43fa

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