Skip to main content

A utility to unify the device of multiple PyTorch tensors.

Project description

Unify Tensors

Tiny package to implement a safety function that moves all the tensors passed to the same device. Useful when a part of a big model has been offloaded and there's a risk of a runtime error because of tensors on different devices.

Installation

pip install unify-tensors

or, if not available on PyPI, clone the repository and install it manually:

git clone https://github.com/giacomoguiduzzi/unify-tensors.git
cd unify-tensors
pip install .

Usage

The unify_tensors function takes any number of tensor arguments and moves them all to the device of the first tensor provided. If you want to move them to a specific device, you can pass the device keyword argument.

from unify_tensors import unify_tensors
import torch

a = torch.randn(2, 2)
b = torch.randn(2, 2).to('cuda')
c = torch.randn(2, 2).to('cpu')

a, b, c = unify_tensors(a, b, c)

# Should print 'cpu' because the first tensor is on CPU
print(a.device, b.device, c.device)

a = a.to('cuda')
a, b, c = unify_tensors(a, b, c)

# Should print 'cuda' because the first tensor is on CUDA
print(a.device, b.device, c.device)

# You can also specify a device explicitly
a, b, c = unify_tensors(a, b, c, device='cpu')

# Should print 'cpu' because we specified the device
print(a.device, b.device, c.device)

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unify_tensors-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unify_tensors-0.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file unify_tensors-0.1.0.tar.gz.

File metadata

  • Download URL: unify_tensors-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for unify_tensors-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fa6ede224cde1e8e3e1ddb9ca773458d9cb20d355c5af66b583c81d5bfce4d87
MD5 d750a3ff37f838bb250c4d5e6d7fcc22
BLAKE2b-256 cfce9001f97bd22eff781cbe47468e33f94fac82642dfceac9f781c7ea5b43e5

See more details on using hashes here.

File details

Details for the file unify_tensors-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: unify_tensors-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for unify_tensors-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d41c33f9e563a8c098c623bcda16256de5ae4e2b285476878319cba0593b7dfc
MD5 dc9813baede0bf703117722b4e4f0f69
BLAKE2b-256 77fa0b66d983140d3092eaead61a54dea77b56bc4f1a9350bbcfb2e90fc900fd

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