blob: 7f261bb282b9c5e37ed23fde7a2ae00b70667225 [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// This source code is licensed under the BSD-style license found in the
5// LICENSE file in the root directory of this source tree.
6
7#include <algorithm>
8#include <cmath>
9#include <functional>
10#include <random>
11#include <vector>
12
13#include <xnnpack.h>
14
15#include <benchmark/benchmark.h>
Frank Barchardbb4c18b2019-09-30 11:05:52 -070016#include "bench/utils.h"
Marat Dukhan9c0db962020-01-28 12:30:14 -080017#ifdef BENCHMARK_TENSORFLOW_LITE
18#include "flatbuffers/include/flatbuffers/flatbuffers.h"
19#include "tensorflow/lite/interpreter.h"
20#include "tensorflow/lite/kernels/register.h"
21#include "tensorflow/lite/model.h"
22#include "tensorflow/lite/schema/schema_generated.h"
23#include "tensorflow/lite/version.h"
24#endif // BENCHMARK_TENSORFLOW_LITE
XNNPACK Teamb455b122019-09-27 18:10:33 -070025
26
Marat Dukhan9c0db962020-01-28 12:30:14 -080027static void xnnpack_softmax_q8(benchmark::State& state) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070028 const size_t batch_size = static_cast<size_t>(state.range(0));
29 const size_t channels = static_cast<size_t>(state.range(1));
30
31 std::random_device random_device;
32 auto rng = std::mt19937(random_device());
33 auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng);
34
35 std::vector<uint8_t> input(batch_size * channels);
36 std::vector<uint8_t> output(batch_size * channels);
37 std::generate(input.begin(), input.end(), std::ref(u8rng));
38 std::fill(output.begin(), output.end(), 0xA5);
39
Marat Dukhan04f03be2019-11-19 12:36:47 -080040 xnn_status status = xnn_initialize(nullptr /* allocator */);
XNNPACK Teamb455b122019-09-27 18:10:33 -070041 if (status != xnn_status_success) {
42 state.SkipWithError("failed to initialize XNNPACK");
43 return;
44 }
45
Marat Dukhanfd8e6892020-01-27 15:25:25 -080046 xnn_operator_t softmax_op = nullptr;
47 status = xnn_create_softmax_nc_q8(
XNNPACK Teamb455b122019-09-27 18:10:33 -070048 channels, channels /* input stride */, channels /* output stride */,
49 1.0f /* input scale */,
50 0 /* output zero point */, 1.0f / 256.0f /* output scale */,
Marat Dukhanfd8e6892020-01-27 15:25:25 -080051 0 /* flags */, &softmax_op);
52 if (status != xnn_status_success || softmax_op == nullptr) {
53 state.SkipWithError("failed to create SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070054 return;
55 }
56
Marat Dukhanfd8e6892020-01-27 15:25:25 -080057 status = xnn_setup_softmax_nc_q8(
58 softmax_op,
XNNPACK Teamb455b122019-09-27 18:10:33 -070059 batch_size,
60 input.data(), output.data(),
61 nullptr /* thread pool */);
62 if (status != xnn_status_success) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080063 state.SkipWithError("failed to setup SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070064 return;
65 }
66
67 for (auto _ : state) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080068 status = xnn_run_operator(softmax_op, nullptr /* thread pool */);
XNNPACK Teamb455b122019-09-27 18:10:33 -070069 if (status != xnn_status_success) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080070 state.SkipWithError("failed to run SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070071 return;
72 }
73 }
74
Marat Dukhanfd8e6892020-01-27 15:25:25 -080075 status = xnn_delete_operator(softmax_op);
XNNPACK Teamb455b122019-09-27 18:10:33 -070076 if (status != xnn_status_success) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080077 state.SkipWithError("failed to delete SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070078 return;
79 }
80
Frank Barchardbb4c18b2019-09-30 11:05:52 -070081 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
82
XNNPACK Teamb455b122019-09-27 18:10:33 -070083 const size_t elements_per_iteration = batch_size * channels;
84 state.counters["elements"] =
85 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
86
87 const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(uint8_t);
88 state.counters["bytes"] =
89 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
90}
91
Marat Dukhan9c0db962020-01-28 12:30:14 -080092static void xnnpack_softmax_f32(benchmark::State& state) {
93 const size_t batch_size = static_cast<size_t>(state.range(0));
94 const size_t channels = static_cast<size_t>(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>(-100.0f, 100.0f), rng);
99
100 std::vector<float> input(batch_size * channels + XNN_EXTRA_BYTES / sizeof(float));
101 std::vector<float> output(batch_size * channels);
102 std::generate(input.begin(), input.end(), std::ref(f32rng));
103 std::fill(output.begin(), output.end(), std::nanf(""));
104
105 xnn_status status = xnn_initialize(nullptr /* allocator */);
106 if (status != xnn_status_success) {
107 state.SkipWithError("failed to initialize XNNPACK");
108 return;
109 }
110
111 xnn_operator_t softmax_op = nullptr;
112 status = xnn_create_softmax_nc_f32(
113 channels, channels /* input stride */, channels /* output stride */,
114 0 /* flags */, &softmax_op);
115 if (status != xnn_status_success || softmax_op == nullptr) {
116 state.SkipWithError("failed to create SoftMax operator");
117 return;
118 }
119
120 status = xnn_setup_softmax_nc_f32(
121 softmax_op,
122 batch_size,
123 input.data(), output.data(),
124 nullptr /* thread pool */);
125 if (status != xnn_status_success) {
126 state.SkipWithError("failed to setup SoftMax operator");
127 return;
128 }
129
130 for (auto _ : state) {
131 status = xnn_run_operator(softmax_op, nullptr /* thread pool */);
132 if (status != xnn_status_success) {
133 state.SkipWithError("failed to run SoftMax operator");
134 return;
135 }
136 }
137
138 status = xnn_delete_operator(softmax_op);
139 if (status != xnn_status_success) {
140 state.SkipWithError("failed to delete SoftMax operator");
141 return;
142 }
143
144 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
145
146 const size_t elements_per_iteration = batch_size * channels;
147 state.counters["elements"] =
148 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
149
150 const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float);
151 state.counters["bytes"] =
152 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
153}
154
155#ifdef BENCHMARK_TENSORFLOW_LITE
156static void tflite_softmax_f32(benchmark::State& state) {
157 const size_t batch_size = state.range(0);
158 const size_t channels = state.range(1);
159
160 std::random_device random_device;
161 auto rng = std::mt19937(random_device());
162 auto f32rng = std::bind(std::uniform_real_distribution<float>(-100.0f, 100.0f), rng);
163
164 flatbuffers::FlatBufferBuilder builder;
165 flatbuffers::Offset<tflite::OperatorCode> operator_code =
166 tflite::CreateOperatorCode(builder, tflite::BuiltinOperator_SOFTMAX);
167
168 flatbuffers::Offset<tflite::SoftmaxOptions> softmax_options =
169 tflite::CreateSoftmaxOptions(builder, 1.0f /* beta */);
170
171 flatbuffers::Offset<tflite::Buffer> buffers[1] = {
172 tflite::CreateBuffer(builder, builder.CreateVector({})),
173 };
174
175 const int32_t input_shape[4] = {
176 static_cast<int32_t>(batch_size),
177 static_cast<int32_t>(1 /* height */),
178 static_cast<int32_t>(1 /* width */),
179 static_cast<int32_t>(channels)
180 };
181 const int32_t output_shape[4] = {
182 static_cast<int32_t>(batch_size),
183 static_cast<int32_t>(1 /* height */),
184 static_cast<int32_t>(1 /* width */),
185 static_cast<int32_t>(channels)
186 };
187
188 flatbuffers::Offset<tflite::Tensor> tensors[2] = {
189 tflite::CreateTensor(builder,
190 builder.CreateVector<int32_t>(input_shape, 4),
191 tflite::TensorType_FLOAT32),
192 tflite::CreateTensor(builder,
193 builder.CreateVector<int32_t>(output_shape, 4),
194 tflite::TensorType_FLOAT32),
195 };
196
197 const int32_t op_inputs[1] = { 0 };
198 const int32_t op_outputs[1] = { 1 };
199 flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator(
200 builder,
201 0 /* opcode_index */,
202 builder.CreateVector<int32_t>(op_inputs, 1),
203 builder.CreateVector<int32_t>(op_outputs, 1),
204 tflite::BuiltinOptions_SoftmaxOptions, softmax_options.Union());
205
206 const int32_t graph_inputs[1] = { 0 };
207 const int32_t graph_outputs[1] = { 1 };
208 flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph(
209 builder,
210 builder.CreateVector(tensors, 2),
211 builder.CreateVector<int32_t>(graph_inputs, 1),
212 builder.CreateVector<int32_t>(graph_outputs, 1),
213 builder.CreateVector(&op, 1));
214
215 flatbuffers::Offset<flatbuffers::String> description = builder.CreateString("Softmax model");
216
217 flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder,
218 TFLITE_SCHEMA_VERSION,
219 builder.CreateVector(&operator_code, 1),
220 builder.CreateVector(&subgraph, 1),
221 description,
222 builder.CreateVector(buffers, 1));
223
224 builder.Finish(model_buffer);
225
226 const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer());
227 tflite::ops::builtin::BuiltinOpResolver resolver;
228 tflite::InterpreterBuilder interpreterBuilder(model, resolver);
229 std::unique_ptr<tflite::Interpreter> interpreter;
230 if (interpreterBuilder(&interpreter) != kTfLiteOk) {
231 state.SkipWithError("failed to create TFLite interpreter");
232 return;
233 }
234 if (interpreter == nullptr) {
235 state.SkipWithError("TFLite interpreter is null");
236 return;
237 }
238 interpreter->SetNumThreads(1);
239
240 if (interpreter->AllocateTensors() != kTfLiteOk) {
241 state.SkipWithError("failed to allocate tensors");
242 return;
243 }
244
245 std::generate(
246 interpreter->typed_tensor<float>(0),
247 interpreter->typed_tensor<float>(0) + batch_size * channels,
248 std::ref(f32rng));
249
250 for (auto _ : state) {
251 if (interpreter->Invoke() != kTfLiteOk) {
252 state.SkipWithError("failed to invoke TFLite interpreter");
253 return;
254 }
255 }
256
257 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
258
259 const size_t elements_per_iteration = batch_size * channels;
260 state.counters["elements"] =
261 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
262
263 const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float);
264 state.counters["bytes"] =
265 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
266
267 interpreter.reset();
268}
269#endif // BENCHMARK_TENSORFLOW_LITE
270
XNNPACK Teamb455b122019-09-27 18:10:33 -0700271static void CharacteristicArguments(benchmark::internal::Benchmark* b)
272{
273 b->ArgNames({"N", "C"});
274
275 // CIFAR-10
276 b->Args({1, 10});
277 // CIFAR-100 */
278 b->Args({1, 100});
279 // ImageNet-1K
280 b->Args({1, 1000});
281 // ImageNet-1K+1
282 b->Args({1, 1001});
283 // ImageNet-22K
284 b->Args({1, 21841});
285}
286
Marat Dukhan9c0db962020-01-28 12:30:14 -0800287BENCHMARK(xnnpack_softmax_q8)->Apply(CharacteristicArguments)->UseRealTime();
288BENCHMARK(xnnpack_softmax_f32)->Apply(CharacteristicArguments)->UseRealTime();
289#ifdef BENCHMARK_TENSORFLOW_LITE
290BENCHMARK(tflite_softmax_f32)->Apply(CharacteristicArguments)->UseRealTime();
291#endif // BENCHMARK_TENSORFLOW_LITE
XNNPACK Teamb455b122019-09-27 18:10:33 -0700292
293#ifndef XNNPACK_BENCHMARK_NO_MAIN
294BENCHMARK_MAIN();
295#endif