Configure cuda+cudnn+pytorch deep learning environment in conda environment

References to this article:

Configuring cuda+cudnn+pytorch deep learning environment in the conda virtual environment (a must-read for novices! Simple and feasible!)_Conda installation cudnn_Jiangjiang ahh's blog-CSDN blog

 1. Create a virtual environment

conda create -n mytorch python=3.8

2. Execute sudo nvidia-smi to check the CUDA version

It can be seen that it is 11.4

The CUDA version of the system determines the highest version of cudatoolkit the system can support. It is backward compatible.

For example, if my CUDA Version=11.4, then it can support CUDATookit version ≤ 11.4

Now that you know the cuda version of your system, I will use an example of using the Tsinghua mirror source to configure the cuda+cudnn+pytorch deep learning environment to briefly and straightforwardly explain how to do it.

Let me remind you again, the following operations must also be performed when you activate your own virtual environment!

3. Install CUDATookit

We need to use the following command

conda install cudatoolkit=11.3 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64/

I chose to download version 11.3. This needs to be changed according to your own needs, as long as your system supports CUDA.

4. Install cuDNN

If you successfully install the version of cudatookit you want, note that the version of cudnn you install now must depend on the version of cudatookit

Here I briefly list the correspondence between some newer versions.

As can be seen from the picture above, I installed CUDA version 11.3, so there are many cuDNN versions available. Here I chose version 8.2.1 of cuDNN, which was also installed using the Tsinghua mirror source.

You can also use conda search cudnn to find the corresponding version

conda install cudnn=8.2.1 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/

 

 

5. Install Pytorch

According to what we have said before, the pytorch version also corresponds to the CUDA version. For example, torch1.6.0 is only suitable for cuda10.2, 10.1, and 9.2, but not for cuda11.0.

Our next operation needs to go to Pytorch's official website Previous PyTorch Versions | PyTorch, where you can check the Pytorch version you want and its adapted CUDA version and get the installation command.

For example, I now want to install version 1.11.0 of Pytorch, and I have already installed version 11.3 of CUDA.

As shown above, this command can satisfy the correspondence between my pytorch and cuda. ​​Therefore, we copy it and run it to install Pytorch 1.11.0

conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch

……

At this point, the installation is complete!

Notice:

You can also use the pip install command to download pytorch, but because conda is used in steps three and four, conda is also used here for convenience;

conda install pytorch installs the torch CPU version, conda install pytorch torchvision -c pytorch installs the GPU version.

Check whether the environment is configured successfully

If the following operations can be performed normally and the corresponding version you installed prints out, then you have successfully configured it.

#Enter virtual environment

conda activate [your virtual environment name]

#Enter python to enter the python environment

python
#加载torch
import torch
print(torch.backends.cudnn.version())
#输出8200,代表着成功安装了cudnn v8.4.0
print(torch.__version__)
#输出1.11.0,代表成功安装了pytorch 1.11.0
print(torch.version.cuda)
#输出11.3,代表成功安装了cuda 11.3
torch.cuda.is_available()
#True

Guess you like

Origin blog.csdn.net/qq_18256855/article/details/131217600