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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.22-cp314-cp314-macosx_15_0_universal2.whl (538.6 kB view details)

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

tensordict_nightly-2026.5.22-cp313-cp313-win_amd64.whl (609.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.22-cp313-cp313-macosx_15_0_universal2.whl (538.4 kB view details)

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

tensordict_nightly-2026.5.22-cp312-cp312-win_amd64.whl (609.1 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.22-cp312-cp312-macosx_15_0_universal2.whl (538.3 kB view details)

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

tensordict_nightly-2026.5.22-cp311-cp311-win_amd64.whl (607.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.22-cp311-cp311-macosx_15_0_universal2.whl (537.5 kB view details)

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

tensordict_nightly-2026.5.22-cp310-cp310-win_amd64.whl (605.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.22-cp310-cp310-macosx_15_0_universal2.whl (536.2 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 7e16ff63e9c03d6e3cf98a2fd46ffc9001a5f69e38f8c3ba7345c8bb3db09c74
MD5 92adef1ee58b8d3bb3565dc8fdf1e8d1
BLAKE2b-256 6bf8c7fea9616c798a0c6f30f1aaa4c55c4cdc23d994fd8426126e46cb8cdf6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 772a9dd87663aee9c83bf950ae1ac093b543ace841efe54c36943a2bb70394f3
MD5 c66be04e8704bae0aef093278dade423
BLAKE2b-256 ea5dab90b844d072e7259d8f69f602ae8b1e033bafbf2ab0f1743b9280ef45d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ce2845e3ad9b9d05ea8ed6e281b3a8cbc53bbe896d435d3b617c6d9cb41fef07
MD5 397d10b64ca7408368a668e0d47aceac
BLAKE2b-256 ef9cc20a0646eecea2cad351223ab1beb2207800cd2bd4d9af1d3026efa224a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 868b94f946b24c1e7eb1d150667f8850922c6a8f98688ec57f3bfab4c3e1fd0f
MD5 c8873b33bc864c5954288b881df7a15a
BLAKE2b-256 5f44e3891c458ee3bffff867e15e02388f48d2a41e28733be84d9c4dc908e791

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9d326c64dce2fbd65e1291fa20c65d4d1521369fd835735319e6270562cc5d30
MD5 dd79f058e48a902ca542b0af513bf77f
BLAKE2b-256 bf94a52dd77bfe95ccf5e3d65d55cd4e3e4d1377eb3267b920eafa09b1988c14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 738bd21326d0f2d92f548c9f99f631266a685dcfcc3da1b5a92c34ea1a56fc88
MD5 87a9f484cb59aaf58b7a0d4a22f87206
BLAKE2b-256 de4b950515795e568f2b46fc3d818d27fec685ac27d3889cf8c410a2608212fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 795a91a12f7cb46cecfdd3c268c02ad435743db1c8b32c393ce2e37a034be7e1
MD5 032c0fd0a61e89007d873fbfc3b2a1a6
BLAKE2b-256 54bbd5b4135a08c82a66157264be3f5425923f46ea8ac0edb27686d9c8b5f25a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 df90cfaae803ad1f5131232e507fd2eec9296dcc7658adf48426c0ae6d4f4a53
MD5 c7355b431b091748d563c1fe3f9cdece
BLAKE2b-256 1923ceb70fa43acae3821d36b657b2b39119ec2922a87bc697b6298da2cccfb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 133f229a555cf67b8dd47b0226e36a43e6113ac401c767fba138aea07e31d692
MD5 13b1fa14491e0c4e917d2a9295d6f53f
BLAKE2b-256 910c1f99bbd1c63ac1650da9e89328941e3b5a097d94bd6aee80d003292e7471

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9ea5f1186807640e843e55a0537f3da5668bd61f9ad0a8749a258a8496a6b47a
MD5 8ccd9c7f871ef049c59b8a6080e1fc1d
BLAKE2b-256 077e817e92f0a5ac684f25fa141d64f99ad5eaec0f988eb0f69d138d48867b97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fcba9b6e7886367ff7e87a3e259f1cfb4c7bc93db8cb4adfde7dffaca92859af
MD5 092ddf6cf39dcd3fdc0d5ec8f2adc8fb
BLAKE2b-256 ed14dc6ff0c289f16dd8639093c521e3bbdf6c739e52006b35e1a34f13753db0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b01677dbac0e1169e8f106e0cf133a874d3ee7c5a7c2f0526b3b7aa2d9f5b0f5
MD5 82c2034b0fed78f4dd296d7b4eed7950
BLAKE2b-256 a37a7a9a34a647a3433c2c1fdd7ae8cc64ed19929f5aabd674cee4a302f8adde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8ddbab7f6facbf46fb7d608192491110cdec4447a89d736fd0fd2661fb6f531f
MD5 6299ddd63bfd26cf71d24f6c0ad84116
BLAKE2b-256 3b8632ae1b98ced55b7d4963f4e2a2c38870e55079ec4a9bd7d937ae0f4d0032

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 16fc72e3ad98b269ec99a4c0523c4527bde3b082d094516034ba9a291444aec3
MD5 32c142479cb26416a4a804e482b3b804
BLAKE2b-256 47b555d67da2a3ce07bd88b61461376fcc1c96f311023816f0971de7dbd3b450

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.22-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a8edb9edbfc3f2b53fd312326a5d769b9519048bcc1771140e9bb92984f2e2d8
MD5 ec8abff44a01757d3e3fb7ce286e5268
BLAKE2b-256 6ddbd69e3088f9b3ee2c505538f4afabc332c07431957dcde4702573074df3d1

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