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

Uploaded CPython 3.14Windows x86-64

tensordict_nightly-2026.4.27-cp314-cp314-macosx_15_0_universal2.whl (523.8 kB view details)

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

tensordict_nightly-2026.4.27-cp313-cp313-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.13Windows x86-64

tensordict_nightly-2026.4.27-cp313-cp313-macosx_15_0_universal2.whl (523.7 kB view details)

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

tensordict_nightly-2026.4.27-cp312-cp312-win_amd64.whl (594.2 kB view details)

Uploaded CPython 3.12Windows x86-64

tensordict_nightly-2026.4.27-cp312-cp312-macosx_15_0_universal2.whl (523.6 kB view details)

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

tensordict_nightly-2026.4.27-cp311-cp311-win_amd64.whl (592.9 kB view details)

Uploaded CPython 3.11Windows x86-64

tensordict_nightly-2026.4.27-cp311-cp311-macosx_15_0_universal2.whl (522.8 kB view details)

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

tensordict_nightly-2026.4.27-cp310-cp310-win_amd64.whl (591.0 kB view details)

Uploaded CPython 3.10Windows x86-64

tensordict_nightly-2026.4.27-cp310-cp310-macosx_15_0_universal2.whl (521.4 kB view details)

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

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 1aa1cf556403569231d99c76e5cfe7fe91a4dbfee5a8a175e306810894b30875
MD5 d0475a1058a72cecc3cf6c5f99be6b6a
BLAKE2b-256 4dc88dabe9f69a15046579f5f934b3e38e1657d87c3bb5781f8abebe9c3cc792

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp314-cp314-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 30935aeb16ccc738acb436c6ecfd756a578627db47840e40e607f760b9018e2d
MD5 89ced8e909a104a955d453cae5b4e0d3
BLAKE2b-256 848de5284fc7ffea2c02fec69b578ff856cbf8efc3a5456451bf4d1c02fcfe17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp314-cp314-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 13ec3e7911356730be5d37d8265611eead26e0d7b78a737c761f299b15fb88a9
MD5 6ec45761e5854627f4d8433e7706939d
BLAKE2b-256 6e418fe250ad1234a76423329e7ecd9a45ec1bf42a63cdd72807ddb85128af2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 090854fe4558d5e17d971a8b9141489bb6bfea9e7afebe55de6434cc233f1226
MD5 80cc23616e5a364374a68f396c7176c3
BLAKE2b-256 c60c205a0dc4572546b6f2c95bf645b00eff200dbbc2bd7f70154e5f6b1038c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp313-cp313-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b1acbb736d627820924ac697f72432e79f251a48e8a427e301a7839e54f4cc54
MD5 c9396b1dbb23a3f8b5e13e82f4a335bd
BLAKE2b-256 e3616a9a15dc042cd893005c579987017bc50078f97a05b8c086f8a151c05907

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp313-cp313-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 dcac3b875eda93032d3cf137d18fb3f9dab3e89d1985759de62126d48479b48e
MD5 6ec34a32c7da16ad37de1dbef9ca8e3e
BLAKE2b-256 ae1557ed0efff3e079c501af81bad9c8ef363ba9431ad3feffff556c1144b988

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 53e4cfd5007b2afc21389da847e9096d1582cb43263fce1f68b5628e902013c5
MD5 8769d0166b5aca25bfca5f25ca295ed3
BLAKE2b-256 da6a1be67d7f29eb4666f627bd872f0a84e26e3354cfb0b14f3aaca6a58cdb91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a6e71b2c9ea0387837a51d0c81e60cf6c9afb1ebebf81856446f566ec3d985e3
MD5 a47aecb9ed19cd704091f597079a38b8
BLAKE2b-256 8995daf029f16c6d58e5ad61a0e6bef87f6fcbfa5858f6b4d7ef0e5732398ab0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp312-cp312-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 003fb400d88ce3144be026b23c6fab8b13a2020412b490610eb8d078e70750dd
MD5 a370530b65b7aff1eb3f797dd610a486
BLAKE2b-256 4f42b2b759ef242abd585feb4e76cf9fd6f51e5cc346bb09a879ede64cb92467

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d8bd7db3177d87c00f637c8a1f872b651562871eeed36e131782028d4c04a69c
MD5 e2e6d5b2912421fcdd990ce822508998
BLAKE2b-256 2b57dc634498838bddd359b3d26c8bf68a5e81ee894a385dadc90e99534f0ec6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c9c32d9db1a5521edce998aa240827b684538db09e62b932f2e1f9a084127bbb
MD5 9bbfa630472b485211b4c56494449096
BLAKE2b-256 577c372128ec0fb7d2aa436784e72251329176bc9064c2604affb3b524817dda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp311-cp311-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 ea3a239068a909645de915c6cd1a638fc974d6dfb54435a2f596c391cefb030b
MD5 2205fd6fc3b14e50695e530777078318
BLAKE2b-256 0bde679df7b764948b78227ab3b7b0b7ea8972f29a45a0ef90f571f53e19db3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5daf06b6c6838ab5c686eead4288efcbceacd6310d6ad127e31be42770eda32d
MD5 efff6583803f7585ccb774b7c68bc1eb
BLAKE2b-256 a0c0b58c6bf06bb52f813a34c65464f8b82f1c4a9c3cc14cad83fa1b30840fe2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f9fa29d698f8908bda55beac6b6d820bece3ea3a39b5e5edf3a52a45b96cab2a
MD5 281fef5b77243155c3cb7bfabc16fa41
BLAKE2b-256 0bc0e1ab2a5741351978400a1be22b308638dd75fd82162d61c1a698f42bdffc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2026.4.27-cp310-cp310-macosx_15_0_universal2.whl
Algorithm Hash digest
SHA256 8fc1f34a45164c31f701cda74cdac6422e763640f3d5eb3813ddab8a1e1286e5
MD5 3bdb436581d57dc4bad8af473cadddb4
BLAKE2b-256 149143eefb1a5c57049dff9f3b364f3b27d1992b7329370fe38dc9b9ebad990d

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