miniconda配置安裝pytorch cuda版本 + pytorch lightning


1. 安裝miniconda

  • 下載安裝包
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
  • 執行程序
bash Miniconda3-latest-Linux-x86_64.sh
  • 重啟終端或者source .bashrc后安裝完畢;執行conda,不報錯說明安裝成功
     

2. 更換為清華源

PS:后添加的會排在channels列表的最上面,因此越后添加的優先級越高。

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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

conda config --set show_channel_urls yes
conda config --set auto_activate_base false
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

或者修改.condarc文件:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true
auto_activate_base: false

PS: 這里的順序最好一致,排在越上面的表示優先級越高,因此在安裝pytorch的時候,會優先在第一個源倉庫中去尋找。而gpu版本的pytorch也只有第一個倉庫中含有。


conda恢復默認源的方法:

config --remove-key channels
conda config --set show_channel_urls yes
conda config --set auto_activate_base false

或移除清華源:

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

或者修改.condarc文件:

channels:
  - defaults
show_channel_urls: true
auto_activate_base: false

 

3. 新建python3.8環境

conda create -n torch python=3.8

並激活該環境,進入該環境后才能將pytorch安裝在此環境中:

conda activate torch

 

4. 安裝pytorch

安裝cuda和cudnn按照這篇文章

安裝pytorch前需要查看本機的cuda版本:

nvcc -V

image

由圖可知cuda版本為11.0.221

查看cudnn版本

  • whereis cudnn 找到相應的目錄
  • cat /usr/local/cudnn/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

image

可知cudnn版本為8.1.1

如果未安裝cuda和cudnn,也可以使用conda來安裝,前提是conda源中含有相應的版本。

  • 利用conda安裝cuda和cudnn(根據顯卡型號安裝相應驅動)
conda install cudatoolkit=11.0.221

因此cudnn的版本最好為8.0.5
(但是我們這里選擇了cudnn=8.1.1,原因是cudnn 8.1.1也支持cuda11.0,選擇支持自己cuda版本的最新版本,向下兼容,見下圖)
image

4.1 使用默認源安裝pytorch

對應cuda版本11.0.221,我們找到了1.7.1版本的pytorch與之對應。

conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0 -c pytorch

4.2 使用清華源安裝pytorch

conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0

4.3 指定channel安裝pytorch

conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=11.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch

4.4 本地安裝pytorch

這里用pytorch 1.7.1 cuda版本舉例。

4.4.1 下載所需的安裝包

從清華開源鏡像中找到所需要的安裝包:
image

使用wget下載對應的安裝包

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64/pytorch-1.7.1-py3.8_cuda11.0.221_cudnn8.0.5_0.tar.bz2

 
或者本地用迅雷下載資源(速度較快)[SCP指令部分]
然后通過scp指令上傳到服務器:

scp /Users/username/Downloads/filename username@servername:/path
補充:多文件拷貝
scp /Users/username/filedir/filename1 filename2 filename3  username@servername:/path

PS:
scp使用密鑰驗證並上傳文件到服務器:

scp -i 證書的絕對路徑/證書.perm 路徑/需要上傳的文件 username@servername:/path

scp下載單文件指令:

scp username@servername:/path/filename /localdir(本地目錄)
補充:
1. 多文件拷貝
scp username@servername:/path/\{filename1,filename2,filename3,filepre.* \} /localdir
2. scp默認連接的遠端主機22端口,如果ssh不是使用標准的22端口(以222為例)則使用-P(P大寫)指定:
scp -P 222 username@servername:/path/filename /localdir(本地目錄)

scp下載目錄文件指令:

scp -r username@servername:/var/www/remote_dir/(遠程目錄) /local_dir(本地目錄)

scp上傳目錄文件指令:

scp  -r local_dir username@servername:remote_dir

4.4.2 conda本地安裝

下載完成后開始本地安裝。

conda install --use-local pytorch-1.7.1-py3.8_cuda11.0.221_cudnn8.0.5_0.tar.bz2

測試時候會報錯,原因是沒有安裝相關依賴:
image

提示說明缺少動態鏈接庫文件,文件在mkl庫中,需要另外安裝。
安裝所需要的依賴后即可:

conda install mkl

image

 

5. 測試pytorch是否安裝成功

終端執行python,並輸入以下命令:

import torch
print(torch.__version__, torch.cuda.is_available())

如果結果如下,則說明cuda版本的pytorch安裝成功。

image

 

6. 安裝pytorch_lightning

image

前提:安裝的pytorch至少應該是1.7版本。

conda install pytorch-lightning


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM