xref: /aosp_15_r20/external/XNNPACK/bench/s16-window.cc (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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/microfnptr.h>
19 #include <xnnpack/window.h>
20 
21 
window(benchmark::State & state,xnn_s16_window_ukernel_function window,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 void window(
23     benchmark::State& state,
24     xnn_s16_window_ukernel_function window,
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 
33   std::vector<int16_t, AlignedAllocator<int16_t, 64>> input(
34       (rows * batch) + XNN_EXTRA_BYTES / sizeof(int16_t));
35   std::vector<int16_t, AlignedAllocator<int16_t, 64>> weights(
36       batch + XNN_EXTRA_BYTES / sizeof(int16_t));
37   std::vector<int16_t, AlignedAllocator<int16_t, 64>> output(
38       (rows * batch) + XNN_EXTRA_BYTES / sizeof(int16_t));
39   std::iota(input.begin(), input.end(), 0);
40   std::fill(weights.begin(), weights.end(), 0);
41   std::iota(output.begin(), output.end(), 0);
42 
43   for (auto _ : state) {
44     window(rows, batch, input.data(), weights.data(), output.data(), 12 /* shift */);
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, 400});
57   b->Args({10, 400});
58 }
59 
60 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
61 BENCHMARK_CAPTURE(window, s16_neon_x8,  xnn_s16_window_ukernel__neon_x8, benchmark::utils::CheckNEON)
62     ->Apply(BenchmarkKernelSize)->UseRealTime();
63 BENCHMARK_CAPTURE(window, s16_neon_x16, xnn_s16_window_ukernel__neon_x16, benchmark::utils::CheckNEON)
64     ->Apply(BenchmarkKernelSize)->UseRealTime();
65 BENCHMARK_CAPTURE(window, s16_neon_x24, xnn_s16_window_ukernel__neon_x24, benchmark::utils::CheckNEON)
66     ->Apply(BenchmarkKernelSize)->UseRealTime();
67 BENCHMARK_CAPTURE(window, s16_neon_x32, xnn_s16_window_ukernel__neon_x32, benchmark::utils::CheckNEON)
68     ->Apply(BenchmarkKernelSize)->UseRealTime();
69 
70 BENCHMARK_CAPTURE(window, s16_shift12_neon_x8,  xnn_s16_window_shift12_ukernel__neon_x8, benchmark::utils::CheckNEON)
71     ->Apply(BenchmarkKernelSize)->UseRealTime();
72 BENCHMARK_CAPTURE(window, s16_shift12_neon_x16, xnn_s16_window_shift12_ukernel__neon_x16, benchmark::utils::CheckNEON)
73     ->Apply(BenchmarkKernelSize)->UseRealTime();
74 BENCHMARK_CAPTURE(window, s16_shift12_neon_x24, xnn_s16_window_shift12_ukernel__neon_x24, benchmark::utils::CheckNEON)
75     ->Apply(BenchmarkKernelSize)->UseRealTime();
76 BENCHMARK_CAPTURE(window, s16_shift12_neon_x32, xnn_s16_window_shift12_ukernel__neon_x32, benchmark::utils::CheckNEON)
77     ->Apply(BenchmarkKernelSize)->UseRealTime();
78 
79 BENCHMARK_CAPTURE(window, s16_shift15_neon_x8,  xnn_s16_window_shift15_ukernel__neon_x8, benchmark::utils::CheckNEON)
80     ->Apply(BenchmarkKernelSize)->UseRealTime();
81 BENCHMARK_CAPTURE(window, s16_shift15_neon_x16, xnn_s16_window_shift15_ukernel__neon_x16, benchmark::utils::CheckNEON)
82     ->Apply(BenchmarkKernelSize)->UseRealTime();
83 BENCHMARK_CAPTURE(window, s16_shift15_neon_x24, xnn_s16_window_shift15_ukernel__neon_x24, benchmark::utils::CheckNEON)
84     ->Apply(BenchmarkKernelSize)->UseRealTime();
85 BENCHMARK_CAPTURE(window, s16_shift15_neon_x32, xnn_s16_window_shift15_ukernel__neon_x32, benchmark::utils::CheckNEON)
86     ->Apply(BenchmarkKernelSize)->UseRealTime();
87 #endif  // XNN_ARCH_ARM || XNN_ARCH_ARM64
88 
89 BENCHMARK_CAPTURE(window, s16_scalar_x1, xnn_s16_window_ukernel__scalar_x1)
90     ->Apply(BenchmarkKernelSize)->UseRealTime();
91 BENCHMARK_CAPTURE(window, s16_scalar_x2, xnn_s16_window_ukernel__scalar_x2)
92     ->Apply(BenchmarkKernelSize)->UseRealTime();
93 BENCHMARK_CAPTURE(window, s16_scalar_x3, xnn_s16_window_ukernel__scalar_x3)
94     ->Apply(BenchmarkKernelSize)->UseRealTime();
95 BENCHMARK_CAPTURE(window, s16_scalar_x4, xnn_s16_window_ukernel__scalar_x4)
96     ->Apply(BenchmarkKernelSize)->UseRealTime();
97 
98 #ifndef XNNPACK_BENCHMARK_NO_MAIN
99 BENCHMARK_MAIN();
100 #endif
101