How to use GPU 🎟

Published

March 27, 2025

1 Using GPU with pyPLNmodels

This tutorial introduces the basics of GPU usage with the pyPLNmodels package. The package is built on top of torch and automatically detects and utilizes the GPU if available. This means you don’t need to manually move tensors to the GPUβ€”pyPLNmodels handles it for you.

In this tutorial, we will walk through a simple example using the Pln model and the scMARK dataset provided by the load_scrna function in the package.


1.1 Step 1: Check GPU Availability

Before proceeding, ensure that your system has a GPU available and that it is properly configured for PyTorch. You can check this by running the following code:

import torch
print(torch.cuda.is_available())
False

If the output is True, your GPU is ready to use. If it returns False, ensure that you have installed the correct GPU drivers and the CUDA-enabled version of PyTorch.


1.2 Step 2: Load the Dataset and Fit the Model

Next, we will load the scMARK dataset and fit a simple Pln model. The pyPLNmodels package will automatically detect the GPU and use it for computations. Here’s the code:

from pyPLNmodels import Pln, load_scrna

# Load the scMARK dataset
data = load_scrna()

# Fit the Pln model
pln = Pln(endog=data["endog"]).fit()
Returning scRNA dataset of size (400, 100)
Setting the offsets to zero.
Fitting a Pln model with full covariance.
Intializing parameters ...
Initialization finished.
Upper bound on the fitting time:   0%|          | 0/400 [00:00<?, ?it/s]Upper bound on the fitting time:   9%|β–‰         | 35/400 [00:00<00:01, 348.01it/s]Upper bound on the fitting time:  18%|β–ˆβ–Š        | 70/400 [00:00<00:00, 347.21it/s]Upper bound on the fitting time:  26%|β–ˆβ–ˆβ–‹       | 106/400 [00:00<00:00, 349.94it/s]Upper bound on the fitting time:  36%|β–ˆβ–ˆβ–ˆβ–Œ      | 143/400 [00:00<00:00, 356.19it/s]Upper bound on the fitting time:  45%|β–ˆβ–ˆβ–ˆβ–ˆβ–Œ     | 180/400 [00:00<00:00, 357.65it/s]Upper bound on the fitting time:  54%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–    | 216/400 [00:00<00:00, 356.53it/s]Upper bound on the fitting time:  63%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž   | 252/400 [00:00<00:00, 356.21it/s]Upper bound on the fitting time:  72%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–  | 288/400 [00:00<00:00, 355.67it/s]Upper bound on the fitting time:  81%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  | 324/400 [00:00<00:00, 353.96it/s]Upper bound on the fitting time:  90%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ | 360/400 [00:01<00:00, 352.76it/s]Upper bound on the fitting time:  99%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰| 396/400 [00:01<00:00, 352.93it/s]Upper bound on the fitting time: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 400/400 [00:01<00:00, 353.40it/s]
Maximum number of iterations (400)  reached in 2.1 seconds.
Last  criterion = 5.4e-06 . Required tolerance = 1e-06

When you run this code, you should see a message indicating that the GPU is being used. This confirms that the computations are offloaded to the GPU.

References

BatardiΓ¨re, Bastien, Joon Kwon, and Julien Chiquet. 2024. β€œpyPLNmodels: A Python Package to Analyze Multivariate High-Dimensional Count Data.” Journal of Open Source Software.