• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
49 }
50 
MobileNetV1(benchmark::State & state)51 static void MobileNetV1(benchmark::State& state) {
52   End2EndBenchmark(state, models::MobileNetV1);
53 }
54 
MobileNetV2(benchmark::State & state)55 static void MobileNetV2(benchmark::State& state) {
56   End2EndBenchmark(state, models::MobileNetV2);
57 }
58 
MobileNetV3Large(benchmark::State & state)59 static void MobileNetV3Large(benchmark::State& state) {
60   End2EndBenchmark(state, models::MobileNetV3Large);
61 }
62 
MobileNetV3Small(benchmark::State & state)63 static void MobileNetV3Small(benchmark::State& state) {
64   End2EndBenchmark(state, models::MobileNetV3Small);
65 }
66 
67 BENCHMARK(MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
68 BENCHMARK(MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
69 BENCHMARK(MobileNetV3Large)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
70 BENCHMARK(MobileNetV3Small)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
71 
72 #ifndef XNNPACK_BENCHMARK_NO_MAIN
73 BENCHMARK_MAIN();
74 #endif