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.9.26-cp312-cp312-win_amd64.whl (348.3 kB view details)

Uploaded CPython 3.12 Windows x86-64

tensordict_nightly-2024.9.26-cp311-cp311-win_amd64.whl (347.7 kB view details)

Uploaded CPython 3.11 Windows x86-64

tensordict_nightly-2024.9.26-cp310-cp310-win_amd64.whl (346.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

tensordict_nightly-2024.9.26-cp39-cp39-win_amd64.whl (346.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 05ae2f03b4631a050056ba54cf3259294a5f031bec224cc1465c3e71eb7e7196
MD5 4017bfb023b337e36ee9c1b1595290c6
BLAKE2b-256 be6cc05ff7d7762a973fc2664b1282521e91a1694935d8e2c64409a2af0c1f84

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp312-cp312-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7ad46a1dcf33f94ac4a5aaef88f0b0c4b8932f10abdb532e0ec0cfd57e7fe306
MD5 578413cafc6e2308d585c28af3c53dfe
BLAKE2b-256 484e0454a429ea7ee66ee6aa5ac904c8b58b9eb88c216d1bd6445d5b8a32d357

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 51fa76327c798837135886b6c1a2a96c295acf2e903f613f0e92086c8e1fef47
MD5 c6664cb43de2e0d6e0b6431fef3bd543
BLAKE2b-256 a29610b2f59d00702f2387e7cd7dba16ae9a673c921bcc3a4bc4ac0e6161f94a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp311-cp311-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1a6105fe43bb364604c4fa2f8870029fbc3d18668abe76f85c0dbcd09ed93646
MD5 051b1d07ea63fe439deae07feded6da7
BLAKE2b-256 7893a6228db40dde77fba9e8c9b7d82c4519b87c5f74548977cea1f46897ed67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 cb4b6d8fb3851ffb8e346797fd6f1fc006b2be72a7954456d842d85da358f0ae
MD5 c7ed17bf7657a6fc135c40740dff4dbc
BLAKE2b-256 ce3b7892031179c79ae07f790942f39f26134ec504c96d5b2b763d76e866a614

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 afef1c49d629c1e9059a9ca92b4d9ef762aeeab886e6d467c19bf568eb7c3856
MD5 2c69bddc3100ae5fed5c53f616d6b43a
BLAKE2b-256 823e017d9b4f45a5e0305c12604742da68c8c6b62ad9a6ea9bd8fc097feb651d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 394bcf58710e0ac39842ef910a5602f20b6ff38ebb2921d6efb3b74c5d2990bd
MD5 ffbfc39be56d5aa90f4ff7c0f5c5d502
BLAKE2b-256 c9437604def8f70dc52d10966dd38735e632644c20afca4de696d05a91c8865e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensordict_nightly-2024.9.26-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 531f47dc7d92d01bc93f85bc6ede96f5c9d2606b769f8857f5945192ca21ffff
MD5 be34b7d6f605294a8f4d88b639f9f8cd
BLAKE2b-256 dfbfdf79babd2fc68522727e97df69e7dd5c0b21348549a726c2a059a797fa00

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