상단에서 runtime -> change runtime type 클릭

사용할수있는 GPU를 선택하고 save 한다.

!nvidia-smi명령어를 실행하면 사용하고 있는 GPU상태를 볼 수있다.

# Set up device agnostic code
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
device
# 'mps' 출력
# Count number of devices
torch.mps.device_count()
# 1 출력
device = "mps" if torch.backends.mps.is_available() else "cpu"
# Create a tensor(default on the CPU)
tensor = torch.tensor([1,2,3], device="cpu")
# tensor not on GPU
print(tensor, tensor.device)

미리 설정해둔 device 를 가져온다.
# Move tensor to MPS (가능하면)
tensor_on_mps = tensor.to(device)
tensor_on_mps

NumPy는 CPU에서만 작동하기 때문에 텐서를 NumPy로 변환하는 것이 좋다.
만일 CPU가 아닌 환경에서 컴퓨팅하려 한다면 다음과 같은 에러가 발생한다.
# If tensor is on GPU or MPS, can't transform it to Numpy
tensor_on_mps.numpy()

# To fix th eGPU or MPS tensor with NumPy issue, we can first set it to the CPU
tensor_back_on_cpu = tensor_on_mps.cpu().numpy()
tensor_back_on_cpu

짱이다!!!!!!!