TensorDict is a pytorch dedicated tensor container.
Project description
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:
TensorDictgeneralizestorch.Tensoroperations to collections of tensors. [tutorial] - Speed: asynchronous transfer to device, fast node-to-node communication through
consolidate, compatible withtorch.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 fortorch.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
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensordict_nightly-2026.3.30-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 592.0 kB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8385cb7e0e60be187441601243a35663a2ccbc23c224fcfb913b191cafdc7272
|
|
| MD5 |
1083ebb475d38351f07b48304144de8c
|
|
| BLAKE2b-256 |
52bb3d10099b96f611cb759f1cd0cfaf02ba8af0a6b2129a2d9930720991ccf5
|
File details
Details for the file tensordict_nightly-2026.3.30-cp314-cp314-manylinux1_x86_64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp314-cp314-manylinux1_x86_64.whl
- Upload date:
- Size: 537.8 kB
- Tags: CPython 3.14
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ec484ec4c9fdc4d6c358ec62ba0985a2200d9264a34bc75b744567cd5f1926c
|
|
| MD5 |
d3fa8fcb1c0837c402a30277930c5f28
|
|
| BLAKE2b-256 |
7abcdff04636cfe66df66ca80bba5e8859bd98dd447bd60290a7165611d3bc7b
|
File details
Details for the file tensordict_nightly-2026.3.30-cp314-cp314-macosx_15_0_universal2.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp314-cp314-macosx_15_0_universal2.whl
- Upload date:
- Size: 519.9 kB
- Tags: CPython 3.14, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef69aa1e0429ae744de9c859e76e13ea04f4f68957a1e849b6f643913ab4b8b9
|
|
| MD5 |
3fc43ce91b5c23d72fa0f54310910204
|
|
| BLAKE2b-256 |
085ec82d93bc782696bbb3b87fedbea8179c2035b091d7dbe4ba1ec186375acf
|
File details
Details for the file tensordict_nightly-2026.3.30-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 590.2 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5138da1cafab3659d4ed3b5c68eebb3878fa207a8d4859d1cf9da250cd32a1ef
|
|
| MD5 |
e9db9f3605f2164ccd6897f32db0c84a
|
|
| BLAKE2b-256 |
ad66f16c34394a235eae545519442090b657e23a859b7834b6dbf72e64793892
|
File details
Details for the file tensordict_nightly-2026.3.30-cp313-cp313-manylinux1_x86_64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp313-cp313-manylinux1_x86_64.whl
- Upload date:
- Size: 537.8 kB
- Tags: CPython 3.13
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c7b8ac39a76f2e13a46459ff4fd371f46d6441c7c78d81519113b54df8ab1a1
|
|
| MD5 |
aef3e7cfa144e49a214a7c445f830dbb
|
|
| BLAKE2b-256 |
7f9032173e73640fa1b9ade39d2277b52b9c48bfd92452ab2bad4cf078081e49
|
File details
Details for the file tensordict_nightly-2026.3.30-cp313-cp313-macosx_15_0_universal2.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp313-cp313-macosx_15_0_universal2.whl
- Upload date:
- Size: 519.8 kB
- Tags: CPython 3.13, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
519676a22aed14e831b5ae12db8c8e110043b55e8361949a794cc5a446f23235
|
|
| MD5 |
8bfa90d276589b40507c7f7df6cbb195
|
|
| BLAKE2b-256 |
73c8b811ba3fddd23159feb5f8cb827b20901c1a37429a11059139c2fea77fa9
|
File details
Details for the file tensordict_nightly-2026.3.30-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 590.2 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e36834f2fa74715fc8dc89254f51e754b9a3be4486f788ae38ad76641cbe9eb0
|
|
| MD5 |
0932fa6843eb2455c1a08f4974055305
|
|
| BLAKE2b-256 |
d06ddbd8f52be45ee90c99b9ad611660ea8303ee6254c6f399aa6665b52e21a5
|
File details
Details for the file tensordict_nightly-2026.3.30-cp312-cp312-manylinux1_x86_64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp312-cp312-manylinux1_x86_64.whl
- Upload date:
- Size: 537.8 kB
- Tags: CPython 3.12
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7706b65981d1613a3aad78010c6b7bcafdc89be292247679a4f8dfe4ec666267
|
|
| MD5 |
2abc5395b149e1af1c8775af81f5c352
|
|
| BLAKE2b-256 |
62f50339dad0f2c0e8bb39c87215ff01f23698b72b689bc5b505f9186f69cb52
|
File details
Details for the file tensordict_nightly-2026.3.30-cp312-cp312-macosx_15_0_universal2.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp312-cp312-macosx_15_0_universal2.whl
- Upload date:
- Size: 519.8 kB
- Tags: CPython 3.12, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74d1bcae26474123bfeb34a771947317d2e63fb5ebf815d2f413baa46455a692
|
|
| MD5 |
1fbbf3170e2de07ef91cc8caafad1731
|
|
| BLAKE2b-256 |
221a21627123befbc020cad36122ff78fb39590ab2e2512b8e5b32e6370824a5
|
File details
Details for the file tensordict_nightly-2026.3.30-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 588.8 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78d6c177d28803f5ce324e731234712851e14b8580ac0f7195502971e985a485
|
|
| MD5 |
6f29cd2815e0c949c9c2f10b84de70d5
|
|
| BLAKE2b-256 |
333bd7c707958b147b2f49b9e3013e2f4f2ac483cda60ce8f28a5a2e7aeeddab
|
File details
Details for the file tensordict_nightly-2026.3.30-cp311-cp311-manylinux1_x86_64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp311-cp311-manylinux1_x86_64.whl
- Upload date:
- Size: 537.8 kB
- Tags: CPython 3.11
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eab92146896b27c7355267309cbfacf3a9f04edbbe4538639e3b4895825dbbff
|
|
| MD5 |
6470996b6a4077da5edc713d8a8ffb94
|
|
| BLAKE2b-256 |
4bb3636859940fff751c511da6486befc3237ac3889417a274473ef404255d0c
|
File details
Details for the file tensordict_nightly-2026.3.30-cp311-cp311-macosx_15_0_universal2.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp311-cp311-macosx_15_0_universal2.whl
- Upload date:
- Size: 518.8 kB
- Tags: CPython 3.11, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c73e8c2d5a0c392ebb31fadbbb4689855003d225b97d6d37383b48a11879ec8e
|
|
| MD5 |
d4735e54d4c5ca7821814d55f3c54eef
|
|
| BLAKE2b-256 |
44d995464bffcbabd1e140f13a355cf9511e07635fe85ed91342e65dd08d7865
|
File details
Details for the file tensordict_nightly-2026.3.30-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 586.9 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b79ab6a2c35f0bcf87ba0f30daf2251125e3c46f4638969b9b832949c224d84f
|
|
| MD5 |
bbdf441472d56eb0abaf7d994ce8db86
|
|
| BLAKE2b-256 |
4f354cf34e5c2119307fd264f4ac6f96b097cde64998cf25b6269e6030240d1d
|
File details
Details for the file tensordict_nightly-2026.3.30-cp310-cp310-manylinux1_x86_64.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp310-cp310-manylinux1_x86_64.whl
- Upload date:
- Size: 536.0 kB
- Tags: CPython 3.10
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7e19d7d5d867ee9a597caca77a1fc49988e1e072b1090236d8ccee4cad4606a
|
|
| MD5 |
403ada18a274612cb92066585a291a6a
|
|
| BLAKE2b-256 |
04a2d178fee508b18cdb61a64ed7bf32b0082a72f4b2532eb2aaaa8623033753
|
File details
Details for the file tensordict_nightly-2026.3.30-cp310-cp310-macosx_15_0_universal2.whl.
File metadata
- Download URL: tensordict_nightly-2026.3.30-cp310-cp310-macosx_15_0_universal2.whl
- Upload date:
- Size: 517.6 kB
- Tags: CPython 3.10, macOS 15.0+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9830f4957fd28993889db666588553ffe1f83a86103b9c1fa0579662aada8c2b
|
|
| MD5 |
9514fac0bc6ffbbedcb583e389f8d842
|
|
| BLAKE2b-256 |
5513fadae3080d3482b483427bf97fd690ace448883bf09c24a639da5e0410ad
|