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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.15-cp314-cp314-macosx_15_0_universal2.whl (532.3 kB view details)

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

tensordict_nightly-2026.5.15-cp313-cp313-win_amd64.whl (602.8 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.15-cp313-cp313-macosx_15_0_universal2.whl (532.1 kB view details)

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

tensordict_nightly-2026.5.15-cp312-cp312-win_amd64.whl (602.8 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.15-cp312-cp312-macosx_15_0_universal2.whl (532.1 kB view details)

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

tensordict_nightly-2026.5.15-cp311-cp311-win_amd64.whl (601.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.15-cp311-cp311-macosx_15_0_universal2.whl (531.2 kB view details)

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

tensordict_nightly-2026.5.15-cp310-cp310-win_amd64.whl (599.6 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.15-cp310-cp310-macosx_15_0_universal2.whl (529.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 f7bf88a7e7a5dce4808d3d369f463fb0375180bc91d96f97c15c6e68bf95c5c7
MD5 a930d90d5041171574f3719abf9a1220
BLAKE2b-256 e8bf0d159d3980fff74543157e5515a5e45ec09c5887371be3c264d20decb734

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a91e037f5a7e342577759caddd0f5db7e5e199c111b5924bfd6c96178eb75486
MD5 ebc5913a90ed8973b628609df1da1bb3
BLAKE2b-256 c379e9ff03d8fed6637b5f57ee9927eb5e27a011d9197a286cc8cb1d4d925650

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5639a88585e1507462fad067ac491ac35eec6164fc8b921078fd4ea58b87c0ad
MD5 586e50ebbd0d0ddbdf227810ba85df02
BLAKE2b-256 cd7eb0f6275525e6a499b5f75e862fb66fae990f369687fc9aa9adf8acf15a41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 338639aa73360dd18a0d1f7ca780bc83ebed53e1a28ea6120cfbb43e2ad77f7b
MD5 c775f45ac377d9672eff47e0c6ce6e12
BLAKE2b-256 a137fc1acc727865fd9ff6719e6dd095adf5ab8cd1447d1adf016c40b969e245

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 50c2e47e47ddb8ec01f8b64e62f16425e3fe2b974ed10855b1e09bb4368353bf
MD5 18b1aaccc5d2ea1cc1bb3a23ed27ffef
BLAKE2b-256 422cc191b6df0c68ec4da38e8406efd7f8a1d605c365ca57167d0173210ceade

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 def5b919a5a4a3a736f836d6b457ce5ea8fa394eccd766647dc9fbe55726b3b1
MD5 b89e95d29976b51c0b770b6e23242bed
BLAKE2b-256 fcce5ffa5ef5c42bce056aaf7264216a9cca4675a6fcbb7439bccd0f5d58bf90

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ea83396928afe0b9707cfa48a5b231cb3dbd8d1a8290b6893d87099ef1973d32
MD5 88671edac1a5100abfe716c2ae8e1634
BLAKE2b-256 b78659de607012c1ecf345a20c83bb8ce27bafeadc259fa5394a4bb06acb45eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c59f5f831b48f1d3e2e056bc86b8ee0415fa3bc2ea3d7b5e1d07bdd30fadc31c
MD5 ba7973ee73138f09519ce1ae31a320aa
BLAKE2b-256 fc743ffc3297b88ca9bea66f596a49f889babe7e8f40415736ebd74643e8d92d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 eed32e632a1e1abdde6092604ddc4cb24ad87a9657204fff99a426de58bca8e2
MD5 90468574d575054abe76dff7e7749792
BLAKE2b-256 75b05f4aa88f52d20ab0ecc44c17966059626b07603ca18cc93ea49c6b41dd7e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 465a62d20d65ec1ce54ec5277824e6a72d4d78f19cdb9068665984a13bb2bb6f
MD5 c0b3fdc8f9be2c259eb7c5f5d5f76575
BLAKE2b-256 17b713bed79030afd84f93473923ef9d2352aba5173b0726a6e85dff8c4876e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f54ed15eab557b095b8434be47342c5376df504c1b9b19d73dadf2bf557ce518
MD5 7361511a3f95a2c7566df8f0ef94edb7
BLAKE2b-256 96f9f30040fe10c11e1869a48961b24194ca9ca4e1be15625a953ebeac324e7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8528fa8e16eddfcef420c07b348f20818a234b01fb5ecd15d255240fb0c49b8c
MD5 47027097caff919f4bdf0406a3e236ad
BLAKE2b-256 d2663dbd70cbe27f058b975eefd5d82640fae810b067ee2abc890c8207855cc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ddbada26a326206a24d562c7f53af82d429962d4b5a479daacebe6df5facb961
MD5 de410cad60b8d8e316e3c692ea03e740
BLAKE2b-256 8c447e984894611ff01070e92a52f9edae478a5e0b6dce7afadc2f2a87f3689e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4f475f84b6c55496e65a1cb6503cbc9e4dc2236818b099ed1b12006b5143796d
MD5 7e16f298d1355dffffd05bad9734f879
BLAKE2b-256 b46848f2f2665224154d6e58a8bb5940b714faee763cf1c91dc4dc420d976ae4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.15-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 0d1f337a8a23c19a0a2a29b3b14063af03c30d05d6807b89c9857b3a213aa76b
MD5 6ee14976600622332fe61697e5ea159e
BLAKE2b-256 80916a863df78553c8f5eea255f03ac135b388813caae0cdc960a3e035e3ac1c

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