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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.12-cp314-cp314-macosx_15_0_universal2.whl (520.2 kB view details)

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

tensordict_nightly-2026.4.12-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.12-cp310-cp310-macosx_15_0_universal2.whl (517.8 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 43d1425cb0aad2780f338c1bc2b9f3cc05ed5d838092140ba3c01afcc8a0c422
MD5 ca314a1378fd8478f298cbcbe7569a71
BLAKE2b-256 7780fe6af5249fb2393ba9fbaf7c399080be249c36e8b2b6a70585b87ae2577a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6ae15e453fbc6c7c23785369ad80de7e3891dbdbf7c2fc8cce821afb3183ca68
MD5 bf234cb57a3d80b8aa000e4d526b0ca0
BLAKE2b-256 43e2e438933fc5f57c379daad597d49a5184110843e6c22184ac93d3ec85ffdd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5178dd326f6ba50a8a2590f73e9da166227e33dae6013a52608ec22bd2c06518
MD5 73ce592d5c3cf225bdd8652c9f33c91d
BLAKE2b-256 2b6717b3dfa6d0dedb6db8efea92237f1b42b09ffa3418447d4cb379469bff2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ffc27942380a8605bf0340129ff548464d6d8b76a74b4ad6f7969b489d348cfd
MD5 8fe9435fafbe42e2d41ca15239e6f6e9
BLAKE2b-256 f959d7eeb3ab6863862b40b1ebd304092d31c73b177b2c64b224a50634bcc3e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7998d4826e034694f779d7e2ba4b8c98410d161e418de771781f16c7e1dff9ba
MD5 d312c28d446ecd5648f3dd052bac5103
BLAKE2b-256 bf690078bf0fbf82a2a4c611eab3bf994a0929aed21f40b4cb7ebffae831a290

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 83848b1a21d611f0187d228ffb69382eae5be83d029d376a36b0e34ce74d31c6
MD5 c6247e189124f75c02a7afd45144e9a3
BLAKE2b-256 e0035e36885e140f390938ba1995ebe2d6b53fbadcaf327b369f63bd5ed6b7f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 cb857684c77c1d88eabeba408cab8ecc016ef6b07d0548ba4a838d9717da8617
MD5 0cd6264f7c8c2e1e9b654ea4297bb75e
BLAKE2b-256 488cf7c87e6573d00564517a8130f83707288ac669b806a82a02edc94eeccfd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4b9500fca7105e1a6ee975852e4568ad372173094c298a222c289ee1b5563513
MD5 abf7e09730fc042ca3b3adb446635a72
BLAKE2b-256 50d1b884f10254d369629ca141b4dc892f2d75d495f906330bbaa198b61e74e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 063c3a71cec0b026e3b12edc021b7e722881189df673e9a3fee9f6829ebb90e7
MD5 b092e21e89dee3c5ea83ed586446c8db
BLAKE2b-256 e63c5ac7f38b3d9e0a669eacd553536dcaf24ba25019f905ae07c5a9fc400c18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 06f089676bc4d1f031756c98d21b5b0b2f91f55e228b88669b435360a61591e9
MD5 c99a183b4afa3ee7f3a90f342a640eac
BLAKE2b-256 ed13820ab7bf8196598f5f2f21b031152df2729561c66eb07342c6076b4f9d60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 27f1ca9491eb7931916bcaefcf2eaf8f81b4a72ebeeacd89a3d799cc10403c3a
MD5 ac10ea37c40d2f6da7da08407d60f0ab
BLAKE2b-256 fae178e65e32b89a8df51b6ffc17f4e7c0f3f2de636d46074648c0107e6b6155

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 edabde7c20fc52f000d55544f97cbdfdc96994c0ed4c572a17522b9d9b779e58
MD5 140f955e92f876dc196267011f9e2f66
BLAKE2b-256 5b205719e61c7db39eeafb93888365c2df7d469c0c27dc6b69e2a5fbe6b0728b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2f259ff85a4aee59af71af3ee58ab04cdb562b041011234e6b13373e7bae7c45
MD5 3b57ea825a5a95dba51c6c36e0e7ff1f
BLAKE2b-256 17ede072058df5025279ffe78b5d0d83abe5a81c6301991a805abc70ed1231e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 292bb5a1a39653bf282482daac3e8fe1cd608ae0b6c33721613f291add34e0c8
MD5 4593721bca6db1ec9666db3a985fe3e6
BLAKE2b-256 7516d9837a8931a6a905961e1a975e4cb31ad1000663293d6ce0a9d996bd7339

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.12-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c44e6e4fe405413d74add270b2d50cca959462bcef0ee4cce4e662a80d26b9ca
MD5 09031be2721ab7740ecad73874cccd60
BLAKE2b-256 039e125e78ff4695f85f469ef105e2f171554f782ab9490bcc7a204e543c21d7

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