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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.26-cp314-cp314-macosx_15_0_universal2.whl (540.3 kB view details)

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

tensordict_nightly-2026.5.26-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.26-cp313-cp313-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.5.26-cp312-cp312-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.26-cp312-cp312-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.5.26-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.26-cp311-cp311-macosx_15_0_universal2.whl (539.2 kB view details)

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

tensordict_nightly-2026.5.26-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.26-cp310-cp310-macosx_15_0_universal2.whl (537.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 d0aa91dba4afb1f1e4ff6edfd9567357484a623d32fcdc8d9e8103f63d2406d1
MD5 c19c53439ef544cf331e7e0c58864472
BLAKE2b-256 e2072a26850d52c3179cee4bfc2a603ed0b0011e91cb68c1af379c8d8320b96a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4c0d87fb14c15d7d901c68076d19df9c3afdcaa07d396350152f29b6a283c4de
MD5 43ab32c0ec68095933850eb18289e832
BLAKE2b-256 72383b47a31f908ce602fc700cf865d632bbc5b22f04e9c3633fb91ebb41ccde

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2d99d038706b6a8d84b04cdc73b8fbe8209e927bde2b4f0e1edec0032adcc146
MD5 c78fc7c71da12c0c3cf28b54975ef96e
BLAKE2b-256 07dc0014aba73d947929d58824969396695e0f98c34210f5a77dd7a2aac965e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bc17ccbe5b716e2c0d4927a0bed12595d44bfbbc926df23d352917ec039e0f27
MD5 83b4aa9ae5126e30379a89420de015ab
BLAKE2b-256 39c8b94790ff1237997a8843c0cdd97474d9c2727080c615fec89d5e02d21433

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 03fc606d82781076f639a4cad1714d9cb9a8f10e7505772cce80ea7a8e0681ca
MD5 a515f8876fa34720ad4aac3b31624765
BLAKE2b-256 724dfe5727320a8d1ff7d4d1955ca6609bc856f0ebe1470d9b85a05ce9ac300f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 816a95dce827fdc3a533036d4d8abe310751b6c666887a21bb467884a1064a2f
MD5 c2024770c2c6a4a0a2181adbe66ce252
BLAKE2b-256 6a5411bc9e68868d241aeb824f5f6d7388eab3135ed1a0c75f4a0b73ca225e70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 411a8de6252ec509a308184bc90d2de2a57068b12582772a395c4445e805ffbc
MD5 01cd5a761c57d2a6b27423edb922b79f
BLAKE2b-256 1fb34e54c58865c916698d0529034949dff11c1228cfcf7d22d32c069ef07d06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4bb52df0eccce2f267d5f12ac5fdf2364603890232579aabe5487426c488f5d7
MD5 cebd3191424a4bf66e5c4996783286de
BLAKE2b-256 65e2b02d75406d472e8c2006509e7765409b0ccedfc811282788b387ca70f521

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 fdb89c37447a4ef380d32e6ef0321d807bdfc434926f6dbef0b0d34b979626fe
MD5 11998baab0a75ba5751ce6e077b6631e
BLAKE2b-256 84cf6a7cb966407f6f0625e650a62f7046ae8d77dfe71ed0cd2084ab10252bdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 efb11d6fef8707e675b4cb1313462b26cb7b880ac78ddb28b0c98b7cb7387e77
MD5 7756f3a678e91062c4e3561f917ab775
BLAKE2b-256 efbf2a05356e4c16ddeb3b363bdcc92ddd074dd139d5393c862e51f108d19191

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ff28e94aa67fe98895716158e89c6f74f6df8dc01ba40f58e62d0bf00be1bc40
MD5 ba741acf3942f9ea12e368972a0a0148
BLAKE2b-256 4fc326ed70703b70cde7801e8386f11ca192c99ddcb91322d24a0f3eec96b47e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f39015938235178f55d95e542f83767b90b0ac51870140e2f86df12e39075dc9
MD5 fae94cdea59a6c41daf1b217cb5db41c
BLAKE2b-256 9b9584ef77222c66617d01de522987aeb1db5991ed8c079daa9ba023af9c06a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ee250d19f23319f86ec408eb6fbe944f8fc9be08e255b222a7168eb8d02ea370
MD5 bd1f285eda58b5143f826f1ace7d6899
BLAKE2b-256 b879971a69ebdddf2b302ef22b843754c518c7f5638021840a7890c376925ff4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3703fbc889d2eef49f050de300170d26996611ae7f927b193f6c4b6d2b474eb9
MD5 d5f32cda9f0da850e9c8f05fce62631d
BLAKE2b-256 36cc981cdcc3a2c6bb9258e63630793e4ddf4b3ecbe4b264b1c5f8b41e233180

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.26-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9bd36b267bb59a4c96d374842b5bfa9b3783565397a5cab479cdcae9a77bf130
MD5 0811de860090d6401393e24a541fb298
BLAKE2b-256 57ee5919a35ad14cc91a1a466ebcf1a201ae8e588de85bee60b6d2d21d0dd1d8

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