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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.9-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.9-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d01e83986215948ae59826a74f8d5b51fe2fa3f7485051cef45cfcd7786de1b5
MD5 f86150b2a9708aa433e19c41ce36909a
BLAKE2b-256 1f00e3cf048d359aa6d717bc98e2ba3315df1431b0d449d80f7adbe8cfea959d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 106a3fd39ec0d40a09aeaa5c2b38a3f71754ee5331b800312f8eaaf20b284228
MD5 dee14382eeb5806fa5172b8da634096c
BLAKE2b-256 78942ec4d378e9aca297a3b29626a5b8b75ba0e9d230d996b495bf9a3461a6bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d33b95f1723fff5fff30f053368af5fba6a13b859af4ce782f474096b16ff475
MD5 a184f40bc340710c731955c20ca0f87a
BLAKE2b-256 42fd4bfb0d80a83cb059fbce6abf3ebb0ce0705bb5b93631e3aa78889518eb84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e13348e6c2c69d679a15bd32e55078caf0e0dadbd1a8ed3781db06ce2767064a
MD5 ef476af1cde45df2d84d93e90a8be2e5
BLAKE2b-256 4def99ff10410cec11097d09de3c7e157fc5c9647b5465acb2ca8ea3f16b65c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 046cc5d5c6dad226d9114eb8832ef973aecd964f95733cecfe80c2572976066b
MD5 f3a42c79f0af524f54f7b8bfc5c01924
BLAKE2b-256 d802f9777bfb1230fe42c5403b1e0ff32ca752a0f4cfa1f4e06271dcb18be777

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f8c5569e2ed9a756883134d152f3f3f4df7bf222f6a33ff9ca95762cf26167be
MD5 3710c9433ab444a459fcb9128663cbc1
BLAKE2b-256 08097e6d65068507eb08e7b922346aba71928dc3cf74e9b10101143bfd912d94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e422e2e3c83caf3f1c5ad90edb7d231da7a85ae0246801b9bfdccf1ecbc95927
MD5 5f4e56946ed6efb7ffea8be527308706
BLAKE2b-256 5f3be72077e25234957b585c0e6033357b270b78998f1fb2055c27a8a263d880

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cde614f6566dcb86e14aa9fadbd5bcb13345412a933a35d987667c335a559d5f
MD5 7a798ab717917db5d6f4a7424cdef8a5
BLAKE2b-256 3f3053a4e5376e24f5af196af20a5b41f038529312e5b0901dc0460e7b74a2f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f6e3bc2d4fa8166ff32685751c46c6998f81ad6ac24b221119c1ade7f9851463
MD5 d73b9f453404643845578cdc105f848d
BLAKE2b-256 f6c916e497a084880e06bab6f0d2dc5840a4e9f1a9533ab55b5a8bcd69e33a2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b2df020245c8010debab05219f573e7e0dbb5d5273bfb98e21f8595405b2cece
MD5 8ed59439373473db4b93f196bbaf4add
BLAKE2b-256 3c4aaefee2d02cf850cac2766617ccbb1acc263aef078fa4d44a9ce2846cdf5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b2cc37626d07ac704b7348c3bdde65a7135d74872db6f7d4fcd507a8479b7169
MD5 475caf13bd97540b4b2249725afeac81
BLAKE2b-256 cc6243824ad9ce72580aaff59909dbbe390e4075bb4fdb748ac14d804d6d906d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 42c1a250c6219db4ce8bc8cfd08e15404a767d9c7276483110bba10ea8eb0c72
MD5 39f43b868a17a68a9705980ea53142bc
BLAKE2b-256 7f29cf1f90e671fd7bc5ba51a826b26edb64b6de40cf53b276bfcf9e8b2ec715

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b3f532b93ca2d6dfce5067a9f603041d9758297ca3c372ef69809260cdc8fa25
MD5 c98f1eea75c3a4cd3d070a098bbfe004
BLAKE2b-256 cd9a14808cf8c5e16802692f7d7f2227add5022fe856a203654f7500fa54b731

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 022bcf841f0b8390893c2274319f0badee8a2de5e9978f16b5e7d28677d10764
MD5 1d4c7ba7126675a280909ab9c72e0f0c
BLAKE2b-256 3789e54d795c8deff13a245944f1f4ad94f65b387274d8e68f017672a30080e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.9-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 bfe83f360f04908fdd77b8d521c374249a5758a6c882d19f9beb6f36bf862c75
MD5 f75bc2f6c2f0303d3e901e8e5a7dbced
BLAKE2b-256 0c6fba50eb8f5ab8aa5f6abf3b8420c1e354123b255cf723fe955bc795daf7c5

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