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