programming3 MIN READ

[AWS] EC2에서 GPU 사용하기

[AWS] EC2에서 GPU 사용하기

gpu 사양을 물고있는 instance에서 gpu를 사용하려면 여러방법이있는데 우선은 아래와 같다

1. deep learning AMI 사용하기

AMI중에 아예 deep learning base로 환경을 세팅해둔 AMI들이있다 deep learning 을 키워드로 검색하면 aws linux나 ubuntu 등 나오는데 이녀석들을 설치해주면 nivida 드라이버가 알아서 다 깔리고 pytorch 버전도 맞춰서 설치되어있다. Deep Learning Base GPU AMI (Ubuntu 20.04) 20231026 Deep Learning AMI GPU PyTorch 2.1.0 (Ubuntu 20.04) 20231103 등등..

2. 기본 이미지에 nvida드라이버 설치

기본적으로 제공되는 amazon linux나 ubuntu 이미지를 사용했다면 아래와같이 가이드에 따라 nvidia드라이버를 따로 깔아주면 된다. https://docs.nvidia.com/datacenter/tesla/tesla-installation-notes/index.html#abstract 가령 ubuntu 라면 아래와 같다

sudo su ubuntu # ubuntu 관리자 계정으로 바꿔줌
The NVIDIA driver requires that the kernel headers and development packages for the running version of the kernel be installed at the time of the driver installation, as well whenever the driver is rebuilt. For example, if your system is running kernel version 4.4.0, the 4.4.0 kernel headers and development packages must also be installed.
The kernel headers and development packages for the currently running kernel can be installed with:
$ sudo apt-get install linux-headers-$(uname -r)
Install the CUDA repository public GPG key. This can be done via the cuda-keyring package or a manual installation of the key. The usage of apt-key is deprecated.
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID | sed -e 's/\.//g')
$ wget https://developer.download.nvidia.com/compute/cuda/repos/$distribution/x86_64/cuda-keyring_1.0-1_all.deb
$ sudo dpkg -i cuda-keyring_1.0-1_all.deb
Update the APT repository cache and install the driver using the cuda-drivers meta-package. Use the --no-install-recommends option for a lean driver install without any dependencies on X packages. This is particularly useful for headless installations on cloud instances.
$ sudo apt-get update
$ sudo apt-get -y install cuda-drivers

단점은 이렇게하면 드라이버가 깨지는 경우가있따 ㅜ 그럴땐 아래와같이 드라이버를 최신으로 업데이트해주자

sudo apt-get update
# 업데이트 중 깨진다면
sudo apt --fix-broken install

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/install-nvidia-driver.html#preinstalled-nvidia-driver
https://docs.nvidia.com/datacenter/tesla/tesla-installation-notes/index.html#abstract