Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -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 | 98ba441 | 2019-10-23 02:14:28 -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 | 98ba441 | 2019-10-23 02:14:28 -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 | 98ba441 | 2019-10-23 02:14:28 -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_m; |
| 30 | const float* output_e; |
| 31 | float* error; |
| 32 | }; |
| 33 | |
| 34 | static void ComputeError( |
| 35 | struct ComputeErrorContext* context, |
| 36 | size_t start, |
| 37 | size_t range) |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 38 | { |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 39 | const float* input = context->input; |
| 40 | const float* output_m = context->output_m; |
| 41 | const float* output_e = context->output_e; |
| 42 | float* error = context->error; |
| 43 | const double inv_ulp = 0x1.0p+24; |
| 44 | for (size_t i = start; i < start + range; i++) { |
| 45 | const double output_ref = std::exp(double(input[i])); |
| 46 | int output_ref_e; |
| 47 | const double output_ref_m = std::frexp(output_ref, &output_ref_e); |
| 48 | const double ulp_error = std::abs(output_ref_m - std::ldexp(double(output_m[i]), int(output_e[i]) - output_ref_e)) * inv_ulp; |
| 49 | error[i] = float(ulp_error); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static void ExtExpError(benchmark::State& state, |
| 54 | xnn_f32_ext_unary_math_function extexp, |
| 55 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
| 56 | { |
| 57 | if (!cpuinfo_initialize()) { |
| 58 | state.SkipWithError("failed cpuinfo init"); |
| 59 | return; |
| 60 | } |
| 61 | if (isa_check && !isa_check(state)) { |
| 62 | return; |
| 63 | } |
| 64 | |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 65 | // The smallest x for which exp(x) (double-precision) is normal (-0x1.6232BCp9f). |
| 66 | const uint32_t min_input = 0xC431195E; |
| 67 | // The largest x for which exp(x) (double-precision) is finite (0x1.62E42Ep9). |
| 68 | const uint32_t max_input = 0x44317217; |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 69 | // Number of elements in one block of inputs/outputs. |
| 70 | // Combining multiple elements in a block reduce function call overhead. |
| 71 | const size_t block_size = 16384; |
| 72 | // Number of elements in one parallelization tile. Worker threads process this many elements in each task. |
| 73 | const size_t tile_size = 64; |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 74 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 75 | uint32_t num_threads = cpuinfo_get_cores_count(); |
| 76 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 77 | // Use all cores except for the least performant cluster |
| 78 | if (cpuinfo_get_clusters_count() > 1) { |
| 79 | num_threads -= cpuinfo_get_cluster(cpuinfo_get_clusters_count() - 1)->core_count; |
| 80 | } |
| 81 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 82 | |
| 83 | std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool( |
| 84 | pthreadpool_create(num_threads), pthreadpool_destroy); |
| 85 | |
| 86 | std::vector<float, AlignedAllocator<float, 64>> x(block_size); |
| 87 | std::vector<float, AlignedAllocator<float, 64>> m(block_size); |
| 88 | std::vector<float, AlignedAllocator<float, 64>> e(block_size); |
| 89 | std::vector<float> ulp_error(block_size); |
| 90 | float max_ulp_error = 0.0f; |
| 91 | |
| 92 | ComputeErrorContext context; |
| 93 | context.input = x.data(); |
| 94 | context.output_m = m.data(); |
| 95 | context.output_e = e.data(); |
| 96 | context.error = ulp_error.data(); |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 97 | for (auto _ : state) { |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 98 | for (uint32_t n = min_input; int32_t(n) < 0; n -= block_size) { |
| 99 | for (uint32_t i = 0; i < block_size; i++) { |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 100 | x[i] = fp32_from_bits(std::max<uint32_t>(n - i, 0x80000000)); |
| 101 | } |
| 102 | std::fill(m.begin(), m.end(), std::nanf("")); |
| 103 | std::fill(e.begin(), e.end(), std::nanf("")); |
| 104 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 105 | extexp(block_size * sizeof(float), x.data(), m.data(), e.data()); |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 106 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 107 | pthreadpool_parallelize_1d_tile_1d( |
| 108 | threadpool.get(), |
| 109 | reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError), |
| 110 | static_cast<void*>(&context), |
| 111 | block_size, tile_size, 0 /* flags */); |
| 112 | |
| 113 | max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error, |
| 114 | static_cast<const float& (*)(const float&, const float&)>(std::max<float>)); |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 115 | } |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 116 | for (uint32_t n = 0; n < max_input; n += block_size) { |
| 117 | for (uint32_t i = 0; i < block_size; i++) { |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 118 | x[i] = fp32_from_bits(std::min<uint32_t>(n + i, max_input)); |
| 119 | } |
| 120 | std::fill(m.begin(), m.end(), std::nanf("")); |
| 121 | std::fill(e.begin(), e.end(), std::nanf("")); |
| 122 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 123 | extexp(block_size * sizeof(float), x.data(), m.data(), e.data()); |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 124 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 125 | pthreadpool_parallelize_1d_tile_1d( |
| 126 | threadpool.get(), |
| 127 | reinterpret_cast<pthreadpool_task_1d_tile_1d_t>(ComputeError), |
| 128 | static_cast<void*>(&context), |
| 129 | block_size, tile_size, 0 /* flags */); |
| 130 | |
| 131 | max_ulp_error = std::accumulate(ulp_error.cbegin(), ulp_error.cend(), max_ulp_error, |
| 132 | static_cast<const float& (*)(const float&, const float&)>(std::max<float>)); |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error); |
| 137 | } |
| 138 | |
| 139 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 140 | BENCHMARK_CAPTURE(ExtExpError, avx512f_p5, |
| 141 | xnn_math_f32_extexp__avx512f_p5, |
| 142 | benchmark::utils::CheckAVX512F) |
| 143 | ->Unit(benchmark::kMillisecond) |
| 144 | ->Iterations(1); |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 145 | |
Marat Dukhan | 3a30521 | 2020-12-06 19:24:27 -0800 | [diff] [blame] | 146 | BENCHMARK_CAPTURE(ExtExpError, avx2_p5, |
| 147 | xnn_math_f32_extexp__avx2_p5, |
| 148 | benchmark::utils::CheckAVX2) |
| 149 | ->Unit(benchmark::kMillisecond) |
| 150 | ->Iterations(1); |
Marat Dukhan | 98ba441 | 2019-10-23 02:14:28 -0700 | [diff] [blame] | 151 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 152 | |
| 153 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 154 | BENCHMARK_MAIN(); |
| 155 | #endif |