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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.3-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.3-cp313-cp313-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.3-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.3-cp312-cp312-win_amd64.whl (590.5 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.3-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.3-cp311-cp311-win_amd64.whl (589.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.3-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.3-cp310-cp310-win_amd64.whl (587.3 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.3-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.3-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 04a21f3e72e2e44db60a4871dc45302e60f3279c26abe4286dbd3645d5da4f7d
MD5 3c3e3da59f2874b38dde6e1d4eeb7b8c
BLAKE2b-256 738f70fecf0a3fdaa8665d1c27f004c4c2e207f902e9c70789b79db01d92c96c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c639368418fdaf797a651736fb55dffa7576b34ba28c98a6fdf18987dc1355b2
MD5 7c63dd31a8e405a4585773b712856513
BLAKE2b-256 135903517740ab182732fed57a3ea2a52ab7be71af1374620956bd127c711f17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 aa1918cea23ac7930cd66bb7a4d61c7faa4c97d070ebad57820334000439c3a1
MD5 c2b159c9359decef3ecca47e3c67f64b
BLAKE2b-256 bb3793488331ea81d897d52de86b16b3eeff147e44c2ac8023297b28f86759e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f8cf44eb2664e896a3f922a180305a0bb4c66e7ded3abfc6dec781be1b305045
MD5 70211aa4f7208d4a90e5c21c0f35745b
BLAKE2b-256 a7f27edfdc20df235b129195f684f86fded9100c3e57931c4e7cb8bed33327ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bf07d59f92d1b8f24ba545c8985797cb37198501532327ac652e21e358a6e9af
MD5 b2e76a0f4a319b284a59d2a20b0ff077
BLAKE2b-256 3cb90c36b778c2f719b6505d3eaf2ab5871c578f3fa01f3d086d8b81fef81df3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 9bb41432e27723d99797cc352815e12f440e44fa8a3b777168e3e3a25fc011d1
MD5 59bb0af697c689ac7f9d607f4e775efd
BLAKE2b-256 7b7f9a3a3e416219a81d19274502abc569b5fade66ba823e7654c59ebf4e87a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0247e382715946521a64c1eebf07d3f68b893fc772487f9bafd5660609337ba2
MD5 176521efb9c5c9012e14097a014858c2
BLAKE2b-256 ce7064a990a59f8631216fc0aa95ecde30eb81c1140fae770c333f4e5c6c716b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4eed94057a8cb0bbeab2744bce92312a70d93aa712c9c1af5be6c0e426d7a9fc
MD5 49f29c7ec47524274eac327f9bcd4de9
BLAKE2b-256 cc5b07475aebae556f35f28346900178d9c2883c3fdf41f2a98d52d03c2b8b65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 dc0ff39fbc662b8dc973bb95e726be47228e97c441b32df4b749f90e1b0d6af1
MD5 2f73827ea981ec821c3367044dae1dd4
BLAKE2b-256 93166edb5e2fdc1a0604b9cfed0e28be1254f08a74363ac068eacdbe752a9dab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 afa3421713e394e7d028c062b52e3cecce1761d5589207aad38f95ae61883bec
MD5 2fa63a2b62c1089a0ab1c43aadd82e6e
BLAKE2b-256 6c68233e6b48041fcf609470ceac4d25bc9b94dee828dc3ae8348f43991b6a4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fa834d0bc06eb5233423bde7d45100bee2412a69588a8fd59dab1734381b1a2b
MD5 c935f32c629adee7aceea70e9d4bbab1
BLAKE2b-256 a62746909cc323789c23c34c0de2a6399c28716cb049fc6d1c9facacf5e84d0c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 192f780d492c5a664255fa4bc02b6d929288a3cb28f74631a176a4337c408706
MD5 b1898bb729f37aca4c4539f40bad1724
BLAKE2b-256 6e26b3a9dc4a253094729229ec0bf21822bb5f8816e4b77b952e4e13d617269e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 46fdf019f80f712bb733a728b7e85bf5733a58b2d85a5b62ccac180681b3fee1
MD5 669e195aab1b64e43b7c396fbf8d28e5
BLAKE2b-256 1dc311127a74849fc4c286455765a0babc10310ba51cdc4ff3ed97e25a391016

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 224ea31efd93127d794606c59319b15c86ea5fd0ad291959f2c041768502b27f
MD5 38632693f939c56a7852ec9592623985
BLAKE2b-256 c326cb7b1dbf9211226f232d6ffd3723afc02c261fbb1432aab45029019c3a17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.3-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8c2f87dad788f30f724eefce84635318156268f6655f8ea5ed4fb76cc5753027
MD5 a7335aa4f79d3775a8a43b924925ac5d
BLAKE2b-256 5f243b590aaa217a91ba6d84f22a7323f0397c0b9a491f97575fcf33a4a14227

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