xref: /aosp_15_r20/external/XNNPACK/bench/truncation.cc (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright 2020 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 <limits>
10 #include <random>
11 #include <vector>
12 
13 #include <xnnpack.h>
14 
15 #include <benchmark/benchmark.h>
16 #include "bench/utils.h"
17 
18 
xnnpack_truncation_f32(benchmark::State & state)19 static void xnnpack_truncation_f32(benchmark::State& state) {
20   const size_t batch_size = state.range(0);
21 
22   std::random_device random_device;
23   auto rng = std::mt19937(random_device());
24   auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
25 
26   std::vector<float> input(batch_size + XNN_EXTRA_BYTES / sizeof(float));
27   std::vector<float> output(batch_size);
28   std::generate(input.begin(), input.end(), std::ref(f32rng));
29   std::fill(output.begin(), output.end(), std::nanf(""));
30 
31   xnn_status status = xnn_initialize(nullptr /* allocator */);
32   if (status != xnn_status_success) {
33     state.SkipWithError("failed to initialize XNNPACK");
34     return;
35   }
36 
37   xnn_operator_t truncation_op = nullptr;
38   status = xnn_create_truncation_nc_f32(
39     1 /* channels */, 1 /* input stride */, 1 /* output stride */,
40     0 /* flags */, &truncation_op);
41   if (status != xnn_status_success || truncation_op == nullptr) {
42     state.SkipWithError("failed to create Truncation operator");
43     return;
44   }
45 
46   status = xnn_setup_truncation_nc_f32(
47     truncation_op, batch_size,
48     input.data(), output.data(),
49     nullptr /* thread pool */);
50   if (status != xnn_status_success) {
51     state.SkipWithError("failed to setup Truncation operator");
52     return;
53   }
54 
55   for (auto _ : state) {
56     status = xnn_run_operator(truncation_op, nullptr /* thread pool */);
57     if (status != xnn_status_success) {
58       state.SkipWithError("failed to run Truncation operator");
59       return;
60     }
61   }
62 
63   status = xnn_delete_operator(truncation_op);
64   if (status != xnn_status_success) {
65     state.SkipWithError("failed to delete Truncation operator");
66     return;
67   }
68 
69   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
70   if (cpu_frequency != 0) {
71     state.counters["cpufreq"] = cpu_frequency;
72   }
73 
74   state.counters["elements"] =
75     benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
76 
77   const size_t bytes_per_iteration = 2 * batch_size * sizeof(float);
78   state.counters["bytes"] =
79     benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
80 }
81 
82 BENCHMARK(xnnpack_truncation_f32)
83   ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
84   ->UseRealTime();
85 
86 #ifndef XNNPACK_BENCHMARK_NO_MAIN
87 BENCHMARK_MAIN();
88 #endif
89