/
power_draw_callback.py
46 lines (31 loc) · 1.21 KB
/
power_draw_callback.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
from tensorflow.keras.callbacks import Callback
class PowerDrawCallback(Callback):
'''A callback for recording GPU power draw (watts) on epoch begin and end.
Example use:
power_draw = PowerDrawCallback()
model.fit(...callbacks=[power_draw]...)
print(power_draw.logs)
'''
def __init__(self):
super(PowerDrawCallback, self).__init__()
import os
import time
self.os = os
self.time = time.time
self.command = "nvidia-smi -i 0 -q | grep -i 'power draw' | tr -s ' ' | cut -d ' ' -f5"
def on_train_begin(self, logs={}):
self.log = {}
self.log['epoch_begin'] = []
self.log['epoch_end'] = []
self.log['seconds'] = []
def on_epoch_begin(self, batch, logs=None):
self.epoch_start_time = self.time()
temp = self.os.popen(self.command).read()
temp = float(temp.strip())
self.log['epoch_begin'].append(temp)
def on_epoch_end(self, batch, logs=None):
temp = self.os.popen(self.command).read()
temp = float(temp.strip())
self.log['epoch_end'].append(temp)
seconds = round(self.time() - self.epoch_start_time, 3)
self.log['seconds'].append(seconds)