-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcheck_regressions.py
224 lines (193 loc) · 6.42 KB
/
check_regressions.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import json
import argparse
from pathlib import Path
from enum import Enum
import io
from typing import Dict
class StageOrder(Enum):
setup = 0
import_model = 1
preprocessing = 2
compilation = 3
construct_inputs = 4
native_inference = 5
compiled_inference = 6
postprocessing = 7
results_summary = 8
Numerics = 9
PASS = 10
def _get_argparse():
msg = "A script for loading two status dictionary jsons checking for regressions."
parser = argparse.ArgumentParser(
prog="check_regressions.py", description=msg, epilog=""
)
parser.add_argument(
"--old",
required=True,
help="specify path to old status dict json",
)
parser.add_argument(
"--new",
required=True,
help="specify path to new status dict json",
)
parser.add_argument(
"-f",
"--report-file",
default="regression_report.md",
help="specify filepath for regression report",
)
parser.add_argument(
"-r",
"--perf_tol_regression",
default="0.05",
help="specify a minimum percent difference required to report a perf regression",
)
parser.add_argument(
"-p",
"--perf_tol_progression",
default="0.05",
help="specify a minimum percent difference required to report a perf progression",
)
return parser
def save_dict(status_dict: Dict[str, Dict], status_dict_json: str):
with io.open(status_dict_json, "w", encoding="utf8") as outfile:
dict_str = json.dumps(
status_dict,
indent=4,
sort_keys=True,
separators=(",", ": "),
ensure_ascii=False,
)
outfile.write(dict_str)
def get_table_header(ordered_items):
s0 = "|model name|"
s1 = "|---|"
for item in ordered_items:
s0 += f"{item}|"
s1 += "---|"
s = s0 + "\n" + s1 + "\n"
return s
def get_table_rows(some_dict: Dict, ordered_items) -> str:
s = ""
for key, d in some_dict.items():
s += f"|{key}|"
for item in ordered_items:
if item not in d.keys():
s += " |"
continue
s += f"{d[item]}|"
s += "\n"
return s
def get_comp_string(prog_regr_dict: Dict, kind_of_change: str = "Regression") -> str:
s = "## "
num_prog = len(prog_regr_dict.keys())
if num_prog == 0:
return s + f"No {kind_of_change}s Found\n\n"
elif num_prog == 1:
s += f"One {kind_of_change} Found:\n\n"
else:
s += f"{num_prog} {kind_of_change}s Found:\n\n"
ordered_items = ["old_status", "new_status"]
s += get_table_header(ordered_items)
s += get_table_rows(prog_regr_dict, ordered_items)
return s + "\n"
def get_perf_string(perf_tol: Dict, perf_comp: Dict):
if len(perf_comp.keys()) == 0:
return ""
s = "## Performance Comparison\n\n"
s += f"regression tolerance: {round(100*perf_tol['perf_tol_regression'],1)}%\n\n"
s += f"progression tolerance: {round(100*perf_tol['perf_tol_progression'],1)}%\n\n"
ordered_items = [
"exit_status",
"analysis",
"old_time_ms",
"new_time_ms",
"change_ms",
"percent_change",
]
s += get_table_header(ordered_items)
s += get_table_rows(perf_comp, ordered_items)
return s + "\n"
def save_comp_report(comp_dict: Dict, filepath):
p = Path(filepath)
p.parent.mkdir(parents=True, exist_ok=True)
perf_string = get_perf_string(comp_dict["perf_tolerances"], comp_dict["perf_comp"])
regr_string = get_comp_string(comp_dict["regressions"], "Regression")
prog_string = get_comp_string(comp_dict["progressions"], "Progression")
s = "# Test Run Comparison Report\n\n"
s += perf_string
s += regr_string
s += prog_string
with open(filepath, "w") as file:
file.write(s)
def check_regressions(
new: Dict, old: Dict, tol_regr: float = 0.05, tol_prog: float = 0.05
) -> Dict:
regressions = dict()
progressions = dict()
perf_comp = dict()
for key, d in new.items():
if key not in old.keys():
continue
old_d = old[key]
new_status = d["exit_status"]
old_status = old_d["exit_status"]
# status comparison:
new_order = StageOrder[new_status].value
old_order = StageOrder[old_status].value
if new_order < old_order:
regressions[key] = {"old_status": old_status, "new_status": new_status}
if new_order > old_order:
progressions[key] = {"old_status": old_status, "new_status": new_status}
# perf comparison
new_time_ms = d["time_ms"]
old_time_ms = old_d["time_ms"]
if isinstance(new_time_ms, float) and isinstance(old_time_ms, float):
delta = new_time_ms - old_time_ms
if old_time_ms <= 0.0:
continue
delta_percent = delta / old_time_ms
perf_comp[key] = {
"exit_status": new_status,
"old_time_ms": round(old_time_ms, 4),
"new_time_ms": round(new_time_ms, 4),
"change_ms": round(delta, 4),
"percent_change": f"{round(100*delta_percent,2)}%",
}
if delta_percent > abs(tol_regr):
perf_comp[key]["analysis"] = "regression"
elif delta_percent < -abs(tol_prog):
perf_comp[key]["analysis"] = "progression"
else:
perf_comp[key]["analysis"] = "within tol"
perf_tol = {
"perf_tol_regression": abs(tol_regr),
"perf_tol_progression": abs(tol_prog),
}
combined_dict = {
"perf_tolerances": perf_tol,
"regressions": regressions,
"progressions": progressions,
"perf_comp": perf_comp,
}
return combined_dict
def main(args):
new = load(args.new)
old = load(args.old)
combined_dict = check_regressions(
new, old, float(args.perf_tol_regression), float(args.perf_tol_progression)
)
save_comp_report(combined_dict, args.report_file)
report_path = Path(args.report_file)
parent_path = report_path.parent
file_name = report_path.stem
json_path = parent_path / f"{file_name}.json"
save_dict(combined_dict, json_path)
def load(pathname):
with open(pathname) as contents:
loaded_dict = json.load(contents)
return loaded_dict
if __name__ == "__main__":
parser = _get_argparse()
main(parser.parse_args())