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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.8-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.8-cp313-cp313-win_amd64.whl (600.6 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.8-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.8-cp312-cp312-win_amd64.whl (600.6 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.8-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.8-cp311-cp311-win_amd64.whl (599.3 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.8-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.8-cp310-cp310-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.8-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.8-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 1e8a29f277dd486e8e34b5bdb10c79752fdc2e2c8cd4c685d5724d7aae1c2dce
MD5 bff443a86c26cd0be675a35a9d87004f
BLAKE2b-256 3e99a9b9fa4bfe26635a69116b9ec623ba9d24946393c03e6dae28b81a23f288

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9cd084e9cf1b5c451fa4aa9c2caf635bf4b7a16af1f74bde2dfd2f8c6c05c658
MD5 6250e1e4476f04a390f0461277c9d325
BLAKE2b-256 82681f8b7db946f3e5507a2f2a3b0f9f03c62ff818e1d7dbd96666987a4b34e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5b51ba322129ef48cabe9770e79237d6bdb95d424670b54430f16c29789b6d8b
MD5 cca7cc98cd2229b63de0e3db9f6e435c
BLAKE2b-256 0a690d45ed3482e62dd9143e73cb342fc48516f635d8aaf3b00087db6418900c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e79f8e9f1fcc5c3fd4eb6b4868330a916979478678438904fd88e2c2f74d07cb
MD5 d3736643e56076cd4958996575ecd9a2
BLAKE2b-256 ecdcea5ca6fb8406fab1f251c26bb556661601a721ae361b00c4455da26688e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 491266d47f584a225e3eca27dd2c000c131ed6664c7358ba5cc288ef86d10ebd
MD5 145ee62010e149b65c26c1bceb9542a3
BLAKE2b-256 4c0d54ecdf6806f0ec70d28f7de73983e77e5aa2118b05ec7ef1cf49c72af1da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9dcb2e62d004e76555ed349d2310ddfa93dc94cb47efb7c7bdf2fbbfc448b272
MD5 b3532eec2d6cba8a5a66ad3869eb631d
BLAKE2b-256 8cc96096779b3fcbe9a6797383a30817c0c4a1972e709493250e216407815e4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f6313b1bdd24860bd52b86a24011a5504ac87d002dc674a37db88c16da8b9239
MD5 f0c11d7ed82e8031cecafb65ba70d7d7
BLAKE2b-256 35faba7fb568a87f704efa1efd12b70d8f175f5daded0888bf8ddec2a7f1e698

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5647b981f35aae9ba9a67ee13afea63d769d495108282a69ae31af8e6158cbce
MD5 aaab4e87d0cc091eb7dab74c94c223b5
BLAKE2b-256 cff13b8b4fdfdcbb81ccfe29719b33a440b3554884ae184909f1fde84bd1eecf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b62aed971f235cff7b00ee2e0b9cb7ea9972e42b31e9427e72f512a1dd0eab2e
MD5 535a1d2697f38552f3fe79a4029a90d9
BLAKE2b-256 e00bf31a8774f9bf485629ff959e2c3529b7c3b5d3aa3b222f4c63266631d6a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3f653462fb2db65e7555ab91273b1f34fb2ca1237b5bd0cb1b83896e469c58c8
MD5 0cbe31af426e22cefb42f6c6048e4495
BLAKE2b-256 ae010ea5f3de21f9213d5bc356527de8d19e22c1b2204456efa0f5f4e4ad27ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b757a4d6d2e1775b087579fbf56c7b6e528eed7edc3080973c398d14246d34fd
MD5 790d1369e85429427cb7df45a9eba484
BLAKE2b-256 72b73de5b45c1ad110ebe0c36a503bb44e420090fb64e732d87072f3138eb250

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 2e62669d9a6ba31b10c8635e03a1fe979688b49da8b7e57ab58640c9e5e29847
MD5 f8aebe68f3d13b11057b06880b4961e0
BLAKE2b-256 c9ef4582e2db31a430ff3d61077911fc6745ee0d248bc6d711b6b8556e538304

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ad83d5c5933355f7457aa4a94795ef4dfdb8cd1b398f37c794772b0f5275201c
MD5 253b2dd80051abfcb0af1f4d59691d11
BLAKE2b-256 247778d9e7fa97019ec227a0b114dd0be5a5ac442c58c159a371e22f25302cc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 263b6b5255b32a6a1f55850f75a2132b51ec8d75aac0d615b9ed9297f8e98935
MD5 a6890e9a2be9315dfa0973664e60e6f6
BLAKE2b-256 4a44d1e86a9cb6666b6faead249b8cd094a31e9e3836c8ce234cab745ff1206d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.8-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 712f09af4d8da94680fba4d6097ae25c5fa8a1bcc0cb8b60dfe77fc7bcee6051
MD5 5083141114ba063f1b29efb5ea80fe31
BLAKE2b-256 744c14c4380960d1fbf971885b312e0370363642ef6cc954abaae284f043acb5

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