Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -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 | 515c977 | 2019-10-17 18:07:57 -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 | 515c977 | 2019-10-17 18:07:57 -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 | 515c977 | 2019-10-17 18:07:57 -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) |
| 37 | { |
| 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 | |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 50 | static void ExpError(benchmark::State& state, |
| 51 | xnn_f32_unary_math_function exp, |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 52 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 53 | { |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 54 | if (!cpuinfo_initialize()) { |
| 55 | state.SkipWithError("failed cpuinfo init"); |
| 56 | return; |
| 57 | } |
| 58 | if (isa_check && !isa_check(state)) { |
| 59 | return; |
| 60 | } |
| 61 | |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 62 | // The smallest x for which expf(x) is normalized (-0x1.5D589Ep6f). |
Marat Dukhan | d28a5a2 | 2020-12-14 15:27:22 -0800 | [diff] [blame] | 63 | const uint32_t min_input = UINT32_C(0xC2AEAC4F); |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 64 | // Number of elements in one block of inputs/outputs. |
| 65 | // Combining multiple elements in a block reduce function call overhead. |
| 66 | const size_t block_size = 16384; |
| 67 | // Number of elements in one parallelization tile. Worker threads process this many elements in each task. |
| 68 | const size_t tile_size = 64; |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 69 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 70 | uint32_t num_threads = cpuinfo_get_cores_count(); |
| 71 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 72 | // Use all cores except for the least performant cluster |
| 73 | if (cpuinfo_get_clusters_count() > 1) { |
| 74 | num_threads -= cpuinfo_get_cluster(cpuinfo_get_clusters_count() - 1)->core_count; |
| 75 | } |
| 76 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 77 | |
| 78 | std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool( |
| 79 | pthreadpool_create(num_threads), pthreadpool_destroy); |
| 80 | |
| 81 | std::vector<float, AlignedAllocator<float, 64>> x(block_size); |
| 82 | std::vector<float, AlignedAllocator<float, 64>> y(block_size); |
| 83 | std::vector<float> ulp_error(block_size); |
| 84 | float max_ulp_error = 0.0f; |
| 85 | |
| 86 | ComputeErrorContext context; |
| 87 | context.input = x.data(); |
| 88 | context.output = y.data(); |
| 89 | context.error = ulp_error.data(); |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 90 | for (auto _ : state) { |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 91 | for (uint32_t n = min_input; int32_t(n) < 0; n -= block_size) { |
| 92 | for (uint32_t i = 0; i < block_size; i++) { |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 93 | x[i] = fp32_from_bits(std::max<uint32_t>(n - i, 0x80000000)); |
| 94 | } |
| 95 | std::fill(y.begin(), y.end(), std::nanf("")); |
| 96 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 97 | exp(block_size * sizeof(float), x.data(), y.data()); |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 98 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 99 | pthreadpool_parallelize_1d_tile_1d( |
| 100 | threadpool.get(), |
| 101 | reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError), |
| 102 | static_cast<void*>(&context), |
| 103 | block_size, tile_size, 0 /* flags */); |
| 104 | |
| 105 | max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error, |
| 106 | static_cast<const float& (*)(const float&, const float&)>(std::max<float>)); |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error); |
| 111 | } |
| 112 | |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 113 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
Marat Dukhan | d28a5a2 | 2020-12-14 15:27:22 -0800 | [diff] [blame] | 114 | BENCHMARK_CAPTURE(ExpError, neonfma_rr2_lut64_p2, |
| 115 | xnn_math_f32_expminus__neonfma_rr2_lut64_p2, |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 116 | benchmark::utils::CheckNEONFMA) |
| 117 | ->Unit(benchmark::kMillisecond) |
| 118 | ->Iterations(1); |
Marat Dukhan | d28a5a2 | 2020-12-14 15:27:22 -0800 | [diff] [blame] | 119 | BENCHMARK_CAPTURE(ExpError, neonfma_rr2_lut2048_p1, |
| 120 | xnn_math_f32_expminus__neonfma_rr2_lut2048_p1, |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 121 | benchmark::utils::CheckNEONFMA) |
| 122 | ->Unit(benchmark::kMillisecond) |
| 123 | ->Iterations(1); |
| 124 | BENCHMARK_CAPTURE(ExpError, neonfma_rr2_p5, |
| 125 | xnn_math_f32_expminus__neonfma_rr2_p5, |
| 126 | benchmark::utils::CheckNEONFMA) |
| 127 | ->Unit(benchmark::kMillisecond) |
| 128 | ->Iterations(1); |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 129 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 130 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 131 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
Marat Dukhan | 5999c92 | 2022-01-05 18:10:20 -0800 | [diff] [blame] | 132 | BENCHMARK_CAPTURE(ExpError, avx2_rr1_p5, |
| 133 | xnn_math_f32_expminus__avx2_rr1_p5, |
| 134 | benchmark::utils::CheckAVX2) |
| 135 | ->Unit(benchmark::kMillisecond) |
| 136 | ->Iterations(1); |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 137 | BENCHMARK_CAPTURE(ExpError, avx2_rr2_p5, |
| 138 | xnn_math_f32_expminus__avx2_rr2_p5, |
| 139 | benchmark::utils::CheckAVX2) |
| 140 | ->Unit(benchmark::kMillisecond) |
| 141 | ->Iterations(1); |
Marat Dukhan | 5e9a91e | 2019-12-22 19:13:03 -0800 | [diff] [blame] | 142 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 143 | BENCHMARK_CAPTURE(ExpError, sse2_rr2_p5, |
| 144 | xnn_math_f32_expminus__sse2_rr2_p5, |
| 145 | benchmark::utils::CheckAVX2) |
| 146 | ->Unit(benchmark::kMillisecond) |
| 147 | ->Iterations(1); |
| 148 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 149 | |
| 150 | BENCHMARK_CAPTURE(ExpError, scalar_rr2_lut64_p2, |
| 151 | xnn_math_f32_expminus__scalar_rr2_lut64_p2) |
| 152 | ->Unit(benchmark::kMillisecond) |
| 153 | ->Iterations(1); |
| 154 | BENCHMARK_CAPTURE(ExpError, scalar_rr2_lut2048_p1, |
| 155 | xnn_math_f32_expminus__scalar_rr2_lut2048_p1) |
| 156 | ->Unit(benchmark::kMillisecond) |
| 157 | ->Iterations(1); |
| 158 | BENCHMARK_CAPTURE(ExpError, scalar_rr2_p5, |
| 159 | xnn_math_f32_expminus__scalar_rr2_p5) |
| 160 | ->Unit(benchmark::kMillisecond) |
| 161 | ->Iterations(1); |
Marat Dukhan | 5e9a91e | 2019-12-22 19:13:03 -0800 | [diff] [blame] | 162 | |
Marat Dukhan | 515c977 | 2019-10-17 18:07:57 -0700 | [diff] [blame] | 163 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 164 | BENCHMARK_MAIN(); |
| 165 | #endif |