Skip to main content

Docs Discord Python version GitHub license pypi version Downloads Conda (channel only)

TensorDict

TensorDict is a batched, nested dict[str, Tensor] that behaves like a tensor.

Move it, slice it, reshape it, stack it, save it, compile it, or do arithmetic on it: every tensor leaf follows the same operation, and one shared batch_size keeps the structure honest.

TensorDict(batch_size=[32])
|-- obs:      Tensor[32, 128]
|-- action:   Tensor[32]
|-- reward:   Tensor[32]
`-- next:
    `-- obs:  Tensor[32, 128]

30-second demo | Why TensorDict | What is new in 0.13 | Patterns | Installation | Ecosystem | Citation

30-second demo

import torch
from tensordict import TensorDict

batch = TensorDict(
    {
        "obs": torch.randn(32, 128),
        "action": torch.randint(0, 4, (32,)),
        "reward": torch.randn(32),
        "next": {"obs": torch.randn(32, 128)},
    },
    batch_size=[32],
)

mini = batch[:8]                 # slices every leaf
device = "cuda" if torch.cuda.is_available() else "cpu"
on_device = batch.to(device)       # moves every leaf; non-blocking internally
scaled = batch * 0.5             # arithmetic on the whole structure
merged = batch + batch           # leaf-wise TensorDict arithmetic
stacked = torch.stack([batch, batch], 0)

print(mini.shape)                # torch.Size([8])
print(stacked.shape)             # torch.Size([2, 32])

The object remains a mapping, but the batch acts like a tensor. That is the point: write the operation once, apply it to every tensor that belongs to the same example, rollout, batch, parameter set, or dataset shard.

Why TensorDict

Plain dictionaries are flexible. TensorDict keeps that flexibility and adds the parts tensor programs need once the code gets serious.

With a plain dict With TensorDict
Manually keep leading dimensions aligned One batch_size validates the structure
Repeat .to(device) for every tensor td.to(device) moves the full batch
Hand-roll slicing, stacking, reshaping td[:32], torch.stack, td.reshape
Manually recurse through nested state Nested keys are first-class
Duplicate arithmetic over leaves td + td, td * scalar, td.abs()
Invent checkpoint formats td.save, td.memmap, load_memmap
Hope generic code keeps working PyTorch-native APIs, torch.compile coverage

Use TensorDict when the unit of data is not one tensor anymore, but it should still move through your program like one tensor.

Performance is part of the API

TensorDict is not just syntax for recursive Python loops. Core paths are built for high-throughput PyTorch workloads:

  • Arithmetic dispatch: operations such as td + td, td * 0.5, td.abs() and in-place variants apply directly to leaves and use PyTorch foreach kernels where available.
  • Device and host transfers: D2H and H2D copies are dispatched across the full structure. TensorDict uses non-blocking leaf transfers internally when possible, so the common path is just td.to(device); pass non_blocking=False only when you need an explicitly synchronous transfer.
  • Shape operations without boilerplate: indexing, view, reshape, permute, unsqueeze, squeeze, flatten, unflatten, stack and cat operate on the batch structure rather than on hand-maintained lists of leaves.
  • Low-allocation workflows: lazy stacks, preallocation, memory mapping and inplace=True shape-changing operations help reduce peak memory in data-heavy pipelines.
  • Compile-aware internals: TensorDict is used in compiled training and RL loops, and the codebase carries dedicated torch.compile coverage for hot paths.

For deeper numbers, see the benchmark notes.

What is new in 0.13

TensorDict 0.13 focuses on making structured tensor programs more practical in large training systems:

  • Tabular import/export for pandas, CSV, Parquet and JSON workflows.
  • More inplace=True shape operations, including gather, repeat, repeat_interleave, roll, reshape, flatten, unflatten and contiguous.
  • Improved torch.compile behavior for TensorClass initialization, dynamic-shape export, locking paths and shallow clones.
  • Safer memmap filenames by default through robust key encoding.
  • A migration path for module state preservation with to_module(..., preserve_module_state=...).
  • CPU-only release wheels for TensorDict, avoiding duplicate GPU wheel artifacts for a package whose compiled extension is device-independent.

Patterns

One batch through the whole training step

TensorDict lets datasets, models and losses agree on one container instead of a long argument list.

for batch in dataloader:
    batch = batch.to(device)
    batch = model(batch)
    loss = loss_module(batch)

    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

That loop can stay stable while the schema changes from classification to segmentation, RL rollouts, model-based prediction or LLM post-training batches.

Nested data without custom plumbing

td = TensorDict(
    {
        "agents": {
            "policy": torch.randn(64, 8),
            "value": torch.randn(64, 1),
        },
        "env": {
            "reward": torch.randn(64),
            "done": torch.zeros(64, dtype=torch.bool),
        },
    },
    batch_size=[64],
)

policy = td["agents", "policy"]
td["env", "reward"] = td["env", "reward"].clip(-1, 1)

Nested keys are part of the API, not an afterthought.

Functional modules and parameter sets

TensorDict can hold module parameters, swap them into modules, vectorize over ensembles and make model state explicit.

from tensordict import TensorDict

params = TensorDict.from_module(module)

with params.to_module(module, preserve_module_state=True):
    out = module(inputs)

This is the same foundation used by TorchRL modules and functional training utilities.

Checkpoint and share large tensor batches

td = TensorDict({"tokens": tokens, "scores": scores}, batch_size=[n])
td.memmap("/tmp/batch")          # memory-map every leaf
reloaded = TensorDict.load_memmap("/tmp/batch")

Memory-mapped TensorDicts are useful for large offline datasets, replay buffers, inter-process handoff and checkpointed intermediate state.

Key features

  • Tensor-like collection ops: indexing, slicing, device casting, dtype casting, reshaping, stacking and concatenation. [tutorial]
  • Nested structures with tuple keys and predictable batch semantics. [tutorial]
  • Fast memory workflows: asynchronous transfers, memmap, consolidated tensors, lazy stacks and preallocation. [tutorial]
  • Functional programming with parameter TensorDicts, to_module and compatibility with torch.vmap. [tutorial]
  • @tensorclass: a tensor-aware dataclass for structured tensor objects. [tutorial]
  • Distributed and multiprocessed pipelines across workers, devices and machines. [doc]
  • Serialization and memory mapping for efficient checkpointing and dataset storage. [doc]

For a longer tour, start with GETTING_STARTED.md or the online documentation.

Installation

With pip:

pip install tensordict

With conda:

conda install -c conda-forge tensordict

Nightly builds:

pip install tensordict-nightly

From source with an existing PyTorch install:

pip install -e . --no-deps

If you use uv with PyTorch nightlies, keep torch pinned to the PyTorch wheel index or install TensorDict with --no-deps so the resolver does not replace your existing PyTorch build:

uv pip install -e . --no-deps
uv pip install -e . --prerelease=allow -f "https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html"

Ecosystem

TensorDict started in reinforcement learning, where batches quickly become nested trajectories. It is now used anywhere tensor batches are structured data: RL rollouts, LLM post-training samples, robotics trajectories, simulation state, model parameters, checkpointed datasets and scientific pipelines.

Domain Projects
Reinforcement Learning TorchRL (PyTorch), DreamerV3-torch, Dreamer4, SkyRL
LLM Post-Training verl, ROLL (Alibaba), LMFlow, LoongFlow (Baidu)
Robotics and Simulation MuJoCo Playground (Google DeepMind), ProtoMotions (NVIDIA), holosoma (Amazon)
Physics and Scientific ML PhysicsNeMo (NVIDIA)
Genomics Medaka (Oxford Nanopore)

Citation

If you use TensorDict, please cite the TorchRL paper:

@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.

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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.8.1-cp314-cp314-macosx_11_0_universal2.whl (574.8 kB view details)

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

tensordict_nightly-2026.8.1-cp313-cp313-win_amd64.whl (646.3 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.8.1-cp313-cp313-macosx_11_0_universal2.whl (574.6 kB view details)

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

tensordict_nightly-2026.8.1-cp312-cp312-win_amd64.whl (646.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.8.1-cp312-cp312-macosx_11_0_universal2.whl (574.6 kB view details)

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

tensordict_nightly-2026.8.1-cp311-cp311-win_amd64.whl (644.6 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.8.1-cp311-cp311-macosx_11_0_universal2.whl (573.8 kB view details)

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

tensordict_nightly-2026.8.1-cp310-cp310-win_amd64.whl (641.6 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.8.1-cp310-cp310-macosx_11_0_universal2.whl (571.9 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 11139ee8af773ee29bf5ed40097bf69bca8c81e3d70e04ca17c28ba886592ba3
MD5 3a62d5fc175f49f86d5713c066d2834f
BLAKE2b-256 4d8683204d40c3ffe560267e2ed996a0e93bd8c7fad2e37fe9e59c2de98ab92b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dafe683a2f8cf5ca72ecae55989b15a9c31267a2d48d7a20c6a871fb24427a2d
MD5 cc137f8fddaa8bf4a4bfe367831c2790
BLAKE2b-256 e6e8dbf11de4c68f0f2ce35d2c50535037a1a74ca11ac65e886739756f3b9d79

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.8.1-cp314-cp314-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp314-cp314-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 41a529ef5e3874e4d2945fdb4ca598330b59dbe808ec4337b01ae3a6b5cb8f6f
MD5 46d176261f4d10520f3138f69b9e5695
BLAKE2b-256 232e810bd807779d60dcf54f8ab41d5a671aeafc3d38d5fdc3c90f3e8678a3f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e65854314c0816b8cf97882e0a58c1fd4f390585491aec25c88b994b596e44c2
MD5 09ee52d99128606723e681e30d223e19
BLAKE2b-256 f06ede3ccfbf57343b132bf41ae6909e384742e051655fd94d5ba887bbbe9dd9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5fe4cdca9148f2c7d2b1fdb2cdd7893bf969c42c64e608cac217ae7770f7b652
MD5 42233937adadb0ab973474e8a95eb599
BLAKE2b-256 3bc63d87042c190ba7d355bfda398f4a584976661c79b9135c7036279a564afd

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.8.1-cp313-cp313-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp313-cp313-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 ffdff20a9e88415e625d552500aaaea3e8660cdca1ea0e953bb5a2bd5e30ceed
MD5 49601c0bc90d1948d74d983d6abf3ddc
BLAKE2b-256 106a31cdce6e8794135ffb2ebbd697d58b16b7b1910f95a36e2ad6ceaf7910f9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 dc5fa800312738d481fa359c77f7ba115689d68d293755330c03f85961cedec6
MD5 0e737aa50e986eaa8d4adef6e747a5ba
BLAKE2b-256 82e02334bac611dfe8e5df927c3bccacceb03fee0c51dfe756b8ea82d8a10fdf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c1cacd7880131be3155ea33291c57a28c553b53ce48576f5417b2a1cf513b5eb
MD5 533c07d33aa8e7f159d388f5b73f00e7
BLAKE2b-256 74629df08cbf66c8f2b898cd4855ae7e0eb08769ad1106f3a281d9099cb20f29

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.8.1-cp312-cp312-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp312-cp312-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 3fb69015e0cd701fbd87eaf28004032c098b8ea4d9deb8c21a5c86d029a6d8d9
MD5 87fce16f903bdeb4fde9f188821f2c7c
BLAKE2b-256 67b1f6d9239bf3ea94b5772ed97a9b7840d2f259c0fd87ba63476f002cc7bcdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1e3c34e2953e37e213a517770bde3659bf1eea7cec15651947f8a2752886d5d0
MD5 d0cd7127be638740fed402e8393d2301
BLAKE2b-256 16e142c0b1048d1a6e3f58f948af2a1d654d62970c0b5d48958d94eb85a8c790

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 89471322b6bdbb93a77c74ae2b208d0e9f56ac335600902be151dbc39fbdd604
MD5 979fe0449b07c09f663638508341b6c7
BLAKE2b-256 5c9a492c2a464f516ed242f9ba3a0e13ec1b991b55a199416ac8c4a975e454d3

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.8.1-cp311-cp311-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp311-cp311-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 40cb425a4da4c0ee1b368dceb9327a6380ae2e1b5f3c80d0fbb322d101d6daae
MD5 9f2cd1a63b033aff763050009b3ab4a5
BLAKE2b-256 89aed577f09cf915bb2c54650723820d4e717d6d7fa3686711dc668247d5669d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2888a7404f024a7a148efbf965866f2369cae4529dcfb5c10f76d7d15ba448bf
MD5 44a81d59f6659fd3ec661643ed479520
BLAKE2b-256 7f1b41ab6944bc68b3ede5ca21ab7c72113b4d0af609a7a239dab4edfbb40de1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9c34e00531f82666494512bb8337b2c5146aa075b86b5282e830fc158e4808ef
MD5 c250e1f9840e29dc4f361d0d51cb40e5
BLAKE2b-256 bde15bd4ca45d61e3c322fd85b9f5a3ec08017ca2f4d7d2e1a2c05a98153f023

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2026.8.1-cp310-cp310-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.8.1-cp310-cp310-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 7af7cf2a6dc0617a3fb8099d58a9af6ca97169c33797e23505a95a8ce2badc31
MD5 b1cbcc7fefa349b9910d43725714e1e5
BLAKE2b-256 28366e5eadf44e576da44b325b0e327bc69a95448605f1bd46cdca07084217a6

See more details on using hashes here.

Release history Release notifications | RSS feed

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page