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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.21-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.21-cp313-cp313-win_amd64.whl (590.6 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.21-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.21-cp312-cp312-win_amd64.whl (590.6 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.21-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.21-cp311-cp311-win_amd64.whl (589.4 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.21-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.21-cp310-cp310-win_amd64.whl (587.4 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.21-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.21-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 861ab55b49767c0e53aefeb9ed2195901c685da0da77cc5a6827e3632e7c3361
MD5 f8a9bc5345150e887444fc65ee8a87e6
BLAKE2b-256 c19085f729cc3aad0803a908e293dbd84947e652b6fa2703cfd69e9cdf829c93

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1c7687bc994a9421413bb70e217bd2a97ee0a1574d694f707cfed11a3acdcb3a
MD5 771d8ce99db2fed4250237b1e5dfb67d
BLAKE2b-256 101e13638bb6d2fd83fd18fe799450e9c25ebcbfca52939d9e7d9e602e93bfcb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 85f21445120e205e8d8aeb7a61d3ab1cf4f956e683f36759c4392c763f3cec46
MD5 e78e2505aafce7f0203b1772af4fadce
BLAKE2b-256 44b8430b83968d27af70a0349b9ec24cb362a29fc0106483f80ea986025756d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 77236e007f89a5db0a20e31d63daaacff146dff73632fbc191acb04fc2696af6
MD5 cad64e708deffa9c24ed43ebf9e0c36e
BLAKE2b-256 f7b49a1891191131b02619ec86aa6daa6c30a07c3746977931e08602c988cad3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0690bb8bf0e5e5d92cff95d9b2b557795ed06d91ac0fd7d82ea4d00ae549e567
MD5 bb96f83014beadbbb110bb1884e8f1a9
BLAKE2b-256 0839053245380126aaf124adfadd54ef3e564719104e524ac519ad9058796cd9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 3276f6711fd7d2b91c7a5ab32c63ab85da9490cf5bb7e8b20e4e31a7ae281450
MD5 4fcf38e2b47106b1e1cede29668e95d6
BLAKE2b-256 0d96f5813428e3c33785d3640d461ae7e82298f7e2e50afe0ba9783fbeafc362

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 28772b0fd548ef469d8d8961755afbf7d3f533a40dbb1644bf1a201c4f7dc2ec
MD5 4db313b118a85892384add4112eb6071
BLAKE2b-256 cd31dedaf39487ee499aeccbe3ad15e14d00e1a69b9bc7361316a932dec9f768

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b79e6d02a851260666a384abd1fb13a617246060086a9db9c930c65b336308d1
MD5 6c213295745a2e52584972a40ee2060b
BLAKE2b-256 73091046b2ca17ff9e8915bd37b5bbf331a922810f92e72389a43163478f6980

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 12e76988d6ab2a4bf71f2619c88b75f9f02176737f6ceddb174fc41d181f1965
MD5 7cec970ee3ccd2e92e87145b0961cd23
BLAKE2b-256 48adcb29ef2e15a795a75442beaa296d6616ec1d7fdc75d3dfbfbe2602a8172a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 748bea78c12b7e68e85c95c531907fc5619681a69fe088aeb7efdaca0193621e
MD5 655196ea5999a414c1cb7239920e3f69
BLAKE2b-256 72a63b14870ec5dbb65422260b4a87d18349e5ee7519f17e9cfcae7147cb5678

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0bed7ab120a4cdc4238df105c2dbefa04655134fee0c1a8673f3a0b2c90e8001
MD5 9ece7a76361810f138149f5bc7badd26
BLAKE2b-256 830de47d56b929355c0dbefcb7918c502402f19ebdeda8197383d94c5858fc07

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 936129b47b79a9b95b1fc22200b2432e855ab1d39a30fe231f36d87c5794f49e
MD5 4c174400e34ec948a90038bc7ffcd962
BLAKE2b-256 e6cd170c11a567bf3af6b8cf1196b841967f89dbea0241b3803bd31d4fc1ecf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c5cfa2d1dac602d83a83b74b6412afc96cbf6dc140b2fef5b41b250297deb887
MD5 33ae2535149624e82ab6017af9d0331c
BLAKE2b-256 475f2db7d9fb322e2d02bc0bda0b9a86d10e63d416ee3b25e8765bd00d7f94e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d9a9415032933ac3071da419b626f68fd689fde7947b97a7981a1bb43bac52ed
MD5 1685ec6d3bd6dbc3a637215ed5caa417
BLAKE2b-256 f401aebad0f278d28689c54e7b577f6e678167a1f9bfb5b5aa880a0b4ccd3382

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.21-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 411da9286415db213b9f109b67ad22411a65f0ecd983be1644b0e6746e580217
MD5 c3719987bfd262892c3d9c7d66f884d0
BLAKE2b-256 4b7e7c503c1ebebe8872caf1bae7539ed4873a1c2cc3ba66a30b7b22a0fbd61e

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