1. 环境:
win10 64,anaconda3,python3.8,VS2019
2. conda创建新的环境
conda create -n torch(环境名) python==3.8
activate torch
对了,conda添加清华的镜像源头,速度快些
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
3. 安装CUDA、cudnn
可以按传统的老方法官网下载或者有大佬上传了百度云:CUDA和cudnn。也可以直接根据自己电脑驱动使用conda下载安装。pytorch 各种版本下载官网:Previous PyTorch Versions | PyTorch
## 我用的这个
pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
可以参考:在conda虚拟环境中安装cuda和cudnn,解决pip install or python setup.py install 将安装包安装到全局环境中的问题(1.2部分)。 - 知乎
验证是否安装好:
import torch
#返回当前设备索引
# torch.cuda.current_device()
#返回GPU的数量
# torch.cuda.device_count()
#返回gpu名字,设备索引默认从0开始
# torch.cuda.get_device_name(0)
#cuda是否可用
# torch.cuda.is_available()
# pytorch 查看cuda 版本
# 由于pytorch的whl 安装包名字都一样,所以我们很难区分到底是基于cuda 的哪个版本。
# print(torch.version.cuda)
# 判断pytorch是否支持GPU加速
# print (torch.cuda.is_available())
# 【PyTorch】查看自己的电脑是否已经准备好GPU加速(CUDA)
# 那么在CUDA已经准备好的电脑上,会输出:cuda:0
# 而在没有CUDA的电脑上则输出:cpu
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
4. 安装其他库:cocoapi、fvcore等
1. 首先安装cythpon
pip install Cython -i https://pypi.tuna.tsinghua.edu.cn/simple
2. 安装cocoapi
点击GitHub - philferriere/cocoapi: Clone of COCO API - Dataset @ http://cocodataset.org/ - with changes to support Windows build and python3下载,然后执行以下指令
pip install Cython -i https://pypi.tuna.tsinghua.edu.cn/simple
cd coco/PythonAPI
python setup.py build_ext --inplace
python setup.py build_ext install
(2) conda安装 fvcore 和 ninja
conda install -c fvcore -c iopath -c conda-forge fvcore
## conda install ninja 出错!!!!
会报错: subprocess.CalledProcessError: Command ‘[‘ninja‘, ‘-v‘]‘ returned non-zero exit status 1.
1. 怀疑ninja没安好:选择ninja-win.zip下载。Releases · ninja-build/ninja (github.com)
2. cuda版本不对 pip 安装时没有自动安装cuda,换conda:
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=10.2 -c pytorch
出错:
error: Setup script exited with error: SandboxViolation: mkdir('C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\easy_install-pzhrecod\\easydict-1.9\\build', 511) {}
The package setup script has attempted to modify files on your system
that are not within the EasyInstall build area, and has been aborted.
This package cannot be safely installed by EasyInstall, and may not
support alternate installation locations even if you run its setup
script by hand. Please inform the package's author and the EasyInstall
maintainers to find out if a fix or workaround is available.
于是在运行中输入optionalfeatures,启用沙盒功能。
解决方法2 :windows内置linux wsl中跑代码
5步搭建wsl2+cuda+docker解决windows深度学习开发问题 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/408403790?utm_source=wechat_session&utm_medium=social&utm_oi=763742012275986432&utm_campaign=shareopn注意事项:
- Windows 开发版(也就是OS内部版本)从20211到20215概率性不能启动WSL虚拟机。NVIDIA Drivers for CUDA on WSL不支持Windows开放版20211-20221。如果当前主机是这几个版本,一定想好要不要往下走。
- 谁也不能保证微软和Nvidia未来的更新是不是完全可用,请关闭Windows自动更新,并做好全系统备份。
- 宿主机不能安装Docker Desktop,与虚拟机Docker冲突。(卸载原来windows安装的docker )
- 微软官方教程 :Enable NVIDIA CUDA on WSL 2 | Microsoft Docs
我遇到的问题:
/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link
查到这个答案:
/usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link · Issue #5663 · microsoft/WSL · GitHub
按照微软官方的回复:
我首先查看系统内核
C:\Users\Administrator> wsl cat /proc/version
Linux version 5.4.72-microsoft-standard-WSL2 (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Wed Oct 28 23:40:43 UTC 2020
官方教程说:
于是更新系统内核
如何更新 Win10 WSL2 的 Linux 内核 - 知乎Windows 10 的 WSL2 的很多特性,带来了比传统虚拟机与主机更佳的集成;同时 vscode 的支持跟进也很快,对于开发者而言,几乎免去了裸机装Linux的步骤。 问题如果你按照官方文档安装了一个发行版,并下载了页面指…https://zhuanlan.zhihu.com/p/355606922
- 找到微软发布的内核更新安装文件 msi https://www.catalog.update.microsoft.com
- 彻底关闭当前的 WSL,更新内核 —— 以管理员身份运行 wsl --shutdown;运行第2步下载的文件,完成内核更新;
二、安装docker及nvidia-docker
这个需要通过命令行安装,我开始使用了Docker Desktop发现有问题,如果使用了Docker Desktop,需要卸载。
参考这个教程安装docker及nvidia-docker
Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU_SUNbrightness的博客-CSDN博客_nvidia-docker windows
测试docker是否安装成功与是否能访问GPU资源
sudo docker run --gpus all nvcr.io/nvidia/k8s/cuda-sample:nbody nbody -gpu -benchmark
显示:
Run "nbody -benchmark [-numbodies=<numBodies>]" to measure performance.
-fullscreen (run n-body simulation in fullscreen mode)
-fp64 (use double precision floating point values for simulation)
-hostmem (stores simulation data in host memory)
-benchmark (run benchmark to measure performance)
-numbodies=<N> (number of bodies (>= 1) to run in simulation)
-device=<d> (where d=0,1,2.... for the CUDA device to use)
-numdevices=<i> (where i=(number of CUDA devices > 0) to use for simulation)
-compare (compares simulation results running once on the default GPU and once on the CPU)
-cpu (run n-body simulation on the CPU)
-tipsy=<file.bin> (load a tipsy model file for simulation)
NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.
> Windowed mode
> Simulation data stored in video memory
> Single precision floating point simulation
> 1 Devices used for simulation
GPU Device 0: "Turing" with compute capability 7.5
> Compute 7.5 CUDA device: [NVIDIA GeForce RTX 2060 with Max-Q Design]
30720 bodies, total time for 10 iterations: 68.014 ms
= 138.754 billion interactions per second
= 2775.083 single-precision GFLOP/s at 20 flops per interaction
有三点一定需要注意:
1. 一定需要加入windows insider program,其它方法可以安装wsl但是无法访问到gpu资源。
2. 要下载wsl专用的nvidia驱动安装在windows。
3.要删除掉windows里的 desktop docker
tips:
WSL2默认是没有systemd服务的,需要自行去安装。
Systemd 是 Linux 系统工具,为系统的启动和管理提供一套完整的解决方案。已成为大多数发行版的标准配置。
根据 Linux 惯例,字母d
是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。
Systemd 并不是一个命令,而是一组命令,涉及到系统管理的方方面面。
WLS2安装systemd
下载仓库的zip文件并解压到windows任意路径(这里以E:/script/)为例
链接:文件 · master · mirrors / DamionGans / ubuntu-wsl2-systemd-script · GitCode
执行命令
cp -r /mnt/e/script ~
cd ~/script/ubuntu-wsl2-systemd-script-master
bash ubuntu-wsl2-systemd-script.sh
验证安装结果
cz@PC-20201218TQUE:~/script/ubuntu-wsl2-systemd-script-master$ systemctl --version
systemd 245 (245.4-4ubuntu3.4)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid
参考:wsl2配置启用systemd一键脚本 | Feng (ukiyo.top)https://blog.ukiyo.top/post/9214/
下载Anaconda3-5.3.1-Linux-x86_64.sh,wsl2中安装
bash Anaconda3-2020.02-Linux-x86_64.sh
记下框中的路径,也就conda的路径,我们需要把它添加进环境变量中。
- 编辑profile
sudo vim /etc/profile
- 配置conda等的环境变量,就是上面提到的路径,写到bin即可。
在文件的最底端,加上下面的这句:
export PATH=/home/cz/anaconda3/bin:$PATH
- 重新加载环境变量
source /etc/profile
- 检查是否成功,输入python,看其是否在anaconda环境下。
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
[GCC 7.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
。。。。。。。。。
另外附赠:
WSL2 安装 GUI,并使用 XRDP实现连接(内含汉化操作) - 简书 (jianshu.com)