xref: /aosp_15_r20/external/XNNPACK/bench/hardswish.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 <array>
8 #include <cmath>
9 #include <functional>
10 #include <limits>
11 #include <random>
12 #include <vector>
13 
14 #include <fp16.h>
15 
16 #include <xnnpack.h>
17 
18 #include <benchmark/benchmark.h>
19 #include "bench/utils.h"
20 #ifdef BENCHMARK_TENSORFLOW_LITE
21 #include "flatbuffers/include/flatbuffers/flatbuffers.h"
22 #include "tensorflow/lite/interpreter.h"
23 #include "tensorflow/lite/kernels/register.h"
24 #include "tensorflow/lite/model.h"
25 #include "tensorflow/lite/schema/schema_generated.h"
26 #include "tensorflow/lite/version.h"
27 #endif  // BENCHMARK_TENSORFLOW_LITE
28 
29 
xnnpack_hardswish_f32(benchmark::State & state)30 static void xnnpack_hardswish_f32(benchmark::State& state) {
31   const size_t batch_size = state.range(0);
32 
33   std::random_device random_device;
34   auto rng = std::mt19937(random_device());
35   auto f32rng = std::bind(std::uniform_real_distribution<float>(-5.0f, 5.0f), std::ref(rng));
36 
37   std::vector<float> input(batch_size + XNN_EXTRA_BYTES / sizeof(float));
38   std::vector<float> output(batch_size);
39   std::generate(input.begin(), input.end(), std::ref(f32rng));
40   std::fill(output.begin(), output.end(), std::nanf(""));
41 
42   xnn_status status = xnn_initialize(nullptr /* allocator */);
43   if (status != xnn_status_success) {
44     state.SkipWithError("failed to initialize XNNPACK");
45     return;
46   }
47 
48   xnn_operator_t hardswish_op = nullptr;
49   status = xnn_create_hardswish_nc_f32(
50     1 /* channels */, 1 /* input stride */, 1 /* output stride */,
51     0 /* flags */, &hardswish_op);
52   if (status != xnn_status_success || hardswish_op == nullptr) {
53     state.SkipWithError("failed to create HardSwish operator");
54     return;
55   }
56 
57   status = xnn_setup_hardswish_nc_f32(
58     hardswish_op, batch_size,
59     input.data(), output.data(),
60     nullptr /* thread pool */);
61   if (status != xnn_status_success) {
62     state.SkipWithError("failed to setup HardSwish operator");
63     return;
64   }
65 
66   for (auto _ : state) {
67     status = xnn_run_operator(hardswish_op, nullptr /* thread pool */);
68     if (status != xnn_status_success) {
69       state.SkipWithError("failed to run HardSwish operator");
70       return;
71     }
72   }
73 
74   status = xnn_delete_operator(hardswish_op);
75   if (status != xnn_status_success) {
76     state.SkipWithError("failed to delete HardSwish operator");
77     return;
78   }
79 
80   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
81   if (cpu_frequency != 0) {
82     state.counters["cpufreq"] = cpu_frequency;
83   }
84 
85   state.counters["elements"] =
86     benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
87 
88   const size_t bytes_per_iteration = 2 * batch_size * sizeof(float);
89   state.counters["bytes"] =
90     benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
91 }
92 
93 #ifndef XNN_NO_F16_OPERATORS
xnnpack_hardswish_f16(benchmark::State & state)94 static void xnnpack_hardswish_f16(benchmark::State& state) {
95   const size_t batch_size = state.range(0);
96 
97   std::random_device random_device;
98   auto rng = std::mt19937(random_device());
99   auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
100   auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
101 
102   std::vector<uint16_t> input(batch_size + XNN_EXTRA_BYTES / sizeof(uint16_t));
103   std::vector<uint16_t> output(batch_size);
104   std::generate(input.begin(), input.end(), std::ref(f16rng));
105   std::fill(output.begin(), output.end(), std::nanf(""));
106 
107   xnn_status status = xnn_initialize(nullptr /* allocator */);
108   if (status != xnn_status_success) {
109     state.SkipWithError("failed to initialize XNNPACK");
110     return;
111   }
112 
113   xnn_operator_t hardswish_op = nullptr;
114   status = xnn_create_hardswish_nc_f16(
115     1 /* channels */, 1 /* input stride */, 1 /* output stride */,
116     0 /* flags */, &hardswish_op);
117   if (status != xnn_status_success || hardswish_op == nullptr) {
118     state.SkipWithError("failed to create HardSwish operator");
119     return;
120   }
121 
122   status = xnn_setup_hardswish_nc_f16(
123     hardswish_op, batch_size,
124     input.data(), output.data(),
125     nullptr /* thread pool */);
126   if (status != xnn_status_success) {
127     state.SkipWithError("failed to setup HardSwish operator");
128     return;
129   }
130 
131   for (auto _ : state) {
132     status = xnn_run_operator(hardswish_op, nullptr /* thread pool */);
133     if (status != xnn_status_success) {
134       state.SkipWithError("failed to run HardSwish operator");
135       return;
136     }
137   }
138 
139   status = xnn_delete_operator(hardswish_op);
140   if (status != xnn_status_success) {
141     state.SkipWithError("failed to delete HardSwish operator");
142     return;
143   }
144 
145   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
146   if (cpu_frequency != 0) {
147     state.counters["cpufreq"] = cpu_frequency;
148   }
149 
150   state.counters["elements"] =
151     benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
152 
153   const size_t bytes_per_iteration = 2 * batch_size * sizeof(uint16_t);
154   state.counters["bytes"] =
155     benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
156 }
157 #endif  // XNN_NO_F16_OPERATORS
158 
159 #ifdef BENCHMARK_TENSORFLOW_LITE
tflite_hardswish_f32(benchmark::State & state)160 static void tflite_hardswish_f32(benchmark::State& state) {
161   const size_t batch_size = state.range(0);
162 
163   std::random_device random_device;
164   auto rng = std::mt19937(random_device());
165   auto f32rng = std::bind(std::uniform_real_distribution<float>(-5.0f, 5.0f), std::ref(rng));
166 
167   flatbuffers::FlatBufferBuilder builder;
168   const flatbuffers::Offset<tflite::OperatorCode> operator_code =
169       CreateOperatorCode(builder, tflite::BuiltinOperator_HARD_SWISH);
170 
171   const std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers{{
172     tflite::CreateBuffer(builder, builder.CreateVector({})),
173   }};
174 
175   const std::array<int32_t, 1> shape{{
176     static_cast<int32_t>(batch_size)
177   }};
178 
179   const std::array<flatbuffers::Offset<tflite::Tensor>, 2> tensors{{
180     tflite::CreateTensor(builder,
181                          builder.CreateVector<int32_t>(shape.data(), shape.size()),
182                          tflite::TensorType_FLOAT32),
183     tflite::CreateTensor(builder,
184                          builder.CreateVector<int32_t>(shape.data(), shape.size()),
185                          tflite::TensorType_FLOAT32),
186   }};
187 
188   const std::array<int32_t, 1> op_inputs{{ 0 }};
189   const std::array<int32_t, 1> op_outputs{{ 1 }};
190   flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator(
191       builder,
192       0 /* opcode_index */,
193       builder.CreateVector<int32_t>(op_inputs.data(), op_inputs.size()),
194       builder.CreateVector<int32_t>(op_outputs.data(), op_outputs.size()));
195 
196   const std::array<int32_t, 1> graph_inputs{{ 0 }};
197   const std::array<int32_t, 1> graph_outputs{{ 1 }};
198   const flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph(
199       builder,
200       builder.CreateVector(tensors.data(), tensors.size()),
201       builder.CreateVector<int32_t>(graph_inputs.data(), graph_inputs.size()),
202       builder.CreateVector<int32_t>(graph_outputs.data(), graph_outputs.size()),
203       builder.CreateVector(&op, 1));
204 
205   const flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder,
206       TFLITE_SCHEMA_VERSION,
207       builder.CreateVector(&operator_code, 1),
208       builder.CreateVector(&subgraph, 1),
209       builder.CreateString("HardSwish model"),
210       builder.CreateVector(buffers.data(), buffers.size()));
211 
212   builder.Finish(model_buffer);
213 
214   const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer());
215   tflite::ops::builtin::BuiltinOpResolverWithoutDefaultDelegates resolver;
216   tflite::InterpreterBuilder interpreterBuilder(model, resolver);
217   std::unique_ptr<tflite::Interpreter> interpreter;
218   if (interpreterBuilder(&interpreter) != kTfLiteOk || interpreter == nullptr) {
219     state.SkipWithError("failed to create TFLite interpreter");
220     return;
221   }
222   interpreter->SetNumThreads(1);
223 
224   if (interpreter->AllocateTensors() != kTfLiteOk) {
225     state.SkipWithError("failed to allocate tensors");
226     return;
227   }
228 
229   std::generate(
230     interpreter->typed_tensor<float>(0),
231     interpreter->typed_tensor<float>(0) + batch_size,
232     std::ref(f32rng));
233 
234   for (auto _ : state) {
235     if (interpreter->Invoke() != kTfLiteOk) {
236       state.SkipWithError("failed to invoke TFLite interpreter");
237       return;
238     }
239   }
240 
241   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
242   if (cpu_frequency != 0) {
243     state.counters["cpufreq"] = cpu_frequency;
244   }
245 
246   state.counters["elements"] =
247     benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
248 
249   const size_t bytes_per_iteration = 2 * batch_size * sizeof(float);
250   state.counters["bytes"] =
251     benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
252 
253   interpreter.reset();
254 }
255 #endif  // BENCHMARK_TENSORFLOW_LITE
256 
257 BENCHMARK(xnnpack_hardswish_f32)
258   ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
259   ->UseRealTime();
260 #ifndef XNN_NO_F16_OPERATORS
261   BENCHMARK(xnnpack_hardswish_f16)
262     ->Apply(benchmark::utils::UnaryElementwiseParameters<uint16_t, uint16_t>)
263     ->UseRealTime();
264 #endif  // XNN_NO_F16_OPERATORS
265 
266 #ifdef BENCHMARK_TENSORFLOW_LITE
267   BENCHMARK(tflite_hardswish_f32)
268     ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
269     ->UseRealTime();
270 #endif  // BENCHMARK_TENSORFLOW_LITE
271 
272 #ifndef XNNPACK_BENCHMARK_NO_MAIN
273 BENCHMARK_MAIN();
274 #endif
275