Frank Barchard | 3c4a952 | 2020-07-18 16:11:22 -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 | |
| 12 | #include <benchmark/benchmark.h> |
| 13 | #include "bench/utils.h" |
| 14 | #include <xnnpack/AlignedAllocator.h> |
| 15 | #include <xnnpack/common.h> |
| 16 | #include <xnnpack/params.h> |
| 17 | #include <xnnpack/vunary.h> |
| 18 | |
| 19 | |
| 20 | static void f32_relu( |
| 21 | benchmark::State& state, |
| 22 | xnn_f32_relu_ukernel_function f32_relu) |
| 23 | { |
| 24 | const size_t n = state.range(0); |
| 25 | |
| 26 | std::random_device random_device; |
| 27 | auto rng = std::mt19937(random_device()); |
| 28 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), rng); |
| 29 | |
| 30 | std::vector<float, AlignedAllocator<float, 64>> x(n); |
| 31 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 32 | std::vector<float, AlignedAllocator<float, 64>> y(n); |
| 33 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 34 | |
| 35 | for (auto _ : state) { |
| 36 | f32_relu(n * sizeof(float), x.data(), y.data(), NULL); |
| 37 | } |
| 38 | |
| 39 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 40 | |
| 41 | state.counters["elements"] = |
| 42 | benchmark::Counter(uint64_t(state.iterations()) * n, benchmark::Counter::kIsRate); |
| 43 | |
| 44 | state.counters["bytes"] = |
| 45 | benchmark::Counter(uint64_t(state.iterations()) * n * sizeof(float), benchmark::Counter::kIsRate); |
| 46 | } |
| 47 | |
| 48 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 49 | BENCHMARK_CAPTURE(f32_relu, sse_x4, xnn_f32_relu_ukernel__sse_x4) |
| 50 | ->RangeMultiplier(10) |
| 51 | ->Range(1000, 100000000) |
| 52 | ->UseRealTime(); |
| 53 | |
| 54 | BENCHMARK_CAPTURE(f32_relu, sse_x8, xnn_f32_relu_ukernel__sse_x8) |
| 55 | ->RangeMultiplier(10) |
| 56 | ->Range(1000, 100000000) |
| 57 | ->UseRealTime(); |
| 58 | |
| 59 | BENCHMARK_CAPTURE(f32_relu, avx_x8, xnn_f32_relu_ukernel__avx_x8) |
| 60 | ->RangeMultiplier(10) |
| 61 | ->Range(1000, 100000000) |
| 62 | ->UseRealTime(); |
| 63 | |
| 64 | BENCHMARK_CAPTURE(f32_relu, avx_x16, xnn_f32_relu_ukernel__avx_x16) |
| 65 | ->RangeMultiplier(10) |
| 66 | ->Range(1000, 100000000) |
| 67 | ->UseRealTime(); |
| 68 | |
| 69 | BENCHMARK_CAPTURE(f32_relu, avx512f_x16, xnn_f32_relu_ukernel__avx512f_x16) |
| 70 | ->RangeMultiplier(10) |
| 71 | ->Range(1000, 100000000) |
| 72 | ->UseRealTime(); |
| 73 | |
| 74 | BENCHMARK_CAPTURE(f32_relu, avx512f_x32, xnn_f32_relu_ukernel__avx512f_x32) |
| 75 | ->RangeMultiplier(10) |
| 76 | ->Range(1000, 100000000) |
| 77 | ->UseRealTime(); |
| 78 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 79 | |
| 80 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 81 | BENCHMARK_CAPTURE(f32_relu, neon_x4, xnn_f32_relu_ukernel__neon_x4) |
| 82 | ->RangeMultiplier(10) |
| 83 | ->Range(1000, 100000000) |
| 84 | ->UseRealTime(); |
| 85 | BENCHMARK_CAPTURE(f32_relu, neon_x8, xnn_f32_relu_ukernel__neon_x8) |
| 86 | ->RangeMultiplier(10) |
| 87 | ->Range(1000, 100000000) |
| 88 | ->UseRealTime(); |
| 89 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
| 90 | |
| 91 | |
| 92 | #if XNN_ARCH_WASM || XNN_ARCH_WASMSIMD |
| 93 | BENCHMARK_CAPTURE(f32_relu, wasm_x1, xnn_f32_relu_ukernel__wasm_x1) |
| 94 | ->RangeMultiplier(10) |
| 95 | ->Range(1000, 100000000) |
| 96 | ->UseRealTime(); |
| 97 | |
| 98 | BENCHMARK_CAPTURE(f32_relu, wasm_x2, xnn_f32_relu_ukernel__wasm_x2) |
| 99 | ->RangeMultiplier(10) |
| 100 | ->Range(1000, 100000000) |
| 101 | ->UseRealTime(); |
| 102 | |
| 103 | BENCHMARK_CAPTURE(f32_relu, wasm_x4, xnn_f32_relu_ukernel__wasm_x4) |
| 104 | ->RangeMultiplier(10) |
| 105 | ->Range(1000, 100000000) |
| 106 | ->UseRealTime(); |
| 107 | #endif // XNN_ARCH_WASM || XNN_ARCH_WASMSIMD |
| 108 | |
| 109 | #if XNN_ARCH_WASMSIMD |
| 110 | BENCHMARK_CAPTURE(f32_relu, wasmsimd_x4, xnn_f32_relu_ukernel__wasmsimd_x4) |
| 111 | ->RangeMultiplier(10) |
| 112 | ->Range(1000, 100000000) |
| 113 | ->UseRealTime(); |
| 114 | |
| 115 | BENCHMARK_CAPTURE(f32_relu, wasmsimd_x8, xnn_f32_relu_ukernel__wasmsimd_x8) |
| 116 | ->RangeMultiplier(10) |
| 117 | ->Range(1000, 100000000) |
| 118 | ->UseRealTime(); |
| 119 | #endif // XNN_ARCH_WASMSIMD |
| 120 | |
| 121 | BENCHMARK_CAPTURE(f32_relu, scalar_x1, xnn_f32_relu_ukernel__scalar_x1) |
| 122 | ->RangeMultiplier(10) |
| 123 | ->Range(1000, 100000000) |
| 124 | ->UseRealTime(); |
| 125 | |
| 126 | BENCHMARK_CAPTURE(f32_relu, scalar_x2, xnn_f32_relu_ukernel__scalar_x2) |
| 127 | ->RangeMultiplier(10) |
| 128 | ->Range(1000, 100000000) |
| 129 | ->UseRealTime(); |
| 130 | |
| 131 | BENCHMARK_CAPTURE(f32_relu, scalar_x4, xnn_f32_relu_ukernel__scalar_x4) |
| 132 | ->RangeMultiplier(10) |
| 133 | ->Range(1000, 100000000) |
| 134 | ->UseRealTime(); |
| 135 | |
| 136 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 137 | BENCHMARK_MAIN(); |
| 138 | #endif |