Skip to main content

No project description provided

Project description

Docs - GitHub.io Discord Shield Benchmarks Python version GitHub license pypi version pypi nightly version Downloads Downloads codecov circleci Conda - Platform Conda (channel only)

📖 TensorDict

TensorDict is a dictionary-like class that inherits properties from tensors, making it easy to work with collections of tensors in PyTorch. It provides a simple and intuitive way to manipulate and process tensors, allowing you to focus on building and training your models.

Key Features | Examples | Installation | Citation | License

Key Features

TensorDict makes your code-bases more readable, compact, modular and fast. It abstracts away tailored operations, making your code less error-prone as it takes care of dispatching the operation on the leaves for you.

The key features are:

  • 🧮 Composability: TensorDict generalizes torch.Tensor operations to collection of tensors.
  • ⚡️ Speed: asynchronous transfer to device, fast node-to-node communication through consolidate, compatible with torch.compile.
  • ✂️ Shape operations: Perform tensor-like operations on TensorDict instances, such as indexing, slicing or concatenation.
  • 🌐 Distributed / multiprocessed capabilities: Easily distribute TensorDict instances across multiple workers, devices and machines.
  • 💾 Serialization and memory-mapping
  • λ Functional programming and compatibility with torch.vmap
  • 📦 Nesting: Nest TensorDict instances to create hierarchical structures.
  • Lazy preallocation: Preallocate memory for TensorDict instances without initializing the tensors.
  • 📝 Specialized dataclass for torch.Tensor (@tensorclass)

tensordict.png

Examples

This section presents a couple of stand-out applications of the library. Check our Getting Started guide for an overview of TensorDict's features!

Fast copy on device

TensorDict optimizes transfers from/to device to make them safe and fast. By default, data transfers will be made asynchronously and synchronizations will be called whenever needed.

# Fast and safe asynchronous copy to 'cuda'
td_cuda = TensorDict(**dict_of_tensor, device="cuda")
# Fast and safe asynchronous copy to 'cpu'
td_cpu = td_cuda.to("cpu")
# Force synchronous copy
td_cpu = td_cuda.to("cpu", non_blocking=False)

Coding an optimizer

For instance, using TensorDict you can code the Adam optimizer as you would for a single torch.Tensor and apply that to a TensorDict input as well. On cuda, these operations will rely on fused kernels, making it very fast to execute:

class Adam:
    def __init__(self, weights: TensorDict, alpha: float=1e-3,
                 beta1: float=0.9, beta2: float=0.999,
                 eps: float = 1e-6):
        # Lock for efficiency
        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))

Training a model

Using tensordict primitives, most supervised training loops can be rewritten in a generic way:

for i, data in enumerate(dataset):
    # the model reads and writes tensordicts
    data = model(data)
    loss = loss_module(data)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

With this level of abstraction, one can recycle a training loop for highly heterogeneous task. Each individual step of the training loop (data collection and transform, model prediction, loss computation etc.) can be tailored to the use case at hand without impacting the others. For instance, the above example can be easily used across classification and segmentation tasks, among many others.

Installation

With Pip:

To install the latest stable version of tensordict, simply run

pip install tensordict

This will work with Python 3.7 and upward as well as PyTorch 1.12 and upward.

To enjoy the latest features, one can use

pip install tensordict-nightly

With Conda:

Install tensordict from conda-forge channel.

conda install -c conda-forge tensordict

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}
}

Disclaimer

TensorDict is at the beta-stage, meaning that there may be bc-breaking changes introduced, but they should come with a warranty. Hopefully these should not happen too often, as the current roadmap mostly involves adding new features and building compatibility with the broader PyTorch ecosystem.

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

tensordict_nightly-2024.8.27-cp312-cp312-win_amd64.whl (331.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

tensordict_nightly-2024.8.27-cp311-cp311-win_amd64.whl (330.7 kB view details)

Uploaded CPython 3.11 Windows x86-64

tensordict_nightly-2024.8.27-cp310-cp310-win_amd64.whl (330.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

tensordict_nightly-2024.8.27-cp39-cp39-win_amd64.whl (329.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

tensordict_nightly-2024.8.27-cp38-cp38-win_amd64.whl (330.2 kB view details)

Uploaded CPython 3.8 Windows x86-64

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b4d85a3ad820fee41c19d443930ef52fc7613a39591542854754e10188c911ef
MD5 c8445329d28515781adf05c59409bfde
BLAKE2b-256 6b041028e59ba4f1ba37f4435bc7c970eb06d9a1c7fceb9e63c8a9f2fe987361

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 47a1b17ea0d4383e7b296d2b0d6887ca7052db3b39a9c9a0249e670c07336751
MD5 a376f24580726849c05dac0eaa1d164a
BLAKE2b-256 5de6010944a95922dfc498fbfc927b145d371c4f4a09add3234ddc7fda765208

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3723547f29d1466c6fbdded7ecabe5d087dee94bc61708a3f694b9378d57ab43
MD5 e51fff973681d1dae2348eb44159b691
BLAKE2b-256 7ba3e26e4fe2d7644d1b4d46e981add62a2148ae7ee38ead54536a83ceeadb12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9aafea7371b2f7d4e418a3fc6adfb47c1e7484389fe2ba1aa0dba59e24020d57
MD5 cc0ed52863505f3bf5bcc532e993da13
BLAKE2b-256 f6124fd865f622ce7b82b988dedaef80db0bf401d57db0f2f89c71c3941ee342

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 706a8a9b6448be8a47105f8b171b7c27dd2a02e201669dbd287cfdef3c1fb4f8
MD5 3773d5af58d38dc626044896ae9bdf3b
BLAKE2b-256 1331ba6125701a33a0181c07f60c96548d53900313ae629310cc943b48b0da48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7306d066c8c443fba16b00051d51d4473f6dd754f3b20fecf97c313515cda0ca
MD5 fdb050c769e5d6d33654522ddd8d5c54
BLAKE2b-256 1b0fb1e1f721714d6bafb3edf148fd08b1da50f8d904dce09bf20f626977b8c5

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2024.8.27-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 82b0591b60d7b95cb0283338329334ba8895ce5e8aed00f81f8827ae9dc2db6a
MD5 4420c92702ed10f6b43fb49e72bd8e30
BLAKE2b-256 4dbd84df28b0067699b2f73521e0ace450c65b98baf04342d451f451d8a8ca18

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2024.8.27-cp39-cp39-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 78a25c3217c7cd4888b3f32ef63e4f5e79afe76ecb0f7e3d93647ced155f55e9
MD5 b0294ee548a7dd082501ac278a2149ee
BLAKE2b-256 64d48319c51d51f6ec2ec7ee2febe26fbc649243d94876b8ede5e8913206865a

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2024.8.27-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 ae4dec927183a0902ec83a04bc193b5b5b665d66f011a67aa4a8f7b966787c56
MD5 aa3c7f22e725492a50bcaea29ef068fc
BLAKE2b-256 a15f79be299940be652a2889d416e07e650435e9776c09f6922da8ccd58de090

See more details on using hashes here.

File details

Details for the file tensordict_nightly-2024.8.27-cp38-cp38-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for tensordict_nightly-2024.8.27-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 371e5c29c9c36ffd5b572a99396a2eb3ae23882fdc0f0807f733fd2e42936307
MD5 2f6eda1fb13935cc74554e0dc9c9f426
BLAKE2b-256 d61dcc7a9a115941b08100279eba3a7424dec5aaf45029840bc958b723928bb0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page