Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 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 | |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 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 | |
| 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 | { |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 26 | if (isa_check && !isa_check(state)) { |
| 27 | return; |
| 28 | } |
| 29 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 30 | const size_t elements = state.range(0); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 31 | |
| 32 | std::random_device random_device; |
| 33 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 34 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng)); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 35 | auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng); |
| 36 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 37 | std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> x(elements); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 38 | std::generate(x.begin(), x.end(), std::ref(f16rng)); |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 39 | std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> y(elements); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 40 | std::generate(x.begin(), x.end(), std::ref(f16rng)); |
| 41 | |
| 42 | for (auto _ : state) { |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 43 | f16_relu(elements * sizeof(uint16_t), x.data(), y.data(), NULL); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 46 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 47 | if (cpu_frequency != 0) { |
| 48 | state.counters["cpufreq"] = cpu_frequency; |
| 49 | } |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 50 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 51 | const size_t elements_per_iteration = elements; |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 52 | state.counters["elements"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 53 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 54 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 55 | const size_t bytes_per_iteration = 2 * elements * sizeof(uint16_t); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 56 | state.counters["bytes"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 57 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
Frank Barchard | c201006 | 2020-07-19 10:04:12 -0700 | [diff] [blame] | 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 |