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

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.17-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.17-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 dad1f2c1635b75bfe87e03a5073390fa8e0e2c4e09ad9fd75bcdef70ec72ce87
MD5 616bcf5e60678216dadd42aea998b032
BLAKE2b-256 7f4ab4132976ae8e6bc71f9a58cd8bee911f89966c3d48aa8803f1479c41b8fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5e2c160147c0027d9fb0bf83c9e13f18ab7b174678c7bfe2c8fae8cc664c40fa
MD5 d7e4b36765c21ef29ca971e25f5c516d
BLAKE2b-256 b3caf5d036cc60404576824cfce998378e5ac4d659e0a1c8e981d19f3f55bb04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d842fd46a0a8a2497630a549311ef816fa0199c2e9d524df00b09b1918267783
MD5 95b66b013c396cb7d3b6bb836a4f15fd
BLAKE2b-256 58e8e5e3a39d5ec19067b5c741c1148302e3c7e53208d43efd4b2d56046de3ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 36b9056c060f80cc5de2bca713fa336ef4d7e2263a957c90277ae6c63ebb7582
MD5 a72d5744bf48f90a72ee4ca3afde2e69
BLAKE2b-256 5068343a46af73d8254f2ed124a47b68102e637456baa207881636a328c62436

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3d8c3cf62de1a5e554c8a5141d447497130ec9f8a5833be89ec2c4dbea252a57
MD5 51fa11b17ace5a3aba9be53e7d4e73c6
BLAKE2b-256 d9ea3d3a4296763b3637f1b2dc36499040023670ebc711c97ccfbda590decbe1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f0bcab193e962f7bb59212ac42e307d7b0ea3954208eaf4f555e3a25f4937b44
MD5 0e006c75153a9f89578a1a8cd8a188bc
BLAKE2b-256 654df115a6229be218fc024cf418ed3b6483fd3f822b017a8787c6a43d5e0410

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 10fafed5d908d6763d752b95db5a0aeaa00e3e52168cf75bd1ab8dc2bf68f4c6
MD5 1a0f0a1bb96c11df617eb6d9f3314b9a
BLAKE2b-256 3cd7bcf79c8b81b949a1c3fb030d717710b1c557012c70879d34e4e94df1423f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cf69c7df239d4de592ea39a4a60cd1ada4466e925ac084d6387753808d33f2cd
MD5 97e4241cdc27780cb0cae89bc1b38b3b
BLAKE2b-256 8063fd822389bb1a56b2384508e5117cc3b77df66a5521c9d720dfc189ffe74c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 7f9c6646b166ac67743c98c0f0007206d94dd633a6543e5508b80ead5886159b
MD5 4d85caba70589044d9e8c058d0312c32
BLAKE2b-256 8b37a355338763fcae77147467d5b599983364c69cba7e062c5490474d955c15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 cabb604d4dfeb8e47036687de0ee480ca28005811caea02d99e184c6bc4ebc20
MD5 04167b19a18fa489399895459af78ba2
BLAKE2b-256 41ab20f297248f692f7025244032da1b9fedaa7cc9771e40d9014b6e97fe2e7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 62a05ce418631f717b4a04ee51e6af9c41ee7eda461bbca2bb454f40bd25e3ef
MD5 4f26d6a06a3aac98143c06fe94630734
BLAKE2b-256 474e87d21f62680db4592463d9c4535a74b657bc2c5881e9c4c17fed2dae3f2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 31136736119fe1b5cdc5d862bceddf6b5ee0222ecf6cfcea2f7dfa361ca9ac79
MD5 bf968e4f189f12af6760aa9e3201be4c
BLAKE2b-256 0fb0de6685140df7e42f9f8d4ff0f7ea4eb8d607b394535e88b4140e8947a86e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 83d6aafa57aeb7195e69d500b1b39b50932c03361a17d5c6a07fa4da4f3d2e25
MD5 4de66018f8e705f0523b4ce1745a0dbb
BLAKE2b-256 b6fb386b91eb8be7dbf1c742b86e4b4bb848419f6e89b97c79d1fbce7809bbd8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3fa8ee4c64638f37bbc388cd7ae859e0f2e3c46b2820629f445d0d43b9e69109
MD5 6fcb4aa40133ef7a09693a0f0208ab30
BLAKE2b-256 1f89cdd52299c7d304749f94d78dd191964e247b2327c3ac3a53b76041474ac2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.17-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8c5345c9186c0f84d30a64082ab9963205de4616a3cd04b2b8c8fb2f133e18c7
MD5 a4e153ae7663f00a3808a61bbe58fe7a
BLAKE2b-256 8d360f8ae40d6de673688681612d176c3e9482d89df15b68a703d601d66d3e35

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