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_transposev_ukernel_function transpose,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 void transpose(
23 benchmark::State& state,
24 xnn_transposev_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 element_size = state.range(2);
33 const size_t tile_hbytes = height * 3;
34 const size_t tile_wbytes = width * 3;
35
36 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> x(
37 height * width * element_size + XNN_EXTRA_BYTES / sizeof(uint8_t));
38 std::vector<uint8_t, AlignedAllocator<uint8_t, 64>> y(
39 height * width * element_size + XNN_EXTRA_BYTES / sizeof(uint8_t));
40 std::iota(x.begin(), x.end(), 0);
41 std::fill(y.begin(), y.end(), 0);
42
43 for (auto _ : state) {
44 transpose(x.data(), y.data(), tile_wbytes, tile_hbytes, element_size,
45 element_size, element_size, width, height);
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
BenchmarkKernelSize(benchmark::internal::Benchmark * b)54 static void BenchmarkKernelSize(benchmark::internal::Benchmark* b)
55 {
56 b->ArgNames({"height", "width", "element_size"});
57 //b->Args({32, 32, 3});
58 //b->Args({64, 64, 3});
59 //b->Args({117, 117, 3});
60 //b->Args({1024, 1024, 3});
61 b->Args({49153, 8, 128});
62 }
63
64 BENCHMARK_CAPTURE(transpose, 1x1_mmemcpy, xnn_xx_transposev_ukernel__1x1_memcpy)
65 ->Apply(BenchmarkKernelSize)->UseRealTime();
66
67 #ifndef XNNPACK_BENCHMARK_NO_MAIN
68 BENCHMARK_MAIN();
69 #endif
70