Marat Dukhan | 5407437 | 2021-09-08 23:28:46 -0700 | [diff] [blame^] | 1 | // Copyright 2021 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 <array> |
| 8 | #include <cmath> |
| 9 | #include <functional> |
| 10 | #include <random> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <benchmark/benchmark.h> |
| 14 | #include "bench/utils.h" |
| 15 | |
| 16 | #include <xnnpack/AlignedAllocator.h> |
| 17 | #include <xnnpack/common.h> |
| 18 | #include <xnnpack/params.h> |
| 19 | #include <xnnpack/lut.h> |
| 20 | |
| 21 | |
| 22 | static void x8_lut( |
| 23 | benchmark::State& state, |
| 24 | xnn_x8_lut_ukernel_function lut, |
| 25 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
| 26 | { |
| 27 | if (isa_check && !isa_check(state)) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const size_t num_elements = state.range(0); |
| 32 | std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> input(num_elements); |
| 33 | std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> output(num_elements); |
| 34 | std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> table(256); |
| 35 | |
| 36 | std::random_device random_device; |
| 37 | auto rng = std::mt19937(random_device()); |
| 38 | auto u8rng = std::bind( |
| 39 | std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), std::ref(rng)); |
| 40 | std::generate(input.begin(), input.end(), std::ref(u8rng)); |
| 41 | std::generate(table.begin(), table.end(), std::ref(u8rng)); |
| 42 | std::fill(output.begin(), output.end(), UINT8_C(0xAA)); |
| 43 | |
| 44 | for (auto _ : state) { |
| 45 | lut(num_elements * sizeof(uint8_t), input.data(), output.data(), table.data()); |
| 46 | } |
| 47 | |
| 48 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 49 | if (cpu_frequency != 0) { |
| 50 | state.counters["cpufreq"] = cpu_frequency; |
| 51 | } |
| 52 | |
| 53 | const size_t elements_per_iteration = num_elements; |
| 54 | state.counters["elements"] = |
| 55 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 56 | |
| 57 | const size_t bytes_per_iteration = 2 * num_elements * sizeof(uint8_t); |
| 58 | state.counters["bytes"] = |
| 59 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 60 | } |
| 61 | |
| 62 | BENCHMARK_CAPTURE(x8_lut, scalar_x1, |
| 63 | xnn_x8_lut_ukernel__scalar_x1) |
| 64 | ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>) |
| 65 | ->UseRealTime(); |
| 66 | BENCHMARK_CAPTURE(x8_lut, scalar_x2, |
| 67 | xnn_x8_lut_ukernel__scalar_x2) |
| 68 | ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>) |
| 69 | ->UseRealTime(); |
| 70 | BENCHMARK_CAPTURE(x8_lut, scalar_x4, |
| 71 | xnn_x8_lut_ukernel__scalar_x4) |
| 72 | ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>) |
| 73 | ->UseRealTime(); |
| 74 | BENCHMARK_CAPTURE(x8_lut, scalar_x8, |
| 75 | xnn_x8_lut_ukernel__scalar_x8) |
| 76 | ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>) |
| 77 | ->UseRealTime(); |
| 78 | BENCHMARK_CAPTURE(x8_lut, scalar_x16, |
| 79 | xnn_x8_lut_ukernel__scalar_x16) |
| 80 | ->Apply(benchmark::utils::UnaryElementwiseParameters<uint8_t, uint8_t>) |
| 81 | ->UseRealTime(); |
| 82 | |
| 83 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 84 | BENCHMARK_MAIN(); |
| 85 | #endif |