blob: d69c1de497156d9139668ef1a1b25daf92b8fea3 [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>
Marat Dukhan5ce30d92020-04-14 03:31:26 -070013#include <limits>
XNNPACK Teamb455b122019-09-27 18:10:33 -070014#include <random>
15#include <vector>
16
17#include <xnnpack.h>
18
19#include <benchmark/benchmark.h>
Frank Barchardbb4c18b2019-09-30 11:05:52 -070020#include "bench/utils.h"
XNNPACK Teamb455b122019-09-27 18:10:33 -070021
22
23void max_pooling_u8(benchmark::State& state, const char* net) {
24 const size_t batch_size = state.range(0);
25 const size_t input_height = state.range(1);
26 const size_t input_width = state.range(2);
27 const size_t pooling_size = state.range(3);
28 const size_t padding_size = state.range(4);
29 const size_t stride = state.range(5);
30 const size_t channels = state.range(6);
31
32 std::random_device random_device;
33 auto rng = std::mt19937(random_device());
Marat Dukhan44f0ca72020-08-02 21:46:58 -070034 auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), std::ref(rng));
XNNPACK Teamb455b122019-09-27 18:10:33 -070035
36 const size_t output_height = (2 * padding_size + input_height - pooling_size) / stride + 1;
37 const size_t output_width = (2 * padding_size + input_width - pooling_size) / stride + 1;
38
39 std::vector<uint8_t> input(batch_size * input_height * input_width * channels);
40 std::generate(input.begin(), input.end(), std::ref(u8rng));
41 std::vector<uint8_t> output(batch_size * output_height * output_width * channels);
42 std::fill(output.begin(), output.end(), 0xA5);
43
Marat Dukhan04f03be2019-11-19 12:36:47 -080044 xnn_status status = xnn_initialize(nullptr /* allocator */);
XNNPACK Teamb455b122019-09-27 18:10:33 -070045 if (status != xnn_status_success) {
46 state.SkipWithError("failed to initialize XNNPACK");
47 return;
48 }
49
50 xnn_operator_t pooling_op = nullptr;
51 status = xnn_create_max_pooling2d_nhwc_u8(
52 padding_size, padding_size, padding_size, padding_size,
53 pooling_size, pooling_size,
54 stride, stride,
55 1 /* dilation height */, 1 /* dilation width */,
56 channels, channels /* input pixel stride */, channels /* output pixel stride */,
57 0, 255,
58 0 /* flags */, &pooling_op);
59 if (status != xnn_status_success) {
60 state.SkipWithError("failed to create Max Pooling operator");
61 return;
62 }
63
64 status = xnn_setup_max_pooling2d_nhwc_u8(
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 Max 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 Max 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 Max Pooling operator");
85 return;
86 }
87 pooling_op = nullptr;
88
Marat Dukhand713e8a2020-12-04 14:23:12 -080089 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
90 if (cpu_frequency != 0) {
91 state.counters["cpufreq"] = cpu_frequency;
92 }
93
XNNPACK Teamb455b122019-09-27 18:10:33 -070094 state.counters["bytes"] = benchmark::Counter(
95 uint64_t(state.iterations()) *
96 batch_size * (input_height * input_width + output_height * output_width) * channels * sizeof(uint8_t),
97 benchmark::Counter::kIsRate);
98}
99
100void max_pooling_f32(benchmark::State& state, const char* net) {
101 const size_t batch_size = state.range(0);
102 const size_t input_height = state.range(1);
103 const size_t input_width = state.range(2);
104 const size_t pooling_size = state.range(3);
105 const size_t padding_size = state.range(4);
106 const size_t stride = state.range(5);
107 const size_t channels = state.range(6);
108
109 std::random_device random_device;
110 auto rng = std::mt19937(random_device());
Marat Dukhan44f0ca72020-08-02 21:46:58 -0700111 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700112
113 const size_t output_height = (2 * padding_size + input_height - pooling_size) / stride + 1;
114 const size_t output_width = (2 * padding_size + input_width - pooling_size) / stride + 1;
115
116 std::vector<float> input(batch_size * input_height * input_width * channels);
117 std::generate(input.begin(), input.end(), std::ref(f32rng));
118 std::vector<float> output(batch_size * output_height * output_width * channels);
119 std::fill(output.begin(), output.end(), nanf(""));
120
Marat Dukhan04f03be2019-11-19 12:36:47 -0800121 xnn_status status = xnn_initialize(nullptr /* allocator */);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700122 if (status != xnn_status_success) {
123 state.SkipWithError("failed to initialize XNNPACK");
124 return;
125 }
126
127 xnn_operator_t pooling_op = nullptr;
128 status = xnn_create_max_pooling2d_nhwc_f32(
129 padding_size, padding_size, padding_size, padding_size,
130 pooling_size, pooling_size,
131 stride, stride,
132 1 /* dilation height */, 1 /* dilation width */,
133 channels, channels /* input pixel stride */, channels /* output pixel stride */,
134 -std::numeric_limits<float>::infinity(), +std::numeric_limits<float>::infinity(),
135 0 /* flags */, &pooling_op);
136 if (status != xnn_status_success) {
137 state.SkipWithError("failed to create Max Pooling operator");
138 return;
139 }
140
141 status = xnn_setup_max_pooling2d_nhwc_f32(
142 pooling_op,
143 batch_size, input_height, input_width,
144 input.data(), output.data(),
145 nullptr /* thread pool */);
146 if (status != xnn_status_success) {
147 state.SkipWithError("failed to setup Max Pooling operator");
148 return;
149 }
150
151 for (auto _ : state) {
152 status = xnn_run_operator(pooling_op, nullptr /* thread pool */);
153 if (status != xnn_status_success) {
154 state.SkipWithError("failed to run Max Pooling operator");
155 return;
156 }
157 }
158
159 status = xnn_delete_operator(pooling_op);
160 if (status != xnn_status_success) {
161 state.SkipWithError("failed to delete Max Pooling operator");
162 return;
163 }
164 pooling_op = nullptr;
165
Marat Dukhand713e8a2020-12-04 14:23:12 -0800166 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
167 if (cpu_frequency != 0) {
168 state.counters["cpufreq"] = cpu_frequency;
169 }
170
XNNPACK Teamb455b122019-09-27 18:10:33 -0700171 state.counters["bytes"] = benchmark::Counter(
172 uint64_t(state.iterations()) *
173 batch_size * (input_height * input_width + output_height * output_width) * channels * sizeof(float),
174 benchmark::Counter::kIsRate);
175}
176
177// ShuffleNet v1/v2.
178static void ShuffleNet(benchmark::internal::Benchmark* b) {
179 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
180
181 /* N H W K P S C */
182 b->Args({1, 112, 112, 3, 1, 2, 24});
183}
184
185// SqueezeNet 1.0
186static void SqueezeNetV10(benchmark::internal::Benchmark* b) {
187 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
188
189 /*********** MaxPool 1 ************/
190 /* N H W K P S C */
191 b->Args({1, 111, 111, 3, 0, 2, 96});
192 /*********** MaxPool 4 ************/
193 /* N H W K P S C */
194 b->Args({1, 27, 27, 3, 0, 2, 256});
195 /*********** MaxPool 8 ************/
196 /* N H W K P S C */
197 b->Args({1, 13, 13, 3, 0, 2, 512});
198}
199
200// SqueezeNet 1.1
201static void SqueezeNetV11(benchmark::internal::Benchmark* b) {
202 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
203
204 /*********** MaxPool 1 ***********/
205 /* N H W K P S C */
206 b->Args({1, 111, 111, 3, 0, 2, 64});
207 /*********** MaxPool 3 ************/
208 /* N H W K P S C */
209 b->Args({1, 55, 55, 3, 0, 2, 128});
210 /*********** MaxPool 5 ************/
211 /* N H W K P S C */
212 b->Args({1, 13, 13, 3, 0, 2, 256});
213}
214
215static void VGG(benchmark::internal::Benchmark* b) {
216 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
217
218 /* N H W K P S C */
219 b->Args({1, 224, 224, 2, 1, 2, 64});
220 b->Args({1, 112, 112, 2, 1, 2, 128});
221 b->Args({1, 56, 56, 2, 1, 2, 256});
222 b->Args({1, 28, 28, 2, 1, 2, 512});
223 b->Args({1, 14, 14, 2, 1, 2, 512});
224}
225
226BENCHMARK_CAPTURE(max_pooling_f32, shufflenet, "ShuffleNet v1/v2")->Apply(ShuffleNet)->UseRealTime();
227BENCHMARK_CAPTURE(max_pooling_f32, squeezenet_v10, "SqueezeNet v1.0")->Apply(SqueezeNetV10)->UseRealTime();
228BENCHMARK_CAPTURE(max_pooling_f32, squeezenet_v11, "SqueezeNet v1.1")->Apply(SqueezeNetV11)->UseRealTime();
229BENCHMARK_CAPTURE(max_pooling_f32, vgg, "VGG")->Apply(VGG);
230
231BENCHMARK_CAPTURE(max_pooling_u8, shufflenet, "ShuffleNet v1/v2")->Apply(ShuffleNet)->UseRealTime();
232BENCHMARK_CAPTURE(max_pooling_u8, squeezenet_v10, "SqueezeNet v1.0")->Apply(SqueezeNetV10)->UseRealTime();
233BENCHMARK_CAPTURE(max_pooling_u8, squeezenet_v11, "SqueezeNet v1.1")->Apply(SqueezeNetV11)->UseRealTime();
234BENCHMARK_CAPTURE(max_pooling_u8, vgg, "VGG")->Apply(VGG);
235
236#ifndef XNNPACK_BENCHMARK_NO_MAIN
237BENCHMARK_MAIN();
238#endif