• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 <xnnpack.h>
15 
16 #include <benchmark/benchmark.h>
17 #include "bench/utils.h"
18 #ifdef BENCHMARK_TENSORFLOW_LITE
19 #include "flatbuffers/include/flatbuffers/flatbuffers.h"
20 #include "tensorflow/lite/interpreter.h"
21 #include "tensorflow/lite/kernels/register.h"
22 #include "tensorflow/lite/model.h"
23 #include "tensorflow/lite/schema/schema_generated.h"
24 #include "tensorflow/lite/version.h"
25 #endif  // BENCHMARK_TENSORFLOW_LITE
26 
27 
xnnpack_abs_f32(benchmark::State & state)28 static void xnnpack_abs_f32(benchmark::State& state) {
29   const size_t batch_size = state.range(0);
30 
31   std::random_device random_device;
32   auto rng = std::mt19937(random_device());
33   auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
34 
35   std::vector<float> input(batch_size + XNN_EXTRA_BYTES / sizeof(float));
36   std::vector<float> output(batch_size);
37   std::generate(input.begin(), input.end(), std::ref(f32rng));
38   std::fill(output.begin(), output.end(), std::nanf(""));
39 
40   xnn_status status = xnn_initialize(nullptr /* allocator */);
41   if (status != xnn_status_success) {
42     state.SkipWithError("failed to initialize XNNPACK");
43     return;
44   }
45 
46   xnn_operator_t abs_op = nullptr;
47   status = xnn_create_abs_nc_f32(
48     1 /* channels */, 1 /* input stride */, 1 /* output stride */,
49     0 /* flags */, &abs_op);
50   if (status != xnn_status_success || abs_op == nullptr) {
51     state.SkipWithError("failed to create Abs operator");
52     return;
53   }
54 
55   status = xnn_setup_abs_nc_f32(
56     abs_op, batch_size,
57     input.data(), output.data(),
58     nullptr /* thread pool */);
59   if (status != xnn_status_success) {
60     state.SkipWithError("failed to setup Abs operator");
61     return;
62   }
63 
64   for (auto _ : state) {
65     status = xnn_run_operator(abs_op, nullptr /* thread pool */);
66     if (status != xnn_status_success) {
67       state.SkipWithError("failed to run Abs operator");
68       return;
69     }
70   }
71 
72   status = xnn_delete_operator(abs_op);
73   if (status != xnn_status_success) {
74     state.SkipWithError("failed to delete Abs operator");
75     return;
76   }
77 
78   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
79   if (cpu_frequency != 0) {
80     state.counters["cpufreq"] = cpu_frequency;
81   }
82 
83   state.counters["elements"] =
84     benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
85 
86   const size_t bytes_per_iteration = 2 * batch_size * sizeof(float);
87   state.counters["bytes"] =
88     benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
89 }
90 
91 #ifdef BENCHMARK_TENSORFLOW_LITE
tflite_abs_f32(benchmark::State & state)92 static void tflite_abs_f32(benchmark::State& state) {
93   const size_t batch_size = state.range(0);
94 
95   std::random_device random_device;
96   auto rng = std::mt19937(random_device());
97   auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
98 
99   flatbuffers::FlatBufferBuilder builder;
100   const flatbuffers::Offset<tflite::OperatorCode> operator_code =
101       CreateOperatorCode(builder, tflite::BuiltinOperator_ABS);
102 
103   const std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers{{
104     tflite::CreateBuffer(builder, builder.CreateVector({})),
105   }};
106 
107   const std::array<int32_t, 1> shape{{
108     static_cast<int32_t>(batch_size)
109   }};
110 
111   const std::array<flatbuffers::Offset<tflite::Tensor>, 2> tensors{{
112     tflite::CreateTensor(builder,
113                          builder.CreateVector<int32_t>(shape.data(), shape.size()),
114                          tflite::TensorType_FLOAT32),
115     tflite::CreateTensor(builder,
116                          builder.CreateVector<int32_t>(shape.data(), shape.size()),
117                          tflite::TensorType_FLOAT32),
118   }};
119 
120   const std::array<int32_t, 1> op_inputs{{ 0 }};
121   const std::array<int32_t, 1> op_outputs{{ 1 }};
122   flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator(
123       builder,
124       0 /* opcode_index */,
125       builder.CreateVector<int32_t>(op_inputs.data(), op_inputs.size()),
126       builder.CreateVector<int32_t>(op_outputs.data(), op_outputs.size()));
127 
128   const std::array<int32_t, 1> graph_inputs{{ 0 }};
129   const std::array<int32_t, 1> graph_outputs{{ 1 }};
130   const flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph(
131       builder,
132       builder.CreateVector(tensors.data(), tensors.size()),
133       builder.CreateVector<int32_t>(graph_inputs.data(), graph_inputs.size()),
134       builder.CreateVector<int32_t>(graph_outputs.data(), graph_outputs.size()),
135       builder.CreateVector(&op, 1));
136 
137   const flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder,
138       TFLITE_SCHEMA_VERSION,
139       builder.CreateVector(&operator_code, 1),
140       builder.CreateVector(&subgraph, 1),
141       builder.CreateString("Abs model"),
142       builder.CreateVector(buffers.data(), buffers.size()));
143 
144   builder.Finish(model_buffer);
145 
146   const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer());
147   tflite::ops::builtin::BuiltinOpResolverWithoutDefaultDelegates resolver;
148   tflite::InterpreterBuilder interpreterBuilder(model, resolver);
149   std::unique_ptr<tflite::Interpreter> interpreter;
150   if (interpreterBuilder(&interpreter) != kTfLiteOk || interpreter == nullptr) {
151     state.SkipWithError("failed to create TFLite interpreter");
152     return;
153   }
154   interpreter->SetNumThreads(1);
155 
156   if (interpreter->AllocateTensors() != kTfLiteOk) {
157     state.SkipWithError("failed to allocate tensors");
158     return;
159   }
160 
161   std::generate(
162     interpreter->typed_tensor<float>(0),
163     interpreter->typed_tensor<float>(0) + batch_size,
164     std::ref(f32rng));
165 
166   for (auto _ : state) {
167     if (interpreter->Invoke() != kTfLiteOk) {
168       state.SkipWithError("failed to invoke TFLite interpreter");
169       return;
170     }
171   }
172 
173   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
174   if (cpu_frequency != 0) {
175     state.counters["cpufreq"] = cpu_frequency;
176   }
177 
178   state.counters["elements"] =
179     benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
180 
181   const size_t bytes_per_iteration = 2 * batch_size * sizeof(float);
182   state.counters["bytes"] =
183     benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
184 
185   interpreter.reset();
186 }
187 #endif  // BENCHMARK_TENSORFLOW_LITE
188 
189 BENCHMARK(xnnpack_abs_f32)
190   ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
191   ->UseRealTime();
192 
193 #ifdef BENCHMARK_TENSORFLOW_LITE
194   BENCHMARK(tflite_abs_f32)
195     ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
196     ->UseRealTime();
197 #endif  // BENCHMARK_TENSORFLOW_LITE
198 
199 #ifndef XNNPACK_BENCHMARK_NO_MAIN
200 BENCHMARK_MAIN();
201 #endif
202