|
| 1 | +// 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 <cmath> |
| 11 | +#include <functional> |
| 12 | +#include <random> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include <xnnpack.h> |
| 16 | + |
| 17 | +#include <benchmark/benchmark.h> |
| 18 | + |
| 19 | + |
| 20 | +static void add_nc_q8(benchmark::State& state) { |
| 21 | + const size_t batch_size = static_cast<size_t>(state.range(0)); |
| 22 | + const size_t channels = static_cast<size_t>(state.range(1)); |
| 23 | + |
| 24 | + std::random_device random_device; |
| 25 | + auto rng = std::mt19937(random_device()); |
| 26 | + auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng); |
| 27 | + |
| 28 | + std::vector<uint8_t> a(batch_size * channels); |
| 29 | + std::vector<uint8_t> b(batch_size * channels); |
| 30 | + std::vector<uint8_t> y(batch_size * channels); |
| 31 | + std::generate(a.begin(), a.end(), std::ref(u8rng)); |
| 32 | + std::generate(b.begin(), b.end(), std::ref(u8rng)); |
| 33 | + |
| 34 | + xnn_status status = xnn_initialize(); |
| 35 | + if (status != xnn_status_success) { |
| 36 | + state.SkipWithError("failed to initialize XNNPACK"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + xnn_operator_t add_op = nullptr; |
| 41 | + status = xnn_create_add_nc_q8( |
| 42 | + channels, channels /* a_stride */, channels /* b_stride */, channels /* sum_stride */, |
| 43 | + 127 /* a:zero point */, 1.0f /* a:scale */, |
| 44 | + 127 /* b:zero point */, 1.0f /* b:scale */, |
| 45 | + 127 /* y:zero point */, 1.0f /* y:scale */, |
| 46 | + 1 /* y:min */, 254 /* y:max */, |
| 47 | + 0 /* flags */, &add_op); |
| 48 | + if (status != xnn_status_success || add_op == nullptr) { |
| 49 | + state.SkipWithError("failed to create Q8 Add operator"); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + status = xnn_setup_add_nc_q8( |
| 54 | + add_op, |
| 55 | + batch_size, |
| 56 | + a.data(), b.data(), y.data(), |
| 57 | + nullptr /* thread pool */); |
| 58 | + if (status != xnn_status_success) { |
| 59 | + state.SkipWithError("failed to setup Q8 Add operator"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + for (auto _ : state) { |
| 64 | + status = xnn_run_operator(add_op, nullptr /* thread pool */); |
| 65 | + if (status != xnn_status_success) { |
| 66 | + state.SkipWithError("failed to run Q8 Add operator"); |
| 67 | + return; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + status = xnn_delete_operator(add_op); |
| 72 | + if (status != xnn_status_success) { |
| 73 | + state.SkipWithError("failed to delete Q8 Add operator"); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + const size_t elements_per_iteration = batch_size * channels; |
| 78 | + state.counters["elements"] = |
| 79 | + benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 80 | + |
| 81 | + const size_t bytes_per_iteration = 3 * elements_per_iteration * sizeof(uint8_t); |
| 82 | + state.counters["bytes"] = |
| 83 | + benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 84 | +} |
| 85 | + |
| 86 | +static void add_nc_q8_inplace(benchmark::State& state) { |
| 87 | + const size_t batch_size = static_cast<size_t>(state.range(0)); |
| 88 | + const size_t channels = static_cast<size_t>(state.range(1)); |
| 89 | + |
| 90 | + std::random_device random_device; |
| 91 | + auto rng = std::mt19937(random_device()); |
| 92 | + auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng); |
| 93 | + |
| 94 | + std::vector<uint8_t> a(batch_size * channels); |
| 95 | + std::vector<uint8_t> y(batch_size * channels); |
| 96 | + std::generate(a.begin(), a.end(), std::ref(u8rng)); |
| 97 | + |
| 98 | + xnn_status status = xnn_initialize(); |
| 99 | + if (status != xnn_status_success) { |
| 100 | + state.SkipWithError("failed to initialize XNNPACK"); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + xnn_operator_t add_op = nullptr; |
| 105 | + status = xnn_create_add_nc_q8( |
| 106 | + channels, channels /* a_stride */, channels /* b_stride */, channels /* sum_stride */, |
| 107 | + 127 /* a:zero point */, 1.0f /* a:scale */, |
| 108 | + 127 /* b:zero point */, 1.0f /* b:scale */, |
| 109 | + 127 /* y:zero point */, 1.0f /* y:scale */, |
| 110 | + 1 /* y:min */, 254 /* y:max */, |
| 111 | + 0 /* flags */, &add_op); |
| 112 | + if (status != xnn_status_success || add_op == nullptr) { |
| 113 | + state.SkipWithError("failed to create Q8 Add operator"); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + status = xnn_setup_add_nc_q8( |
| 118 | + add_op, |
| 119 | + batch_size, |
| 120 | + a.data(), y.data(), y.data(), |
| 121 | + nullptr /* thread pool */); |
| 122 | + if (status != xnn_status_success) { |
| 123 | + state.SkipWithError("failed to setup Q8 Add operator"); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + for (auto _ : state) { |
| 128 | + status = xnn_run_operator(add_op, nullptr /* thread pool */); |
| 129 | + if (status != xnn_status_success) { |
| 130 | + state.SkipWithError("failed to run Q8 Add operator"); |
| 131 | + return; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + status = xnn_delete_operator(add_op); |
| 136 | + if (status != xnn_status_success) { |
| 137 | + state.SkipWithError("failed to delete Q8 Add operator"); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + const size_t elements_per_iteration = batch_size * channels; |
| 142 | + state.counters["elements"] = |
| 143 | + benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 144 | + |
| 145 | + const size_t bytes_per_iteration = 3 * elements_per_iteration * sizeof(uint8_t); |
| 146 | + state.counters["bytes"] = |
| 147 | + benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 148 | +} |
| 149 | + |
| 150 | +static void CharacteristicArguments(benchmark::internal::Benchmark* b) |
| 151 | +{ |
| 152 | + b->ArgNames({"N", "C"}); |
| 153 | + |
| 154 | + int32_t c = 16; |
| 155 | + for (int32_t n = 224; n >= 7; n /= 2) { |
| 156 | + b->Args({n * n, c}); |
| 157 | + c *= 2; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +BENCHMARK(add_nc_q8)->Apply(CharacteristicArguments)->UseRealTime(); |
| 162 | +BENCHMARK(add_nc_q8_inplace)->Apply(CharacteristicArguments)->UseRealTime(); |
| 163 | + |
| 164 | +#ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 165 | +BENCHMARK_MAIN(); |
| 166 | +#endif |
0 commit comments