Open In App

How to Fix "fast.ai Not Using the GPU"?

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Fast.ai is one of the leading deep-learning frameworks built on top of PyTorch.e. The library provides high-level components that make it easy to build and train neural networks.

This library simplifies the deep learning process making the process easy in high-level API building for building and training neural networks.

In this article, I am gonna show you some troubleshooting methods you can try to Fix the "fast.ai Not Using the GPU" error, while you training the fast.ai model.

1. Check GPU Availability in your Environment

First, You need to make sure that in which environment, you are implementing this code, is it really compatible with GPU? Are your system recognize it? Add this Python code, to check,

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

If you see False, it means, the system is not comparable with GPU, if it is True, go to the next method if still an error.

2. Verify CUDA Installation

Now check the version of CUDA installed. Is the CUDA installed correctly in your system?

1. Open up your terminal window. This is where you'll type in commands.

2. Now, type in this command and press enter.

nvcc --version 

It show the version of CUDA which is instakked succefuly in your system. If CUDA is not installed on the system, you have to download and install it from the NVIDIA official website.

3. Set Up PyTorch for GPU

After testing the CUDA installation, now check you PyTorch installation supports CUDA.

Python
import torch
print(torch.version.cuda)


If PyTorch doesn't list a CUDA version, you might need to reinstall it with CUDA support. My recommendation is to use, it without directly upgrading the torch, first uninstall it, then reinstall.

#uninstall comand
pip uninstall torch

# Install the right version for your CUDA

pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/rocm5.6

# CUDA 11.8
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu118

# CUDA 12.1
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu121

4. Update fast.ai

Upgrading the fast.ai package, and with its dependencies can fix this issue.

pip install fastai --upgrade

5. Use CUDA With fast.ai

After updating the package, fast.ai, you need to make sure that you set the device for your model to use the GPU. Use this code to check it;

Python
from fastai.vision.all import *
defaults.device = torch.device('cuda')

6. Check GPU Utilization

Check how much GPU is used during the model training, if other processes of other programs are using GPU the most, then it will fail.

That's why , I am saying, during training, go to another terminal,, and his any of these below commands,

nvidia-smi 
watch nvidia-smi 

This command provides real-time information about GPU usage, memory, and more. If the GPU is being used, you should see some activity corresponding to Python processes in the output.

7. Example fast.ai Code

Here in this example, I have shown the little code for utilizing the first ai on GPU.

Python
from fastai.vision.all import *
defaults.device = torch.device('cuda')

import torch


print(torch.version.cuda)

path = untar_data(URLs.MNIST_SAMPLE)
dls = ImageDataLoaders.from_folder(path, train='train', valid='valid', bs=64, size=28)
learn = cnn_learner(dls, resnet18, metrics=accuracy)
learn.fit_one_cycle(1, 0.01)


# Print out the final accuracy
print(f"Final accuracy: {learn.recorder.values[-1][2]*100:.2f}%")
Screenshot-from-2024-06-24-20-40-03

Conclusion

By following these methods, I can assure you, that your error of fast.ai on GPU utilization will be resolved. Basically, the common issue of this error is the outdated CUDA Toolkit, Multiple Processes and Environment issue. I hope from this in-depth guide, you have got your solution.

1. What are the common issues of "fast.ai Not Using the GPU"?

The most common issues are;

1. Multiple Processes: If other programs are using the GPU, they might limit fast.ai's access.

2. Double-check the environment variables are set correctly.

3. Outdated CUDA: Keep in mind, you have the latest CUDA version which is compatible with the sever GPU.

2. Can I use fast.ai with multiple GPUs?

Yes, fast.ai supports multi-GPU setups. You can use DataParallel to distribute the workload across multiple GPUs.

3. How do I know if my GPU is being used?

While you start model training, in other bashes, hit this command, nvidia-smi to check the GPU utilization.


Next Article
Article Tags :
Practice Tags :

Similar Reads