-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarker.py
644 lines (560 loc) · 21.9 KB
/
benchmarker.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import pandas as pd
import numpy as np
import onnxruntime as ort
import time
import math
import datetime
import os
from PIL import Image
from onnxruntime.capi.onnxruntime_pybind11_state import ExecutionMode
# Trial count
TRIAL_COUNT = 101
# region Input vectors
# Images
image_1080_1920 = np.asarray(Image.open("images/myna_fhd.png")).astype(np.double)[:, :, 0:3]
image_1440_2560 = np.asarray(Image.open("images/warbler_qhd.png")).astype(np.double)[:, :, 0:3]
image_1800_3200 = np.asarray(Image.open("images/kingfisher_qhdp.png")).astype(np.double)[:, :, 0:3]
image_2160_3840 = np.asarray(Image.open("images/nightjar_4kuhd.png")).astype(np.double)[:, :, 0:3]
image_2880_5120 = np.asarray(Image.open("images/turaco_5k.png")).astype(np.double)[:, :, 0:3]
# Adaptation
adaptation_original_illuminant = np.asarray([[
23.289217, 21.678232, 20.067247, 21.226551, 22.385855, 27.010095, 31.634336, 33.395136, 35.155936, 34.238822, 33.321707, 37.926632, 42.531557, 48.077153, 53.622750, 56.375462, 59.128174, 61.194617, 63.261060, 65.987549, 68.714039, 70.422382, 72.130726, 75.794712, 79.458699, 81.187633, 82.916567, 84.553845, 86.191122, 89.841377, 93.491632, 94.697762, 95.903892, 97.802676, 99.701460, 99.850730, 100.000000, 99.818633, 99.637266, 101.536536, 103.435806, 105.290685, 107.145563, 112.925216, 118.704869, 122.396039, 126.087210, 128.018091, 129.948972, 129.644313, 129.339655, 135.804835, 142.270015, 142.339123, 142.408231, 147.687385, 152.966540, 159.272415, 165.578290, 164.689066, 163.799843, 152.122902, 140.445960, 146.575115, 152.704269, 150.113597, 147.522925, 134.679552, 121.836180, 128.614117, 135.392055, 139.710190, 144.028324, 132.669019, 121.309714, 106.190606, 91.071498, 110.771973, 130.472449, 126.245983, 122.019517, 0, 0, 0, 0, 0, 0, 0, 0
]])
adaptation_target_illuminant = np.asarray([[
49.428921, 51.759850, 54.090779, 68.025953, 81.961127, 86.313504, 90.665880, 91.655559, 92.645238, 89.319349, 85.993460, 95.060474, 104.127488, 110.199807, 116.272126, 116.704928, 117.137730, 115.706664, 114.275597, 114.837711, 115.399824, 111.895876, 108.391928, 108.703479, 109.015030, 108.268521, 107.522013, 106.057800, 104.593588, 106.069007, 107.544427, 105.928756, 104.313086, 104.157277, 104.001468, 102.000734, 100.000000, 98.184891, 96.369783, 96.119049, 95.868315, 92.348821, 88.829328, 89.531304, 90.233281, 90.059665, 89.886050, 88.959839, 88.033629, 85.844284, 83.654939, 83.904045, 84.153151, 82.327113, 80.501074, 80.631971, 80.762868, 81.835533, 82.908199, 80.915868, 78.923538, 74.590608, 70.257678, 71.238575, 72.219472, 73.564167, 74.908862, 68.486759, 62.064656, 66.225988, 70.387321, 73.001133, 75.614945, 69.824749, 64.034552, 55.396835, 46.759117, 57.025699, 67.292280, 65.562176, 63.832072, 0, 0, 0, 0, 0, 0, 0, 0,
]])
# endregion
# region TestCase data structure
class TestCase:
def __init__(self,
shape: list[int],
gpu: bool,
cpu: bool,
inputs: dict[str, np.ndarray]):
self.shape = shape
self.gpu = gpu
self.cpu = cpu
self.inputs = inputs
# endregion
# region Test case database
cases = {
("adaptation", "programs/adaptation.py"): [
TestCase(
shape=[1080, 1920],
gpu=True,
cpu=True,
inputs={
"original_illuminant": adaptation_original_illuminant,
"target_illuminant": adaptation_target_illuminant,
"image": image_1080_1920
}
),
TestCase(
shape=[1440, 2560],
gpu=True,
cpu=True,
inputs={
"original_illuminant": adaptation_original_illuminant,
"target_illuminant": adaptation_target_illuminant,
"image": image_1440_2560
}
),
TestCase(
shape=[1800, 3200],
gpu=True,
cpu=True,
inputs={
"original_illuminant": adaptation_original_illuminant,
"target_illuminant": adaptation_target_illuminant,
"image": image_1800_3200
}
),
TestCase(
shape=[2160, 3840],
gpu=True,
cpu=True,
inputs={
"original_illuminant": adaptation_original_illuminant,
"target_illuminant": adaptation_target_illuminant,
"image": image_2160_3840
}
),
TestCase(
shape=[2880, 5120],
gpu=True,
cpu=True,
inputs={
"original_illuminant": adaptation_original_illuminant,
"target_illuminant": adaptation_target_illuminant,
"image": image_2880_5120
}
)
],
("rgb2op", "programs/rgb2op.py"): [
TestCase(
shape=[1080, 1920],
gpu=True,
cpu=True,
inputs={
"image": image_1080_1920
}
),
TestCase(
shape=[1440, 2560],
gpu=True,
cpu=True,
inputs={
"image": image_1440_2560
}
),
TestCase(
shape=[1800, 3200],
gpu=True,
cpu=True,
inputs={
"image": image_1800_3200
}
),
TestCase(
shape=[2160, 3840],
gpu=True,
cpu=True,
inputs={
"image": image_2160_3840
}
),
TestCase(
shape=[2880, 5120],
gpu=True,
cpu=True,
inputs={
"image": image_2880_5120
}
)
],
("colorblind", "programs/colorblind.py"): [
TestCase(
shape=[1080, 1920],
gpu=True,
cpu=True,
inputs={
"image": image_1080_1920,
"colorblind_matrix": np.asarray(
[
[0, 0, 0],
[2.02344, 1, 0],
[-2.52581, 0, 1]
]
)
}
),
TestCase(
shape=[1440, 2560],
gpu=True,
cpu=True,
inputs={
"image": image_1440_2560,
"colorblind_matrix": np.asarray(
[
[0, 0, 0],
[2.02344, 1, 0],
[-2.52581, 0, 1]
]
)
}
),
TestCase(
shape=[1800, 3200],
gpu=True,
cpu=True,
inputs={
"image": image_1800_3200,
"colorblind_matrix": np.asarray(
[
[0, 0, 0],
[2.02344, 1, 0],
[-2.52581, 0, 1]
]
)
}
),
TestCase(
shape=[2160, 3840],
gpu=True,
cpu=True,
inputs={
"image": image_2160_3840,
"colorblind_matrix": np.asarray(
[
[0, 0, 0],
[2.02344, 1, 0],
[-2.52581, 0, 1]
]
)
}
),
TestCase(
shape=[2880, 5120],
gpu=True,
cpu=True,
inputs={
"image": image_2880_5120,
"colorblind_matrix": np.asarray(
[
[0, 0, 0],
[2.02344, 1, 0],
[-2.52581, 0, 1]
]
)
}
)
],
("interpolation", "programs/interpolation.py"): [
TestCase(
shape=[1080, 1920],
gpu=True,
cpu=True,
inputs={
"image1": image_1080_1920,
"image2": np.random.rand(1080, 1920, 3)
}
),
TestCase(
shape=[1440, 2560],
gpu=True,
cpu=True,
inputs={
"image1": image_1440_2560,
"image2": np.random.rand(1440, 2560, 3)
}
),
TestCase(
shape=[1800, 3200],
gpu=True,
cpu=True,
inputs={
"image1": image_1800_3200,
"image2": np.random.rand(1800, 3200, 3)
}
),
TestCase(
shape=[2160, 3840],
gpu=True,
cpu=True,
inputs={
"image1": image_2160_3840,
"image2": np.random.rand(2160, 3840, 3)
}
),
TestCase(
shape=[2880, 5120],
gpu=True,
cpu=True,
inputs={
"image1": image_2880_5120,
"image2": np.random.rand(2880, 5120, 3)
}
)
],
("mixing", "programs/mixing.py"): [
TestCase(
shape=[200, 200],
gpu=True,
cpu=True,
inputs={
"scattering1": np.random.rand(200, 200, 89),
"scattering2": np.random.rand(200, 200, 89),
"absorption1": np.random.rand(200, 200, 89),
"absorption2": np.random.rand(200, 200, 89),
"light": np.random.rand(200, 200, 89),
"density1": np.random.rand(200, 200, 1),
"density2": np.random.rand(200, 200, 1)
}
),
TestCase(
shape=[400, 400],
gpu=True,
cpu=True,
inputs={
"scattering1": np.random.rand(400, 400, 89),
"scattering2": np.random.rand(400, 400, 89),
"absorption1": np.random.rand(400, 400, 89),
"absorption2": np.random.rand(400, 400, 89),
"light": np.random.rand(400, 400, 89),
"density1": np.random.rand(400, 400, 1),
"density2": np.random.rand(400, 400, 1)
}
),
TestCase(
shape=[600, 600],
gpu=True,
cpu=True,
inputs={
"scattering1": np.random.rand(600, 600, 89),
"scattering2": np.random.rand(600, 600, 89),
"absorption1": np.random.rand(600, 600, 89),
"absorption2": np.random.rand(600, 600, 89),
"light": np.random.rand(600, 600, 89),
"density1": np.random.rand(600, 600, 1),
"density2": np.random.rand(600, 600, 1)
}
),
],
("lab2hsv", "programs/lab2hsv.py"): [
TestCase(
shape=[1080, 1920],
gpu=True,
cpu=True,
inputs={
"image": image_1080_1920
}
),
TestCase(
shape=[1440, 2560],
gpu=True,
cpu=True,
inputs={
"image": image_1440_2560
}
),
TestCase(
shape=[1800, 3200],
gpu=True,
cpu=True,
inputs={
"image": image_1800_3200
}
),
TestCase(
shape=[2160, 3840],
gpu=True,
cpu=True,
inputs={
"image": image_2160_3840
}
),
]
}
# endregion
# region Path calculation
def get_compiled_path(test_name: str, test_shape: list[int]) -> str:
return "compiled/" + test_name + "-" + "_".join([str(dim) for dim in test_shape]) + ".onnx"
def get_optimized_path(test_name: str, test_shape: list[int]) -> str:
return "optimized/" + test_name + "-" + "_".join([str(dim) for dim in test_shape]) + ".onnx"
def get_compiled_ort_path(test_name: str, test_shape: list[int]) -> str:
return "compiled/" + test_name + "-" + "_".join([str(dim) for dim in test_shape]) + ".ort"
def get_optimized_ort_path(test_name: str, test_shape: list[int]) -> str:
return "optimized/" + test_name + "-" + "_".join([str(dim) for dim in test_shape]) + ".ort"
def get_compile_data_path(time_string: str) -> str:
return "results/compile/" + time_string + ".csv"
def get_optimize_data_path(time_string: str) -> str:
return "results/optimize/" + time_string + ".csv"
def get_run_data_path(time_string: str) -> str:
return "results/run/" + time_string + ".csv"
# endregion
# region Compilation benchmarking function
# Dataframe for compilation times
compile_data = pd.DataFrame(columns=['test', 'y', 'x', 'trial', 'time'])
# Function to benchmark compilation time for a single test case
def benchmark_compile(test_name: str, test_program: str, test_case: TestCase):
output_path = get_compiled_path(test_name, test_case.shape)
for trial in range(1):
# Delete compiled file if it already exists
try:
os.remove(output_path)
except OSError:
pass
# Perform compilation benchmark
start_time = time.time()
os.system(" ".join(["taskset -c 4", "python3", test_program, output_path, str(test_case.shape[0]), str(test_case.shape[1])]))
end_time = time.time()
# Ensure that the compiled file was actually generated before recording time
if os.path.isfile(output_path):
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "compilation trial", trial, "success")
compile_time = end_time - start_time
compile_data.loc[len(compile_data)] = pd.Series({
'test': test_name,
'y': test_case.shape[0],
'x': test_case.shape[1],
'trial': trial,
'time': compile_time
})
else:
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "compilation trial", trial, "FAIL")
break
# endregion
# region Optimization benchmarking function
# Dataframe for optimization times
optimize_data = pd.DataFrame(columns=['test', 'y', 'x', 'trial', 'time'])
# Function to benchmark optimization time for a single test case
def benchmark_optimize(test_name: str, test_case: TestCase):
compiled_path = get_compiled_path(test_name, test_case.shape)
optimized_path = get_optimized_path(test_name, test_case.shape)
# Ensure that the compiled path exists, otherwise abort
if not os.path.isfile(compiled_path):
print(test_name, " by ".join([str(dim) for dim in test_case.shape]),
"optimization benchmarking failed due to missing compiled executable")
return
for trial in range(1):
# Delete optimized file if it already exists
try:
os.remove(optimized_path)
except OSError:
pass
# Benchmark optimization
start_time = time.time()
os.system(" ".join(["taskset -c 4", "python3 -m onneggs", compiled_path, optimized_path]))
end_time = time.time()
# Ensure that the optimized file was actually generated before recording time
if not os.path.isfile(optimized_path):
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "optimization trial", trial,
"failed due to missing output")
break
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "optimization trial", trial, "succeeded")
optimize_time = end_time - start_time
optimize_data.loc[len(optimize_data)] = pd.Series({
'test': test_name,
'y': test_case.shape[0],
'x': test_case.shape[1],
'trial': trial,
'time': optimize_time
})
# endregion
# region Running benchmarking function
run_data = pd.DataFrame(columns=['test', 'y', 'x', 'processor', 'executable', 'trial', 'time'])
def benchmark_run(test_name: str, test_case: TestCase):
unoptimized_path = get_compiled_ort_path(test_name, test_case.shape)
optimized_path = get_optimized_ort_path(test_name, test_case.shape)
# Ensure that the executable paths exist, otherwise abort
if not os.path.isfile(unoptimized_path):
print(test_name, " by ".join([str(dim) for dim in test_case.shape]),
"running benchmarking failed due to missing unoptimized executable")
return
if not os.path.isfile(optimized_path):
print(test_name, " by ".join([str(dim) for dim in test_case.shape]),
"running benchmarking failed due to missing optimized executable")
return
# CPU benchmarking
if test_case.cpu:
session_options = ort.SessionOptions()
session_options.execution_mode = ExecutionMode.ORT_PARALLEL
cpu_unoptimized_session = ort.InferenceSession(
unoptimized_path,
providers=["CPUExecutionProvider"]
)
cpu_optimized_session = ort.InferenceSession(
optimized_path,
providers=["CPUExecutionProvider"]
)
for trial in range(TRIAL_COUNT):
results = benchmark_run_single_trial(cpu_unoptimized_session, cpu_optimized_session, test_case.inputs)
if results is False:
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "running cpu trial", trial,
"failed due to output mismatch")
break
unoptimized_time, optimized_time = results
run_data.loc[len(run_data)] = pd.Series({
'test': test_name,
'y': test_case.shape[0],
'x': test_case.shape[1],
'processor': "cpu",
'executable': "unoptimized",
'trial': trial,
'time': unoptimized_time
})
run_data.loc[len(run_data)] = pd.Series({
'test': test_name,
'y': test_case.shape[0],
'x': test_case.shape[1],
'processor': "cpu",
'executable': "optimized",
'trial': trial,
'time': optimized_time
})
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "cpu running trial", trial,
"succeeded with", unoptimized_time, "unoptimized and", optimized_time, "optimized")
# GPU benchmarking
if test_case.gpu:
session_options = ort.SessionOptions()
session_options.execution_mode = ExecutionMode.ORT_PARALLEL
gpu_unoptimized_session = ort.InferenceSession(
unoptimized_path,
providers=["CUDAExecutionProvider"]
)
gpu_optimized_session = ort.InferenceSession(
optimized_path,
providers=["CUDAExecutionProvider"]
)
for trial in range(TRIAL_COUNT):
results = benchmark_run_single_trial(gpu_unoptimized_session, gpu_optimized_session, test_case.inputs)
if results is False:
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "running gpu trial", trial,
"failed due to unoptimized optimized output mismatch")
break
unoptimized_time, optimized_time = results
run_data.loc[len(run_data)] = pd.Series({
'test': test_name,
'y': test_case.shape[0],
'x': test_case.shape[1],
'processor': "gpu",
'executable': "unoptimized",
'trial': trial,
'time': unoptimized_time
})
run_data.loc[len(run_data)] = pd.Series({
'test': test_name,
'y': test_case.shape[0],
'x': test_case.shape[1],
'processor': "gpu",
'executable': "optimized",
'trial': trial,
'time': optimized_time
})
print(test_name, " by ".join([str(dim) for dim in test_case.shape]), "gpu running trial", trial,
"succeeded with", unoptimized_time, "unoptimized and", optimized_time, "optimized")
def benchmark_run_single_trial(
unoptimized_session: ort.InferenceSession,
optimized_session: ort.InferenceSession,
inputs: dict
):
# Benchmark unoptimized
unoptimized_start_time = time.time()
unoptimized_output = unoptimized_session.run(None, inputs)
unoptimized_end_time = time.time()
# Benchmark optimized
optimized_start_time = time.time()
optimized_output = optimized_session.run(None, inputs)
optimized_end_time = time.time()
# Check to ensure equality
unoptimized_output_numpy = np.nan_to_num(np.asarray(unoptimized_output))
optimized_output_numpy = np.nan_to_num(np.asarray(optimized_output))
if not np.isclose(np.nan_to_num(unoptimized_output_numpy), np.nan_to_num(optimized_output_numpy)).all():
print(np.asarray(unoptimized_output)[0, 0])
print(np.asarray(optimized_output)[0, 0])
return False
return unoptimized_end_time - unoptimized_start_time, optimized_end_time - optimized_start_time
# endregion
# region Main function
def main():
# Compilation benchmarking
for (test_name, test_function), test_cases in cases.items():
for test_case in test_cases:
benchmark_compile(test_name, test_function, test_case)
# Optimization benchmarking
for (test_name, test_function), test_cases in cases.items():
for test_case in test_cases:
benchmark_optimize(test_name, test_case)
# Running benchmarking
for (test_name, test_function), test_cases in cases.items():
for test_case in test_cases:
benchmark_run(test_name, test_case)
# Output results to csv files
now = datetime.datetime.now()
current_time_string = now.strftime("%m-%d-%Y-%H-%M-%S")
compile_data.to_csv(get_compile_data_path(current_time_string), index=False)
optimize_data.to_csv(get_optimize_data_path(current_time_string), index=False)
run_data.to_csv(get_run_data_path(current_time_string), index=False)
# Main boilerplate
if __name__ == "__main__":
main()
# endregion