forked from jtchilders/deephyper_pytorch_layers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
38 lines (28 loc) · 950 Bytes
/
utils.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
import subprocess
def get_gpu_memory_map():
"""Get the current gpu usage.
https://discuss.pytorch.org/t/access-gpu-memory-usage-in-pytorch/3192/3
Returns
-------
gpu_memory_map: dict
Keys are device ids as integers.
Values are memory usage as integers in MB.
"""
result = subprocess.check_output(
["nvidia-smi", "--query-gpu=memory.used", "--format=csv,nounits,noheader"]
)
# Python 3 compatibility:
# https://stackoverflow.com/questions/49595663/find-a-gpu-with-enough-memory
result = result.decode("utf-8")
gpu_memory = [int(x) for x in result.strip().split("\n")]
gpu_memory_map = dict(zip(range(len(gpu_memory)), gpu_memory))
return gpu_memory_map
def get_first_gpu_memory_usage():
"""Get the current gpu usage.
Returns
-------
usage: int
Memory usage as integer in MB.
"""
mem = get_gpu_memory_map()
return int(mem[0])