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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.6-cp314-cp314-macosx_15_0_universal2.whl (529.0 kB view details)

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

tensordict_nightly-2026.5.6-cp313-cp313-win_amd64.whl (599.4 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.6-cp313-cp313-macosx_15_0_universal2.whl (528.8 kB view details)

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

tensordict_nightly-2026.5.6-cp312-cp312-win_amd64.whl (599.4 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.6-cp312-cp312-macosx_15_0_universal2.whl (528.8 kB view details)

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

tensordict_nightly-2026.5.6-cp311-cp311-win_amd64.whl (598.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.6-cp311-cp311-macosx_15_0_universal2.whl (527.9 kB view details)

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

tensordict_nightly-2026.5.6-cp310-cp310-win_amd64.whl (596.2 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.6-cp310-cp310-macosx_15_0_universal2.whl (526.6 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0b27214b7cf673a49178f7d27c0490dd441a03df6fc1195c6e3463e591f4bc33
MD5 04d82f66e63efdf25baa4c422ece510d
BLAKE2b-256 157b94a646129e7e5bb9f18ec7a87d9ef218dbac74a59ec11473c0c6992e8267

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 464b6c0964125047ca5be7998ea1c08f50183cb5ba87a34fcdac6813293785a3
MD5 dd629a3d2f5d1b307cb844532c0e7611
BLAKE2b-256 1e807827b3014828ba5ad230ca368d937cd3f8015f7eb31435902989b89b42a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5b370e8bdd5765ef1c1fe4ba30eb8ee6d000286e012033cd7a2484f9532000bf
MD5 1e2e08a43b5908d354328c631bf5cc91
BLAKE2b-256 0e709356cd609b93fc2cae250f82f7d2a898893774c69f9aad7912998d2442ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8cfeeaa342153040cc9a98897cb84674e575c834b7846e433f976dbf6d49dc6f
MD5 0496a45a80446873faaf42b12e1e8036
BLAKE2b-256 a85c5fe2adcbf4a783a407f469e902d3d201ad3406d9ad3e0f4d30c0d4307de1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5af61624f5bf7dee52aa79ed9f490d8d1b13dce28c98f75c1509d3212d4aba17
MD5 b9aa84899921c72bfe551de3f4351a83
BLAKE2b-256 3986102eb7ceaf401bc61bc958356ed9c14950f665a191785ceed494e2bfd94b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 d8fa7906f0c68787033f3e221e80861975852d0a23db788fde5d6c3cd22ebd4f
MD5 3b8adb8318dd3d5488409963df565f28
BLAKE2b-256 47a3c6468c0f486c6c26c5d6f622e7166b499b11ed0f7941488ffb28079465aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3285bbca1fb0b9ea2ee1cd2e354bac8cc57162196fb8d73c8c6cf625dbdd0444
MD5 071ec216d88a8c102052c1da601d04e0
BLAKE2b-256 f36855538ec7373ed1001e89e2ff3112e784f3fd6d9808aa464bdca8f9e4fd2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8c93afb70edd9d2db3bfddc7c72a03490ab42aa275cce0ebe3f07e5c9a3dacb3
MD5 45175f03beaaa2d9b55d17d16a596ec5
BLAKE2b-256 c06315e2d59506f54b62d18f5060bbb0aeccffccbc485a1d1d6ea4bfc0562ee7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 80fec73c9f777682fad5bad933878beca91a33a1a875303629b2951582abb830
MD5 da3ecd7d323db469496d33b9e2e57a3f
BLAKE2b-256 460a290ff301745bc753c2b2c0bfb93aef308954a36ec55df1f2d43e8be0db36

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c2641863add2e892c1f34824bd0945072094dcfcaa9f35f45d25fd32fd8b17c6
MD5 fa8f67d9b1ddfb6bc6b5959b96280dfa
BLAKE2b-256 99c12c4aa4b67f4ad97ef623f0cf3ac9435a75932984cd2669a3eed4561f88ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1988e5fcc59e7bd823ba969530cab6ca097113713d530fe26f7c4fccec102c2a
MD5 ad92ae7d619d92065e3c20b1bb69c94e
BLAKE2b-256 f4848d03282a6c6edd9ed928989933e49adcf6cdb4bc59bdb1a90b1700372947

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c84bfdf847d14016f87477f776cfa6352de1bef7ac10a4cf387d97b93fbaa54a
MD5 e47b31197e92bb8daf97cff1d20893ae
BLAKE2b-256 60c505c50a2162e8e6c24a7793d6dba96d7aa30920de79ad5dba79453e25ba8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 39439ee00a7b245dd70323afeff05a9dbb8566dce2a1abf04d8eed32d89dcec4
MD5 d7ff88abcb626d1a8047ac26c88d9728
BLAKE2b-256 eb34da27ef9b5239692f7dc77feeac8b681a90e58e7ca97e53e1adaad3c767b2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 885ac19e656d0cb26eb2c59115f61abac40de31572748beb9706cc7a1e2541f7
MD5 1dfff7b58b7c90f02f6cede872314b7f
BLAKE2b-256 2fac94ab20b2e50c49a95055e1163f2e1a2fec35b68aa950e4e48ff39e91def2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.6-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b69944ea9509c3dbd30ab5969647ca7b08ecf8a2807c038ba3052e78d862bc43
MD5 83f7e135d6289e976c14af9c928ffa5a
BLAKE2b-256 a8b38f176f594061c2fefa6dbd336b0cfc520e5d44ce0cca6d52d997b1f56e17

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