-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathverify_aggregated_params.py
More file actions
381 lines (341 loc) · 12.9 KB
/
Copy pathverify_aggregated_params.py
File metadata and controls
381 lines (341 loc) · 12.9 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import argparse
from collections import OrderedDict, Counter
from pass_bench import analysis_util
from pass_bench import samples_statistics
from pass_bench.positive_tolerance_interpretation import (
PositiveToleranceInterpretation,
)
from pass_bench.samples_statistics import (
get_errno_from_error_type,
)
def determine_tolerances(
samples: list,
positive_tolerance_interpretation: PositiveToleranceInterpretation,
) -> range:
"""Determine tolerance range based on observed errno categories."""
max_errno = positive_tolerance_interpretation.num_errno_enum_values()
return range(-10, max_errno + 2)
def extract_statistics_at_tolerance(
samples: list,
tolerance: int,
positive_tolerance_interpretation: PositiveToleranceInterpretation,
) -> dict:
"""Extract statistics for a given tolerance level."""
sample_data = [
(
idx,
sample,
sample.get("performance", {}).get("speedup", {}).get("e2e"),
*analysis_util.check_sample_correctness(sample, tolerance),
)
for idx, sample in enumerate(samples)
]
correct_samples = [
(idx, speedup) for idx, _, speedup, is_correct, _ in sample_data if is_correct
]
correct_count = len(correct_samples)
correct_speedups = [
speedup for _, speedup in correct_samples if speedup is not None
]
slowdown_speedups = [speedup for speedup in correct_speedups if speedup < 1]
correct_negative_speedup_count = len(slowdown_speedups)
errno2count = dict(
Counter(
get_errno_from_error_type(fail_type, positive_tolerance_interpretation)
for _, _, _, _, fail_type in sample_data
if fail_type is not None
)
)
return {
"correct_count": correct_count,
"correct_speedups": correct_speedups,
"slowdown_speedups": slowdown_speedups,
"correct_negative_speedup_count": correct_negative_speedup_count,
"errno2count": errno2count,
}
def _freeze_statistics_at_tolerance(
stats: dict,
total_samples: int,
frozen_stats: dict,
first_errno_tolerance: int,
) -> dict:
"""Freeze statistics at first_errno_tolerance and calculate pi."""
pi = samples_statistics.calculate_pi(
stats["errno2count"], total_samples, stats["correct_speedups"]
)
frozen_stats.update(
{
"correct_count": stats["correct_count"],
"correct_negative_speedup_count": stats["correct_negative_speedup_count"],
"correct_speedups": stats["correct_speedups"],
"slowdown_speedups": stats["slowdown_speedups"],
"errno2count": stats["errno2count"].copy(),
}
)
return pi
def select_statistics_for_calculation(
tolerance: int, current_stats: dict, frozen_stats: dict
) -> dict:
"""Select statistics to use based on tolerance level."""
if tolerance < 1:
return {
"correct_count": current_stats["correct_count"],
"correct_speedups": current_stats["correct_speedups"],
"slowdown_speedups": current_stats["slowdown_speedups"],
"errno2count": current_stats["errno2count"],
}
else:
return {
"correct_count": frozen_stats["correct_count"],
"correct_speedups": frozen_stats["correct_speedups"],
"slowdown_speedups": frozen_stats["slowdown_speedups"],
"errno2count": frozen_stats["errno2count"],
}
def calculate_es_constructor_params_for_tolerance(
tolerance: int,
total_samples: int,
stats: dict,
pi: dict,
negative_speedup_penalty: float,
fpdb: float,
positive_tolerance_interpretation: PositiveToleranceInterpretation,
) -> dict:
"""Calculate ES(t) constructor parameters (alpha, beta, gamma, lambda, eta) and final scores for a tolerance level."""
aggregated_params = samples_statistics.calculate_es_components_values(
total_samples=total_samples,
correct_speedups=stats["correct_speedups"],
errno2count=stats["errno2count"],
tolerance=tolerance,
negative_speedup_penalty=negative_speedup_penalty,
b=fpdb,
pi=pi,
positive_tolerance_interpretation=positive_tolerance_interpretation,
)
alpha = aggregated_params["alpha"]
beta = aggregated_params["beta"]
lambda_ = aggregated_params["lambda"]
eta = aggregated_params["eta"]
gamma = aggregated_params["gamma"]
expected_s = samples_statistics.calculate_s_t_from_aggregated(
alpha, beta, lambda_, eta, negative_speedup_penalty, fpdb
)
expected_es = samples_statistics.calculate_es_t_from_aggregated(
alpha, beta, lambda_, eta, gamma, negative_speedup_penalty
)
return {
"alpha": alpha,
"beta": beta,
"lambda": lambda_,
"eta": eta,
"gamma": gamma,
"expected_s": expected_s,
"expected_es": expected_es,
}
def print_tolerance_details(
tolerance: int,
total_samples: int,
stats: dict,
es_constructor_params: dict,
pi: dict,
fpdb: float,
):
"""Print detailed information for a tolerance level."""
print(f"\nTolerance t = {tolerance}:")
print(f" Total samples: {total_samples}")
print(
f" Correct samples: {stats['correct_count']} (lambda = {es_constructor_params['lambda']:.6f})"
)
print(f" Correct speedups collected: {len(stats['correct_speedups'])}")
print(
f" Slowdown cases: {len(stats['slowdown_speedups'])} (eta = {es_constructor_params['eta']:.6f})"
)
print(
f" alpha (geometric mean of correct speedups): {es_constructor_params['alpha']:.6f}"
)
if stats["correct_speedups"]:
print(
f" - Correct speedups: {stats['correct_speedups'][:10]}{'...' if len(stats['correct_speedups']) > 10 else ''}"
)
print(
f" beta (geometric mean of slowdown speedups): {es_constructor_params['beta']:.6f}"
)
if stats["slowdown_speedups"]:
print(
f" - Slowdown speedups: {stats['slowdown_speedups'][:10]}{'...' if len(stats['slowdown_speedups']) > 10 else ''}"
)
print(f" gamma (average error penalty): {es_constructor_params['gamma']:.6f}")
if tolerance >= 1:
errnos = sorted(pi.keys())
pi_list = [pi[errno] for errno in errnos]
indicator = [1 if tolerance < 1 else 0, 1 if tolerance < 3 else 0]
pi_indicator_sum = sum(
pi.get(errno, 0.0) * indicator[min(i, len(indicator) - 1)]
for i, errno in enumerate(errnos)
)
print(f" - pi (errno -> proportion): {dict(sorted(pi.items()))}")
print(f" - pi (as list): {pi_list}")
print(f" - indicator: {indicator}")
print(
f" - gamma = fpdb^(sum(pi[i] * indicator[i])) = {fpdb}^{pi_indicator_sum:.6f} = {es_constructor_params['gamma']:.6f}"
)
print(f" Expected S(t) from aggregated: {es_constructor_params['expected_s']:.6f}")
print(
f" Expected ES(t) from aggregated: {es_constructor_params['expected_es']:.6f}"
)
class ToleranceReportBuilder:
"""Stateful helper for building tolerance reports in order."""
def __init__(
self,
samples: list,
total_samples: int,
negative_speedup_penalty: float,
fpdb: float,
positive_tolerance_interpretation: PositiveToleranceInterpretation,
):
self.samples = samples
self.total_samples = total_samples
self.negative_speedup_penalty = negative_speedup_penalty
self.fpdb = fpdb
self.pi: dict[int, float] = {}
self.frozen_stats: dict = {
"correct_count": 0,
"correct_speedups": [],
"slowdown_speedups": [],
"errno2count": {},
}
self.positive_tolerance_interpretation = positive_tolerance_interpretation
def build_report(self, tolerance: int) -> dict:
current_stats = extract_statistics_at_tolerance(
self.samples, tolerance, self.positive_tolerance_interpretation
)
if tolerance == 1:
self.pi = _freeze_statistics_at_tolerance(
current_stats,
self.total_samples,
self.frozen_stats,
first_errno_tolerance=1,
)
if self.total_samples == 0:
return self._empty_report(tolerance)
stats_for_calc = select_statistics_for_calculation(
tolerance, current_stats, self.frozen_stats
)
# For tolerance < 1, pass None to let calculate_es_components_values recalculate pi
# For tolerance >= 1, use frozen pi from t=1
pi_for_calc = None if tolerance < 1 else self.pi
es_constructor_params = calculate_es_constructor_params_for_tolerance(
tolerance,
self.total_samples,
stats_for_calc,
pi_for_calc,
self.negative_speedup_penalty,
self.fpdb,
self.positive_tolerance_interpretation,
)
# Use calculated pi from es_constructor_params for display and return
calculated_pi = es_constructor_params.get("pi", self.pi)
print_tolerance_details(
tolerance,
self.total_samples,
stats_for_calc,
es_constructor_params,
calculated_pi,
self.fpdb,
)
return {
**es_constructor_params,
"pi": calculated_pi,
"correct_count": stats_for_calc["correct_count"],
"total_samples": self.total_samples,
"correct_speedups_count": len(stats_for_calc["correct_speedups"]),
"slowdown_count": len(stats_for_calc["slowdown_speedups"]),
}
def _empty_report(self, tolerance: int) -> dict:
print(f"\nTolerance t = {tolerance}: No samples to analyze")
return {
"alpha": 1.0,
"beta": 1.0,
"gamma": self.fpdb,
"lambda": 0.0,
"eta": 0.0,
"pi": self.pi,
"expected_s": self.fpdb,
"expected_es": self.fpdb,
"correct_count": 0,
"total_samples": 0,
"correct_speedups_count": 0,
"slowdown_count": 0,
}
def verify_es_constructor_params_across_tolerances(
samples: list,
folder_name: str,
positive_tolerance_interpretation: PositiveToleranceInterpretation,
negative_speedup_penalty: float = 0,
fpdb: float = 0.1,
) -> dict:
"""
Verify and print ES constructor parameters (alpha, beta, gamma, lambda, eta, pi) for each
tolerance level independently. This logic mirrors `calculate_s_scores` but is split
out for focused validation of aggregated calculations.
Returns:
Dictionary mapping tolerance -> dict of ES constructor parameters and calculated scores
"""
total_samples = len(samples)
print(f"\n{'=' * 80}")
print(f"Verifying Aggregated Parameters for '{folder_name}'")
print(f"{'=' * 80}")
tolerances = determine_tolerances(samples, positive_tolerance_interpretation)
builder = ToleranceReportBuilder(
samples=samples,
total_samples=total_samples,
negative_speedup_penalty=negative_speedup_penalty,
fpdb=fpdb,
positive_tolerance_interpretation=positive_tolerance_interpretation,
)
results = OrderedDict(
(tolerance, builder.build_report(tolerance)) for tolerance in tolerances
)
print(f"\n{'=' * 80}")
print("Aggregated Parameter Verification Complete")
print(f"{'=' * 80}\n")
return results
def main():
"""Main execution function for verifying aggregated parameters."""
parser = argparse.ArgumentParser(
description="Verify aggregated parameters (alpha, beta, gamma, lambda, eta, pi) calculation.",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--benchmark-path",
type=str,
required=True,
help="Path to the benchmark log file or directory containing benchmark JSON files or sub-folders.",
)
parser.add_argument(
"--negative-speedup-penalty",
type=float,
default=0.0,
help="Penalty power (p) for negative speedup. Formula: speedup**(p+1). Default: 0.0.",
)
parser.add_argument(
"--fpdb",
type=float,
default=0.1,
help="Base penalty for severe errors (e.g., crashes, correctness failures).",
)
args = parser.parse_args()
# Scan folders to get data
all_results = analysis_util.scan_all_folders(args.benchmark_path)
if not all_results:
print("No valid data found. Exiting.")
return
# Calculate and print aggregated parameters for each curve
for folder_name, samples in all_results.items():
_ = verify_es_constructor_params_across_tolerances(
samples,
folder_name,
negative_speedup_penalty=args.negative_speedup_penalty,
fpdb=args.fpdb,
) # noqa: F841
if __name__ == "__main__":
main()