Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cfloat> |
| 8 | #include <cmath> |
| 9 | #include <functional> |
Marat Dukhan | ced880d | 2020-12-13 17:52:27 -0800 | [diff] [blame] | 10 | #include <memory> |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 11 | #include <numeric> |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 12 | #include <random> |
| 13 | #include <vector> |
| 14 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 15 | #include <cpuinfo.h> |
| 16 | #include <pthreadpool.h> |
| 17 | |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 18 | #include <benchmark/benchmark.h> |
| 19 | #include <fp16/fp16.h> |
| 20 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 21 | #include "bench/utils.h" |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 22 | #include <xnnpack/AlignedAllocator.h> |
| 23 | #include <xnnpack/common.h> |
| 24 | #include <xnnpack/math-stubs.h> |
| 25 | |
| 26 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 27 | struct ComputeErrorContext { |
| 28 | const float* input; |
| 29 | const float* output; |
| 30 | float* error; |
| 31 | }; |
| 32 | |
| 33 | static void ComputeError( |
| 34 | struct ComputeErrorContext* context, |
| 35 | size_t start, |
| 36 | size_t range) |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 37 | { |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 38 | const float* input = context->input; |
| 39 | const float* output = context->output; |
| 40 | float* error = context->error; |
| 41 | for (size_t i = start; i < start + range; i++) { |
| 42 | const double output_ref = std::exp(double(input[i])); |
| 43 | const double abs_error = std::abs(output_ref - double(output[i])); |
| 44 | const float output_abs = std::abs(output_ref); |
| 45 | const float output_ulp = fp32_from_bits(fp32_to_bits(output_abs) + 1) - output_abs; |
| 46 | error[i] = float(abs_error / output_ulp); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void ExpError( |
| 51 | benchmark::State& state, |
| 52 | xnn_f32_unary_math_function exp, |
| 53 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
| 54 | { |
| 55 | if (!cpuinfo_initialize()) { |
| 56 | state.SkipWithError("failed cpuinfo init"); |
| 57 | return; |
| 58 | } |
| 59 | if (isa_check && !isa_check(state)) { |
| 60 | return; |
| 61 | } |
| 62 | |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 63 | // The smallest x for which expf(x) is non-zero (-0x1.9FE368p+6f). |
| 64 | const uint32_t min_input = 0xC2CFF1B4; |
| 65 | // The largest x for which expf(x) is finite (0x1.62E42Ep6f). |
| 66 | const uint32_t max_input = 0x42B17217; |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 67 | // Number of elements in one block of inputs/outputs. |
| 68 | // Combining multiple elements in a block reduce function call overhead. |
| 69 | const size_t block_size = 16384; |
| 70 | // Number of elements in one parallelization tile. Worker threads process this many elements in each task. |
| 71 | const size_t tile_size = 64; |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 72 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 73 | uint32_t num_threads = cpuinfo_get_cores_count(); |
| 74 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 75 | // Use all cores except for the least performant cluster |
| 76 | if (cpuinfo_get_clusters_count() > 1) { |
| 77 | num_threads -= cpuinfo_get_cluster(cpuinfo_get_clusters_count() - 1)->core_count; |
| 78 | } |
| 79 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 80 | |
| 81 | std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool( |
| 82 | pthreadpool_create(num_threads), pthreadpool_destroy); |
| 83 | |
| 84 | std::vector<float, AlignedAllocator<float, 64>> x(block_size); |
| 85 | std::vector<float, AlignedAllocator<float, 64>> y(block_size); |
| 86 | std::vector<float> ulp_error(block_size); |
| 87 | float max_ulp_error = 0.0f; |
| 88 | |
| 89 | ComputeErrorContext context; |
| 90 | context.input = x.data(); |
| 91 | context.output = y.data(); |
| 92 | context.error = ulp_error.data(); |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 93 | for (auto _ : state) { |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 94 | for (uint32_t n = min_input; int32_t(n) < 0; n -= block_size) { |
| 95 | for (uint32_t i = 0; i < block_size; i++) { |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 96 | x[i] = fp32_from_bits(std::max<uint32_t>(n - i, 0x80000000)); |
| 97 | } |
| 98 | std::fill(y.begin(), y.end(), std::nanf("")); |
| 99 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 100 | exp(block_size * sizeof(float), x.data(), y.data()); |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 101 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 102 | pthreadpool_parallelize_1d_tile_1d( |
| 103 | threadpool.get(), |
| 104 | reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError), |
| 105 | static_cast<void*>(&context), |
| 106 | block_size, tile_size, 0 /* flags */); |
| 107 | |
| 108 | max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error, |
| 109 | static_cast<const float& (*)(const float&, const float&)>(std::max<float>)); |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 110 | } |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 111 | for (uint32_t n = 0; n < max_input; n += block_size) { |
| 112 | for (uint32_t i = 0; i < block_size; i++) { |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 113 | x[i] = fp32_from_bits(std::min<uint32_t>(n + i, max_input)); |
| 114 | } |
| 115 | std::fill(y.begin(), y.end(), std::nanf("")); |
| 116 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 117 | exp(block_size * sizeof(float), x.data(), y.data()); |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 118 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 119 | pthreadpool_parallelize_1d_tile_1d( |
| 120 | threadpool.get(), |
| 121 | reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError), |
| 122 | static_cast<void*>(&context), |
| 123 | block_size, tile_size, 0 /* flags */); |
| 124 | |
| 125 | max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error, |
| 126 | static_cast<const float& (*)(const float&, const float&)>(std::max<float>)); |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error); |
| 131 | } |
| 132 | |
Marat Dukhan | 797a8fe | 2019-11-14 20:21:57 -0800 | [diff] [blame] | 133 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 134 | BENCHMARK_CAPTURE(ExpError, neonfma_rr2_lut64_p2, |
| 135 | xnn_math_f32_exp__neonfma_rr2_lut64_p2, |
| 136 | benchmark::utils::CheckNEONFMA) |
| 137 | ->Unit(benchmark::kMillisecond) |
| 138 | ->Iterations(1); |
| 139 | BENCHMARK_CAPTURE(ExpError, neonfma_rr2_p5, |
| 140 | xnn_math_f32_exp__neonfma_rr2_p5, |
| 141 | benchmark::utils::CheckNEONFMA) |
| 142 | ->Unit(benchmark::kMillisecond) |
| 143 | ->Iterations(1); |
Marat Dukhan | 797a8fe | 2019-11-14 20:21:57 -0800 | [diff] [blame] | 144 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 145 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 146 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 147 | BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut16_p3_perm, |
| 148 | xnn_math_f32_exp__avx512f_rr2_lut16_p3_perm, |
| 149 | benchmark::utils::CheckAVX512F) |
| 150 | ->Unit(benchmark::kMillisecond) |
| 151 | ->Iterations(1); |
| 152 | BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut16_p3_perm_scalef, |
| 153 | xnn_math_f32_exp__avx512f_rr2_lut16_p3_perm_scalef, |
| 154 | benchmark::utils::CheckAVX512F) |
| 155 | ->Unit(benchmark::kMillisecond) |
| 156 | ->Iterations(1); |
| 157 | BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut32_p2_perm2, |
| 158 | xnn_math_f32_exp__avx512f_rr2_lut32_p2_perm2, |
| 159 | benchmark::utils::CheckAVX512F) |
| 160 | ->Unit(benchmark::kMillisecond) |
| 161 | ->Iterations(1); |
| 162 | BENCHMARK_CAPTURE(ExpError, avx512f_rr2_lut32_p2_perm2_scalef, |
| 163 | xnn_math_f32_exp__avx512f_rr2_lut32_p2_perm2_scalef, |
| 164 | benchmark::utils::CheckAVX512F) |
| 165 | ->Unit(benchmark::kMillisecond) |
| 166 | ->Iterations(1); |
| 167 | BENCHMARK_CAPTURE(ExpError, avx512f_rr2_p5, |
| 168 | xnn_math_f32_exp__avx512f_rr2_p5, |
| 169 | benchmark::utils::CheckAVX512F) |
| 170 | ->Unit(benchmark::kMillisecond) |
| 171 | ->Iterations(1); |
| 172 | BENCHMARK_CAPTURE(ExpError, avx512f_rr2_p5_scalef, |
| 173 | xnn_math_f32_exp__avx512f_rr2_p5_scalef, |
| 174 | benchmark::utils::CheckAVX512F) |
| 175 | ->Unit(benchmark::kMillisecond) |
| 176 | ->Iterations(1); |
| 177 | |
| 178 | BENCHMARK_CAPTURE(ExpError, avx2_rr2_lut8_p3_perm, |
| 179 | xnn_math_f32_exp__avx2_rr2_lut8_p3_perm, |
| 180 | benchmark::utils::CheckAVX2) |
| 181 | ->Unit(benchmark::kMillisecond) |
| 182 | ->Iterations(1); |
| 183 | BENCHMARK_CAPTURE(ExpError, avx2_rr2_lut8_p4_perm, |
| 184 | xnn_math_f32_exp__avx2_rr2_lut8_p4_perm, |
| 185 | benchmark::utils::CheckAVX2) |
| 186 | ->Unit(benchmark::kMillisecond) |
| 187 | ->Iterations(1); |
| 188 | BENCHMARK_CAPTURE(ExpError, avx2_rr2_p5, |
| 189 | xnn_math_f32_exp__avx2_rr2_p5, |
| 190 | benchmark::utils::CheckAVX2) |
| 191 | ->Unit(benchmark::kMillisecond) |
| 192 | ->Iterations(1); |
| 193 | |
| 194 | BENCHMARK_CAPTURE(ExpError, avx_rr2_p5, |
| 195 | xnn_math_f32_exp__avx_rr2_p5, |
| 196 | benchmark::utils::CheckAVX) |
| 197 | ->Unit(benchmark::kMillisecond) |
| 198 | ->Iterations(1); |
| 199 | |
| 200 | BENCHMARK_CAPTURE(ExpError, sse2_rr2_lut64_p2, |
| 201 | xnn_math_f32_exp__sse2_rr2_lut64_p2) |
| 202 | ->Unit(benchmark::kMillisecond) |
| 203 | ->Iterations(1); |
| 204 | BENCHMARK_CAPTURE(ExpError, sse2_rr2_p5, |
| 205 | xnn_math_f32_exp__sse2_rr2_p5) |
| 206 | ->Unit(benchmark::kMillisecond) |
| 207 | ->Iterations(1); |
| 208 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 209 | |
Marat Dukhan | 6adff4e | 2019-10-14 18:32:07 -0700 | [diff] [blame] | 210 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 211 | BENCHMARK_MAIN(); |
| 212 | #endif |