xref: /aosp_15_r20/external/XNNPACK/bench/x24-transpose.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 <benchmark/benchmark.h>
13 #include "bench/utils.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/transpose.h>
20 
21 
transpose(benchmark::State & state,xnn_x24_transposec_ukernel_function transpose,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 void transpose(
23     benchmark::State& state,
24     xnn_x24_transposec_ukernel_function transpose,
25     benchmark::utils::IsaCheckFunction isa_check = nullptr)
26 {
27   if (isa_check && !isa_check(state)) {
28     return;
29   }
30   const size_t height = state.range(0);
31   const size_t width = state.range(1);
32   const size_t tile_hbytes = height * 3;
33   const size_t tile_wbytes = width * 3;
34 
35   std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> x(
36       height * width * 3 + XNN_EXTRA_BYTES / sizeof(uint8_t));
37   std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> y(
38       height * width * 3 + XNN_EXTRA_BYTES / sizeof(uint8_t));
39   std::iota(x.begin(), x.end(), 0);
40   std::fill(y.begin(), y.end(), 0);
41 
42   for (auto _ : state) {
43     transpose(x.data(), y.data(), tile_wbytes, tile_hbytes, width,
44               height);
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({"height", "width"});
56   b->Args({32, 32});
57   b->Args({64, 64});
58   b->Args({117, 117});
59   b->Args({1024, 1024});
60 }
61 
62 BENCHMARK_CAPTURE(transpose, 1x2_scalar, xnn_x24_transposec_ukernel__1x2_scalar)
63     ->Apply(BenchmarkKernelSize)->UseRealTime();
64 BENCHMARK_CAPTURE(transpose, 1x4_scalar, xnn_x24_transposec_ukernel__1x4_scalar)
65     ->Apply(BenchmarkKernelSize)->UseRealTime();
66 BENCHMARK_CAPTURE(transpose, 2x1_scalar, xnn_x24_transposec_ukernel__2x1_scalar)
67     ->Apply(BenchmarkKernelSize)->UseRealTime();
68 BENCHMARK_CAPTURE(transpose, 2x2_scalar, xnn_x24_transposec_ukernel__2x2_scalar)
69     ->Apply(BenchmarkKernelSize)->UseRealTime();
70 BENCHMARK_CAPTURE(transpose, 2x4_scalar, xnn_x24_transposec_ukernel__2x4_scalar)
71     ->Apply(BenchmarkKernelSize)->UseRealTime();
72 BENCHMARK_CAPTURE(transpose, 4x1_scalar, xnn_x24_transposec_ukernel__4x1_scalar)
73     ->Apply(BenchmarkKernelSize)->UseRealTime();
74 BENCHMARK_CAPTURE(transpose, 4x2_scalar, xnn_x24_transposec_ukernel__4x2_scalar)
75     ->Apply(BenchmarkKernelSize)->UseRealTime();
76 BENCHMARK_CAPTURE(transpose, 4x4_scalar, xnn_x24_transposec_ukernel__4x4_scalar)
77     ->Apply(BenchmarkKernelSize)->UseRealTime();
78 
79 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
80   BENCHMARK_CAPTURE(transpose, 2x2_neon_tbl, xnn_x24_transposec_ukernel__2x2_neon_tbl)
81       ->Apply(BenchmarkKernelSize)->UseRealTime();
82 #endif  // XNN_ARCH_ARM || XNN_ARCH_ARM64
83 
84 #if XNN_ARCH_ARM64
85   BENCHMARK_CAPTURE(transpose, 4x4_aarch64_neon_tbl, xnn_x24_transposec_ukernel__4x4_aarch64_neon_tbl)
86       ->Apply(BenchmarkKernelSize)->UseRealTime();
87 #endif  // XNN_ARCH_ARM64
88 
89 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
90 BENCHMARK_CAPTURE(transpose, 4x4_ssse3, xnn_x24_transposec_ukernel__4x4_ssse3, benchmark::utils::CheckSSSE3)
91     ->Apply(BenchmarkKernelSize)->UseRealTime();
92 #endif  // XNN_ARCH_X86 || XNN_ARCH_X86_64
93 
94 #ifndef XNNPACK_BENCHMARK_NO_MAIN
95 BENCHMARK_MAIN();
96 #endif
97