AssertionError: Invalid CUDA '--device 1' requested, use '--device cpu' or pass valid CUDA device(s)

torch.cuda.is_available()=True
But I stll get AssertionError: Invalid CUDA ‘–device 1’ requested, use ‘–device cpu’ or pass valid CUDA device(s)
Please help me.

Based on the error message it seems you are trying to use a GPU with a device id 1, which is invalid. In case your system has a single GPU, use the device id 0.

I tried it but got the same result.
AssertionError: Invalid CUDA ‘–device 0’ requested, use ‘–device cpu’ or pass valid CUDA device(s)

I have the same problem as him

In the linked topic the user was installing a CPU-only PyTorch binary and fixed the issue by installing the right PyTorch binary with a CUDA runtime.

So I need to install a CPU-only PyTorch in a new virtual environment, or how can I do.
I need GPU to run my program

No, if you want to use your GPU you would need to uninstall all CPU-only builds and install the PyTorch binary with the picked CUDA runtime.

First of all, thank you very much for your help, I am a student still learning AI by myself
I didn’t install CPU-only builds.
By the way, when I execute my program in pycharm, there will be an error message like the topic.
I don’t know if it’s a problem with pycharm or virtual environment.

Based on the error message CUDA is not available either because you’ve installed the wrong binary, have multiple binaries installed and are using the wrong one, your system has any driver issue and cannot communicate with the GPU etc.
Create a new and empty virtual environment, install the PyTorch binary with the desired CUDA runtime, and make sure you are able to use the GPU.


I have tried installing CUDA 11.3 in a new and empty virtual environment twice, but failed both times.
Do I need to install CUDA 11.6?