blob: 9b7d562166d2ab9fa955e457e7be6125049a5047 [file] [log] [blame]
Marat Dukhan98ba4412019-10-23 02:14:28 -07001// 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 Dukhanced880d2020-12-13 17:52:27 -080010#include <memory>
Marat Dukhan3a305212020-12-06 19:24:27 -080011#include <numeric>
Marat Dukhan98ba4412019-10-23 02:14:28 -070012#include <random>
13#include <vector>
14
Marat Dukhan3a305212020-12-06 19:24:27 -080015#include <cpuinfo.h>
16#include <pthreadpool.h>
17
Marat Dukhan98ba4412019-10-23 02:14:28 -070018#include <benchmark/benchmark.h>
19#include <fp16/fp16.h>
20
Marat Dukhan3a305212020-12-06 19:24:27 -080021#include "bench/utils.h"
Marat Dukhan98ba4412019-10-23 02:14:28 -070022#include <xnnpack/AlignedAllocator.h>
23#include <xnnpack/common.h>
24#include <xnnpack/math-stubs.h>
25
26
Marat Dukhan3a305212020-12-06 19:24:27 -080027struct ComputeErrorContext {
28 const float* input;
29 const float* output_m;
30 const float* output_e;
31 float* error;
32};
33
34static void ComputeError(
35 struct ComputeErrorContext* context,
36 size_t start,
37 size_t range)
Marat Dukhan98ba4412019-10-23 02:14:28 -070038{
Marat Dukhan3a305212020-12-06 19:24:27 -080039 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
53static 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 Dukhan98ba4412019-10-23 02:14:28 -070065 // 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 Dukhan3a305212020-12-06 19:24:27 -080069 // 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 Dukhan98ba4412019-10-23 02:14:28 -070074
Marat Dukhan3a305212020-12-06 19:24:27 -080075 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 Dukhan98ba4412019-10-23 02:14:28 -070097 for (auto _ : state) {
Marat Dukhan3a305212020-12-06 19:24:27 -080098 for (uint32_t n = min_input; int32_t(n) < 0; n -= block_size) {
99 for (uint32_t i = 0; i < block_size; i++) {
Marat Dukhan98ba4412019-10-23 02:14:28 -0700100 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 Dukhan3a305212020-12-06 19:24:27 -0800105 extexp(block_size * sizeof(float), x.data(), m.data(), e.data());
Marat Dukhan98ba4412019-10-23 02:14:28 -0700106
Marat Dukhan3a305212020-12-06 19:24:27 -0800107 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 Dukhan98ba4412019-10-23 02:14:28 -0700115 }
Marat Dukhan3a305212020-12-06 19:24:27 -0800116 for (uint32_t n = 0; n < max_input; n += block_size) {
117 for (uint32_t i = 0; i < block_size; i++) {
Marat Dukhan98ba4412019-10-23 02:14:28 -0700118 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 Dukhan3a305212020-12-06 19:24:27 -0800123 extexp(block_size * sizeof(float), x.data(), m.data(), e.data());
Marat Dukhan98ba4412019-10-23 02:14:28 -0700124
Marat Dukhan3a305212020-12-06 19:24:27 -0800125 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 Dukhan98ba4412019-10-23 02:14:28 -0700133 }
134 }
135
136 state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error);
137}
138
139#if XNN_ARCH_X86 || XNN_ARCH_X86_64
Marat Dukhan3a305212020-12-06 19:24:27 -0800140 BENCHMARK_CAPTURE(ExtExpError, avx512f_p5,
141 xnn_math_f32_extexp__avx512f_p5,
142 benchmark::utils::CheckAVX512F)
143 ->Unit(benchmark::kMillisecond)
144 ->Iterations(1);
Marat Dukhan98ba4412019-10-23 02:14:28 -0700145
Marat Dukhan3a305212020-12-06 19:24:27 -0800146 BENCHMARK_CAPTURE(ExtExpError, avx2_p5,
147 xnn_math_f32_extexp__avx2_p5,
148 benchmark::utils::CheckAVX2)
149 ->Unit(benchmark::kMillisecond)
150 ->Iterations(1);
Marat Dukhan98ba4412019-10-23 02:14:28 -0700151#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
152
153#ifndef XNNPACK_BENCHMARK_NO_MAIN
154BENCHMARK_MAIN();
155#endif