import torch
# Returns a bool indicating if CUDA is currently available.
torch.cuda.is_available()
# True
# Returns the index of a currently selected device.
torch.cuda.current_device()
# 0
# Returns the number of GPUs available.
torch.cuda.device_count()
# 1
# Gets the name of a device.
torch.cuda.get_device_name(0)
# 'GeForce GTX 1060'
# Context-manager that changes the selected device.
# device (torch.device or int) – device index to select.
torch.cuda.device(0)
import torch
# Default CUDA device
cuda = torch.device('cuda')
# allocates a tensor on default GPU
a = torch.tensor([1., 2.], device=cuda)
# transfers a tensor from 'C'PU to 'G'PU
b = torch.tensor([1., 2.]).cuda()
# Same with .cuda()
b2 = torch.tensor([1., 2.]).to(device=cuda)
# Releases all unoccupied cached memory currently held by
# the caching allocator so that those can be used in other
# GPU application and visible in nvidia-smi
torch.cuda.empty_cache()
참고:https://yonghyuc.wordpress.com/2019/08/06/pytorch-cuda-gpu-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0/