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 | |
| 25 | inline uint32_t divide_round_up(uint32_t x, uint32_t q) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 26 | return x / q + uint32_t(x % q != 0); |
| 27 | } |
| 28 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 29 | inline uint32_t round_up(uint32_t x, uint32_t q) { |
| 30 | return q * divide_round_up(x, q); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 33 | inline uint32_t min(uint32_t a, uint32_t b) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 34 | return a < b ? a : b; |
| 35 | } |
| 36 | |
| 37 | class Requantization : public benchmark::Fixture { |
| 38 | public: |
| 39 | inline Requantization() |
| 40 | { |
| 41 | cpuinfo_initialize(); |
| 42 | const size_t l1d_size = cpuinfo_get_l1d_cache(0)->size; |
| 43 | const size_t l1d_reserve = 1024; |
| 44 | n_ = (l1d_size - l1d_reserve) / (sizeof(int32_t) + sizeof(uint8_t)); |
| 45 | n_ = n_ / 16 * 16; |
| 46 | } |
| 47 | |
| 48 | virtual void SetUp(const benchmark::State&) override |
| 49 | { |
| 50 | std::random_device random_device; |
| 51 | auto rng = std::mt19937(random_device()); |
| 52 | auto s32rng = std::bind(std::uniform_int_distribution<int32_t>(), rng); |
| 53 | |
| 54 | input_.resize(n()); |
| 55 | std::generate(input_.begin(), input_.end(), std::ref(s32rng)); |
| 56 | output_.resize(n()); |
| 57 | std::fill(output_.begin(), output_.end(), 0xA5); |
| 58 | } |
| 59 | |
| 60 | virtual void TearDown(benchmark::State& state) override |
| 61 | { |
| 62 | state.SetItemsProcessed(uint64_t(state.iterations()) * n()); |
| 63 | state.SetBytesProcessed(uint64_t(state.iterations()) * n() * (sizeof(int32_t) + sizeof(uint8_t))); |
| 64 | input_.clear(); |
| 65 | output_.clear(); |
| 66 | } |
| 67 | |
| 68 | inline const int32_t* input() const |
| 69 | { |
| 70 | return input_.data(); |
| 71 | } |
| 72 | |
| 73 | inline uint8_t* output() |
| 74 | { |
| 75 | return output_.data(); |
| 76 | } |
| 77 | |
| 78 | inline size_t n() const |
| 79 | { |
| 80 | return n_; |
| 81 | } |
| 82 | |
| 83 | protected: |
| 84 | std::vector<int32_t, AlignedAllocator<int32_t, 32>> input_; |
| 85 | std::vector<uint8_t> output_; |
| 86 | size_t n_; |
| 87 | }; |
| 88 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 89 | BENCHMARK_F(Requantization, precise__scalar_unsigned32)(benchmark::State& state) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 90 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 91 | xnn_qu8_requantize_precise__scalar_unsigned32( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 92 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 93 | } |
| 94 | } |
| 95 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 96 | BENCHMARK_F(Requantization, precise__scalar_unsigned64)(benchmark::State& state) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 97 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 98 | xnn_qu8_requantize_precise__scalar_unsigned64( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 99 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 100 | } |
| 101 | } |
| 102 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 103 | BENCHMARK_F(Requantization, precise__scalar_signed64)(benchmark::State& state) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 104 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 105 | xnn_qu8_requantize_precise__scalar_signed64( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 106 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 107 | } |
| 108 | } |
| 109 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 110 | BENCHMARK_F(Requantization, fp32__scalar_lrintf)(benchmark::State& state) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 111 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 112 | xnn_qu8_requantize_fp32__scalar_lrintf( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 113 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 114 | } |
| 115 | } |
| 116 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 117 | BENCHMARK_F(Requantization, fp32__scalar_magic)(benchmark::State& state) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 118 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 119 | xnn_qu8_requantize_fp32__scalar_magic( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 120 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 121 | } |
| 122 | } |
| 123 | |
Marat Dukhan | a199d49 | 2020-07-24 15:01:25 -0700 | [diff] [blame] | 124 | #if !XNN_ARCH_WASM && !XNN_COMPILER_MSVC && !XNN_COMPILER_ICC |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 125 | BENCHMARK_F(Requantization, precise__psimd)(benchmark::State& state) { |
| 126 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 127 | xnn_qu8_requantize_precise__psimd( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 128 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 129 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 130 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 131 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 132 | BENCHMARK_F(Requantization, fp32__psimd)(benchmark::State& state) { |
| 133 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 134 | xnn_qu8_requantize_fp32__psimd( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 135 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 136 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 137 | } |
Marat Dukhan | a199d49 | 2020-07-24 15:01:25 -0700 | [diff] [blame] | 138 | #endif // !XNN_ARCH_WASM && !XNN_COMPILER_MSVC && !XNN_COMPILER_ICC |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 139 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 140 | |
Marat Dukhan | 290055c | 2020-06-09 12:24:29 -0700 | [diff] [blame] | 141 | #if XNN_ARCH_WASMSIMD |
| 142 | BENCHMARK_F(Requantization, fp32__wasmsimd)(benchmark::State& state) { |
| 143 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 144 | xnn_qu8_requantize_fp32__wasmsimd( |
Marat Dukhan | 290055c | 2020-06-09 12:24:29 -0700 | [diff] [blame] | 145 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 146 | } |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 151 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 152 | BENCHMARK_F(Requantization, precise__neon)(benchmark::State& state) { |
| 153 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 154 | xnn_qu8_requantize_precise__neon( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 155 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 156 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 157 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 158 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 159 | BENCHMARK_F(Requantization, fp32__neon)(benchmark::State& state) { |
| 160 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 161 | xnn_qu8_requantize_fp32__neon( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 162 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 163 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 164 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 165 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 166 | BENCHMARK_F(Requantization, q31__neon)(benchmark::State& state) { |
| 167 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 168 | xnn_qu8_requantize_q31__neon( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 169 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 170 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 171 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 172 | #endif |
| 173 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 174 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 175 | BENCHMARK_F(Requantization, precise__sse2)(benchmark::State& state) { |
| 176 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 177 | xnn_qu8_requantize_precise__sse2( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 178 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 179 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 180 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 181 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 182 | BENCHMARK_F(Requantization, precise__ssse3)(benchmark::State& state) { |
| 183 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 184 | xnn_qu8_requantize_precise__ssse3( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 185 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 186 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 187 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 188 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 189 | BENCHMARK_F(Requantization, precise__sse4)(benchmark::State& state) { |
| 190 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 191 | xnn_qu8_requantize_precise__sse4( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 192 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 193 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 194 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 195 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 196 | BENCHMARK_F(Requantization, fp32__sse2)(benchmark::State& state) { |
| 197 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 198 | xnn_qu8_requantize_fp32__sse2( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 199 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 200 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 201 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 202 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 203 | BENCHMARK_F(Requantization, q31__sse2)(benchmark::State& state) { |
| 204 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 205 | xnn_qu8_requantize_q31__sse2( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 206 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 207 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 208 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 209 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 210 | BENCHMARK_F(Requantization, q31__ssse3)(benchmark::State& state) { |
| 211 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 212 | xnn_qu8_requantize_q31__ssse3( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 213 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 214 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 215 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 216 | |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 217 | BENCHMARK_F(Requantization, q31__sse4)(benchmark::State& state) { |
| 218 | for (auto _ : state) { |
Marat Dukhan | 5b69f8b | 2020-07-24 15:26:48 -0700 | [diff] [blame] | 219 | xnn_qu8_requantize_q31__sse4( |
Marat Dukhan | fe7acb6 | 2020-03-09 19:30:05 -0700 | [diff] [blame] | 220 | n(), input(), 0x1.0p-12f /* scale */, 128 /* zero point */, 1 /* qmin */, 254 /* qmax */, output()); |
| 221 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 222 | } |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 223 | #endif |
| 224 | |
| 225 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 226 | BENCHMARK_MAIN(); |
| 227 | #endif |