AssertionError:Torch not compiled with CUDA enabled

Source of problem:

        In the process of installing the local GPU version of pytorch, I repeatedly hit a wall. The first problem is that the download command line given by the pytorch official website fails to execute. The second problem is that the vector value is moved to cuda 0 after successfully downloading pytorch on the local GPU. When the system reports an error, the content is as shown in the title. The specific solutions to these two problems are as follows.

Solution one:

        First of all, for the first question, since the cuda version I downloaded locally is 11.7, but because the pytorch official website has not been updated to the pytorch corresponding to the cuda 11.7 version, the highest version is only the pytorch corresponding to the cuda 11.6, so I choose to download the pytorch version corresponding to the cuda 11.6. It is worth noting that if your local cuda is also 11.x, you should not download the pytorch corresponding to cuda 10.x. If so, some unpredictable bugs may appear later. After selecting the cuda version, paste the generated command line to the Anaconda Powershell Prompt to run, and find that it keeps reporting errors. The running screenshot is as follows:

         I am very puzzled, why the command line given by the official website does not work? Later, after querying the information, I found that it was because the pytorch corresponding to this version of cuda could not be downloaded directly, so I found the URL for manually downloading torch and the corresponding torchvision on the Internet and downloaded it.

        The download URL for torch is:  https://download.pytorch.org/whl/torch_stable.html . Download the torch version corresponding to your local python version to the specified folder, open cmd directly in the folder, and execute the following command on the command line: pip install ***.whl. The corresponding relationship between the python version and the torch and torchvision versions is shown in the figure below:

        After performing the above operations on torch and torchvision respectively, open the anaconda powershell prompt and enter the following command:

>python

>>>import torch

>>>a = torch.ones((3,1)) //Create a matrix with length 3 and value 1

>>>a = a.cuda(0) //Move the matrix to cuda 0, which is also the local GPU

         If no error is reported during the execution of the above command line, it means that torch and the corresponding torchvision have been successfully installed on this machine.

Solution two:

        If solution one cannot solve the error reporting problem, try the second solution.

        It is possible that when executing the last command line of Solution 1, the following error occurs:

         This situation is most likely caused by the version incompatibility between torch and CUDA. In order to verify this assumption, you can enter the following command in the terminal:

         If, like me, the output result is False, it means that the cause of the error is the version incompatibility between torch and CUDA. At this point, first check the CUDA version, and enter the following command in the terminal:

nvcc -V

        The output result is shown in the figure below

         You can see that the CUDA version of my machine is 11.7. Many netizens like to find CUDA version information on the Nvidia control panel, but in some cases, the CUDA version information seen on the Nvidia control panel may be different from what you see here, so this shall prevail!

        Then visit the official website of Pytorch, find the version of Pytorch corresponding to your local CUDA, and install it. The specific operation is as follows:

        If your CUDA version does not appear in the range of optional options given on the official website, go to the entrance to download other versions to download the historical version:

        After downloading, the incompatibility between CUDA and torch versions was solved, and the command that reported the error was executed repeatedly, and it was found that the execution was successful and the error was resolved.

Guess you like

Origin blog.csdn.net/m0_59705760/article/details/125737765