xref: /aosp_15_r20/external/XNNPACK/bench/u32-vlog.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/vlog.h>
20 
21 
vlog(benchmark::State & state,xnn_u32_vlog_ukernel_function vlog,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 void vlog(
23     benchmark::State& state,
24     xnn_u32_vlog_ukernel_function vlog,
25     benchmark::utils::IsaCheckFunction isa_check = nullptr)
26 {
27   if (isa_check && !isa_check(state)) {
28     return;
29   }
30   const size_t batch = state.range(0);
31 
32   std::vector<uint32_t, AlignedAllocator<uint32_t, 64>> input(
33       batch + XNN_EXTRA_BYTES / sizeof(uint32_t));
34   std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> output(batch);
35   std::iota(input.begin(), input.end(), 0);
36   std::iota(output.begin(), output.end(), 0);
37 
38   for (auto _ : state) {
39     vlog(batch, input.data(), 4, 16, output.data());
40   }
41 
42   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
43   if (cpu_frequency != 0) {
44     state.counters["cpufreq"] = cpu_frequency;
45   }
46 }
47 
BenchmarkKernelSize(benchmark::internal::Benchmark * b)48 static void BenchmarkKernelSize(benchmark::internal::Benchmark* b)
49 {
50   b->ArgNames({"batch"});
51   b->Args({32});
52   b->Args({64});
53   b->Args({117});
54   b->Args({216});
55   b->Args({400});
56   b->Args({1000});
57   b->Args({10000});
58 }
59 
60 
61 BENCHMARK_CAPTURE(vlog, u32_scalar_x1, xnn_u32_vlog_ukernel__scalar_x1)
62     ->Apply(BenchmarkKernelSize)->UseRealTime();
63 BENCHMARK_CAPTURE(vlog, u32_scalar_x2, xnn_u32_vlog_ukernel__scalar_x2)
64     ->Apply(BenchmarkKernelSize)->UseRealTime();
65 BENCHMARK_CAPTURE(vlog, u32_scalar_x3, xnn_u32_vlog_ukernel__scalar_x3)
66     ->Apply(BenchmarkKernelSize)->UseRealTime();
67 BENCHMARK_CAPTURE(vlog, u32_scalar_x4, xnn_u32_vlog_ukernel__scalar_x4)
68     ->Apply(BenchmarkKernelSize)->UseRealTime();
69 
70 #ifndef XNNPACK_BENCHMARK_NO_MAIN
71 BENCHMARK_MAIN();
72 #endif
73