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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.1-cp314-cp314-macosx_15_0_universal2.whl (529.2 kB view details)

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

tensordict_nightly-2026.5.1-cp313-cp313-win_amd64.whl (599.7 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.1-cp313-cp313-macosx_15_0_universal2.whl (529.1 kB view details)

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

tensordict_nightly-2026.5.1-cp312-cp312-win_amd64.whl (599.7 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.1-cp312-cp312-macosx_15_0_universal2.whl (529.0 kB view details)

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

tensordict_nightly-2026.5.1-cp311-cp311-win_amd64.whl (598.4 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.1-cp311-cp311-macosx_15_0_universal2.whl (528.2 kB view details)

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

tensordict_nightly-2026.5.1-cp310-cp310-win_amd64.whl (596.5 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.1-cp310-cp310-macosx_15_0_universal2.whl (526.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a9851c62e0c2b4e1c8ab2acce1b737b16698e655ab13d6411709bb6a3f89ae62
MD5 cc07546c5f6125fc9b26b5d831e8b986
BLAKE2b-256 8df09113b59a781ec509f29b41aaff3ce5b1ef40a2893ca49b9182142ec28337

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3fab81333e14e1718ccf33f1987218a7bec6d8d6bbf0d6507f804426df718bd4
MD5 350947ff5c79489ebd849d50cea18245
BLAKE2b-256 a4e5a969191ef857a129c079482af66f50f508e73531ba850921c0cf2a6ee3f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 b54e59dda4955c5620f84537e9525dfae54d778e8b259e5cc0ec23d6503f7ff3
MD5 13524c8cbd91949add23fe3edb39d6b4
BLAKE2b-256 f521fc603502d8fab19e1c731aabb8a4ad3f52dd87dc44571f0305761380a417

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6536825a30edea2769f9b3c09f33e339f6c3109afb13712c837e3e2366f2fe76
MD5 275c4620a836b71d57d7f4d98aee3b88
BLAKE2b-256 f6040501e79041f03c499b94ea764743828e76aa1465c6a8b715ae2e26118e7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ab5513f813423b19ff69f78525533c97589082dc87389b2bbd42b7a3c9458585
MD5 810c8c30d0089bfaedb95c4d948b7f97
BLAKE2b-256 a12a4a83aec763ad82401a2a3113cbf14f3062f88a9a42474667a974eee833d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 89db5b688b82d6c548e9eea58a5d3de5402f928a6591cc6f5399857e340b63a3
MD5 a2db54ecff9597c0aa8c0ae8bdb09b79
BLAKE2b-256 26ce7d0a899365d144fa70d76ebbb4ae68d6d3d9fa7b5eaf1e4736e85d5d4376

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b45e58c6e0f9b26f6e142ead50292b9d40d0bdf919c731cff86126076bff4e4e
MD5 fd4e3db67ea86beb436055dba4ff736e
BLAKE2b-256 06c7ad27b48fe39ccf192b1589f3873d59bd5b051415bf9fd64a9762d38e218f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f27cc96e1c7b38566ab3febc7880d2a16619bc3f62a51773ccf5745e81db2095
MD5 9bfa79c6b15c085358b2c7674d058120
BLAKE2b-256 777ccb944f71da3cd80ceb66e57a9050df02075cd7f1fd00664248b425c61a29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 e4c813dda651f668fbc91bc9490bb6094767aa7a25fa2d310817637d10aed8df
MD5 2136f8200b4390f4e8ae623cd89c45ee
BLAKE2b-256 1af746d2c528f81a2ccdbabec5e26fbf0e3053abd747954d806a71ed071d5c6e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 044a0715ec66c91efbd204dc8a03ce235901666dfd0a48184a83346b1f327475
MD5 9e8ed0611aee9ff5ab7530a091d12e14
BLAKE2b-256 58b0587ac2b18d6b040176d39ef5089aa3988d3fcb3a09420c66f86bbb76b3f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ecdc13fb72484ab302d0581b4a4fbe4ca465b24e2e2ad5a92945c3ff0bc013ef
MD5 c002b8fd7f29bd7b29564693db3b08d2
BLAKE2b-256 22683aa69ff448be40be1810fb5798d4876f4591d9fcb1e33a88660d00a44956

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 17a397bbbc2e2de2b6437532a14a8b5fc9f4fff397cd62fdb52bda115b7391d5
MD5 e68adcf6c9b151d48cd9a1b1f9da0dfa
BLAKE2b-256 7de1e4afcb8c9bcf0ab7658a7506351816fa70352788c41ab39a2dd70ab9b30d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a3ef05378674dabb565cb83bbd7e068832fccfaa31209cae832e83a53d2aec82
MD5 3c8961d844a8067d61029abf5c7a9b89
BLAKE2b-256 8194270e4d5efce18edec6b1554ba5c6b5143d7d7effe764530986a0c3dd1d3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5a7c2206192dfcd94be03f48cf83898f60657a41511f9f764d630098a9f2cca1
MD5 e9df1ed050d94dd9f319a6f6626b9080
BLAKE2b-256 39a3ce168ca65f9ca39f56f4509f3d90bf2d3a42b53b03c5cef8a38febccabcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.1-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3f15f5e8136733cbf12706e6d7f17900e2a2fdde637319c35e6b1dc61b39dc0e
MD5 d6f9fdead3ee4aa59caec8bc914206ae
BLAKE2b-256 50489298562c3b0ae55a02b09f3a1f28fc04db0c71631b82fec2ac6499a05ff6

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