-
Notifications
You must be signed in to change notification settings - Fork 1
/
GlobalSummaryWriter.py
54 lines (45 loc) · 1.85 KB
/
GlobalSummaryWriter.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
42
43
44
45
46
47
48
49
50
51
52
53
54
from tensorboardX import SummaryWriter
from subprocess import check_output
from psutil import cpu_percent, virtual_memory, swap_memory
class GlobalSummaryWriter(SummaryWriter):
def __get_gpu_memory_usage(self):
result = check_output(
[
'nvidia-smi', '--query-gpu=memory.used',
'--format=csv,nounits,noheader'
], encoding='utf-8')
# Convert lines into a dictionary
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[0]
def __get_gpu_utilization(self):
"""Get the current gpu usage.
Returns
-------
usage: dict
Keys are device ids as integers.
Values are memory usage as integers in MB.
"""
result = check_output(
[
'nvidia-smi', '--query-gpu=utilization.gpu',
'--format=csv,nounits,noheader'
], encoding='utf-8')
# Convert lines into a dictionary
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[0]
def __get_cpu_utilization(self):
return cpu_percent()
def __get_memory_utilization_percentage(self):
return virtual_memory().percent
def __get_swap_memory_utilization_percentage(self):
return swap_memory().percent
def log_system_metrics(self, global_step=None):
self.add_scalar('System/GPUMemoryUtilization', self.__get_gpu_memory_usage(), global_step=global_step)
self.add_scalar('System/GPUUtilization', self.__get_gpu_utilization(), global_step=global_step)
self.add_scalar('System/CPUUtilization', self.__get_cpu_utilization(), global_step=global_step)
self.add_scalar('System/MemoryUsagePercentage', self.__get_memory_utilization_percentage(),
global_step=global_step)
self.add_scalar('System/SwapMemoryUsagePercentage', self.__get_swap_memory_utilization_percentage(),
global_step=global_step)