WSL CUDA
Y
Yerim
- Setup
wsl --installwsl --status
wsl --list --online$ nvidia-sim$ sudo apt update
$ sudo apt install build-essential$ wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda_12.1.0_530.30.02_linux.run
$ sudo sh cuda_12.1.0_530.30.02_linux.runsudo apt update
sudo apt install gcc-9 g++-9sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 10sudo update-alternatives --config gcc
sudo update-alternatives --config g++There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-13 100 auto mode
* 1 /usr/bin/gcc-9 90 manual mode
Press enter to keep the current choice[*], or type selection number: 1$ sudo sh cuda_12.1.0_530.30.02_linux.run===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-12.1/
Please make sure that
- PATH includes /usr/local/cuda-12.1/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-12.1/lib64, or, add /usr/local/cuda-12.1/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-12.1/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 530.00 is required for CUDA 12.1 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
sudo <CudaInstaller>.run --silent --driver
Logfile is /var/log/cuda-installer.log$ sudo sh cuda_12.1.0_530.30.02_linux.run$ sudo sh cuda_12.1.0_530.30.02_linux.run===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-12.1/
Please make sure that
- PATH includes /usr/local/cuda-12.1/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-12.1/lib64, or, add /usr/local/cuda-12.1/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-12.1/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 530.00 is required for CUDA 12.1 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
sudo <CudaInstaller>.run --silent --driver
Logfile is /var/log/cuda-installer.log$ ls /usr/local/ | grep cuda
cuda
cuda-12.1wget https://developer.download.nvidia.com/compute/cuda/11.6.2/local_installers/cuda_11.6.2_510.47.03_linux.run$ chmod +x cuda_11.6.2_510.47.03_linux.run
$ sudo ./cuda_11.6.2_510.47.03_linux.run --silent --toolkit
$ ls /usr/local/ | grep cuda
cuda
cuda-11.6
cuda-12.1$ wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-9.0.0.312_cuda11-archive.tar.xz
$ wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-x86_64/cudnn-linux-x86_64-9.0.0.312_cuda12-archive.tar.xztar -xvf cudnn-linux-x86_64-9.0.0.312_cuda11-archive.tar.xz
tar -xvf cudnn-linux-x86_64-9.0.0.312_cuda12-archive.tar.xz$ cd cudnn-linux-x86_64-9.0.0.312_cuda11-archive
$ ls
LICENSE include lib
$ sudo cp include/cudnn*.h /usr/local/cuda-11.6/include
$ sudo cp lib/libcudnn* /usr/local/cuda-11.6/lib64
$ cd ../cudnn-linux-x86_64-9.0.0.312_cuda12-archive
$ ls
LICENSE include lib
$ sudo cp include/cudnn*.h /usr/local/cuda-12.1/include
$ sudo cp lib/libcudnn* /usr/local/cuda-12.1/lib64$ nvcc -vCommand 'nvcc' not found, but can be installed with:
sudo apt install nvidia-cuda-toolkit$ vi ~/.bashrc... 기존 .bashrc 내용들
function _switch_cuda {
v=$1
export PATH=$PATH:/usr/local/cuda-$v/bin
export CUDADIR=/usr/local/cuda-$v
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-$v/lib64
nvcc --version
}$ source ~/.bashrc
$ _switch_cuda 12.1nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Feb__7_19:32:13_PST_2023
Cuda compilation tools, release 12.1, V12.1.66
Build cuda_12.1.r12.1/compiler.32415258_0# ~/.bashrc
# 기본 CUDA 버전 설정 (12.1)
export PATH=$PATH:/usr/local/cuda-12.1/bin
export CUDADIR=/usr/local/cuda-12.1
# 필요 시 cuda-11.6 사용
alias use-cuda-11.6='export PATH=/usr/local/cuda-11.6/bin:$PATH && export CUDADIR=/usr/local/cuda-11.6'
alias use-cuda-12.1='export PATH=/usr/local/cuda-12.1/bin:$PATH && export CUDADIR=/usr/local/cuda-12.1'
$ wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh$ bash Anaconda3-2024.10-1-Linux-x86_64.sh$ source ~/.bashrcexport PATH="/home/username/anaconda3/bin:$PATH"conda env listconda create -n [가상환경 이름] python=파이썬버전
conda actiavte [가상환경 이름]jupyter notebookconda install pytorch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 pytorch-cuda=12.1 -c pytorch -c nvidia# cuda: 0 또는 GPU 장치를 나타내면 GPU 사용 중
print(model.device)code#cuda 사용 가능 여부
import torch
print(torch.cuda.is_available())
#train.py 실행 시 GPU 사용 여부 체크
import torch
if torch.cuda.is_available():
device = torch.device("cuda")
print(f'Training on GPU {torch.cuda.get_device_name(0)}')
else:
device = torch.device("cpu")
print('Training on CPU')