import torch
print(torch.cuda.is_available())False
pyPLNmodelsThis 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.
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.
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.