-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter_time.py
116 lines (106 loc) · 3.98 KB
/
filter_time.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
This script is used to filter the metric valus, such as execution time, memory usage etc., from the log file.
"""
import argparse
import os
import re
from numpy import mean
import time
def work_multi_models(input_file, output_file):
content = ''
with open(input_file, 'r') as fin:
content = fin.read()
content_s = [_ for _ in content.split(
"@Yueming Hao origin") if _.strip()][1:]
results = {}
for amodel in content_s:
model_name = amodel.strip().split()[0].strip()
# when run tflops and profile for each model, there will be two cpu time and gpu time.
if amodel.find("Saved TensorBoard Profiler traces") != -1:
amodel = re.split(r"Running .* method from", amodel)[-1]
measurements = reg_filter(amodel)
if not measurements:
continue
mean_results = {}
for k in measurements:
if k == 'guard_checks':
mean_results[k] = str(measurements[k])
else:
mean_results[k] = mean(measurements[k])
results[model_name] = mean_results
first_model = list(results.keys())[0]
metrics_order = ['cpu', 'gpu', 'tflops', 'cpu_mem', 'gpu_mem', 'batch_size', 'guard_checks']
table_head = 'model'
for metric in metrics_order:
if metric in results[first_model]:
table_head += ', %s' % metric
table_head += '\n'
with open(output_file, 'w') as fout:
print("writing to file %s" % output_file)
fout.write(table_head)
for model in results:
fout.write("%s, " % model)
for metric in metrics_order:
if metric in results[model]:
fout.write(f"{results[model][metric]}" + ', ')
fout.write('\n')
def guard_checks(raw_str):
from collections import Counter
reg = re.compile(r"guard_types': (.*),")
all_results = reg.findall(raw_str)
if all_results:
return Counter(all_results)
return None
def reg_filter(raw_str):
measurements = {}
def check_bs(tmp_batch_str="", tmp_total_str=""):
reg_cpu = re.compile(r"CPU %sWall Time%s:(.*) milliseconds" %
(tmp_total_str, tmp_batch_str))
return reg_cpu.findall(raw_str)
batch_str = " per batch"
total_str = "Total "
if not check_bs(tmp_total_str=total_str):
if not check_bs(tmp_batch_str=batch_str):
print("error when processing ", raw_str)
return None
total_str = ""
else:
batch_str = ""
reg_cpu = re.compile(r"CPU %sWall Time%s:(.*) milliseconds" %
(total_str, batch_str))
reg_gpu = re.compile(r"GPU Time%s:(.*) milliseconds" % batch_str)
reg_flops = re.compile(r"FLOPS:(.*) TFLOPs per second")
reg_cpu_mem = re.compile(r"CPU Peak Memory:(.*) GB")
reg_gpu_mem = re.compile(r"GPU Peak Memory:(.*) GB")
reg_batch_size = re.compile(r"with input batch size (\d+)")
regs = {
'cpu': reg_cpu,
'gpu': reg_gpu,
'tflops': reg_flops,
'cpu_mem': reg_cpu_mem,
'gpu_mem': reg_gpu_mem,
'batch_size': reg_batch_size
}
for k in regs:
result = regs[k].findall(raw_str)
if result:
tmp = [float(_.strip()) for _ in result]
measurements[k] = tmp
reg2 = {
'guard_checks': lambda x: guard_checks(x)
}
for k in reg2:
result = reg2[k](raw_str)
if result:
measurements[k] = result
return measurements
if __name__ == '__main__':
# get current time and date like 202304141010
current_time = time.strftime("%Y%m%d%H%M", time.localtime())
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str,
default='/home/yhao/d/tmp/run_all_speedup_aug4.log')
parser.add_argument('-o', '--output', type=str,
default='filter_time_%s.csv' % current_time)
args = parser.parse_args()
work_multi_models(args.input, args.output)