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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.6-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.6-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 545d688320072c757f6232b9a616bde2910491562cf299eb47c0f1f60a27b882
MD5 97195d6f69b49700b7a39a7d6a6c40b3
BLAKE2b-256 a8bb50498a41315febd356c457c0fe8d12b58cded544899f5648d67e2893ddc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e58e871c3816c2c395f3619fd08a33f9440f8e11f85beddd70c74fa13539c717
MD5 139b2f7973b2036a4dbf59c8dcf5e141
BLAKE2b-256 90b03f75d9d752532a4ec9ac63dae92bff89bb72099ae709795cdc034327dc72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 776e187d8b3072f105581fbe1a36b68bfd3c9be86a1aa12a72b7fb22cf3c0529
MD5 143f4995792157356444cb92cd974e4f
BLAKE2b-256 d03815cdcc418c5acf082c90f5ee51cf3c9c28078e48508cb02d7ed5b262bad5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 9aa168817448ec7990ea6820d53a1ab9ba7f3eaf6c8007c273da0b2afda0ef19
MD5 407e79773cc6728d8cdb32cac4b75b8d
BLAKE2b-256 7b93227c92f7db30756dd7b61f263c99d48e5a6061b2b0a30aa87cf5b30d1c5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 66a56396e77f7a5affb54cc49bcb8e84fdd335b4e3c35580fe17defec869b458
MD5 4aaa8144d5800541a8f89df02cc2c564
BLAKE2b-256 e7a85826c1903afcc3c526f02ca021b57c0e04acdf2e4b999b19fd8104e92b57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ce2348c9bcd5412f5b115b70f7ece6674a234ad6b8a0d5c8be039a30c73271a2
MD5 582ebbcdc57ee948b7ce987d04c47285
BLAKE2b-256 7961ecedfd2ec9d73f1da5477735d841ce6ca7c494f82cbf362fbbfd1c6022a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 73705bebc2aa5faa52ca0fe574350fb182ed7ec6c429780a5080d88e7df6852b
MD5 2fb4d284cda9a0018c4d1701f11424a9
BLAKE2b-256 92e58d06d9648559f2d18641f7bb7c2ab06e866e1ad2554c538defff4e37bb75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 caa761b2cf1c857c986195335fa2b8de93aca273b1c371ac1741723ca778a3b2
MD5 5cdf10a8db1fe6bf9b0e673881349f4b
BLAKE2b-256 41b89b5547af858d6392a77ac04e0716550c23eba078ff515ea0152fe6629540

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5d57e4e98d4d4b770b781cb7310d1e4626a65dc4eea3990bff4cc37f367a3789
MD5 959efc1460a50424e3dcd554c1c8eeb9
BLAKE2b-256 18c251f3bf772dba6bbac6fa71b34f733c7121759f33b88b6362c25f6694ffa2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e6b08a355cdd2dd619001a9ebf4fcd482700cf41b2da88c9e2462c2e06404738
MD5 34b3093dcee03f557b528199a0050372
BLAKE2b-256 f56cf2e568cd316cf1ec2ab83ca128abe4ce89f094dfc4232c17e33180b4bf67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 872b8d8d7eef37cfbe7662b4ead21df37e354175b3066c757eb28456db8fdbf8
MD5 e8545808c11d6b2951b82dd206c000aa
BLAKE2b-256 365d3a6dd0d4c952f425c3834a13bbfdbc9c62a74448529143060a4c44887038

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4d4cae7b13b9915fbfb0bab9eb9af517f53ca5bacebff11c91689a95f8574fad
MD5 c042c5e8f604d62d4637f129590a80de
BLAKE2b-256 a01a664db762a0dd06778028d60cf725a279640c617dd4f664cd76d4471cf469

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 577f009c7f7287a0a935628a9f76b5af35a63a2d01ec55229e060b331996f7c6
MD5 53ab38cbe9f3e8b4d98e094cd5fa4329
BLAKE2b-256 637a1dabbcffe513505d15a2e47d979ba203a232ea09a69c928b48138f1dadc8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1d96ef8295446cc9dbf699a4ec16204e2d391d1d156ec82ddb5a0756b8b482fa
MD5 bf2103ae8d980d82e0dc24808f128324
BLAKE2b-256 77de4002d62be5bc84564c24758be1881fdeca0e4f36eb5a11174d55e678f3fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.6-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e587907965ae1e30e799e2a7f36c0349c4033adcfa0dfb05d8d9fb11baaf20a3
MD5 3f7e034773daf8c8c81b316a61c6ad15
BLAKE2b-256 dcc368dca0c246d83cd8b30df43f5eb42b43fd48f355487fb425121443d37391

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