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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.20-cp314-cp314-macosx_15_0_universal2.whl (520.3 kB view details)

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

tensordict_nightly-2026.4.20-cp313-cp313-win_amd64.whl (590.6 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.20-cp313-cp313-macosx_15_0_universal2.whl (520.2 kB view details)

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

tensordict_nightly-2026.4.20-cp312-cp312-win_amd64.whl (590.6 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.20-cp312-cp312-macosx_15_0_universal2.whl (520.1 kB view details)

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

tensordict_nightly-2026.4.20-cp311-cp311-win_amd64.whl (589.4 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.20-cp311-cp311-macosx_15_0_universal2.whl (519.3 kB view details)

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

tensordict_nightly-2026.4.20-cp310-cp310-win_amd64.whl (587.4 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.20-cp310-cp310-macosx_15_0_universal2.whl (517.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 eacb515175358b7de5468972be4e4ebf9f38de0aae668145161f029afa1277d5
MD5 b7e8472f38716c0a2e2919894f65b8af
BLAKE2b-256 e067e777c86d25c3584da6adabe6af706bb04ebc524d80f817306e38152a28cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dea9188d720b4deba7e45bdd920940324c1bed50862e2fdb1706b20dc744139a
MD5 1d53b5c81bbf357bdcd0b49976ea7f43
BLAKE2b-256 720f5ba065883cd44f571bc492bf99d81cb9ba71fab8c0e0f48952c9e392e6b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4e4f42a660b55a177dbadda0bfd886aab2d0fb28bb1ba197e4dca09cb42c33bc
MD5 bbc9b7b7ba6c99d747b49c67c661622d
BLAKE2b-256 bf450b3c5a0766c08c82a3b16fdc27b38a0516e1fc92a6c9a936fd273cc0f00e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 35fa987368d7544b55aea6690ea46224d6148e37a39bb0732db2364704a1454d
MD5 a2886a2f1fad4f7c2f112aa866987058
BLAKE2b-256 403ecd0017a59411b70b0f9e51a7de95fd571275781821715ede94d2337beac5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 25caa29d86843fc5293e5f9f35f69d6f88142bf95d62cd6ee42d74e3819acf57
MD5 2cc7434620783c0e575117a67f21ae4b
BLAKE2b-256 84e72fa42ae88d3fba8c3f090f6d7f64a3bad11fc95907b446b53f95ff25e446

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3fdc850beb208f021afe09817b03d5231b5ceb8b098269d462a8aa9f4e391ca2
MD5 ea7a89cea6fe4aba72e642a5ffed20e6
BLAKE2b-256 98cfda17bde36911d191b78cbd8d7e60e4b18778c52e6415d3216cd1034fc4f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d00c1721f457239056c98e42ee60ec9e07ce2bcf991c25fef76f33d2cc24dfca
MD5 4b5d03ac798528cd86c3e54aaeec7590
BLAKE2b-256 fe7eda234baf5bb850e7d0fff98d6f17074945be96b8ede69d9dd0ea0bcc671b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c13aa2bede3e2aa0edc10cc0a95a5b9ea116d31bf1c2fe5844a67082db210e0b
MD5 6b5c459bdc4861820205574cc2df846e
BLAKE2b-256 b95f196bcb212c102a740139ce70e19098877183c283420c24b853e8ee25175b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4af05f1f95b22b9a56e099ae2cda5b7065fc6a4b8cbc91735a31b5d720624f8e
MD5 5d58637cca3189d6aed4bedf797dfd9b
BLAKE2b-256 7a75e152970990045e6e65e4c6573b886dd7f3e26fde3963840c0d5e9ad9de6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ac891ad65e521509f7a808e714175dce0e3445d4eeebb352109d857b8e2c509a
MD5 609b6a484bbde3b649376bd520cd41ed
BLAKE2b-256 a7c275e9957f5550f6e98c003090861dae7dc66907d2bc5e08f46cd6d10a514f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a74582663bc631059747147089486710a9b86af5dde0270aca010f6261b1a0b4
MD5 7983b2dbd0b8e288d63020e148f9fb5d
BLAKE2b-256 b7161d20fe0f91ac884cd45098a9d7834a74f17564c33f3f200ec8fea2b16d06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c7275e17a2a866b94edf9de4aea737149279cea56412bf80632709cf1848916c
MD5 10a897e7c499f78ae3d0107c65305a3d
BLAKE2b-256 412a84c912c0e8bdaf1fb7b855082611ed449648acd768cfce6961eaa35f8c6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 28eaa0aeb0d71144ed6de7fbd883d4d53cc0c994cb4ea340eb9f13de70a988ac
MD5 8aecd9dec3e79befd878f605758c82c7
BLAKE2b-256 219788f83833ae615b22cc7100a55c0e8e9a751770a376dede0c8629446daabe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0fa27f06033b748bfbcc59d55f646a7be4260dbed7c126c3f81c45c64addea20
MD5 3d0cb75ad1395ccb73ffef70a9c4cded
BLAKE2b-256 0fbfee2ea9b5b30826aff169b2209ceb8c701aa2d4ab24a1205f61c324660c0e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.20-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ff814726a55d0cd81fb254756e5423969a43d9ee83684986f6ceba916941d97b
MD5 7b21c5f89ccc0e79a0876092aa657324
BLAKE2b-256 50a05b84f44d61364c5ea6262fd6299fc66524af430047b57ac32f5652a520e3

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