device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
torch.tensor([1,2], device=device)
torch.tensor([1,2]).to(device)
torch.tensor([1,2]).cuda()
Argument에 따른 방식
import argparse
import torch
parser = argparse.ArgumentPasrser()
parser.add_argument('--cpu', action='store_true', help='run in cpu')
args = parser.parse_args()
if args.cpu:
device = torch.device('cpu')
else:
device = torch.device('cuda')
x = torch.tensor([1.,2.]).to(device)
특정 GPU
- shell 에서 환경 변수로 설정
>> export CUDA_VISIBLE_DEVICES = 1
>> python main.py
- in code
device = torch.device('cuda:1')
with torch.cuda.device(0):
# GPU 0에 할당
X = torch.tensor([1,2]).to("cuda")
# GPU 1에 할당하도록 명시
X = torch.tensor([1,2]).to(device)
https://y-rok.github.io/pytorch/2020/10/03/pytorch-gpu.html
'DL' 카테고리의 다른 글
Data Augmentation of Computer Vision (0) | 2023.12.06 |
---|---|
model.train() / model.eval() / torch.no_grad() / torch.Tensor.require_grads (0) | 2023.12.04 |
[VIT] Vision Transformer (2021) (0) | 2023.07.27 |
U-Net 실습4 - 모델 테스트 (0) | 2023.07.07 |
U-Net 실습3 - 모델 학습 및 Tensorboard (0) | 2023.07.06 |