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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.7-cp314-cp314-macosx_15_0_universal2.whl (529.0 kB view details)

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

tensordict_nightly-2026.5.7-cp313-cp313-win_amd64.whl (599.4 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.7-cp313-cp313-macosx_15_0_universal2.whl (528.8 kB view details)

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

tensordict_nightly-2026.5.7-cp312-cp312-win_amd64.whl (599.4 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.7-cp312-cp312-macosx_15_0_universal2.whl (528.8 kB view details)

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

tensordict_nightly-2026.5.7-cp311-cp311-macosx_15_0_universal2.whl (527.9 kB view details)

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

tensordict_nightly-2026.5.7-cp310-cp310-win_amd64.whl (596.2 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.7-cp310-cp310-macosx_15_0_universal2.whl (526.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 efd6495bcb19c6ed1b0509575426ccb7f53065040fd4fe90b3caa1be4b2e1f67
MD5 86c703a05cff10157926c031774b9e04
BLAKE2b-256 039fa22a4cbe74fdf4f4c9399bd9e46474d817e30c0ae53ab8b3b46e7465eb3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4e0e1e5ede9fc8dda9d6715553d780aa98bec5e150f6071eb72cc829d67e0146
MD5 d0b782f94ded01612033a0514fc55403
BLAKE2b-256 a2b080e320fdd77747a5204f872919e29a7e24c611ec419f2d8057cb835c647a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2ce5d32b45a50cd3ae54c0226fcee8ce06b72823ebc6b0ba08ae3fcaa2e1a26c
MD5 bea0179af3d639f9dd84d882ab15c9fb
BLAKE2b-256 dcb42c83ace41c4e3bb31ea6293bc969c8a6885dcc85dea463f1d83cc1bc52e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fe67737f750b2d32dcb282ca261ce55d7c486e54be7ba2134f312f5156f9ce67
MD5 26d0d488649f2058b5eba80ea69a655c
BLAKE2b-256 755d3281d0390dab56d456563fd96a444742b326e9e5a24ff258156bb254a870

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5aace09c28103894cddf4ea3f0e3beea5a48d171d0a791d5f8718e52104c3aa3
MD5 bed1d7568b89eebdd155cf3cfb7cb909
BLAKE2b-256 9796e981ccf7712167da38c125139e7f4a9dfeab80eef171448c5cc14153ff51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5bd16d0873c15d484414d3136dbedb2227c9bac1932e931ef2ff4ed0d5ba8474
MD5 17420d603f8a61655efa315da0340bf1
BLAKE2b-256 1d7ecd2055581cf342ef580af6ecd5cf912e4266bb55cd6531e3c932f65aac6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ca86ff7a4b4bb0c5a8e1664b1d3ffdabe79180b19878876d196cef9596d16cba
MD5 5cf3ac36a6f3bbdf0c57236eeb96cf42
BLAKE2b-256 f8fca72277630328194b0f51768ce1a76c60d3fa4d5a0a18b9a07f8c0fb0bef1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c12a0371e1c42ce199141b2449141bb6e42806ff4ff9e9bda2d7c30576794b5c
MD5 17438d7ebb38fcf99e3bef1ab6f80503
BLAKE2b-256 d3242941bf49850054a9aa3d5c32b1c985b8f44dae0a581b1259f4df6354efc7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b004e347ee4120efc70ad8125f6ebdaf524680139ebf4ec177080b31a1c9d1a4
MD5 3e75bb3a3b3a91e3128493f65c04aa85
BLAKE2b-256 5176f11de504b80a70f779fbbe0bb2724e0548963eb3a69f6465df05dc6b5733

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fa258e05e72a5b7ff647e95d0c4b063be4c240d234d9b4c4589d432c040124ea
MD5 52302846819617b2149d7c4d38ffe49a
BLAKE2b-256 4026178bb74822d80e669613a3a08de34449b261c8931773783a9794873690ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1df464dc4805b795bd325934a8268e824af45ec0c8509145f3c897c4721bd0f0
MD5 301dfb2012c0f4a47bea81495d368b2e
BLAKE2b-256 318a14672a38e1b5068bb7aeb063e15c22a15f1b18e3ccdf471b6f50e4070e65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7f4b57d898ec77b57ef9790a7c5f5453ed337fdd06fd0e33302979d0022ed423
MD5 b96be59e5c04f45a691d89b2db0a11e9
BLAKE2b-256 232516e9e666e804dd5cdb55050a50c9e770c766d3279ff2d7ab6efe2bf8bd0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a1a7258cd3a7fb7f1e88555a97e95e032b810fc4423fb6fad258b636bb67270f
MD5 287edeb5641aa5bb7fec8cbddc2abd88
BLAKE2b-256 79ed931b949cf37fcccb7346fa0266cec1871fdcace04b6da6d0f11f063b0bbf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.7-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 849d62aa91e842a742153d7cf0c36a0420d34074e5170af1b2d47a15ef4b8fdb
MD5 a48be6883b215e19948df64d33d84f0a
BLAKE2b-256 fa8d52237b8c44b8b7431467dbfb3d52f08b5670c72b2822d0a396f0db76d23b

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