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.3.tar.gz (4.7 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.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: unify_tensors-0.1.3.tar.gz
  • Upload date:
  • Size: 4.7 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.3.tar.gz
Algorithm Hash digest
SHA256 0d4d5718ff864f32fd3e57bfe1424ea7650410a6524c4db8c930c13bd09600d2
MD5 1374649b23e83f62b0b0289f9e9450d0
BLAKE2b-256 7127492f87c59e6a2dbb2b9ce3ae917f0b5a796ef5ae6e0b0bb5b41d2d9b99f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: unify_tensors-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.8 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 474917410134444700bd58d355fa13f5adde655224c39aa525974d15e28e2a1b
MD5 f63a34fc2925450fa9a6a83fcb04f32e
BLAKE2b-256 63c62c9c5cff6bebe366c7c63607905c3c302cccc9c6559950c0f141e2a78b1e

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