-
Notifications
You must be signed in to change notification settings - Fork 3
/
device.py
41 lines (33 loc) · 1.07 KB
/
device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from io import BytesIO
import pandas as pd
import torch
import subprocess
import sys
USE_GPU = torch.cuda.is_available()
def select_device():
"""Selects GPU with the most available memory or CPU if cuda is not enabled."""
if USE_GPU:
try:
gpus = subprocess.check_output(['nvidia-smi', '--format=csv', '--query-gpu=memory.used,memory.free'])
stream = BytesIO(gpus)
dataframe = pd.read_csv(stream, names=['memory.used', 'memory.free'], skiprows=1)
dataframe['memory.free'] = dataframe['memory.free'].map(lambda x: float(x.rstrip(' [MiB]')))
device_idx = dataframe['memory.free'].idxmax()
device = torch.device(f'cuda:{device_idx}')
except Exception:
device_idx = -1
device = torch.device('cpu')
else:
device_idx = -1
device = torch.device('cpu')
return USE_GPU, device, device_idx
def with_cpu(x):
if USE_GPU:
return x.cpu()
else:
return x
def with_gpu(x):
if USE_GPU:
return x.cuda()
else:
return x