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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.5-cp314-cp314-macosx_15_0_universal2.whl (526.9 kB view details)

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

tensordict_nightly-2026.5.5-cp313-cp313-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.5-cp313-cp313-macosx_15_0_universal2.whl (526.8 kB view details)

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

tensordict_nightly-2026.5.5-cp312-cp312-win_amd64.whl (597.4 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.5-cp312-cp312-macosx_15_0_universal2.whl (526.7 kB view details)

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

tensordict_nightly-2026.5.5-cp311-cp311-win_amd64.whl (596.1 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.5-cp311-cp311-macosx_15_0_universal2.whl (525.9 kB view details)

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

tensordict_nightly-2026.5.5-cp310-cp310-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.5-cp310-cp310-macosx_15_0_universal2.whl (524.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0df6fcc7495c8c1a87fe6a653022d1be486a80e77c36855413314c8f1e2660f3
MD5 ccd57e913973128b88e5349e411f6acf
BLAKE2b-256 19b2f01bc137376941b32cedd727aa200140fe343c2fade9faff09f642bb7749

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cf155b0e1ec82ee52853404ebd52ce0d938d564651098f7c7a9c8436b9e9ae5a
MD5 050d8c6f9526c8f18fb04a8ee47b751c
BLAKE2b-256 a122b233c3277449d75672f11ec54f99a8807e0c1cb8a182200e06cea347d7f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9d330749c383ae955df242320c85db446892ed8f209275926a84c3d544468b16
MD5 86db1ad07188017a20615f47472c53f1
BLAKE2b-256 9f4b3b7eb9260d4ee25264f93848ad5055dcb50b18039abefbe5f27287b68a5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fa924dcb4310663e603902277c8847ea8533c6059fbd306173733b5afe407b15
MD5 1ebc76990f4dc62ebd01ce284f85e38c
BLAKE2b-256 ebf828c4a72b677f757ca81cbd501d0cac16b3f00a73f5fd5f2598326ae117af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c27c5eec7fa8477870184af5b7b02082ce710c1d266985394a2d238941770561
MD5 5e3f481f3daac2bb941d3457bc8ef6c8
BLAKE2b-256 49edcd1e062baf3cb697743929891c92f8a9e8ba8a2a5b104127c2c0cb41cb6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 26251b169b9b8486edc1427af86fbe4ef3df7c481a673fb2d04d722676242feb
MD5 e7713428a9085bbeeae3bf6c2e3fa49a
BLAKE2b-256 55a51d31392d1027351b01212a14e65032879c1fd89be546dab3278170e13e59

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 18b7b8d3ee0eec8924867f7880a547927c66afeb4aef78b9b162bc255a7eb0e9
MD5 40260f6565186bd089a10fa0718c3cba
BLAKE2b-256 a012b25d24ac4ea0d4ec5c83d35f800affeb72ef630740bc1c887cb992705ef4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 58cd717ef23b9041214fa54478abd3a5ee02e623b659267bd1abb27ca44839e2
MD5 8e7ad8e59c4f8f6ecf1aeaef928e0ef3
BLAKE2b-256 98987f66056efef989416f4f4fb1f395621e232b628247332759294a533ac202

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 1744524c8fa5724198ae62c42aea17fd8725f91762ecdc2468dc4eac72c7ccf8
MD5 d7df4d67e8aa29de213dabe9151c86ca
BLAKE2b-256 e040425a77bec2891f9ef3e20cb8580adad6b57068e070e884cb3f6b08c189af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 284306c4df7d45bff630490286fbae243fa627e763f39227c223616e84f41c82
MD5 60df530214cda3215c739b0acbc332cf
BLAKE2b-256 555f205b12985c5f22ff0243e2a454f3d07ec6dbe701dcafcbf0106597b872ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0196dcf8b6346e8962fd3fead5089b42936899753126e2ec288cf9ee44c09036
MD5 52a6d866cbece20f5f6e9202ffe584ef
BLAKE2b-256 b7c112a5e6caf5580fe76a6dec18630f3d3a65c69a266c17e699157916847155

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 11b84b7bc181307eebee6252abc0024f9d8d623ef34328c0dc4c19c920963c25
MD5 fefd7a4491f7802e1ec74a47b72b5916
BLAKE2b-256 bedd21787ddec690bf6a65dfb18a5ec905b5f5d2a881caabe816351a8b127404

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8b03e5074d6280212a3634f08ac8e903c58b8bb209a218fcc01b870c5eebfd63
MD5 97fe89bb99c6e72416ec8644c605aaac
BLAKE2b-256 c3c603ec0a922cceb094ec32b5d4be34b8c50ac544251b0fe982af5059479198

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5c8856c4243a195ea317cefc9bf028a119092bb335be6c4f17fb22042f156332
MD5 efb916f11621e7965d12355b61837a29
BLAKE2b-256 4e4af3b0adbb0e12871eeadd3d6a41021f8ed588938d385b4a060d5a0b5b8b1b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.5-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4281df51680ed631a7974f770678a81a441161161eea0ecbd82428fc5fba3550
MD5 ef82dfaf86ec96175d7a9dc6e4233c44
BLAKE2b-256 88efefb6ae66e407a1d40a307404aa8531694ecb6036d0dcc169bc73cbb4c1fb

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