XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 <cfloat> |
| 11 | #include <cmath> |
| 12 | #include <functional> |
| 13 | #include <random> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include <cpuinfo.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 17 | |
| 18 | #include <benchmark/benchmark.h> |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 19 | #include "bench/utils.h" |
| 20 | #include <xnnpack/AlignedAllocator.h> |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 21 | #include <xnnpack/common.h> |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 22 | #include <xnnpack/requantization-stubs.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 23 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 24 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 25 | class Requantization : public benchmark::Fixture { |
| 26 | public: |
| 27 | inline Requantization() |
| 28 | { |
| 29 | cpuinfo_initialize(); |
| 30 | const size_t l1d_size = cpuinfo_get_l1d_cache(0)->size; |
| 31 | const size_t l1d_reserve = 1024; |
| 32 | n_ = (l1d_size - l1d_reserve) / (sizeof(int32_t) + sizeof(uint8_t)); |
| 33 | n_ = n_ / 16 * 16; |
| 34 | } |
| 35 | |
Frank Barchard | bd3c9aa | 2021-07-15 16:02:59 -0700 | [diff] [blame] | 36 | virtual void SetUp(benchmark::State& state) override |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 37 | { |
| 38 | std::random_device random_device; |
| 39 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | ecd8311 | 2020-08-03 21:50:28 -0700 | [diff] [blame] | 40 | auto i32rng = std::bind(std::uniform_int_distribution<int32_t>(), std::ref(rng)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 41 | |
| 42 | input_.resize(n()); |
Marat Dukhan | ecd8311 | 2020-08-03 21:50:28 -0700 | [diff] [blame] | 43 | std::generate(input_.begin(), input_.end(), std::ref(i32rng)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 44 | output_.resize(n()); |
| 45 | std::fill(output_.begin(), output_.end(), 0xA5); |
Frank Barchard | bd3c9aa | 2021-07-15 16:02:59 -0700 | [diff] [blame] | 46 | |
| 47 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 48 | if (cpu_frequency != 0) { |
| 49 | state.counters["cpufreq"] = cpu_frequency; |
| 50 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | virtual void TearDown(benchmark::State& state) override |
| 54 | { |
| 55 | state.SetItemsProcessed(uint64_t(state.iterations()) * n()); |
| 56 | state.SetBytesProcessed(uint64_t(state.iterations()) * n() * (sizeof(int32_t) + sizeof(uint8_t))); |
| 57 | input_.clear(); |
| 58 | output_.clear(); |
| 59 | } |
| 60 | |
| 61 | inline const int32_t* input() const |
| 62 | { |
| 63 | return input_.data(); |
| 64 | } |
| 65 | |
| 66 | inline uint8_t* output() |
| 67 | { |
| 68 | return output_.data(); |
| 69 | } |
| 70 | |
| 71 | inline size_t n() const |
| 72 | { |
| 73 | return n_; |
| 74 | } |
| 75 | |
| 76 | protected: |
Marat Dukhan | e13e639 | 2021-07-26 22:22:35 -0700 | [diff] [blame] | 77 | std::vector<int32_t, AlignedAllocator<int32_t, 64>> input_; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 78 | std::vector<uint8_t> output_; |
| 79 | size_t n_; |
| 80 | }; |
| 81 | |
Marat Dukhan | 290055c | 2020-06-09 12:24:29 -0700 | [diff] [blame] | 82 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 83 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 84 | BENCHMARK_F(Requantization, fp32__neon)(benchmark::State& state) { |
| 85 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 86 | xnn_qu8_requantize_fp32__neon( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 87 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 88 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 89 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 90 | |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 91 | BENCHMARK_F(Requantization, gemmlowp__neon)(benchmark::State& state) { |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 92 | for (auto _ : state) { |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 93 | xnn_qu8_requantize_gemmlowp__neon( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 94 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 95 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 96 | } |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 97 | |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 98 | BENCHMARK_F(Requantization, rndna__neon)(benchmark::State& state) { |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 99 | for (auto _ : state) { |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 100 | xnn_qu8_requantize_rndna__neon( |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 101 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 102 | } |
| 103 | } |
| 104 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 105 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 106 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 107 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 108 | BENCHMARK_F(Requantization, fp32__sse2)(benchmark::State& state) { |
| 109 | for (auto _ : state) { |
| 110 | xnn_qu8_requantize_fp32__sse2( |
| 111 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | BENCHMARK_F(Requantization, gemmlowp__sse2)(benchmark::State& state) { |
| 116 | for (auto _ : state) { |
| 117 | xnn_qu8_requantize_gemmlowp__sse2( |
| 118 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | BENCHMARK_F(Requantization, gemmlowp__ssse3)(benchmark::State& state) { |
| 123 | for (auto _ : state) { |
| 124 | xnn_qu8_requantize_gemmlowp__ssse3( |
| 125 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | BENCHMARK_F(Requantization, gemmlowp__sse4)(benchmark::State& state) { |
| 130 | for (auto _ : state) { |
| 131 | xnn_qu8_requantize_gemmlowp__sse4( |
| 132 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 133 | } |
| 134 | } |
| 135 | |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 136 | BENCHMARK_F(Requantization, rndna__sse2)(benchmark::State& state) { |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 137 | for (auto _ : state) { |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 138 | xnn_qu8_requantize_rndna__sse2( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 139 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 140 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 141 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 142 | |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 143 | BENCHMARK_F(Requantization, rndna__ssse3)(benchmark::State& state) { |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 144 | for (auto _ : state) { |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 145 | xnn_qu8_requantize_rndna__ssse3( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 146 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 147 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 148 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 149 | |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 150 | BENCHMARK_F(Requantization, rndna__sse4)(benchmark::State& state) { |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 151 | for (auto _ : state) { |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 152 | xnn_qu8_requantize_rndna__sse4( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 153 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 154 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 155 | } |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 156 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 157 | |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 158 | |
| 159 | #if XNN_ARCH_WASMSIMD |
| 160 | BENCHMARK_F(Requantization, fp32__wasmsimd)(benchmark::State& state) { |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 161 | for (auto _ : state) { |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 162 | xnn_qu8_requantize_fp32__wasmsimd( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 163 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 164 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 165 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 166 | |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 167 | BENCHMARK_F(Requantization, gemmlowp__wasmsimd)(benchmark::State& state) { |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 168 | for (auto _ : state) { |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 169 | xnn_qu8_requantize_gemmlowp__wasmsimd( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 170 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 171 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 172 | } |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 173 | #endif // XNN_ARCH_WASMSIMD |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 174 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 175 | |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 176 | BENCHMARK_F(Requantization, fp32__scalar_lrintf)(benchmark::State& state) { |
| 177 | for (auto _ : state) { |
| 178 | xnn_qu8_requantize_fp32__scalar_lrintf( |
| 179 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 180 | } |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | BENCHMARK_F(Requantization, fp32__scalar_magic)(benchmark::State& state) { |
| 184 | for (auto _ : state) { |
| 185 | xnn_qu8_requantize_fp32__scalar_magic( |
| 186 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | BENCHMARK_F(Requantization, gemmlowp__scalar)(benchmark::State& state) { |
| 191 | for (auto _ : state) { |
| 192 | xnn_qu8_requantize_gemmlowp__scalar( |
| 193 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 194 | } |
| 195 | } |
| 196 | |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 197 | BENCHMARK_F(Requantization, rndna__scalar_signed64)(benchmark::State& state) { |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 198 | for (auto _ : state) { |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 199 | xnn_qu8_requantize_rndna__scalar_signed64( |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 200 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 201 | } |
| 202 | } |
| 203 | |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 204 | BENCHMARK_F(Requantization, rndna__scalar_unsigned32)(benchmark::State& state) { |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 205 | for (auto _ : state) { |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 206 | xnn_qu8_requantize_rndna__scalar_unsigned32( |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 207 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 208 | } |
| 209 | } |
| 210 | |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 211 | BENCHMARK_F(Requantization, rndna__scalar_unsigned64)(benchmark::State& state) { |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 212 | for (auto _ : state) { |
Marat Dukhan | 0671624 | 2021-05-26 15:56:39 -0700 | [diff] [blame] | 213 | xnn_qu8_requantize_rndna__scalar_unsigned64( |
Marat Dukhan | 9976cd8 | 2021-05-24 23:15:45 -0700 | [diff] [blame] | 214 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 215 | } |
| 216 | } |
| 217 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 218 | |
| 219 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 220 | BENCHMARK_MAIN(); |
| 221 | #endif |