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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.9-cp314-cp314-macosx_15_0_universal2.whl (530.1 kB view details)

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

tensordict_nightly-2026.5.9-cp313-cp313-win_amd64.whl (600.6 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.9-cp313-cp313-macosx_15_0_universal2.whl (530.0 kB view details)

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

tensordict_nightly-2026.5.9-cp312-cp312-win_amd64.whl (600.6 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.9-cp312-cp312-macosx_15_0_universal2.whl (529.9 kB view details)

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

tensordict_nightly-2026.5.9-cp311-cp311-win_amd64.whl (599.3 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.9-cp311-cp311-macosx_15_0_universal2.whl (529.1 kB view details)

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

tensordict_nightly-2026.5.9-cp310-cp310-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.9-cp310-cp310-macosx_15_0_universal2.whl (527.7 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 2288e86f1880bde918f7ee789936a3370aae7bbcda9e53d202de6b477c25be15
MD5 b9d411a70106c1bb9a13f8ccf5900ec9
BLAKE2b-256 402ee64c52a13f85607175e247c6bb3e7ad470e1753c1f0f0a5c03c7bceb980b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 830e20ae116848f8f790926e74e8e86825cbeda7d38f0b69e50393f6a8bf7b33
MD5 8120576c7fd29de4f7de479c3bacab65
BLAKE2b-256 685708242e482a82b40f88e62f91ca59aba02bf43435890700235d16b175ad0a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a68deee04d966e110adee0ef150c098ac08167cd3be05f71c4c03c89e26481a4
MD5 de11f93f44f9e8fdc428af8575f72022
BLAKE2b-256 84e5a0a5a7f506ad6544664f3aeb882535556944961050cb7d8ecfb5549ac2ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f9f287343902504a9568796b4fd82a12826aefb8b8b74da8552b9f4aa3568c02
MD5 3bee254eedf50cf1d6c261fd4ae08156
BLAKE2b-256 c90d32da34722283665f39fcc708bbfa3437964f7ad265ef9dcc22078e4cf747

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cf3468d73ea0c881b82bbc19c3d439aebe6064bfbc203c72e84da7993ef83e30
MD5 5b5dfda2fce54f92edc7e7fd6efd4512
BLAKE2b-256 928be4c273c02a141fac64736dfef18f32d5e398d59042c5b490a87cbce7da0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2818a16f09287cfd126eda67528da86d3b3f047be106d866f13e1f5487f8926e
MD5 d3f068ba4d0e1e2938252248e34a3152
BLAKE2b-256 7e615db72e2b92d4f7e4d48ae83491f41ffd6e477764682f5fe7f85b9643d234

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4673e5f37aa89b8a841df13307dc10857e6d06f314cb275a835da26e66cdb046
MD5 54558f3852d792c63e1d74fa42c7b5a3
BLAKE2b-256 e06e0374e4896383dcf2eee2e8b335d77440757233f7d78efce20194bcb8f1e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 32fb31c25dc5ec824dd5f6efae7109e87a1a32395414bf4160a542e20ac37723
MD5 6c7330ee8d0854f431653bb80f54a094
BLAKE2b-256 20046c0991c1379e4ae708531e851fb85f7bb70d04e54fc89d72fdb7e7bb085c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 7f406f46a9448a80e32d0cebe032e7acc0710f1f601158e042b8aec3713e7b99
MD5 a7844546068ac20467b56dfeb208e6c6
BLAKE2b-256 c61217c4697046fc752a83cf8c4ec4e1be800f868fabd30fec18d9179fce3c1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ef3545362e0682de0fc04d78f04c1ebe91b1b5e0d65abf77f79ea0341b7d9c54
MD5 d9490bbf0df2f03654af8a182edefb7a
BLAKE2b-256 5d38e015a97bb032c0faf20dbc18193f80c31f729bb8f55c57af7d9045146df9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e5138d2c2df30de1aef7e4a8f27819b909ff17c2aa8e05fd5e569ad3d2a6b7b5
MD5 6aff8556116482281bac7366b5c602fd
BLAKE2b-256 930767208ff30a663bf47b69f975e01008c36d4f059326992ec13e53d1f11983

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b1ebe8ed5e46d6add281f5ebd90147f7ba14410dfcbea9f9930b71cf2fb80469
MD5 5ca4918499f2b3f091bd2067d6ebd46f
BLAKE2b-256 db30eba294f315fe2f5391eabe4974e39d11e1ff9577a08c7bad9cc641720669

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 de36c3412154f510b9a106a519e2952fed9cdc1ce5043191035765e9ff18a441
MD5 e87880e8145f7e53df5fcf69bde9dcce
BLAKE2b-256 5e18239d1c35e3ca8579d480df06bf65445d483eefaa8e9693d766472119c25e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fc4de6a070480787c1fca86411bafabffd27cb404e30330e61fe51ef6df5d457
MD5 59cb226066b26836c6a0396d93931d5e
BLAKE2b-256 7bf47e0e65acdf839120fe548ac000c2f4c7963d1398586bfdcd8d8c5df2f77f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.9-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 262c5ae6940f79bb61cc115fe5387147904d551844bf739a73a4c12c2cdd0500
MD5 65cdce08ba95ff4a7b5595f311e0c458
BLAKE2b-256 54b3a8758763db368d824705bfcc81dc2ee7aadaa9346627511a74433e22f231

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