blob: 5052bdac65a8d7a559d70c6e526a67f241193401 [file] [log] [blame]
Marat Dukhanc068bb62019-10-04 13:24:39 -07001// 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
Frank Barchardc712fa42019-10-31 14:00:21 -070016#include "bench/utils.h"
Marat Dukhanc068bb62019-10-04 13:24:39 -070017#include "models/models.h"
18
19
Marat Dukhanbad48fe2019-11-04 10:35:22 -080020static void End2EndBenchmark(
21 benchmark::State& state,
22 models::ExecutionPlanFactory model_factory)
23{
Marat Dukhan04f03be2019-11-19 12:36:47 -080024 if (xnn_initialize(nullptr /* allocator */) != xnn_status_success) {
Marat Dukhanc068bb62019-10-04 13:24:39 -070025 state.SkipWithError("failed to initialize XNNPACK");
26 return;
27 }
28
Marat Dukhanbad48fe2019-11-04 10:35:22 -080029 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());
Marat Dukhanc068bb62019-10-04 13:24:39 -070034 if (execution_plan.empty()) {
Marat Dukhanbad48fe2019-11-04 10:35:22 -080035 state.SkipWithError("failed to create a model");
Marat Dukhanc068bb62019-10-04 13:24:39 -070036 return;
37 }
38
39 for (auto _ : state) {
40 for (const std::unique_ptr<xnn_operator, decltype(&xnn_delete_operator)>& op : execution_plan) {
Marat Dukhanbad48fe2019-11-04 10:35:22 -080041 xnn_status status = xnn_run_operator(op.get(), threadpool.get());
Marat Dukhanc068bb62019-10-04 13:24:39 -070042 if (status != xnn_status_success) {
Marat Dukhanbad48fe2019-11-04 10:35:22 -080043 state.SkipWithError("failed to run a model");
Marat Dukhanc068bb62019-10-04 13:24:39 -070044 return;
45 }
46 }
47 }
Frank Barchardc712fa42019-10-31 14:00:21 -070048 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
Marat Dukhanc068bb62019-10-04 13:24:39 -070049}
50
Marat Dukhan270a2c42020-06-26 16:45:52 -070051static void FP32MobileNetV1(benchmark::State& state) {
52 End2EndBenchmark(state, models::FP32MobileNetV1);
Marat Dukhanbad48fe2019-11-04 10:35:22 -080053}
54
Marat Dukhan270a2c42020-06-26 16:45:52 -070055static void FP32MobileNetV2(benchmark::State& state) {
56 End2EndBenchmark(state, models::FP32MobileNetV2);
Marat Dukhanc068bb62019-10-04 13:24:39 -070057}
58
Marat Dukhan270a2c42020-06-26 16:45:52 -070059static void FP32MobileNetV3Large(benchmark::State& state) {
60 End2EndBenchmark(state, models::FP32MobileNetV3Large);
Marat Dukhanc08cdf52019-12-09 09:17:51 -080061}
62
Marat Dukhan270a2c42020-06-26 16:45:52 -070063static void FP32MobileNetV3Small(benchmark::State& state) {
64 End2EndBenchmark(state, models::FP32MobileNetV3Small);
Marat Dukhanc08cdf52019-12-09 09:17:51 -080065}
66
Marat Dukhan270a2c42020-06-26 16:45:52 -070067static void FP16MobileNetV1(benchmark::State& state) {
68 End2EndBenchmark(state, models::FP16MobileNetV1);
69}
70
71static void FP16MobileNetV2(benchmark::State& state) {
72 End2EndBenchmark(state, models::FP16MobileNetV2);
73}
74
Marat Dukhan66f3ccd2020-09-14 16:09:38 -070075static void FP16MobileNetV3Large(benchmark::State& state) {
76 End2EndBenchmark(state, models::FP16MobileNetV3Large);
77}
78
79static void FP16MobileNetV3Small(benchmark::State& state) {
80 End2EndBenchmark(state, models::FP16MobileNetV3Small);
81}
82
Marat Dukhan0743cdf2020-08-04 18:52:07 -070083static void QS8MobileNetV1(benchmark::State& state) {
84 End2EndBenchmark(state, models::QS8MobileNetV1);
85}
86
Marat Dukhan70a96182020-09-03 17:13:58 -070087static void QS8MobileNetV2(benchmark::State& state) {
88 End2EndBenchmark(state, models::QS8MobileNetV2);
89}
90
Marat Dukhan270a2c42020-06-26 16:45:52 -070091BENCHMARK(FP32MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
92BENCHMARK(FP32MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
93BENCHMARK(FP32MobileNetV3Large)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
94BENCHMARK(FP32MobileNetV3Small)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
95
96BENCHMARK(FP16MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
97BENCHMARK(FP16MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
Marat Dukhan66f3ccd2020-09-14 16:09:38 -070098BENCHMARK(FP16MobileNetV3Large)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
99BENCHMARK(FP16MobileNetV3Small)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
Marat Dukhanc068bb62019-10-04 13:24:39 -0700100
Marat Dukhan0743cdf2020-08-04 18:52:07 -0700101BENCHMARK(QS8MobileNetV1)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
Marat Dukhan70a96182020-09-03 17:13:58 -0700102BENCHMARK(QS8MobileNetV2)->Apply(benchmark::utils::MultiThreadingParameters)->Unit(benchmark::kMicrosecond)->UseRealTime();
Marat Dukhan0743cdf2020-08-04 18:52:07 -0700103
Marat Dukhanc068bb62019-10-04 13:24:39 -0700104#ifndef XNNPACK_BENCHMARK_NO_MAIN
105BENCHMARK_MAIN();
106#endif