2
$\begingroup$

I have created an EC2 instance with GPU g4dn.xlarge and launched it. I want to run some code from command line and this code is pytorch based. While pycuda is able to identify the GPUs the pytorch is not able to identify it.

import pycuda.driver as cuda
import torch
cuda.init()
num_gpus = cuda.Device.count()
print(f"Number of GPUs: {num_gpus}")
print("is torch cuda avaialable",torch.cuda.is_available())
print("torch cuda count",torch.cuda.device_count())

the output for the above code will be

Number of GPUs: 1
is torch cuda avaialable False
torch cuda count 0

Here are the pytorch and cuda version I am using

pytorch                   2.0.1           cpu_py310h07ccb54_0
cudatoolkit               11.7.0              h254b3b0_10    nvidia
pycuda                    2021.1          py310h06b8198_3    conda-forge
$\endgroup$

3 Answers 3

2
$\begingroup$

It looks like the issue is you have the cpu version of pytorch installed instead of the gpu version. If you go to the pytorch home page: https://pytorch.org/get-started/locally/ you can use the configuration table to install the cuda 11 or cuda 12 version of pytorch and you should be good to go.

$\endgroup$
3
  • 1
    $\begingroup$ I have made use of the get started locally installation command locally on on whole fresh conda environment and I am still getting the error. conda install pytorch==2.0.1 torchvision==0.15.2 pytorch-cuda=11.7 -c pytorch -c nvidia. This command additionally had torchaudio which I removed from the install command as the package was not found during conda install $\endgroup$
    – Shruti
    Commented Oct 7, 2023 at 17:31
  • 1
    $\begingroup$ hmm, is it possibly a mismatch between pytorch version and Cuda version? Do you need pytorch-cuda 11.7 or can you upgrade to the latest version (12.1 I believe)? Same question for the drivers - do you have the latest Nvidia drivers for pytorch for your card and Cuda version? $\endgroup$ Commented Oct 8, 2023 at 13:06
  • $\begingroup$ this is solved as I had to install GPU CUDA and also match the cuda and nvidia driver version for compatibility $\endgroup$
    – Shruti
    Commented Nov 3, 2023 at 16:18
1
$\begingroup$

Use nvidia-smi to troubleshoot. Depending on what it says, you need to get and install the nvidia cuda driver. If you've upgraded your kernel (or had it automatically applied) you need to reinstall the cuda driver.

If nvidia-smi shows correct GPU data, then something is wrong with pytorch install.

$\endgroup$
1
$\begingroup$

Use nvidia-smi to match the correct nvidia driver that can work with your desired cuda toolkit

Make sure to install GPU version of the cuda toolkit. https://pytorch.org/get-started/locally/ should be able to help you choose your desired version.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.