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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.4-cp314-cp314-macosx_15_0_universal2.whl (520.1 kB view details)

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

tensordict_nightly-2026.4.4-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.4-cp313-cp313-macosx_15_0_universal2.whl (520.0 kB view details)

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

tensordict_nightly-2026.4.4-cp312-cp312-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.4-cp312-cp312-macosx_15_0_universal2.whl (519.9 kB view details)

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

tensordict_nightly-2026.4.4-cp311-cp311-win_amd64.whl (589.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.4-cp311-cp311-macosx_15_0_universal2.whl (519.1 kB view details)

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

tensordict_nightly-2026.4.4-cp310-cp310-win_amd64.whl (587.3 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.4-cp310-cp310-macosx_15_0_universal2.whl (517.7 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 f1f4a74eae82277d6845218578b21b7ed6b5977b6a1d97d891204c009b329155
MD5 a7d2ae1e9f507fb15ff127702313d9c9
BLAKE2b-256 53b02fc94ef150fd9bf57258e2b8621d02cc50439bbb05beefaef82125c59b19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a46a76e29dbea8d736ef04d46eb969aa7af3ecc326a4e91940c7450e73d1be22
MD5 cc93fe60cdf365a9d3e25df8780a6350
BLAKE2b-256 309958ee8da6b6aa7aba2c43320757f7c63992846200e27387036edf18c57bdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 80bb7f6bc31d9f806b861f3d1f936c97d05e8d0e1520cf3c18289ef032c61f0a
MD5 d6038fe494db7574ea70827980e668e9
BLAKE2b-256 b09f210744475b9a573fc1e27ff4bf268794b68b002bde8597890501b39ed3ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 53c120eaa9049fea9c656414e83c7bf1079396ef3a39006b33921066272529f4
MD5 0f0679f4cc62e9d21fc13c47f4498b0c
BLAKE2b-256 bbfee824bdf00f9ad1d1e4e1bba23150afc4df084f622058b0f95b0ee0c9b58d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 66ced32d63524366777eec1a4b8f26f2dfaa031967bef7df183bf3a6c2f8d74f
MD5 a6b9378d811d68769873cfd76c9475e0
BLAKE2b-256 fc7f03a25ce2b4753afacc59abb3b7599d6a072f723777670eae58fc72974b22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 5049743bd809cc14333e608d4269563bb07cec4020157e6f9f20f3ab67f2bcbd
MD5 233dfebe26c23dd85312afbc682b5328
BLAKE2b-256 c2c59724c5a4b5c1f54c2741e428119746da1df5533828013a7a70b9d147666e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f60ded443ab60a8c95be129ba4fca565eb5662882b8161942d7203eb86676ea8
MD5 7b82ae261d2bca1d4272ca300c91d0d6
BLAKE2b-256 9c93ee2e5b5d4618208a4255ffad168598d03112abe8c7fd6566064a848ed83c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5f98b1179b83dc1dc697c70dbc48aff6ba7f3758bf9989326389335b3f1f1230
MD5 8eb820c3fba1ab8c1474ddec02184706
BLAKE2b-256 bc3910096a397aad12f6cb0c21facf31dd4ee629c2c5370c73415dea93c9780b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 4d16a17ec2a81cbbc9f80db2ebd476230d2c9305c17e5cde030f4b4a17aae5ad
MD5 f2b8bedbf998d8f448604d22106d1a60
BLAKE2b-256 2ede37fcc1c4123fb23d3fe0bfd847fc2df924a3a29f1ac09df44b325c6a1f63

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 651aee796893e736c12c4697c861c6ccdf5b2c06021ab4beab28c18c3c1cf5f3
MD5 15a7993f5de48f5a7574b048d4e7bc15
BLAKE2b-256 6b3785d46476c0701cb0cdca0b0b7805a9e86853521205030c89f6db7aae1b65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 17a6906280b2da0c06ab42e8acd23dd06ad6729526a08c2c30bd0adfa21d3ee0
MD5 88c8aa687ed4b7ad98628f79bf1e438a
BLAKE2b-256 c7a2fbaee958477ea0d5d98e8c61c510007af3e12e420d4f860ff33cf63c311c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 f036ae87b53a74f377b99fbb657a060eadfb35ec3283336deafacf85a01ca477
MD5 6144930807d6a3606b1658fb5e6bbbc2
BLAKE2b-256 501359e633ef255ea339afc2623a6886cb35747a66e9849e117c85ed11a95024

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0f3b6cc239c190c784080e2a3fbb7e4aad14c816df81cc9e91ff8ad02848e4f6
MD5 b6214d5aa645750d6da3eadcb0e0f1d2
BLAKE2b-256 6ee418d6c347e561d7764449792db5f9abf3b84b91a296eba7bd9903f367faf3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6af91a2434bbc08a6633d2b4f85a12c69af7f24c6e6d50fd86da7f9e0b7c246b
MD5 090fcebf6e62e222feca8fd33922096d
BLAKE2b-256 c146b8974b8ad2398383d20190b2848e12de5534df18bf835db608d3f01e7798

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.4-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 cf8fdcc354cb6d03ec6e1ba3670e4f604e7215d7b4829e413947bd619482b115
MD5 5118fc2b7f54b00fce062ed1255980f5
BLAKE2b-256 8eb79e0c9bd433b2f0a716bf9af6cc7e250952f7962e415b6c51cb740c032436

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