blob: a3e9189dd9f25f3719eb9a51a46f62a71118b873 [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 global_average_pooling_q8(benchmark::State& state) {
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 channels = state.range(3);
27
28 std::random_device random_device;
29 auto rng = std::mt19937(random_device());
30 auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng);
31
32 std::vector<uint8_t> input(batch_size * input_height * input_width * channels);
33 std::generate(input.begin(), input.end(), std::ref(u8rng));
34 std::vector<uint8_t> output(batch_size * channels);
35
Marat Dukhan04f03be2019-11-19 12:36:47 -080036 xnn_status status = xnn_initialize(nullptr /* allocator */);
XNNPACK Teamb455b122019-09-27 18:10:33 -070037 if (status != xnn_status_success) {
38 state.SkipWithError("failed to initialize XNNPACK");
39 }
40
41 xnn_operator_t global_pooling_op = nullptr;
42 status = xnn_create_global_average_pooling_nwc_q8(
43 channels, channels /* input stride */, channels /* output stride */,
44 127 /* input zero point */, 0.75f /* input scale */,
45 127 /* output zero point */, 1.25f /* output scale */,
46 0, 255,
47 0 /* flags */, &global_pooling_op);
48 if (status != xnn_status_success) {
49 state.SkipWithError("failed to create Global Average Pooling operator");
50 }
51
52 status = xnn_setup_global_average_pooling_nwc_q8(
53 global_pooling_op,
54 batch_size, input_height * input_width,
55 input.data(), output.data(),
56 nullptr /* thread pool */);
57 if (status != xnn_status_success) {
58 state.SkipWithError("failed to setup Global Average Pooling operator");
59 }
60
61 for (auto _ : state) {
62 xnn_run_operator(global_pooling_op, nullptr /* thread pool */);
63 }
64
65 status = xnn_delete_operator(global_pooling_op);
66 if (status != xnn_status_success) {
67 state.SkipWithError("failed to delete Global Average Pooling operator");
68 }
69 global_pooling_op = nullptr;
70
Frank Barchardbb4c18b2019-09-30 11:05:52 -070071 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
XNNPACK Teamb455b122019-09-27 18:10:33 -070072 state.counters["bytes"] = benchmark::Counter(
73 uint64_t(state.iterations()) *
74 batch_size * (input_height * input_width + 1) * channels * sizeof(uint8_t),
75 benchmark::Counter::kIsRate);
76}
77
78static void ImageNetArguments(benchmark::internal::Benchmark* b) {
79 b->ArgNames({"N", "H", "W", "C"});
80
81 /* N IH IW C */
82 b->Args({1, 7, 7, 1000});
83 b->Args({1, 13, 13, 1000});
84}
85
86BENCHMARK(global_average_pooling_q8)->Apply(ImageNetArguments)->UseRealTime();
87
88#ifndef XNNPACK_BENCHMARK_NO_MAIN
89BENCHMARK_MAIN();
90#endif