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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.11-cp314-cp314-macosx_15_0_universal2.whl (531.6 kB view details)

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

tensordict_nightly-2026.5.11-cp313-cp313-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.11-cp313-cp313-macosx_15_0_universal2.whl (531.5 kB view details)

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

tensordict_nightly-2026.5.11-cp312-cp312-win_amd64.whl (602.1 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.11-cp312-cp312-macosx_15_0_universal2.whl (531.4 kB view details)

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

tensordict_nightly-2026.5.11-cp311-cp311-win_amd64.whl (600.8 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.11-cp311-cp311-macosx_15_0_universal2.whl (530.5 kB view details)

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

tensordict_nightly-2026.5.11-cp310-cp310-win_amd64.whl (598.9 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.11-cp310-cp310-macosx_15_0_universal2.whl (529.2 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 dc81846cb3c5b586faab9cc89730316d8437bb9d2e9ec3067ddd997ec9963b49
MD5 0b73c80133da0964e01e803ef466e5e8
BLAKE2b-256 703ec6c927058b31c15e71217e5259e7deec0f7d9e5c4b52a8d97bf5113da2f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5cf16ec0b43d9161f87d4508b922eae6daf61c135e2b9140da3ff4084a41ba1f
MD5 36e30c91b2608040b09c8f95f4150aa4
BLAKE2b-256 697201d5d0ef3b0123bd2bf6f26ba97f140e03b103d04ba328b0cfb20c3d1469

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b0ddc160e2053c07086747e87856ed20e622ebc1c6b44a3e89bd797bacfabe70
MD5 b4f3b815f9d2e60aaaeb67fa3ede4550
BLAKE2b-256 84dcd892f83a04dbdd6f68090eca92c38b0cfecf2ddc211b55bb25b6b3f79512

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 c2a3548c5a3d092d1e0df50b3a6e41f920c4d6a702c488832caea792167a50d2
MD5 c8da08ee81a1423f2cfcdde82b7802be
BLAKE2b-256 515ff0fa9e9068a365e511cc8f37348449c239b551d16ed7dd74e5049e2f42e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2d9c4f7c5b746dbe336ec3d751ef12f5914dd7715f4b92ac6bd21c2d5a2418dd
MD5 417e4199cd44158e8425e6910484a518
BLAKE2b-256 3da5b1155928a7f883d17e76d5ab78f934de0f7240d686d40e2ecd55c0b34239

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 6458df0b3ad35f3d1cbc6fc7a18b8791195c23d3a7823371479bbb0fcab49ec1
MD5 30fd93f3c6f64053672e1f1402e45393
BLAKE2b-256 292002932ea1cbcfcec0350bacc454ff364dfd2197288f828c905b99c41517e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 08f5fdba95da62abe41117e3576691081fa49d826d4c394142ef9849c69bdd55
MD5 75552f819c47bc544999d9da6a4834db
BLAKE2b-256 7694c898688a22aa96ee5d3e2e6039740668a9c356f02443540da3f68de1fcd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0e485a61d440720593792056f4a87831b65e774c810a413ecae3ad0382cca406
MD5 3021cbabccb2f2f4fa8f10d1423ad612
BLAKE2b-256 8e7a03066009681b02a45727b12a06cf2b160ab1ae3d426f100e8ee8f079bb8f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3111cfccefb76516d3dacd10e15a690512c04ef877f03de0c10eb6386eae6305
MD5 c130af17e6a4270708c7b6a24b312983
BLAKE2b-256 318f748200007c04cd33e0edab30733ac9d47a1bb5eb89942be655b2ccf10e16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0a02d235cdc3a12b06aa9fb67dc4ba8f4703ac2d6672e34782b31c95114803ca
MD5 54ef8faf5e9d51a7ab8151b00ce7c2d1
BLAKE2b-256 ef1936d9df864438930ae88d38ddf7ffad799af99a501ebbb4a3bbd1b5bb2dfb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d72ff9f68f2f9df343fd270330045884631827f23e7dea31cc804946812cfd39
MD5 7fe84ab45cc0eabad9ea34e70b74bb1d
BLAKE2b-256 a7872481a4532ca2da5751c14908250432b6dd14e99b83fbae6a3ac2abce4a4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 52b85ccd208ec24f3f98624ff719799bf33516583aa71a35cbe0bf8f17f1d6fe
MD5 cbbb7f2d82c1fb08c5b3f1b3b5e8a486
BLAKE2b-256 ebdb1553e3bdfde590d6959f24bdc942d558d8546aa07f5f75c8e51036b40b1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c5448de7987fcaf5e2888867fdb6e0b3d2fb1ecbfb25bc4934b9a2421a6ce610
MD5 5b73d05632af94457e58e97969e9516f
BLAKE2b-256 a94021db88bd6bcf265693bb950b342febc6b40ac4870913fe9932f4ffa4d383

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d230fbcf3505206c5c29da821ddccaed52bc4b1a4b5e03a3078e96ae79f0b42e
MD5 25bc75628bf630d785926be45bcbd52a
BLAKE2b-256 0f5d6a5e757e90898a7042e7089f24f3d1f80c8a9b59c917e0a322965f4c28a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.11-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 a102ad845e5c26f27aaac71ce611c4b2725f3b731298d85bc460b3b3ad1e5152
MD5 7f3049582c25050ec95066a8f77e4bd8
BLAKE2b-256 6d25dbea4f325e728bb4438157d9537ab0041a0930753ae5dfb9a93e95bddd96

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