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