JUST WRITE

AWS EC2에서 GPU를 사용하려면?! 본문

MLOps

AWS EC2에서 GPU를 사용하려면?!

천재보단범재 2023. 4. 24. 22:23

Nvidia Driver 설치

AWS EC2 GPU를 사용하려면?!

딥러닝 작업은 단순사칙연산을 수행합니다.

단순사칙연산 작업은 CPU보다는 GPU에 작업 시 효율적으로 동작합니다.

GPU 서버에서 GPU를 활용해서 딥러닝 작업을 하려면 추가적인 조치가 필요합니다.

해당 조치중에 Nvidia Driver를 설치하여 GPU를 활용할 수 있도록 하는 방안이 있습니다.

GPU를 지원하는 AWS EC2에서 Nvidia Driver 설치 과정을 정리하였습니다.

EC2 인스턴스 시작

AWS EC2중에서 GPU를 지원하는 인스턴스 유형이 있습니다.

해당 링크에서 인스턴스 유형을 확인할 수 있습니다.

  • P3 인스턴스는 Nvidia Tesla V100 GPU 사용
  • P4 인스턴스는 Nvidia Tesla A100 GPU 사용
  • G3 인스턴스는 Nvidia Tesla M60 GPU 사용
  • G4 인스턴스는 Nvidia T4 GPU 사용
  • G5 인스턴스는 Nvidia A10G GPU 사용
  • G5g 인스턴스는 ARM기반 AWSGraviton2 Proccessor 사용

이번 포스팅에서는 p2.xlarge로 EC2 인스턴스를 생성하여 진행하였습니다.

OS는 Ubuntu LTS 22.04 LTS로 진행하였습니다.

Nvidia Driver 설치

p2.xlarge로 EC2 인스턴스를 생성하고 해당 서버에 GPU가 있는지 command로 확인 가능합니다.

lspci(List PCI) command로 확인 가능합니다.

Tesla K80 GPU를 확인할 수 있습니다.

$ lspci | grep -i nvidia
00:1e.0 3D controller: NVIDIA Corporation GK210GL [Tesla K80] (rev a1)

설치 전 확인

먼저 gcc 설치가 필요합니다.

# gcc check
$ gcc --version
Command 'gcc' not found, but can be installed with

# gcc install
$ sudo apt-get update
$ sudo apt install gcc -y
$ gcc --version
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Linux Kernel Header를 설치합니다.

$ sudo apt-get install linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
linux-headers-5.15.0-1031-aws is already the newest version (5.15.0-1031.35).
0 upgraded, 0 newly installed, 0 to remove and 32 not upgraded.

ubuntu-driver 패키지를 설치합니다.

$ sudo apt install ubuntu-drivers-common -y

Nvidia Driver 설치

ubuntu-driver를 통해 nvidia-driver 추천 버전을 확인할 수 있습니다.

$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:1e.0 ==
modalias : pci:v000010DEd0000102Dsv000010DEsd0000106Cbc03sc02i00
vendor   : NVIDIA Corporation
model    : GK210GL [Tesla K80]
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-450-server - distro non-free
driver   : nvidia-driver-470 - distro non-free recommended
driver   : nvidia-driver-418-server - distro non-free
driver   : nvidia-driver-390 - distro non-free
driver   : xserver-xorg-video-nouveau - distro free builtin

nvidia-driver-470을 추천해 주는 것을 확인할 수 있습니다.

설치할 Driver 버전을 위한 Repository를 추가합니다.

$ 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
--2023-04-24 14:15:51--  https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
Resolving developer.download.nvidia.com (developer.download.nvidia.com)... 152.199.39.144
Connecting to developer.download.nvidia.com (developer.download.nvidia.com)|152.199.39.144|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4332 (4.2K) [application/x-deb]
Saving to: ‘cuda-keyring_1.0-1_all.deb’

cuda-keyring_1.0-1_all.deb         100%[===============================================================>]   4.23K  --.-KB/s    in 0s      

2023-04-24 14:15:51 (165 MB/s) - ‘cuda-keyring_1.0-1_all.deb’ saved [4332/4332]

$ sudo dpkg -i cuda-keyring_1.0-1_all.deb
Selecting previously unselected package cuda-keyring.
(Reading database ... 68023 files and directories currently installed.)
Preparing to unpack cuda-keyring_1.0-1_all.deb ...
Unpacking cuda-keyring (1.0-1) ...
Setting up cuda-keyring (1.0-1) ...

A deprecated public CUDA GPG key appear to be installed.
To remove the key, run this command:
sudo apt-key del 7fa2af80

Repository를 추가된 것을 확인할 수 있습니다.

$ cat /etc/apt/sources.list.d/cuda-ubuntu2204-x86_64.list
deb [signed-by=/usr/share/keyrings/cuda-archive-keyring.gpg] https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /

그리고 이제 nvidia-driver-470을 설치합니다.

$ sudo apt-get update
$ sudo apt-get install nvidia-driver-470

nvidia-smi 명령어를 실행하면 Nvidia-driver와 연결이 안 된다는 것을 확인할 수 있습니다.

$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

하지만 서버를 재시작하면 정상적으로 Nvidia-driver 설치가 된 것을 확인할 수 있습니다.

$ nvidia-smi
Mon Apr 24 14:34:27 2023       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.182.03   Driver Version: 470.182.03   CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla K80           Off  | 00000000:00:1E.0 Off |                    0 |
| N/A   27C    P0    71W / 149W |      0MiB / 11441MiB |     54%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
728x90
반응형

'MLOps' 카테고리의 다른 글

What is CI/CD?  (0) 2022.03.13
Comments