1 // Copyright 2022 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 <numeric>
10 #include <vector>
11
12 #include "bench/utils.h"
13 #include <benchmark/benchmark.h>
14
15 #include <xnnpack.h>
16 #include <xnnpack/aligned-allocator.h>
17 #include <xnnpack/common.h>
18 #include <xnnpack/filterbank.h>
19 #include <xnnpack/microfnptr.h>
20
21
filterbank_accumulate(benchmark::State & state,xnn_u32_filterbank_accumulate_ukernel_function filterbank_accumulate,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 void filterbank_accumulate(
23 benchmark::State& state,
24 xnn_u32_filterbank_accumulate_ukernel_function filterbank_accumulate,
25 benchmark::utils::IsaCheckFunction isa_check = nullptr)
26 {
27 if (isa_check && !isa_check(state)) {
28 return;
29 }
30 const size_t rows = state.range(0);
31 const size_t batch = state.range(1);
32 const size_t input_size = (rows + 1) * batch;
33
34 std::vector<uint32_t, AlignedAllocator<uint32_t, 64>> input(input_size);
35 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> weight_widths(rows + 1);
36 std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> weights(input_size * 2);
37 std::vector<uint64_t, AlignedAllocator<uint64_t, 64>> output(rows);
38 std::iota(input.begin(), input.end(), 0);
39 std::fill(weight_widths.begin(), weight_widths.end(), batch);
40 std::iota(weights.begin(), weights.end(), 0);
41 std::iota(output.begin(), output.end(), 0);
42
43 for (auto _ : state) {
44 filterbank_accumulate(rows, input.data(), weight_widths.data(), weights.data(), output.data());
45 }
46
47 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
48 if (cpu_frequency != 0) {
49 state.counters["cpufreq"] = cpu_frequency;
50 }
51 }
52
BenchmarkKernelSize(benchmark::internal::Benchmark * b)53 static void BenchmarkKernelSize(benchmark::internal::Benchmark* b)
54 {
55 b->ArgNames({"rows", "batch"});
56 b->Args({1, 237});
57 b->Args({5, 1});
58 b->Args({10, 2});
59 b->Args({7, 3});
60 b->Args({5, 4});
61 b->Args({5, 5});
62 b->Args({3, 6});
63 b->Args({4, 7});
64 b->Args({2, 8});
65 b->Args({2, 9});
66 b->Args({2, 10});
67 b->Args({3, 11});
68 b->Args({1, 13});
69 }
70
71 #if XNN_ARCH_ARM && XNN_ENABLE_ASSEMBLY
72 BENCHMARK_CAPTURE(filterbank_accumulate, u32_aarch32_arm_x1, xnn_u32_filterbank_accumulate_ukernel__aarch32_arm_x1)->Apply(BenchmarkKernelSize)->UseRealTime();
73 BENCHMARK_CAPTURE(filterbank_accumulate, u32_aarch32_neon_x1, xnn_u32_filterbank_accumulate_ukernel__aarch32_neon_x1, benchmark::utils::CheckNEON)->Apply(BenchmarkKernelSize)->UseRealTime();
74 BENCHMARK_CAPTURE(filterbank_accumulate, u32_aarch32_neon_x2, xnn_u32_filterbank_accumulate_ukernel__aarch32_neon_x2, benchmark::utils::CheckNEON)->Apply(BenchmarkKernelSize)->UseRealTime();
75 #endif // XNN_ARCH_ARM && XNN_ENABLE_ASSEMBLY
76
77 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
78 BENCHMARK_CAPTURE(filterbank_accumulate, u32_neon_x1, xnn_u32_filterbank_accumulate_ukernel__neon_x1, benchmark::utils::CheckNEON)->Apply(BenchmarkKernelSize)->UseRealTime();
79 BENCHMARK_CAPTURE(filterbank_accumulate, u32_neon_x2, xnn_u32_filterbank_accumulate_ukernel__neon_x2, benchmark::utils::CheckNEON)->Apply(BenchmarkKernelSize)->UseRealTime();
80 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
81
82 BENCHMARK_CAPTURE(filterbank_accumulate, u32_scalar_x1, xnn_u32_filterbank_accumulate_ukernel__scalar_x1)->Apply(BenchmarkKernelSize)->UseRealTime();
83
84 #ifndef XNNPACK_BENCHMARK_NO_MAIN
85 BENCHMARK_MAIN();
86 #endif
87