[cuda 소개]
CUDA (Computed Unified Device Architecture)는 NVIDIA에서 개발한 GPU 개발 툴이다.
많은 연구자들이 딥러닝에 사용할 수 있도록, 쉽게 설치할 수 있도록 오픈하였다.
CUDA를 사용하는 이유는 매우 간단한데, 많은 양의 연산을 동시에 처리하는 것이 목표이다.
MultiProcessing, Multi-Threading 등을 이용하여 CPU가 보유한 코어 갯수 만큼의 Multi-Core를 이용하여 연산을 할 수 있다. 일반적으로 딥러닝에서 사용하는, 특히 pytorch나 tensorflow 에서, data loader 파트에서, core 갯수를 주고 데이터 loading 하는 부분이 여기에 속한다.
-> cuda는 nvidia의 gpu를 사용하기 위한 것이다.
-> pytorch에서는 cuda만을 사용해서 gpu를 활용한다.
-> 즉, pytorch와 cuda를 사용할 수 있어야한다.
[실제 활용 소개]
파이토치에서 GPU를 사용가능하도록 하는 명령어
-> tensorflow는 gpu에 자동할당해주지만 pytorch에서는 gpu에 해당 tensor을 올리라고 코드를 작성해야한다.
1) gpu 사용가능여부 확인
nvidia drivers와 cuda library가 설치되어있는 지 확인한다. 아래 코드가 true면 nvidia driver가 있어서 cuda 활용이 가능하다는 의미다.
import torch
torch.cuda.is_available()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
이때 .to(device) 를 붙여주면 gpu로 연산을 진행합니다.
2) gpu사용하기
일반적으로 tensor는 cpu에서 사용할 수 있는 메모리에 생성된다.
>>> X_train = torch.FloatTensor([0., 1., 2.])
>>> X_train.is_cuda
False
텐서와 모델을 gpu에 할당해줘야한다.

3) 텐서가 같은 device를 사용하도록 해야한다.
multiple cuda devices가 있는지 확인하기
torch.get_device
torch.cuda.set_device(0) # or 1,2,3
4) torch cuda 패키지
- 현재 device정보
torch.cuda.current_device() # returns 0 in my case
- 사용 가능한 device 수
torch.cuda.device_count() # returns 1 in my case
- device 이름
torch.cuda.get_device_name(0) # good old Tesla K80
5) 실습
keras와 weights and biases를 사용해서 gpu resource consumption을 관찰한다.
MNIST Classification using PyTorch
Colaboratory notebook
colab.research.google.com
*weights and biases
Weights & Biases helps you keep track of your machine learning experiments. Use our tool to log hyperparameters and output metrics from your runs, then visualize and compare results and quickly share findings with your colleagues.
Get started in 5 minutes.
Never lose your progress – Instrument W&B in 5 mins
We're building developer tools for deep learning. Add a couple lines of code to your training script and we'll keep track of your hyperparameters, system metrics, and outputs so you can compare experiments, see live graphs of training, and easily share you
wandb.ai
6) 디바이스 상태 확인
!pip install pycudaimport torch
import pycuda.driver as cuda
cuda.init()
## Get Id of default device
torch.cuda.current_device()
# 0
cuda.Device(0).name() # '0' is the id of your GPU
참고:
https://wandb.ai/wandb/common-ml-errors/reports/How-To-Use-GPU-with-PyTorch---VmlldzozMzAxMDk
How To Use GPU with PyTorch
A short tutorial on using GPUs for your deep learning models with PyTorch. Made by Ayush Thakur using Weights & Biases
wandb.ai
https://medium.com/ai%C2%B3-theory-practice-business/use-gpu-in-your-pytorch-code-676a67faed09
Use GPU in your PyTorch code
Recently I installed my gaming notebook with Ubuntu 18.04, and took some time to make Nvidia driver as the default graphics driver ( since…
medium.com
'이제는 사용하지 않는 공부방 > Artificial intelligence' 카테고리의 다른 글
| BERT (bidirectional encoder representations from transformers) (0) | 2021.10.04 |
|---|---|
| Transformer ( attention is all you need ) (0) | 2021.10.01 |
| how to decide number of layers and number of neurons (0) | 2021.09.20 |
| 자꾸 헷갈리는 axis 정리 (0) | 2021.09.14 |
| 자연어처리 이론 한번에 정리하기 (0) | 2021.09.01 |