blob: 213f5ee70f712086e60ed5287ad972ae89e9d321 [file] [log] [blame]
Marat Dukhan87727142020-06-24 15:24:10 -07001// 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 <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
28static void xnnpack_ceiling_f32(benchmark::State& state) {
29 const size_t batch_size = state.range(0);
30 const size_t channels = state.range(1);
31
32 std::random_device random_device;
33 auto rng = std::mt19937(random_device());
34 auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), rng);
35
36 std::vector<float> input(batch_size * channels);
37 std::vector<float> output(batch_size * channels);
38 std::generate(input.begin(), input.end(), std::ref(f32rng));
39 std::fill(output.begin(), output.end(), std::nanf(""));
40
41 xnn_status status = xnn_initialize(nullptr /* allocator */);
42 if (status != xnn_status_success) {
43 state.SkipWithError("failed to initialize XNNPACK");
44 return;
45 }
46
47 xnn_operator_t ceiling_op = nullptr;
48 status = xnn_create_ceiling_nc_f32(
49 channels, channels /* input stride */, channels /* output stride */,
50 0 /* flags */, &ceiling_op);
51 if (status != xnn_status_success || ceiling_op == nullptr) {
52 state.SkipWithError("failed to create Ceiling operator");
53 return;
54 }
55
56 status = xnn_setup_ceiling_nc_f32(
57 ceiling_op,
58 batch_size,
59 input.data(), output.data(),
60 nullptr /* thread pool */);
61 if (status != xnn_status_success) {
62 state.SkipWithError("failed to setup Ceiling operator");
63 return;
64 }
65
66 for (auto _ : state) {
67 status = xnn_run_operator(ceiling_op, nullptr /* thread pool */);
68 if (status != xnn_status_success) {
69 state.SkipWithError("failed to run Ceiling operator");
70 return;
71 }
72 }
73
74 status = xnn_delete_operator(ceiling_op);
75 if (status != xnn_status_success) {
76 state.SkipWithError("failed to delete Ceiling operator");
77 return;
78 }
79
80 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
81
82 const size_t elements_per_iteration = batch_size * channels;
83 state.counters["elements"] =
84 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
85
86 const size_t bytes_per_iteration = 2 * elements_per_iteration * 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
92static void tflite_ceiling_f32(benchmark::State& state) {
93 const size_t batch_size = state.range(0);
94 const size_t channels = state.range(1);
95
96 std::random_device random_device;
97 auto rng = std::mt19937(random_device());
98 auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), rng);
99
100 flatbuffers::FlatBufferBuilder builder;
101 const flatbuffers::Offset<tflite::OperatorCode> operator_code =
102 CreateOperatorCode(builder, tflite::BuiltinOperator_CEIL);
103
104 const std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers{{
105 tflite::CreateBuffer(builder, builder.CreateVector({})),
106 }};
107
108 const std::array<int32_t, 4> input_shape{{
109 static_cast<int32_t>(batch_size),
110 static_cast<int32_t>(1 /* height */),
111 static_cast<int32_t>(1 /* width */),
112 static_cast<int32_t>(channels)
113 }};
114 const std::array<int32_t, 4> output_shape{{
115 static_cast<int32_t>(batch_size),
116 static_cast<int32_t>(1 /* height */),
117 static_cast<int32_t>(1 /* width */),
118 static_cast<int32_t>(channels)
119 }};
120
121 const std::array<flatbuffers::Offset<tflite::Tensor>, 2> tensors{{
122 tflite::CreateTensor(builder,
123 builder.CreateVector<int32_t>(input_shape.data(), input_shape.size()),
124 tflite::TensorType_FLOAT32),
125 tflite::CreateTensor(builder,
126 builder.CreateVector<int32_t>(output_shape.data(), output_shape.size()),
127 tflite::TensorType_FLOAT32),
128 }};
129
130 const std::array<int32_t, 1> op_inputs{{ 0 }};
131 const std::array<int32_t, 1> op_outputs{{ 1 }};
132 flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator(
133 builder,
134 0 /* opcode_index */,
135 builder.CreateVector<int32_t>(op_inputs.data(), op_inputs.size()),
136 builder.CreateVector<int32_t>(op_outputs.data(), op_outputs.size()));
137
138 const std::array<int32_t, 1> graph_inputs{{ 0 }};
139 const std::array<int32_t, 1> graph_outputs{{ 1 }};
140 const flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph(
141 builder,
142 builder.CreateVector(tensors.data(), tensors.size()),
143 builder.CreateVector<int32_t>(graph_inputs.data(), graph_inputs.size()),
144 builder.CreateVector<int32_t>(graph_outputs.data(), graph_outputs.size()),
145 builder.CreateVector(&op, 1));
146
147 const flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder,
148 TFLITE_SCHEMA_VERSION,
149 builder.CreateVector(&operator_code, 1),
150 builder.CreateVector(&subgraph, 1),
151 builder.CreateString("Ceil model"),
152 builder.CreateVector(buffers.data(), buffers.size()));
153
154 builder.Finish(model_buffer);
155
156 const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer());
157 tflite::ops::builtin::BuiltinOpResolver resolver;
158 tflite::InterpreterBuilder interpreterBuilder(model, resolver);
159 std::unique_ptr<tflite::Interpreter> interpreter;
160 if (interpreterBuilder(&interpreter) != kTfLiteOk) {
161 state.SkipWithError("failed to create TFLite interpreter");
162 return;
163 }
164 if (interpreter == nullptr) {
165 state.SkipWithError("TFLite interpreter is null");
166 return;
167 }
168 interpreter->SetNumThreads(1);
169
170 if (interpreter->AllocateTensors() != kTfLiteOk) {
171 state.SkipWithError("failed to allocate tensors");
172 return;
173 }
174
175 std::generate(
176 interpreter->typed_tensor<float>(0),
177 interpreter->typed_tensor<float>(0) + batch_size * channels,
178 std::ref(f32rng));
179
180 for (auto _ : state) {
181 if (interpreter->Invoke() != kTfLiteOk) {
182 state.SkipWithError("failed to invoke TFLite interpreter");
183 return;
184 }
185 }
186
187 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
188
189 const size_t elements_per_iteration = batch_size * channels;
190 state.counters["elements"] =
191 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
192
193 const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float);
194 state.counters["bytes"] =
195 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
196
197 interpreter.reset();
198}
199#endif // BENCHMARK_TENSORFLOW_LITE
200
201static void CharacteristicArguments(benchmark::internal::Benchmark* b)
202{
203 b->ArgNames({"N", "C"});
204
205 int32_t c = 16;
206 for (int32_t n = 224; n >= 7; n /= 2) {
207 b->Args({n * n, c});
208 c *= 2;
209 }
210}
211
212BENCHMARK(xnnpack_ceiling_f32)->Apply(CharacteristicArguments)->UseRealTime();
213
214#ifdef BENCHMARK_TENSORFLOW_LITE
215 BENCHMARK(tflite_ceiling_f32)->Apply(CharacteristicArguments)->UseRealTime();
216#endif // BENCHMARK_TENSORFLOW_LITE
217
218#ifndef XNNPACK_BENCHMARK_NO_MAIN
219BENCHMARK_MAIN();
220#endif