Graphic ์ฒ๋ฆฌ๋ฅผ ์ํด ๋ฑ์ฅํ ์ฐ์ฐ์ฅ์น๋ก, ์์ฒ ๊ฐ์ ์ฝ์ด๋ก ๊ตฌ์ฑ๋์ด ์์ด ๋ณ๋ ฌ ์ฐ์ฐ์ ๋งค์ฐ ๊ฐ๋ ฅํ ์ฅ์น
GPU vs CPU
| ํญ๋ชฉ | CPU (Central Processing Unit) | GPU (Graphics Processing Unit) |
|---|---|---|
| ์ฃผ์ ๋ชฉ์ | ๋ฒ์ฉ ์ฐ์ฐ, ์์คํ ์ ์ด | ๋๊ท๋ชจ ๋ฐ์ดํฐ ๋ณ๋ ฌ ์ฐ์ฐ ์ฒ๋ฆฌ |
| ์ฝ์ด ์ | ์ ์ง๋ง ๊ณ ์ฑ๋ฅ (์ ๊ฐ ~ ์์ญ ๊ฐ) | ๋ง๊ณ ๋จ์ํ ์ฝ์ด ์์ฒ ๊ฐ |
| ์ฒ๋ฆฌ ๋ฐฉ์ | ์ง๋ ฌ ์ฒ๋ฆฌ(Sequential Processing) | ๋ณ๋ ฌ ์ฒ๋ฆฌ(Parallel Processing) |
| ๋จ์ผ ์์ ์ฑ๋ฅ | ๊ฐํจ | ์๋์ ์ผ๋ก ์ฝํจ |
| ๋๊ท๋ชจ ์ฐ์ฐ ์ฑ๋ฅ | ์ฝํจ | ๋งค์ฐ ๊ฐํจ |
| ์ฌ์ฉ ์์ | OS ์คํ, ๋ ผ๋ฆฌ ์ ์ด, ์ฑ ์คํ ๋ฑ | ๋ฅ๋ฌ๋, ๊ทธ๋ํฝ ์ฒ๋ฆฌ, ํ๋ ฌ ์ฐ์ฐ ๋ฑ |
| ์ ํฉํ ์์ | ๋ณต์กํ ์กฐ๊ฑด๋ฌธ, ๋ถ๊ธฐ ์ฒ๋ฆฌ | ๋ฐ๋ณต์ ์ด๊ณ ์ ์ฌํ ์ฐ์ฐ์ ๋๋ ์ฒ๋ฆฌ |
์์ฝ: CPU๋ ๋๋ํ ์์์ ์์ ์, GPU๋ ๋จ์ํ์ง๋ง ๋ง์ ์์ ์
TPU(Tensor Processing Unit)
- ๊ตฌ๊ธ์ด ๊ฐ๋ฐํ ํน์ ํ๋์จ์ด๋ก, ์ฃผ๋ก ๋ฅ๋ฌ๋๊ณผ ๋จธ์ ๋ฌ๋ ์์ ์ ์ต์ ํํ๊ธฐ ์ํด ์ค๊ณ
- ๊ณ ์ ์ฐ์ฐ: TPU๋ ํนํ ํ๋ ฌ ์ฐ์ฐ๊ณผ ๊ฐ์ ์ํ์ ์์ ์ ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ ์ ์์ด, ๋ฅ๋ฌ๋ ๋ชจ๋ธ ํ์ต๊ณผ ์ถ๋ก ์์ ๋์ ์ฑ๋ฅ
- ๋ง์ถคํ ์ค๊ณ: CPU๋ GPU๋ณด๋ค ๋ฅ๋ฌ๋์ ํนํ๋ ์ค๊ณ๋ก, ํ ์ ์ฐ์ฐ์ ์ต์ ํ๋์ด ์์
- ๊ตฌ๊ธ์ ์์ฌ์ TPU ํ๋์จ์ด ์ค๊ณ๋ฅผ ๊ณต๊ฐX, ํด๋ผ์ฐ๋ ๋ฑ ์๋น์ค ํํ๋ก๋ง ์ ๊ณต
ํ ์, ๋ชจ๋ธ, ์ฐ์ฐ ์ค๊ฐ๊ฐ ์ ์ฅํ๋ ๊ณต๊ฐ (GPU ๋ด๋ถ์ ์์น )
GPU ์ ํ import torch
print(torch.cuda.is_available()) # True๋ฉด GPU ์ฌ์ฉ ๊ฐ๋ฅ
# PyTorch, TensorFlow ๋ฑ ์ฃผ๋ฅ ๋ฅ๋ฌ๋ ํ๋ ์์ํฌ๋ ์ด๊ธฐ๋ถํฐ ์ฐ์
ํ์ค์ด ๋์ด๋ฒ๋ฆฐ NVIDIA CUDA๋ฅผ ์ค์ฌ์ผ๋ก ๊ฐ๋ฐ๋จ
PyTorch์์๋ ์ฐ์ฐ ๋์์ด ๋๋ ๋ชจ๋ ํ
์๊ฐ ๋์ผํ ์ฅ์น(device e.g., CPU์ RAM ํน์ GPU์ VRAM) ์ ์์ด์ผ ํจ์ ์ ์ํด์ผ ํ๋ค.
์๋ก ๋ค๋ฅธ ์ฅ์น(device)์ ์๋ ํ
์๋ผ๋ฆฌ ์ฐ์ฐ์ ์ํํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
| ๊ธฐ๋ฅ | ์ฝ๋ ์์ | ์ค๋ช |
|---|---|---|
| GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ | torch.cuda.is_available() | GPU ์ฌ์ฉ ๊ฐ๋ฅํ ํ๊ฒฝ์ธ์ง ํ์ธ |
| ํ ์๋ฅผ GPU๋ก ์ด๋ | x.cuda() ๋๋ x.to('cuda') | CPU โ GPU |
| ํ ์๋ฅผ CPU๋ก ์ด๋ | x.cpu() ๋๋ x.to('cpu') | GPU โ CPU |
| ์ฅ์น ํ์ธ | x.device | ํ ์์ ํ์ฌ ์ฅ์น ์ ๋ณด ์ถ๋ ฅ |
| ์ฅ์น ์ผ์น ํ์ | x + y ์ฐ์ฐ ์ | x์ y๋ ๋์ผ device์ ์์ด์ผ ํจ |
| ๋ชจ๋ธ์ GPU๋ก ์ด๋ | linear.cuda() | CPU โ GPU |
| ๋ชจ๋ธ์ CPU๋ก ์ด๋ | linear.cpu() | CPU โ GPU |
| ์ฅ์น ํ์ธ | linear.device โ | ๋ชจ๋ธ์ ํ์ฌ ์ฅ์น๋ฅผ ์ถ๋ ฅํ๋ ๊ธฐ๋ฅ โ |
import torch
data = [
[1, 2],
[3, 4]
]
# CPU ์์ Tensor ์์ฑ
x = torch.tensor(data)
print("์ด๊ธฐ ์ํ:", x.is_cuda) # False
# GPU๋ก ์ด๋
if torch.cuda.is_available():
x = x.cuda() # copy์
# x = x.to('cuda')
print("GPU ์ด๋ ํ:", x.is_cuda) # True
# ๋ค์ CPU๋ก ์ด๋
x = x.cpu()
# x = x.to('cpu')
print("๋ค์ CPU ์ด๋ ํ:", x.is_cuda) # False
# GPU ์ฅ์น์ ํ
์
a = torch.tensor([
[1, 1],
[2, 2]
]).cuda()
# CPU ์ฅ์น์ ํ
์
b = torch.tensor([
[5, 6],
[7, 8]
])
# print(torch.matmul(a, b)) # ์ค๋ฅ ๋ฐ์
# RuntimeError: Expected all tensors to be on the same device, but found at least two devices, ~~
print(torch.matmul(a.cpu(), b))
cpu(), cuda()๋ ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๋ฏ๋ก ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ ์
import torch
x = torch.randn(1000, 1000, device="cuda:0") # VRAM์ ์์ฑ
y = x.cpu() # RAM์ ๋ณต์ฌ๋ณธ ์์ฑ
z = y.cuda() # VRAM์ ๋ ๋ค๋ฅธ ๋ณต์ฌ๋ณธ ์์ฑ
# Tip 1: ๋ณ์ ์ด๋ฆ์ด ๊ฐ๋๋ก ์ค์ ํ๋ฉด ๊ฐ๋น์ง ์ปฌ๋ ์
์ผ๋ก ์ ๋ฆฌ
# Tip 2: ์ฌ์ฉํ์ง ์๋ ํ
์๋ del๋ก ์ ๋ฆฌํ๊ธฐ
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.tensor([[1, 2], [3, 4]], device=device)
print(x.device) # cuda:0 ๋๋ cpu
| ํญ๋ชฉ | CPU ๋ฉ๋ชจ๋ฆฌ (RAM) | GPU ๋ฉ๋ชจ๋ฆฌ (VRAM) |
|---|---|---|
| ์์น | ์์คํ ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ | ๊ทธ๋ํฝ์นด๋ ์ ์ฉ ๋ฉ๋ชจ๋ฆฌ |
| ์ญํ | ์ผ๋ฐ ํ๋ก๊ทธ๋จ ์คํ | ๊ทธ๋ํฝ ์ฒ๋ฆฌ, ๋ฅ๋ฌ๋ ์ฐ์ฐ |
| ์์ | ์์ , ๋ธ๋ผ์ฐ์ ๋ฑ | Tensor, ๋ชจ๋ธ ํ๋ผ๋ฏธํฐ ๋ฑ |
import psutil
# CPU ์ฌ์ฉ๋ฅ (%)
print(f"CPU ์ฌ์ฉ๋ฅ : {psutil.cpu_percent(interval=1)}%")
# ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ํํฉ
mem = psutil.virtual_memory()
print(f"์ฌ์ฉ ์ค ๋ฉ๋ชจ๋ฆฌ: {mem.used / 1024**3:.2f} GB / ์ ์ฒด {mem.total / 1024**3:.2f} GB")
import torch
# ์ฌ์ฉ ๊ฐ๋ฅํ GPU ์
print(torch.cuda.device_count())
# ํ์ฌ ํ์ฑํ๋ GPU ID
print(torch.cuda.current_device())
# GPU ์ด๋ฆ
print(torch.cuda.get_device_name(0))
# ์ค์ ๋ก ํ์ฌ ํ
์๋ค์ด ์ฌ์ฉํ๊ณ ์๋ ๋ฉ๋ชจ๋ฆฌ ํฌ๊ธฐ
print(f"Allocated: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
# PyTorch๊ฐ ๋ฏธ๋ฆฌ ํ๋ณดํด๋ ์ด GPU ๋ฉ๋ชจ๋ฆฌ ํฌ๊ธฐ (์บ์ ํฌํจ)
print(f"Reserved : {torch.cuda.memory_reserved() / 1024**2:.2f} MB")
or
# !pip install gputil
import GPUtil
gpus = GPUtil.getGPUs()
for gpu in gpus:
print(f"GPU: {gpu.name}")
print(f"์ฌ์ฉ๋ฅ : {gpu.load * 100:.1f}%")
print(f"๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋: {gpu.memoryUsed}MB / {gpu.memoryTotal}MB")