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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.20-cp314-cp314-macosx_15_0_universal2.whl (538.3 kB view details)

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

tensordict_nightly-2026.5.20-cp313-cp313-win_amd64.whl (608.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.20-cp312-cp312-win_amd64.whl (608.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.20-cp312-cp312-macosx_15_0_universal2.whl (538.1 kB view details)

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

tensordict_nightly-2026.5.20-cp311-cp311-win_amd64.whl (607.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.20-cp311-cp311-macosx_15_0_universal2.whl (537.2 kB view details)

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

tensordict_nightly-2026.5.20-cp310-cp310-win_amd64.whl (605.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.20-cp310-cp310-macosx_15_0_universal2.whl (535.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 c2cc346cd9c38e38f38d3ee4e5d5d1a4c08b1feeecba33c6b42e00aa28e7ba27
MD5 ff90524aaafc57a9219dc594b085b072
BLAKE2b-256 8d87c1fc41018833ecc78a9b5122f488d5d0a0f8d4fdf69c3bfc843b8e77d996

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bd1473b6cfabac50aeee9a394722286baeeb75563c7f8e699c6360529402e9dc
MD5 0733052d03c82c0f2bf0c4317d5338a3
BLAKE2b-256 a6410a4b1bf3bfa18ee34ac8162d2e1b28ba1d1e27131957e731060a6c8f773d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8382ee07a821f69521629330b1475051e139eebe41089b350b373b824a0324cd
MD5 bc79c5a6e2f97426ec25b85f6e8eb2a5
BLAKE2b-256 66bcd3c3355338991f206f5ac4bff80778bdea9f9ee8f3654896f427113ba65c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8f83ab575b9557fecc6b20d70185a6b0678ea1f935809e51263c6f9ca8b03450
MD5 7b6287e9b0995d9010295342d9518004
BLAKE2b-256 beed9b3411e4f38424b8b6c54a2a3e14b2409874bc9bacdd883a25e3c311a8a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1319521f7f6e65c5286eb92e01b4cda0558efcc338fa7887a59687b208db0d27
MD5 73818999dea1b01ac355a9a825dae9ed
BLAKE2b-256 36571ca79bdafd2c28182241b67d3e78efb61b8e068abc659d16fd8d3e45fc45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1667eecb4b5b7c391d80c507b87b5b50d567a852e51eb434da443f3ec4913048
MD5 dbbaf3544f9cf717e4dd73e8207544aa
BLAKE2b-256 c8b74b1a95ceb34cf234a0030e57295f81c339ea8ec1e6f0f631f87b54b023d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b129bb48444a51c437e0dc81f23fab8a51a471ab4704eda002f39f88c46c6661
MD5 d41b36f229c36233202045a36346251b
BLAKE2b-256 50b4d385c5765d09bc11d792f8664d8930dc546a005ee1945a2574114ed4cb9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 23aded5b20fcc83690ffb1abf949e14faabd8c2838a539b74ee44c46c1aaa717
MD5 c8756a4d9a653164d20da51c1112b173
BLAKE2b-256 e4a8dae4a261fbfd0434b830bc06e514c5795f2dabba12d97a27a507acb7731f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 87aa3a6569722dfa5dbd44d617c319cc3b205b696a3736a5694d7cd30065b099
MD5 f61f2ef3e0501d55993083179f06ad18
BLAKE2b-256 be6489131aee6580162934696052212ff53c2ac7ced9e7c25cb3f19133aefd9d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0686c52bb538ec6c37caf81b5b08fee01b540d699e6770d152184edc693fe811
MD5 ec2b6e352824a84031079df7587b0459
BLAKE2b-256 f1e53aaa7ce408867339da08aff316445e31abd05c604043dc4ce3b6aec6ba91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f971240ca9e6ff460c4b342da7bf5c5d065773e3c6eee10946b212fccdf261c8
MD5 e1f82537d3773ff16007c9f97ac0d971
BLAKE2b-256 09b8c19a5e2a53ba5d4d6f536c36cf90249a0c7271fb114994dd5806b40fa799

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 45a1ab2c71d8f3b75f6a4abf5782b2611500bd3259bb614c12e29de611cc8a4b
MD5 d9b5a5e8ba459e8759373a5b3f8068af
BLAKE2b-256 cb671ec8415a90ee50fcfdeb7fd486ede8fbb55003db70d56b073db1600304a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 17661610c2f49b55b78f232a311caabce6298055cd3106dee70c2b03964fd694
MD5 a00c436d1e7cd3e38935e25c507cf4a8
BLAKE2b-256 965bd8cd45a823963e36469e2fa3b8aad6e399869fb5013392a96c1c1a11cd7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.20-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 052ded80de342fbb98189bcb18d0493be071fe432238870b27bd7ab940eb0f77
MD5 6d64e5afeec375d56a5e0d6862a0faa6
BLAKE2b-256 31d9d8f30a0e0424f5a7ddc3699a25d9f6f0ce142582e24f64b88c444d2cf4d0

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