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 you prefer to go directly through GitHub, 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 as unify_tensors
import torch
from torch import nn

# Example with Tensors
a = torch.randn(2, 2)
b = torch.randn(2, 2).to('cuda')
c = torch.randn(2, 2).to('cpu')

# Unifying tensors
a_unified, b_unified, c_unified = unify_tensors(a, b, c)

print(f"Tensors unified to: {a_unified.device}")

# Example with a Tensor and a Model
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.param = nn.Parameter(torch.randn(1))

    def forward(self, x):
        return x * self.param

model = MyModel().to('cpu')
input_tensor = torch.randn(1).to('cuda')

# Unifying a tensor and a model
model_unified, input_unified = unify_tensors(model, input_tensor)

print(f"Model unified to: {next(model_unified.parameters()).device}")
print(f"Input tensor unified to: {input_unified.device}")

# 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.1.tar.gz (4.6 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.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: unify_tensors-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 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.1.tar.gz
Algorithm Hash digest
SHA256 5bd8ed342deff288ce6c740bae2cfe7997181072c6c98bb2c7162d2c6be72192
MD5 d638aa6252d333512bb716a52c35429b
BLAKE2b-256 87c9a5ef297a7242dcf7bb06106d47ef73599fb5b58aefc43981013aafa8a568

See more details on using hashes here.

File details

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

File metadata

  • Download URL: unify_tensors-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b368b324d6106f63e2634fab612274f3b64b79967ee277b0740701f8abe8368
MD5 dc558197135fee0c88ef2445eeeecc88
BLAKE2b-256 298fe5b28af97d57fc625c5fc49686d060ca0f66fddb06ee594c290823b353d7

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