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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.5.2-cp314-cp314-macosx_15_0_universal2.whl (529.3 kB view details)

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

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

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.5.2-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.2-cp312-cp312-win_amd64.whl (599.7 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.5.2-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.2-cp311-cp311-win_amd64.whl (598.4 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.5.2-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.2-cp310-cp310-win_amd64.whl (596.5 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.5.2-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.2-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 5c8c75e081490a62252c1d849a0fddfb6cb21eff04859ac2e758580148ebe9bd
MD5 6a815597266000dd57ec1e2780024ccb
BLAKE2b-256 42720d3cd5832d35bef12281d2f08abbe4926a232320a0ae0ec3d0c07783d3a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3e2308e5c7b59ac44f7778f5df0803ca05888349f8e09b5e222b626a9021fdef
MD5 9f04f2e034153e0d118c1a9165b7fb16
BLAKE2b-256 0fd57690703ee8a6aba975cb9cc3fca77ac084bcba540ebb259efcfa2fcf8a95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 fb32ba58f94c53bd76b75bb6888210dd27815e4e0bf6c17c0bc91261402de853
MD5 06e08d276d540e544d13dd1f91d9918c
BLAKE2b-256 3c722e1e9b1fc25c57946b990eac7598c0b475874bc6d36e5a7a155522a8e566

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ed650692fd8334cd1ae844868b980512f6f2a622a47f264cde23ae767f563210
MD5 567b2f79c0a38e08d392f5850c3aedc0
BLAKE2b-256 afc8ca67c0438b03fc440ea4c7b6701a9168f84ef2089ce9c90e121d8dd83831

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cd9529f0ef2e44e7b0947e9e2e9f93f4067ca68d0cc6d10e12a776ebe3ce27d5
MD5 8873f3a4e3fd7c972abe9e5032f2dce1
BLAKE2b-256 76b494ad1d649e586785eda89f2ea69919e89df236c49edf39c58c6fcbbe5d41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 47084ee144f711523440e751120a89f23cc5342d8df55b29aa0420be34418d2b
MD5 c92f1add6e7b91dea4fca75a0d1ad356
BLAKE2b-256 c32d8339352938a56ebc56a9158977029b040fdf9ad9c5896ccda75a9023df50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 feb6e41d4d1439903ad69f70ad562096285aa2e1638fa126ef60705965c5f66c
MD5 f268f16ce0cf724f603447c1f1e167a0
BLAKE2b-256 4d8440a9a454143425618606c9b6e4fae97ac40b158c6e6afd0e704b5d6b33ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fd19bde7a21380ce5876fe85595bd67bcf0b6769f33172a1469d4121c4dc6010
MD5 d556309547ef0aded9f34064d73936eb
BLAKE2b-256 e2b3d72cc0da05540a8af22423eb51568c0e68f4eb6d6264ea5fd7f3d99017b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 481d9ff889445d94ecf987beb7ff0b19044bcc4a45c045d30eb796e1d53b0124
MD5 1e13ce4e025283c0d35f0d7bc68f4ea3
BLAKE2b-256 82e4eef07a483e307e99d3e5f7c313e193ab3a21142978637e8f0315e361432d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6749a978e93101e24628f3f9dd2c7c46c33b68354c0b7f23493485e7b3a96379
MD5 b94a79c4b23beb6cb53547b82b1357a7
BLAKE2b-256 3776f0821446c727947b634e18bc24d59c6aa3acf1eb6d968de1d4d184a9e844

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c2938ea79376196e8d0f96abbdae35fd561e1a6fc00b30451c794713638cc29e
MD5 14034236e45cf5c0841daa9782657702
BLAKE2b-256 9b313c64848392b59a4f9d10a08c0c31bf67b74cec599682824f66da1f73155b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 c2ee939995bac00db97304ccdcfba86b2569c3537828a9acb490fe8be929d6b6
MD5 6f23154164c19408854761884c2e0870
BLAKE2b-256 0a0451636ce8f172825bf0e7d40377e922c6374518bbf56936e99c74984220fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8cf240c9103c0dcef9b3e2a391e9ac8d70aba648141bdbd76c45f080f6cd4ca8
MD5 3306ad127808eb49203a4d0f8da2f42a
BLAKE2b-256 8a6852095de6644e660bbec0d03190d66d3e51b609d8f0ce63bed2de725f9f41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e62b8aa1faabaeefe779db15c1d5621992b641acc1939210acca78e4e3f18fe6
MD5 96fd1f0e79139fcdddf1ec2c07315d7d
BLAKE2b-256 cb65d37af7adffd09a67f9d712f4cc4676706d7eeb9aaae678c0239f7fada346

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.5.2-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3acbfeb14711d76285d0709a6bb488e30b5f03c71ea1f3388762311e01624f41
MD5 d101ab540d785deff71e03bfa1f6267f
BLAKE2b-256 932593babb6534c52f79800308f9c90253b99bba5c81c8fbf874d9df647d5e43

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