blob: ea52622570694ec1c96acddf69036ebf0a42d57f [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
Frank Barchardbb4c18b2019-09-30 11:05:52 -070089 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
XNNPACK Teamb455b122019-09-27 18:10:33 -070090 state.counters["bytes"] = benchmark::Counter(
91 uint64_t(state.iterations()) *
92 batch_size * (input_height * input_width + output_height * output_width) * channels * sizeof(uint8_t),
93 benchmark::Counter::kIsRate);
94}
95
96void max_pooling_f32(benchmark::State& state, const char* net) {
97 const size_t batch_size = state.range(0);
98 const size_t input_height = state.range(1);
99 const size_t input_width = state.range(2);
100 const size_t pooling_size = state.range(3);
101 const size_t padding_size = state.range(4);
102 const size_t stride = state.range(5);
103 const size_t channels = state.range(6);
104
105 std::random_device random_device;
106 auto rng = std::mt19937(random_device());
Marat Dukhan44f0ca72020-08-02 21:46:58 -0700107 auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), std::ref(rng));
XNNPACK Teamb455b122019-09-27 18:10:33 -0700108
109 const size_t output_height = (2 * padding_size + input_height - pooling_size) / stride + 1;
110 const size_t output_width = (2 * padding_size + input_width - pooling_size) / stride + 1;
111
112 std::vector<float> input(batch_size * input_height * input_width * channels);
113 std::generate(input.begin(), input.end(), std::ref(f32rng));
114 std::vector<float> output(batch_size * output_height * output_width * channels);
115 std::fill(output.begin(), output.end(), nanf(""));
116
Marat Dukhan04f03be2019-11-19 12:36:47 -0800117 xnn_status status = xnn_initialize(nullptr /* allocator */);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700118 if (status != xnn_status_success) {
119 state.SkipWithError("failed to initialize XNNPACK");
120 return;
121 }
122
123 xnn_operator_t pooling_op = nullptr;
124 status = xnn_create_max_pooling2d_nhwc_f32(
125 padding_size, padding_size, padding_size, padding_size,
126 pooling_size, pooling_size,
127 stride, stride,
128 1 /* dilation height */, 1 /* dilation width */,
129 channels, channels /* input pixel stride */, channels /* output pixel stride */,
130 -std::numeric_limits<float>::infinity(), +std::numeric_limits<float>::infinity(),
131 0 /* flags */, &pooling_op);
132 if (status != xnn_status_success) {
133 state.SkipWithError("failed to create Max Pooling operator");
134 return;
135 }
136
137 status = xnn_setup_max_pooling2d_nhwc_f32(
138 pooling_op,
139 batch_size, input_height, input_width,
140 input.data(), output.data(),
141 nullptr /* thread pool */);
142 if (status != xnn_status_success) {
143 state.SkipWithError("failed to setup Max Pooling operator");
144 return;
145 }
146
147 for (auto _ : state) {
148 status = xnn_run_operator(pooling_op, nullptr /* thread pool */);
149 if (status != xnn_status_success) {
150 state.SkipWithError("failed to run Max Pooling operator");
151 return;
152 }
153 }
154
155 status = xnn_delete_operator(pooling_op);
156 if (status != xnn_status_success) {
157 state.SkipWithError("failed to delete Max Pooling operator");
158 return;
159 }
160 pooling_op = nullptr;
161
Frank Barchardbb4c18b2019-09-30 11:05:52 -0700162 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700163 state.counters["bytes"] = benchmark::Counter(
164 uint64_t(state.iterations()) *
165 batch_size * (input_height * input_width + output_height * output_width) * channels * sizeof(float),
166 benchmark::Counter::kIsRate);
167}
168
169// ShuffleNet v1/v2.
170static void ShuffleNet(benchmark::internal::Benchmark* b) {
171 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
172
173 /* N H W K P S C */
174 b->Args({1, 112, 112, 3, 1, 2, 24});
175}
176
177// SqueezeNet 1.0
178static void SqueezeNetV10(benchmark::internal::Benchmark* b) {
179 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
180
181 /*********** MaxPool 1 ************/
182 /* N H W K P S C */
183 b->Args({1, 111, 111, 3, 0, 2, 96});
184 /*********** MaxPool 4 ************/
185 /* N H W K P S C */
186 b->Args({1, 27, 27, 3, 0, 2, 256});
187 /*********** MaxPool 8 ************/
188 /* N H W K P S C */
189 b->Args({1, 13, 13, 3, 0, 2, 512});
190}
191
192// SqueezeNet 1.1
193static void SqueezeNetV11(benchmark::internal::Benchmark* b) {
194 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
195
196 /*********** MaxPool 1 ***********/
197 /* N H W K P S C */
198 b->Args({1, 111, 111, 3, 0, 2, 64});
199 /*********** MaxPool 3 ************/
200 /* N H W K P S C */
201 b->Args({1, 55, 55, 3, 0, 2, 128});
202 /*********** MaxPool 5 ************/
203 /* N H W K P S C */
204 b->Args({1, 13, 13, 3, 0, 2, 256});
205}
206
207static void VGG(benchmark::internal::Benchmark* b) {
208 b->ArgNames({"N", "H", "W", "K", "P", "S", "C"});
209
210 /* N H W K P S C */
211 b->Args({1, 224, 224, 2, 1, 2, 64});
212 b->Args({1, 112, 112, 2, 1, 2, 128});
213 b->Args({1, 56, 56, 2, 1, 2, 256});
214 b->Args({1, 28, 28, 2, 1, 2, 512});
215 b->Args({1, 14, 14, 2, 1, 2, 512});
216}
217
218BENCHMARK_CAPTURE(max_pooling_f32, shufflenet, "ShuffleNet v1/v2")->Apply(ShuffleNet)->UseRealTime();
219BENCHMARK_CAPTURE(max_pooling_f32, squeezenet_v10, "SqueezeNet v1.0")->Apply(SqueezeNetV10)->UseRealTime();
220BENCHMARK_CAPTURE(max_pooling_f32, squeezenet_v11, "SqueezeNet v1.1")->Apply(SqueezeNetV11)->UseRealTime();
221BENCHMARK_CAPTURE(max_pooling_f32, vgg, "VGG")->Apply(VGG);
222
223BENCHMARK_CAPTURE(max_pooling_u8, shufflenet, "ShuffleNet v1/v2")->Apply(ShuffleNet)->UseRealTime();
224BENCHMARK_CAPTURE(max_pooling_u8, squeezenet_v10, "SqueezeNet v1.0")->Apply(SqueezeNetV10)->UseRealTime();
225BENCHMARK_CAPTURE(max_pooling_u8, squeezenet_v11, "SqueezeNet v1.1")->Apply(SqueezeNetV11)->UseRealTime();
226BENCHMARK_CAPTURE(max_pooling_u8, vgg, "VGG")->Apply(VGG);
227
228#ifndef XNNPACK_BENCHMARK_NO_MAIN
229BENCHMARK_MAIN();
230#endif