1 // Copyright 2020 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 <cmath>
8 #include <functional>
9 #include <random>
10 #include <vector>
11
12 #include <benchmark/benchmark.h>
13 #include <fp16/fp16.h>
14 #include "bench/utils.h"
15 #include <xnnpack/AlignedAllocator.h>
16 #include <xnnpack/common.h>
17 #include <xnnpack/params.h>
18 #include <xnnpack/vunary.h>
19
20
f16_relu(benchmark::State & state,xnn_f16_relu_ukernel_function f16_relu,benchmark::utils::IsaCheckFunction isa_check=nullptr)21 static void f16_relu(
22 benchmark::State& state,
23 xnn_f16_relu_ukernel_function f16_relu,
24 benchmark::utils::IsaCheckFunction isa_check = nullptr)
25 {
26 if (isa_check && !isa_check(state)) {
27 return;
28 }
29
30 const size_t elements = state.range(0);
31
32 std::random_device random_device;
33 auto rng = std::mt19937(random_device());
34 auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
35 auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
36
37 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> x(elements);
38 std::generate(x.begin(), x.end(), std::ref(f16rng));
39 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> y(elements);
40 std::generate(x.begin(), x.end(), std::ref(f16rng));
41
42 for (auto _ : state) {
43 f16_relu(elements * sizeof(uint16_t), x.data(), y.data(), NULL);
44 }
45
46 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
47 if (cpu_frequency != 0) {
48 state.counters["cpufreq"] = cpu_frequency;
49 }
50
51 const size_t elements_per_iteration = elements;
52 state.counters["elements"] =
53 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
54
55 const size_t bytes_per_iteration = 2 * elements * sizeof(uint16_t);
56 state.counters["bytes"] =
57 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
58 }
59
60 #if XNN_ARCH_ARM64
61 BENCHMARK_CAPTURE(f16_relu, neonfp16arith_x8, xnn_f16_relu_ukernel__neonfp16arith_x8, benchmark::utils::CheckNEONFP16ARITH)
62 ->RangeMultiplier(10)
63 ->Range(1000, 100000000)
64 ->UseRealTime();
65 BENCHMARK_CAPTURE(f16_relu, neonfp16arith_x16, xnn_f16_relu_ukernel__neonfp16arith_x16, benchmark::utils::CheckNEONFP16ARITH)
66 ->RangeMultiplier(10)
67 ->Range(1000, 100000000)
68 ->UseRealTime();
69 #endif // XNN_ARCH_ARM64
70
71
72 #ifndef XNNPACK_BENCHMARK_NO_MAIN
73 BENCHMARK_MAIN();
74 #endif
75