准备Ubuntu20.4 VM
安装Cuda12.4
1.进入如下界面安装安装Cuda12.4版本:
CUDA Toolkit 12.4 Update 1 Downloads | NVIDIA Developerhttps://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=20.04&target_type=deb_local
切换到ubuntu进行安装。
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda-repo-ubuntu2004-12-4-local_12.4.1-550.54.15-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-12-4-local_12.4.1-550.54.15-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2004-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-4
sudo apt-get install -y cuda-drivers
2.验证安装
nvidia-smi
安装CUDA Toolkit
CUDA Toolkit (nvidia): CUDA完整的工具安装包,其中提供了 Nvidia 驱动程序、开发 CUDA 程序相关的开发工具包等可供安装的选项。包括 CUDA 程序的编译器、IDE、调试器等,CUDA 程序所对应的各式库文件以及它们的头文件。
1.登录到ubuntu用户,执行如下命令,安装toolkit
sudo apt install nvidia-cuda-toolkit
2.验证安装nvcc
nvcc -V
ubuntu@msl-instance-3:~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243
ubuntu@msl-instance-3:~$
3.设置环境变量:~/.bashrc
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
4.source ~/.bashrc
5.nvcc和nvidia-smi的cuda版本就一致了
安装cuDNN
cudnn:为深度学习计算设计的软件库。
Cuda和cuDNN映射关系:
Support Matrix — NVIDIA cuDNN v9.1.1 documentation
1.安装zlib1g
sudo apt-get install zlib1g
2.下载
wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-9.1.0.70_cuda12-archive.tar.xz
3. 解压cuDNN package
$ tar -xvf cudnn-linux-x86_64-9.1.0.70_cuda12-archive.tar.xz
4,把文件copy到CUDA Toolkit 目录中
$ sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include
$ sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
5.验证安装
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
有如下内容输出,则说明安装成功了。
Pytorch安装并验证
1.安装Anaconda
参考:超详细Ubuntu安装Anaconda步骤+Anconda常用命令_ubuntu安装conda-CSDN博客
下载:https://www.anaconda.com/download#downloads
wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh
使用bash命令安装Anaconda,一路回车
~/.bashrc文件中配置的conda环境变量。
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/ubuntu/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/ubuntu/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/ubuntu/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/ubuntu/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
2.创建pytorch的虚拟环境
conda create -n pytorch python=3.11.7
3.安装pytorch最新版
pip3 install torch torchvision torchaudio
4.安装验证:
python
import torch
print('CUDA版本:',torch.version.cuda)
print('Pytorch版本:',torch.__version__)
print('显卡是否可用:','可用' if(torch.cuda.is_available()) else '不可用')
print('显卡数量:',torch.cuda.device_count())
print('是否支持BF16数字格式:','支持' if (torch.cuda.is_bf16_supported()) else '不支持')
print('当前显卡型号:',torch.cuda.get_device_name())
print('当前显卡的CUDA算力:',torch.cuda.get_device_capability())
print('当前显卡的总显存:',torch.cuda.get_device_properties(0).total_memory/1024/1024/1024,'GB')
print('是否支持TensorCore:','支持' if (torch.cuda.get_device_properties(0).major >= 7) else '不支持')
print('当前显卡的显存使用率:',torch.cuda.memory_allocated(0)/torch.cuda.get_device_properties(0).total_memory*100,'%')
# Check if CUDA is available
is_cuda_available = torch.cuda.is_available()
if is_cuda_available:
print("CUDA is available.")
else:
print("CUDA is not available.")
# Check if cuDNN is available
from torch.backends import cudnn
cudnn.is_acceptable_type = lambda t: t.is_floating_point or t.is_integer
is_cudnn_available = cudnn.is_available()
if is_cudnn_available:
print("cuDNN is available.")
else:
print("cuDNN is not available.")