blob: a757890614b51d9f593209345d5524f62d77f3cd [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// Copyright 2019 Google LLC
5//
6// This source code is licensed under the BSD-style license found in the
7// LICENSE file in the root directory of this source tree.
8
9#include <algorithm>
10#include <cfloat>
11#include <cmath>
12#include <functional>
13#include <random>
14#include <vector>
15
16#include <xnnpack.h>
17
18#include <benchmark/benchmark.h>
Frank Barchardbb4c18b2019-09-30 11:05:52 -070019#include "bench/utils.h"
XNNPACK Teamb455b122019-09-27 18:10:33 -070020
21
22static void average_pooling_q8(benchmark::State& state, const char* net) {
23 const size_t batch_size = state.range(0);
24 const size_t input_height = state.range(1);
25 const size_t input_width = state.range(2);
26 const size_t pooling_size = state.range(3);
27 const size_t padding_size = state.range(4);
28 const size_t stride = state.range(5);
29 const size_t channels = state.range(6);
30
31 std::random_device random_device;
32 auto rng = std::mt19937(random_device());
33 auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng);
34
35 const size_t output_height = (2 * padding_size + input_height - pooling_size) / stride + 1;
36 const size_t output_width = (2 * padding_size + input_width - pooling_size) / stride + 1;
37
38 std::vector<uint8_t> input(batch_size * input_height * input_width * channels + XNN_EXTRA_BYTES / sizeof(uint8_t));
39 std::generate(input.begin(), input.end(), std::ref(u8rng));
40 std::vector<uint8_t> output(batch_size * output_height * output_width * channels);
41 std::fill(output.begin(), output.end(), 0xA5);
42
Marat Dukhan04f03be2019-11-19 12:36:47 -080043 xnn_status status = xnn_initialize(nullptr /* allocator */);
XNNPACK Teamb455b122019-09-27 18:10:33 -070044 if (status != xnn_status_success) {
45 state.SkipWithError("failed to initialize XNNPACK");
46 return;
47 }
48
49 xnn_operator_t pooling_op = nullptr;
50 status = xnn_create_average_pooling2d_nhwc_q8(
51 padding_size, padding_size, padding_size, padding_size,
52 pooling_size, pooling_size,
53 stride, stride,
54 channels, channels /* input pixel stride */, channels /* output pixel stride */,
55 127 /* input zero point */, 0.75f /* input scale */,
56 127 /* output zero point */, 1.25f /* output scale */,
57 0, 255,
58 0 /* flags */, &pooling_op);
59 if (status != xnn_status_success) {
60 state.SkipWithError("failed to create Average Pooling operator");
61 return;
62 }
63
64 status = xnn_setup_average_pooling2d_nhwc_q8(
65 pooling_op,
66 batch_size, input_height, input_width,
67 input.data(), output.data(),
68 nullptr /* thread pool */);
69 if (status != xnn_status_success) {
70 state.SkipWithError("failed to setup Average Pooling operator");
71 return;
72 }
73
74 for (auto _ : state) {
75 status = xnn_run_operator(pooling_op, nullptr /* thread pool */);
76 if (status != xnn_status_success) {
77 state.SkipWithError("failed to run Average Pooling operator");
78 return;
79 }
80 }
81
82 status = xnn_delete_operator(pooling_op);
83 if (status != xnn_status_success) {
84 state.SkipWithError("failed to delete Average Pooling operator");
85 return;
86 }
87 pooling_op = nullptr;
88
Frank Barchardbb4c18b2019-09-30 11:05:52 -070089 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
90
XNNPACK Teamb455b122019-09-27 18:10:33 -070091 state.counters["bytes"] = benchmark::Counter(
92 uint64_t(state.iterations()) *
93 batch_size * (input_height * input_width + output_height * output_width) * channels * sizeof(uint8_t),
94 benchmark::Counter::kIsRate);
95}
96
97// ShuffleNet v1 with 1 group.
98static void ShuffleNetV1G1(benchmark::internal::Benchmark* b) {
99 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
100
101 /* N H W K P S C */
102 b->Args({1, 56, 56, 3, 1, 2, 24});
103 b->Args({1, 28, 28, 3, 1, 2, 144});
104 b->Args({1, 14, 14, 3, 1, 2, 288});
105 b->Args({1, 7, 7, 3, 1, 2, 576});
106}
107
108// ShuffleNet v1 with 2 groups.
109static void ShuffleNetV1G2(benchmark::internal::Benchmark* b) {
110 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
111
112 /* N H W K P S C */
113 b->Args({1, 56, 56, 3, 1, 2, 24});
114 b->Args({1, 28, 28, 3, 1, 2, 200});
115 b->Args({1, 14, 14, 3, 1, 2, 400});
116 b->Args({1, 7, 7, 3, 1, 2, 800});
117}
118
119// ShuffleNet v1 with 3 groups.
120static void ShuffleNetV1G3(benchmark::internal::Benchmark* b) {
121 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
122
123 /* N H W K P S C */
124 b->Args({1, 56, 56, 3, 1, 2, 24});
125 b->Args({1, 28, 28, 3, 1, 2, 240});
126 b->Args({1, 14, 14, 3, 1, 2, 480});
127 b->Args({1, 7, 7, 3, 1, 2, 960});
128}
129
130// ShuffleNet v1 with 4 groups.
131static void ShuffleNetV1G4(benchmark::internal::Benchmark* b) {
132 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
133
134 /* N H W K P S C */
135 b->Args({1, 56, 56, 3, 1, 2, 24});
136 b->Args({1, 28, 28, 3, 1, 2, 272});
137 b->Args({1, 14, 14, 3, 1, 2, 576});
138 b->Args({1, 7, 7, 3, 1, 2, 1088});
139}
140
141// ShuffleNet v1 with 8 groups.
142static void ShuffleNetV1G8(benchmark::internal::Benchmark* b) {
143 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
144
145 /* N H W K P S C */
146 b->Args({1, 56, 56, 3, 1, 2, 24});
147 b->Args({1, 28, 28, 3, 1, 2, 384});
148 b->Args({1, 14, 14, 3, 1, 2, 768});
149 b->Args({1, 7, 7, 3, 1, 2, 1536});
150}
151
152BENCHMARK_CAPTURE(average_pooling_q8, shufflenet_v1_g1, "ShuffleNet v1 (1 group)")->Apply(ShuffleNetV1G1)->UseRealTime();
153BENCHMARK_CAPTURE(average_pooling_q8, shufflenet_v1_g2, "ShuffleNet v1 (2 groups)")->Apply(ShuffleNetV1G2)->UseRealTime();
154BENCHMARK_CAPTURE(average_pooling_q8, shufflenet_v1_g3, "ShuffleNet v1 (3 groups)")->Apply(ShuffleNetV1G3)->UseRealTime();
155BENCHMARK_CAPTURE(average_pooling_q8, shufflenet_v1_g4, "ShuffleNet v1 (4 groups)")->Apply(ShuffleNetV1G4)->UseRealTime();
156BENCHMARK_CAPTURE(average_pooling_q8, shufflenet_v1_g8, "ShuffleNet v1 (8 groups)")->Apply(ShuffleNetV1G8)->UseRealTime();
157
158#ifndef XNNPACK_BENCHMARK_NO_MAIN
159BENCHMARK_MAIN();
160#endif