WSL2笔记4 PyTorch深度学习框架的多环境CUDA安装配置
1、 Conda多环境下的不同CUDA版本实现
1.1 实现思路
1.1.1 根环境(base)安装最新独立版本CUDA ToolKit工具包
NVIDIA CUDA ToolKit
https://developer.nvidia.com/cuda-downloads
安装与显卡驱动匹配的最新版本CUDA ToolKit
详见【WSL2笔记2】 搭建深度学习开发环境
https://blog.csdn.net/fuweipeng2012/article/details/129980680
1.1.2 在不同的Python虚拟子环境,根据需求,安装相应版本的CUDA和cuDDN
conda install cudatoolkit=版本号
Conda 安装的CUDA ToolKit 包含了CUDA和cuDDN
Conda虚拟环境调用的是子环境下的CUDA ToolKit的CUDA/cuDDN,不是调用根环境版本的CUDA工具包
虚拟子环境CUDA ToolKit版本一定要低于本机根环境的独立CUDA版本
2、 Python 3.10 + PyTorch 2.0.0 + CUDA 11.8
2.1 创建Python310环境
创建
conda create -n py310 python=3.10
激活
conda activate py310
(base) xf@VP01:~/ai/env$ conda create -n py310 python=3.10
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /home/xf/anaconda3/envs/py310
added / updated specs:
- python=3.10
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2022.12.7 | py310h06a4308_0 150 KB defaults
pip-23.0.1 | py310h06a4308_0 2.6 MB defaults
python-3.10.10 | h7a1cb2a_2 26.9 MB defaults
setuptools-65.6.3 | py310h06a4308_0 1.2 MB defaults
tzdata-2023c | h04d1e81_0 116 KB defaults
wheel-0.38.4 | py310h06a4308_0 64 KB defaults
------------------------------------------------------------
Total: 31.0 MB
The following NEW packages will be INSTALLED:
_libgcc_mutex anaconda/pkgs/main/linux-64::_libgcc_mutex-0.1-main
_openmp_mutex anaconda/pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
bzip2 anaconda/pkgs/main/linux-64::bzip2-1.0.8-h7b6447c_0
ca-certificates anaconda/pkgs/main/linux-64::ca-certificates-2023.01.10-h06a4308_0
certifi anaconda/pkgs/main/linux-64::certifi-2022.12.7-py310h06a4308_0
ld_impl_linux-64 anaconda/pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
libffi anaconda/pkgs/main/linux-64::libffi-3.4.2-h6a678d5_6
libgcc-ng anaconda/pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
libgomp anaconda/pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
libstdcxx-ng anaconda/pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
libuuid anaconda/pkgs/main/linux-64::libuuid-1.41.5-h5eee18b_0
ncurses anaconda/pkgs/main/linux-64::ncurses-6.4-h6a678d5_0
openssl anaconda/pkgs/main/linux-64::openssl-1.1.1t-h7f8727e_0
pip anaconda/pkgs/main/linux-64::pip-23.0.1-py310h06a4308_0
python anaconda/pkgs/main/linux-64::python-3.10.10-h7a1cb2a_2
readline anaconda/pkgs/main/linux-64::readline-8.2-h5eee18b_0
setuptools anaconda/pkgs/main/linux-64::setuptools-65.6.3-py310h06a4308_0
sqlite anaconda/pkgs/main/linux-64::sqlite-3.41.1-h5eee18b_0
tk anaconda/pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
tzdata anaconda/pkgs/main/noarch::tzdata-2023c-h04d1e81_0
wheel anaconda/pkgs/main/linux-64::wheel-0.38.4-py310h06a4308_0
xz anaconda/pkgs/main/linux-64::xz-5.2.10-h5eee18b_1
zlib anaconda/pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate py310
#
# To deactivate an active environment, use
#
# $ conda deactivate
(base) xf@VP01:~/ai/env$ conda activate py310
(py310) xf@VP01:~/ai/env$
2.2 安装PyTorch200 GPU版本
2.2.1 官网链接
PyTorch最新版
https://pytorch.org/get-started/locally/
Lunix系统PyTorch CUDA 对Python版本的要求 >= Python 3.7
Installing on Linux
PythonPython 3.7 or greater is generally installed by default on any of our supported Linux distributions, which meets our recommendation.
Win系统PyTorch CUDA 对Python版本的要求 >= Python 3.7 <= Python 3.9
Installing on Windows
PythonCurrently, PyTorch on Windows only supports Python 3.7-3.9; Python 2.x is not supported.
2.2.2 Conda安装方法
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
package | build
-----------------------------|-----------------
> pytorch-2.0.0 | py3.10_cuda11.8_cudnn8.7.0_0 1.41 GB pytorch
...
> torchaudio-2.0.0 | py310_cu118 7.5 MB pytorch
...
> torchvision-0.15.0 | py310_cu118 8.0 MB pytorch
在安装包列表中了解即将安装的是否CUDA版本, 第2列版本build中带_cu118,说明是CUDA加速版本
(py310) xf@VP01:~/ai/env$ conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /home/xf/anaconda3/envs/py310
added / updated specs:
- pytorch
- pytorch-cuda=11.8
- torchaudio
- torchvision
The following packages will be downloaded:
package | build
---------------------------|-----------------
blas-1.0 | mkl 6 KB defaults
brotlipy-0.7.0 |py310h7f8727e_1002 712 KB defaults
cffi-1.15.1 | py310h5eee18b_3 243 KB defaults
cryptography-39.0.1 | py310h9ce1e76_0 1.4 MB defaults
filelock-3.9.0 | py310h06a4308_0 18 KB defaults
flit-core-3.8.0 | py310h06a4308_0 84 KB defaults
gmpy2-2.1.2 | py310heeb90bb_0 517 KB defaults
idna-3.4 | py310h06a4308_0 97 KB defaults
jinja2-3.1.2 | py310h06a4308_0 215 KB defaults
markupsafe-2.1.1 | py310h7f8727e_0 34 KB defaults
mkl-service-2.4.0 | py310h7f8727e_0 177 KB defaults
mkl_fft-1.3.1 | py310hd6ae3a3_0 567 KB defaults
mkl_random-1.2.2 | py310h00e6091_0 1009 KB defaults
mpmath-1.2.1 | py310h06a4308_0 769 KB defaults
networkx-2.8.4 | py310h06a4308_1 2.7 MB defaults
numpy-1.23.5 | py310hd5efca6_0 10 KB defaults
numpy-base-1.23.5 | py310h8e6c178_0 6.7 MB defaults
pillow-9.4.0 | py310h6a678d5_0 730 KB defaults
pyopenssl-23.0.0 | py310h06a4308_0 97 KB defaults
pysocks-1.7.1 | py310h06a4308_0 28 KB defaults
pytorch-2.0.0 |py3.10_cuda11.8_cudnn8.7.0_0 1.41 GB pytorch
requests-2.28.1 | py310h06a4308_1 100 KB defaults
sympy-1.11.1 | py310h06a4308_0 11.8 MB defaults
torchaudio-2.0.0 | py310_cu118 7.5 MB pytorch
torchtriton-2.0.0 | py310 62.6 MB pytorch
torchvision-0.15.0 | py310_cu118 8.0 MB pytorch
typing_extensions-4.4.0 | py310h06a4308_0 46 KB defaults
urllib3-1.26.15 | py310h06a4308_0 199 KB defaults
------------------------------------------------------------