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> |
| 10 | #include <random> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <benchmark/benchmark.h> |
| 14 | #include <fp16/fp16.h> |
| 15 | |
| 16 | #include <xnnpack/AlignedAllocator.h> |
| 17 | #include <xnnpack/common.h> |
| 18 | #include <xnnpack/math-stubs.h> |
| 19 | |
| 20 | |
| 21 | static void ExpError(benchmark::State& state, |
| 22 | xnn_f32_ext_unary_math_function extexp, |
| 23 | size_t tile_size) |
| 24 | { |
| 25 | // The smallest x for which exp(x) (double-precision) is normal (-0x1.6232BCp9f). |
| 26 | const uint32_t min_input = 0xC431195E; |
| 27 | // The largest x for which exp(x) (double-precision) is finite (0x1.62E42Ep9). |
| 28 | const uint32_t max_input = 0x44317217; |
| 29 | // Number of tiles in one block of inputs/outputs. Combining multiple tiles in a block reduce function call overhead. |
| 30 | const size_t num_tiles = 100; |
| 31 | |
| 32 | double max_ulp_error = 0.0; |
| 33 | std::vector<float, AlignedAllocator<float, 64>> x(tile_size * num_tiles); |
| 34 | std::vector<float, AlignedAllocator<float, 64>> m(tile_size * num_tiles); |
| 35 | std::vector<float, AlignedAllocator<float, 64>> e(tile_size * num_tiles); |
| 36 | for (auto _ : state) { |
| 37 | for (uint32_t n = min_input; int32_t(n) < 0; n -= tile_size * num_tiles) { |
| 38 | for (uint32_t i = 0; i < tile_size * num_tiles; i++) { |
| 39 | x[i] = fp32_from_bits(std::max<uint32_t>(n - i, 0x80000000)); |
| 40 | } |
| 41 | std::fill(m.begin(), m.end(), std::nanf("")); |
| 42 | std::fill(e.begin(), e.end(), std::nanf("")); |
| 43 | |
| 44 | extexp(tile_size * num_tiles * sizeof(float), x.data(), m.data(), e.data()); |
| 45 | |
| 46 | for (uint32_t i = 0; i < tile_size * num_tiles; i++) { |
| 47 | const double y_ref = std::exp(double(x[i])); |
| 48 | int y_ref_e; |
| 49 | const double y_ref_m = std::frexp(y_ref, &y_ref_e); |
| 50 | const double ulp_error = std::abs(y_ref_m - std::ldexp(double(m[i]), int(e[i]) - y_ref_e)) * 0x1.0p+24; |
| 51 | max_ulp_error = std::max<double>(max_ulp_error, ulp_error); |
| 52 | } |
| 53 | } |
| 54 | for (uint32_t n = 0; n < max_input; n += tile_size * num_tiles) { |
| 55 | for (uint32_t i = 0; i < tile_size * num_tiles; i++) { |
| 56 | x[i] = fp32_from_bits(std::min<uint32_t>(n + i, max_input)); |
| 57 | } |
| 58 | std::fill(m.begin(), m.end(), std::nanf("")); |
| 59 | std::fill(e.begin(), e.end(), std::nanf("")); |
| 60 | |
| 61 | extexp(tile_size * num_tiles * sizeof(float), x.data(), m.data(), e.data()); |
| 62 | |
| 63 | for (uint32_t i = 0; i < tile_size * num_tiles; i++) { |
| 64 | const double y_ref = std::exp(double(x[i])); |
| 65 | int y_ref_e; |
| 66 | const double y_ref_m = std::frexp(y_ref, &y_ref_e); |
| 67 | const double ulp_error = std::abs(y_ref_m - std::ldexp(double(m[i]), int(e[i]) - y_ref_e)) * 0x1.0p+24; |
| 68 | max_ulp_error = std::max<double>(max_ulp_error, ulp_error); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error); |
| 74 | } |
| 75 | |
| 76 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 77 | static void f32_extexp__avx512f_p5(benchmark::State& state) { |
| 78 | ExpError(state, xnn_math_f32_extexp__avx512f_p5, 16); |
| 79 | } |
| 80 | static void f32_extexp__avx2_p5(benchmark::State& state) { |
| 81 | ExpError(state, xnn_math_f32_extexp__avx2_p5, 8); |
| 82 | } |
| 83 | |
| 84 | BENCHMARK(f32_extexp__avx512f_p5)->Unit(benchmark::kMillisecond)->Iterations(1); |
| 85 | BENCHMARK(f32_extexp__avx2_p5)->Unit(benchmark::kMillisecond)->Iterations(1); |
| 86 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 87 | |
| 88 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 89 | BENCHMARK_MAIN(); |
| 90 | #endif |