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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.3.28-cp314-cp314-macosx_15_0_universal2.whl (519.9 kB view details)

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

tensordict_nightly-2026.3.28-cp313-cp313-win_amd64.whl (590.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.3.28-cp313-cp313-macosx_15_0_universal2.whl (519.8 kB view details)

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

tensordict_nightly-2026.3.28-cp312-cp312-win_amd64.whl (590.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.3.28-cp312-cp312-macosx_15_0_universal2.whl (519.8 kB view details)

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

tensordict_nightly-2026.3.28-cp311-cp311-win_amd64.whl (588.8 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.3.28-cp311-cp311-macosx_15_0_universal2.whl (518.8 kB view details)

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

tensordict_nightly-2026.3.28-cp310-cp310-win_amd64.whl (586.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.3.28-cp310-cp310-macosx_15_0_universal2.whl (517.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 9ebb5156e76d3d13cdd57ed38cd8556c8940503e078d7abd10185966df59b92a
MD5 a45792be61ebb36997125a5542a21253
BLAKE2b-256 f6cb77cbae8078c9bb5eb03d320d1f24fc3c15fbe26b88b46555ddb20c1a1593

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0187cb26579ab2f15ea59009ff67eb4fa8f65e1d6a2cbf05c751e1f7a4f5f902
MD5 8ae01cb74aa4f194ec04c45c78316e88
BLAKE2b-256 15865f6986e2b3173177b0b789020c63c9b31b8c33aac01d5891e688020ed035

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 df99486b75e6cb47cd446df83c8a9d59517a9b016dc252195e3d8d8441cd4d3c
MD5 647e4a21b51eac4126674b80860809f5
BLAKE2b-256 023dd49460b42197dd93d1979ff7c797412b000ca9fd32500521984f36ff9b9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 72f229b4f2793d5553a1b118cbe91213b3f1aee47367276fe3964aa2b9dc4956
MD5 17da3249deea3cace241482e4b1eea70
BLAKE2b-256 f0c0b6435068481b1202e35b72ebafd8092a19bb28102b00120b9a813f5280e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 280bd9abe2e7cb41238662d3c46083dbb5e77c678875f57d94abf8e86f24732e
MD5 f9a6694cc7268e9272f6f2dbeb9fa49e
BLAKE2b-256 53d69e96840c606bd0e957040d395df1decc970af610bea8749e2611f6af8069

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3aba39843ffe6060ecf86590a9efddf9b644f8295fc229d6610f9f297ca2602d
MD5 a047d88c6b2681784773adc33d883193
BLAKE2b-256 153bcd1d8fbcc0e5239246dba726cf4ad1bc59d592993db87d297febac27cdf9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 21c71ad73e737b85035156075502a404c08984802748340045c362b95600a0cd
MD5 1d9947b49c806988a27931425960cc3a
BLAKE2b-256 d282354b727100c5d7f836e8714642d6e0d6ffb9dd0171f8a36f16d103f12fa9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9e8e1f39b2427ef60a6a794a3c425551c3859d2efab54be1bb0b78ed1eabe956
MD5 d884c14bcbd8b51f08ce028189cfcf6c
BLAKE2b-256 919c3bccf09cf4309d2d2caae3f9d335bb985a5fcd785c83a3a1f58f3008628e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4633c1aa209fc8a05fe3daf7fda4374ad262ef4c6ad566000064c63a1cd878b0
MD5 c4becaf71c43b4c72d607b2bc89f9cb5
BLAKE2b-256 3aa79477cb93f05f07afc94f29b81ffdaddd5f78d16db3a5e2a64d8a6814659b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 397ba5ed68d3578964b3769bcb2622d785ac741a0113290ccdd3ff2a8148701f
MD5 01e22cec966ff209b9017b8954774052
BLAKE2b-256 212f567587957c445f33341c78c3a368e600768b858506f7d048d9eedc265f0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9046e0e476789560d7ebd9bdefb3a281608201d6f1b6f72439fe1a4f6d363e31
MD5 3293448b39ac922a2da7ce7dcc3284b4
BLAKE2b-256 24c11b2d5f4e550600f19913c223b8d89ecf2823496f93e73aac2a698170e9ab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 83fffef1aa2cc6767eb21b16db654c1593c8484cd22fd95b80dc129728256a92
MD5 7338f61eac3968a1c3cbc9731aab979b
BLAKE2b-256 5c4b518a05aa33e5673861c8eecb877b07a4e3e819a72782270488b63e1be631

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3938c5eb432f0cc559cd7f43187ae76c9441e62cbe9c75aed15661196f8308ed
MD5 9cd986e4566a971587400a57644dff7e
BLAKE2b-256 ee782726c3c2d2ffa6ce3a54ef1ec6a37224f155e6b49098a2bc3a2ebe909d72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ef0df34e62c516ed2e9f21d466ae9fec7a3254e054d26df2721fac49d3635b58
MD5 97bcec012be73f3195da8666b1ee5385
BLAKE2b-256 74e18c98ab14cd7b7777a9b481e97a48ba838a2ab744d0894fbbc3f7044c9819

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.3.28-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6d6b60e80a509b02f4714cb0c5f23ac98255f8a68ed0c3133c3bca3801465e42
MD5 3babaa5148952f6c868efcbfc1e4c9d1
BLAKE2b-256 105e9e34c393be0cafd815b81a6e296e35269ec0a87370badb733931fb517d7a

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