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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.6.3-cp314-cp314-macosx_15_0_universal2.whl (540.3 kB view details)

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

tensordict_nightly-2026.6.3-cp313-cp313-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.6.3-cp313-cp313-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.6.3-cp312-cp312-win_amd64.whl (610.9 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.6.3-cp312-cp312-macosx_15_0_universal2.whl (540.1 kB view details)

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

tensordict_nightly-2026.6.3-cp311-cp311-win_amd64.whl (609.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.6.3-cp311-cp311-macosx_15_0_universal2.whl (539.2 kB view details)

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

tensordict_nightly-2026.6.3-cp310-cp310-win_amd64.whl (607.7 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.6.3-cp310-cp310-macosx_15_0_universal2.whl (537.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 3dcce3cc2acdf880e6759458f93c86c369014043898393d924bd30649f32ba35
MD5 f0607c66423e1fec38ba79e24a718fa4
BLAKE2b-256 e634def6d7df0375d7bd754352bc7007e92a7e32ffb72df4bedfea66cc457eef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 07d867e65b33ffc3ce54895a4f02e23927438eec7220961faac637c694654a67
MD5 96fa81618af1f3f507a8e60ef32e5f8b
BLAKE2b-256 90b6be1911c914a11168f969228e2b9449bffa832d4a0829739c44b70101c49b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e917273e300c57aa1381823ec9666648141685ec694d44c54ab699f01b5e060f
MD5 703573b30bcb1795a87af2f5c6107806
BLAKE2b-256 ffd9b32c868caa7de84538ea671ac2dc45442cc1eaf85dfb76b10af237412437

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 289d130599796838b820c803a3b3bc36620a5885ce6a5013a0a74b6bee2fa7e2
MD5 c43d7e311ad0c9d4d63d23bb1f49ba83
BLAKE2b-256 acfa9bb83c59a5346d505c52f1614a0d6c20725d144b430b2724fe6f8cb7377d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d3b1797623c87bd2e36cc5fd01439efd2a067165b278ac5ee163cb084ee19872
MD5 0d11686f76b99960fd9693d1ea821f9c
BLAKE2b-256 3c8367942bd8f7425e0b51563c46eaf26e01da0ce9e38be7240de4560f8a87fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f5b165ff852158eaddd43f1dd08a09c4b72527fc2de02cf3ff6d6155d33c9050
MD5 31613c05c232a0c2bf1fd6da9dea5aeb
BLAKE2b-256 85c16f8ca56f8d6e5a1fd7e3aff8079c2a302e7684f70caa4692d8bd0429f9e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 82df08321a7d58400d4d85f963eb1fadc067ead8152dab5702e24e3fcfe2f543
MD5 6d750c2cc6a808e9f9e156a52b211ec1
BLAKE2b-256 e53022b9edc9cfa69ac7ca63367badf6fa18f7942fea65cc20a10f1033ce5721

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ff2189d8b8d1f11a5f0811f4e274088e9e2f7858cb7cf8182a1d249af2f17f70
MD5 b5b1d369e414c3f99e2cbc8821bb68a8
BLAKE2b-256 c23add7efd8aebb6323be6ad448f370ada91dbe5fcae445209ee77be61b96685

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9eb0d881539d2b16d2aa03433acd99c3403148b61070e992fb3f7c719333cc27
MD5 19e4157f4536d25aa6b77a53198ca20a
BLAKE2b-256 3174cb61af3daba9e3d5094bca48fa49964d44f9e26593e7b8d82911ab66b9d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c0fe75822df1d98c6834a67a6c20b435c29ac7478d988c0f47cf4a17506a6f42
MD5 9298dc4223b3e35f97c74121e56975b2
BLAKE2b-256 476f87e67712484a9a0e5e033793997cacc5c0dbf8227f64cd3f00a78b938f84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 70d6803f296991c93c709605a9df2a9facaac76a790f3693ce14ce5f94450a7c
MD5 a33ddfad6f63b36e7c35f4187714e82b
BLAKE2b-256 0c3972133e556563d1cdc1bb8a5f53323de5d75b798445d6569cc274635beabf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ebdd23b4fd8ca609adfffbd2320074e2a7c03859aaaf1cc6cae0a2c4488544bc
MD5 115a962ae613292bac0afdaf8099971d
BLAKE2b-256 2a93bf98543a61363aef613b53c4118c323f582b7c36da8c7da7d4e1093814f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e9ff65ff1595c3e5631ac9297ac7c28e569222ae716a4454b23ed19bca44c999
MD5 7b8116562d2d6bdd6d216c079a04f768
BLAKE2b-256 e8d6966341d68b8cfe2082dff39e2b2861ec35eca26565d95228903c33d5d64f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 72f6460a3a2ce7f03ac0288964ec86f9ca0ebecb4bf402a5ea1ddf3460784e1a
MD5 52029fe5b20f1d6bd3cffbd356e64e1d
BLAKE2b-256 d65bfef32cfd5fe3aa14f507f6e9e67798ec3a1fd376176ce72ba1bff4d1dde8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.6.3-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 332db9b4a63f04009165390077386f14b71c39fe2cc1b6a644a812983a7c5faf
MD5 46c57a60009ea8d066fcd0a7bd2177ad
BLAKE2b-256 cb120c5544f7d3f64f924462e1b91c910a1f97d251416562378ba5edae2dec2c

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