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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.13-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.13-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0d0b0a45f63f8c536bbbce5fa518e675d81cdfa2d2e2788e3cb52ec6bb0c762d
MD5 f64b0f24c37f42d6f3ce86a6bec2d7f5
BLAKE2b-256 70941a68248c5172f50134fe452db60e1b3b64d27ebe86fa067ae1a249709681

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 af72c6d743e5f37e54699d8c1b1df62ad10dff237e3256639c6093f26cfb19c2
MD5 4ff81b7ed830611e34977116cd4e5af1
BLAKE2b-256 77cdd8f84ec5361d81e33cec647d1192e3d296337c34fb75a04203d69de0d4c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 87533564e0963d96567dd66351d6fa7242cff403d743f78a3e39b5d3244ea536
MD5 f5bb022e3a77e94c91dbd5ef69b9324a
BLAKE2b-256 80ec2a76ff2c6f2484f17ccca5b8c907d7e1532a3c0e6921eb649d1c949a710f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 31823032ce582a22070264b155ac11c101a33c8c8b68eb2d800474fb4bfb8c16
MD5 326976486be03c901d2dc187eacbf57a
BLAKE2b-256 f8e279f60a4a3119458af48c6f8a6ad9057cc89be08ebea5b61191a8e8de9ec2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ba3e01429854a1cfc266c261213cfb6b43ec03e6b8e0c1ddcfb0088e837fe75b
MD5 0126a37419a55b154d23a3180692cee0
BLAKE2b-256 344e0d38b5137f7a4ecd3d9014df88adc621500846d91ef31cb9d8b04dbe23f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9166f04b7deb972b9ea5fa8966d2ad970a5cca8adbf7c7db1b8c9ac8ec35dbfe
MD5 51b0c61e5caeef8434ff223f8b4f88a3
BLAKE2b-256 aeca2707bfd3e9ce3ff75e63e6fc85975fbe746568bf9560176f5a2c2ba3df69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8fd210d1d918e95628370ee0b403d70e3f369b847269d10ad07dd65be2a5df41
MD5 75d58bbee2e9806eac99238982b1afbd
BLAKE2b-256 e377aa06b72faefc99ef6cdac7869b1ee54c0ac6601e5a3d9174b8f3315a2bda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6d9e2561fb01641a4233674db64d1e2f4b082c8f250c9f738466a8dd8b9ec4cb
MD5 1498309d83aa6b70ad83993c85fbebb7
BLAKE2b-256 53ffae965ac9482f33530a3b234dc83e96966f1ab57074fa2a9f2ce9b0edf67e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 279dfe07ee928290fba0f2e072cda62328019fdb9c04e7ec6d4f6be27434bee8
MD5 67a0222346c127aba11b524dcd048190
BLAKE2b-256 be735afb0a2855130aaee008959228b92f099ecf98c7431b4fa90873b79b9ae5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e826ea52d71d2c68b5932c83826bb3bd3c5419e43778baa191a27f3e234a7677
MD5 25c60e63fcff98e5f14900e8837e19fa
BLAKE2b-256 8ccd204ab9435ff94d124bf3261254cce9db72a713781a447f62a2e8e78b3b1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c9787af02f670e7a3d3cca48e499a86405c2931cc472575775afaa5db8e19c7f
MD5 65aaf92e421f25306c4d5e16b7ceed20
BLAKE2b-256 f0f1edcdba1b657fd7432733ed63fa1695ff6f943659d36c984e679c36a23dd3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9066e14f516360fdbbe6b950bee5b4c50946e25b00588ca59ab5b8729b5c807f
MD5 ffe56f2c69fc4becf976e66bb9b41fa1
BLAKE2b-256 112662a9314b6c2fd69cf84d43e6b5cafda8d661065507a590ab53548ec6f667

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 deda49b03707ceda3411378acf390e199678bc3d847d9e6ebda0cebb48dea310
MD5 e9b4d206bfa3fafa519a950f776e6126
BLAKE2b-256 6d90e83d02cd84883edb1742de20a10f7d84c1b3700bf1106bec48ef3e1010b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b9df85862dd967689d590b6a396520a9de63f18c7d04748df5f6dc9236b6c8fa
MD5 702ae8e51dbb6bb088aba1f76633cbbe
BLAKE2b-256 1b3a3187343b58557866498dc97c00bc634faec487d3103980e0f31f46ff0d11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.13-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3e61d6f2238da2fece5e33a3f677b7a4bd93a42f678986b738023d672a0f0f74
MD5 3eabe4f3064b72e59048383e719f3a17
BLAKE2b-256 2361a6d724adda26679d3ce8c286583114d9f989796b57a987cae10727549235

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