xref: /aosp_15_r20/external/XNNPACK/bench/end2end.cc (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright 2019 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <algorithm>
7 #include <cmath>
8 #include <functional>
9 #include <random>
10 #include <vector>
11 
12 #include <xnnpack.h>
13 
14 #include <benchmark/benchmark.h>
15 
16 #include "bench/utils.h"
17 #include "models/models.h"
18 
19 
End2EndBenchmark(benchmark::State & state,models::ExecutionPlanFactory model_factory)20 static void End2EndBenchmark(
21   benchmark::State& state,
22   models::ExecutionPlanFactory model_factory)
23 {
24   if (xnn_initialize(nullptr /* allocator */) != xnn_status_success) {
25     state.SkipWithError("failed to initialize XNNPACK");
26     return;
27   }
28 
29   const size_t num_threads = state.range(0);
30   std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool(
31     pthreadpool_create(num_threads), pthreadpool_destroy);
32 
33   auto execution_plan = model_factory(threadpool.get());
34   if (execution_plan.empty()) {
35     state.SkipWithError("failed to create a model");
36     return;
37   }
38 
39   for (auto _ : state) {
40     for (const std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)>& op : execution_plan) {
41       xnn_status status = xnn_run_operator(op.get(), threadpool.get());
42       if (status != xnn_status_success) {
43         state.SkipWithError("failed to run a model");
44         return;
45       }
46     }
47   }
48 
49   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
50   if (cpu_frequency != 0) {
51     state.counters["cpufreq"] = cpu_frequency;
52   }
53 }
54 
FP32MobileNetV1(benchmark::State & state)55 static void FP32MobileNetV1(benchmark::State& state) {
56   End2EndBenchmark(state, models::FP32MobileNetV1);
57 }
58 
FP32MobileNetV2(benchmark::State & state)59 static void FP32MobileNetV2(benchmark::State& state) {
60   End2EndBenchmark(state, models::FP32MobileNetV2);
61 }
62 
FP32MobileNetV3Large(benchmark::State & state)63 static void FP32MobileNetV3Large(benchmark::State& state) {
64   End2EndBenchmark(state, models::FP32MobileNetV3Large);
65 }
66 
FP32MobileNetV3Small(benchmark::State & state)67 static void FP32MobileNetV3Small(benchmark::State& state) {
68   End2EndBenchmark(state, models::FP32MobileNetV3Small);
69 }
70 
FP32Sparse80MobileNetV1(benchmark::State & state)71 static void FP32Sparse80MobileNetV1(benchmark::State& state) {
72   End2EndBenchmark(state, [](pthreadpool_t threadpool) {
73     return models::FP32SparseMobileNetV1(0.8f, threadpool);
74   });
75 }
76 
FP32Sparse80MobileNetV2(benchmark::State & state)77 static void FP32Sparse80MobileNetV2(benchmark::State& state) {
78   End2EndBenchmark(state, [](pthreadpool_t threadpool) {
79     return models::FP32SparseMobileNetV2(0.8f, threadpool);
80   });
81 }
82 
FP32Sparse80MobileNetV3Large(benchmark::State & state)83 static void FP32Sparse80MobileNetV3Large(benchmark::State& state) {
84   End2EndBenchmark(state, [](pthreadpool_t threadpool) {
85     return models::FP32SparseMobileNetV3Large(0.8f, threadpool);
86   });
87 }
88 
FP32Sparse80MobileNetV3Small(benchmark::State & state)89 static void FP32Sparse80MobileNetV3Small(benchmark::State& state) {
90   End2EndBenchmark(state, [](pthreadpool_t threadpool) {
91     return models::FP32SparseMobileNetV3Small(0.8f, threadpool);
92   });
93 }
94 
FP16MobileNetV1(benchmark::State & state)95 static void FP16MobileNetV1(benchmark::State& state) {
96   End2EndBenchmark(state, models::FP16MobileNetV1);
97 }
98 
FP16MobileNetV2(benchmark::State & state)99 static void FP16MobileNetV2(benchmark::State& state) {
100   End2EndBenchmark(state, models::FP16MobileNetV2);
101 }
102 
FP16MobileNetV3Large(benchmark::State & state)103 static void FP16MobileNetV3Large(benchmark::State& state) {
104   End2EndBenchmark(state, models::FP16MobileNetV3Large);
105 }
106 
FP16MobileNetV3Small(benchmark::State & state)107 static void FP16MobileNetV3Small(benchmark::State& state) {
108   End2EndBenchmark(state, models::FP16MobileNetV3Small);
109 }
110 
QC8MobileNetV1(benchmark::State & state)111 static void QC8MobileNetV1(benchmark::State& state) {
112   End2EndBenchmark(state, models::QC8MobileNetV1);
113 }
114 
QC8MobileNetV2(benchmark::State & state)115 static void QC8MobileNetV2(benchmark::State& state) {
116   End2EndBenchmark(state, models::QC8MobileNetV2);
117 }
118 
QS8MobileNetV1(benchmark::State & state)119 static void QS8MobileNetV1(benchmark::State& state) {
120   End2EndBenchmark(state, models::QS8MobileNetV1);
121 }
122 
QS8MobileNetV2(benchmark::State & state)123 static void QS8MobileNetV2(benchmark::State& state) {
124   End2EndBenchmark(state, models::QS8MobileNetV2);
125 }
126 
QU8MobileNetV1(benchmark::State & state)127 static void QU8MobileNetV1(benchmark::State& state) {
128   End2EndBenchmark(state, models::QU8MobileNetV1);
129 }
130 
QU8MobileNetV2(benchmark::State & state)131 static void QU8MobileNetV2(benchmark::State& state) {
132   End2EndBenchmark(state, models::QU8MobileNetV2);
133 }
134 
135 BENCHMARK(FP32MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
136 BENCHMARK(FP32MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
137 BENCHMARK(FP32MobileNetV3Large)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
138 BENCHMARK(FP32MobileNetV3Small)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
139 
140 BENCHMARK(FP32Sparse80MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
141 BENCHMARK(FP32Sparse80MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
142 BENCHMARK(FP32Sparse80MobileNetV3Large)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
143 BENCHMARK(FP32Sparse80MobileNetV3Small)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
144 
145 BENCHMARK(FP16MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
146 BENCHMARK(FP16MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
147 BENCHMARK(FP16MobileNetV3Large)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
148 BENCHMARK(FP16MobileNetV3Small)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
149 
150 BENCHMARK(QC8MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
151 BENCHMARK(QC8MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
152 
153 BENCHMARK(QS8MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
154 BENCHMARK(QS8MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
155 
156 BENCHMARK(QU8MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
157 BENCHMARK(QU8MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
158 
159 #ifndef XNNPACK_BENCHMARK_NO_MAIN
160 BENCHMARK_MAIN();
161 #endif
162