blob: 9876e72865347303f1065fd60ecaec92b986ffaa [file] [log] [blame]
Marat Dukhan1b092292019-11-18 08:46:36 -08001// Copyright 2019 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
Marat Dukhan95b22432019-10-30 16:30:14 -07006#include <algorithm>
7#include <cfloat>
8#include <cmath>
9#include <functional>
10#include <random>
11#include <vector>
12
13#include <xnnpack.h>
14
15#include <benchmark/benchmark.h>
Marat Dukhan1b092292019-11-18 08:46:36 -080016#include "bench/utils.h"
Marat Dukhan95b22432019-10-30 16:30:14 -070017#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
25
26
27void xnnpack_prelu_f32(benchmark::State& state, const char* net) {
28 const size_t batch_size = state.range(0);
29 const size_t height = state.range(1);
30 const size_t width = state.range(2);
31 const size_t channels = state.range(3);
32
33 std::random_device random_device;
34 auto rng = std::mt19937(random_device());
35 auto f32irng = std::bind(std::uniform_real_distribution<float>(-1.0f, 1.0f), rng);
36 auto f32wrng = std::bind(std::uniform_real_distribution<float>(0.25f, 0.75f), rng);
37
38 std::vector<float> input(batch_size * height * width * channels + XNN_EXTRA_BYTES / sizeof(float));
39 std::generate(input.begin(), input.end(), std::ref(f32irng));
40 std::vector<float> slope(channels);
41 std::generate(slope.begin(), slope.end(), std::ref(f32wrng));
42 std::vector<float> output(batch_size * height * width * channels);
43
Marat Dukhan04f03be2019-11-19 12:36:47 -080044 xnn_status status = xnn_initialize(nullptr /* allocator */);
Marat Dukhan95b22432019-10-30 16:30:14 -070045 if (status != xnn_status_success) {
46 state.SkipWithError("failed to initialize XNNPACK");
47 return;
48 }
49
50 xnn_operator_t prelu_op = nullptr;
51 status = xnn_create_prelu_nc_f32(
52 channels, channels /* input stride */, channels /* output stride */,
53 slope.data(),
Marat Dukhan95b22432019-10-30 16:30:14 -070054 0 /* flags */, &prelu_op);
55 if (status != xnn_status_success) {
56 state.SkipWithError("failed to create FP32 PReLU operator");
57 return;
58 }
59
60 status = xnn_setup_prelu_nc_f32(
61 prelu_op,
62 batch_size * height * width,
63 input.data(), output.data(),
64 nullptr /* thread pool */);
65 if (status != xnn_status_success) {
66 state.SkipWithError("failed to setup FP32 PReLU operator");
67 return;
68 }
69
70 for (auto _ : state) {
71 status = xnn_run_operator(prelu_op, nullptr /* thread pool */);
72 if (status != xnn_status_success) {
73 state.SkipWithError("failed to run FP32 PReLU operator");
74 return;
75 }
76 }
77
78 status = xnn_delete_operator(prelu_op);
79 if (status != xnn_status_success) {
80 state.SkipWithError("failed to delete FP32 PReLU operator");
81 return;
82 }
83 prelu_op = nullptr;
84
Marat Dukhan1b092292019-11-18 08:46:36 -080085 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
86
Marat Dukhan95b22432019-10-30 16:30:14 -070087 const size_t elements_per_iteration = batch_size * height * width * channels;
88 state.counters["elements"] =
89 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
90
Marat Dukhan1b092292019-11-18 08:46:36 -080091 const size_t bytes_per_iteration = (2 * elements_per_iteration + channels) * sizeof(float);
Marat Dukhan95b22432019-10-30 16:30:14 -070092 state.counters["bytes"] =
93 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
94}
95
96#ifdef BENCHMARK_TENSORFLOW_LITE
97void tflite_prelu_f32(benchmark::State& state, const char* net) {
98 const size_t batch_size = state.range(0);
99 const size_t height = state.range(1);
100 const size_t width = state.range(2);
101 const size_t channels = state.range(3);
102
103 std::random_device random_device;
104 auto rng = std::mt19937(random_device());
105 auto f32irng = std::bind(std::uniform_real_distribution<float>(-1.0f, 1.0f), rng);
106 auto f32wrng = std::bind(std::uniform_real_distribution<float>(0.25f, 0.75f), rng);
107
108 std::vector<float> slope(channels);
109 std::generate(slope.begin(), slope.end(), std::ref(f32wrng));
110
111 flatbuffers::FlatBufferBuilder builder;
112 flatbuffers::Offset<tflite::OperatorCode> operator_code =
Marat Dukhan1b092292019-11-18 08:46:36 -0800113 CreateOperatorCode(builder, tflite::BuiltinOperator_PRELU);
Marat Dukhan95b22432019-10-30 16:30:14 -0700114
Marat Dukhan1b092292019-11-18 08:46:36 -0800115 flatbuffers::Offset<tflite::Buffer> buffers[2] = {
Marat Dukhan95b22432019-10-30 16:30:14 -0700116 tflite::CreateBuffer(builder, builder.CreateVector({})),
117 tflite::CreateBuffer(builder, builder.CreateVector(
118 reinterpret_cast<const uint8_t*>(slope.data()),
119 sizeof(float) * slope.size())),
120 };
121
122 const int32_t input_shape[4] = {
123 static_cast<int32_t>(batch_size),
124 static_cast<int32_t>(height),
125 static_cast<int32_t>(width),
126 static_cast<int32_t>(channels)
127 };
128 const int32_t output_shape[4] = {
129 static_cast<int32_t>(batch_size),
130 static_cast<int32_t>(height),
131 static_cast<int32_t>(width),
132 static_cast<int32_t>(channels)
133 };
134 const int32_t slope_shape[1] = {
135 static_cast<int32_t>(channels)
136 };
137
138 flatbuffers::Offset<tflite::Tensor> tensors[3] = {
139 tflite::CreateTensor(builder,
140 builder.CreateVector<int32_t>(input_shape, 4),
Marat Dukhan1b092292019-11-18 08:46:36 -0800141 tflite::TensorType_FLOAT32),
Marat Dukhan95b22432019-10-30 16:30:14 -0700142 tflite::CreateTensor(builder,
143 builder.CreateVector<int32_t>(slope_shape, 1),
144 tflite::TensorType_FLOAT32,
Marat Dukhan1b092292019-11-18 08:46:36 -0800145 1 /* buffer id */),
Marat Dukhan95b22432019-10-30 16:30:14 -0700146 tflite::CreateTensor(builder,
147 builder.CreateVector<int32_t>(output_shape, 4),
Marat Dukhan1b092292019-11-18 08:46:36 -0800148 tflite::TensorType_FLOAT32),
Marat Dukhan95b22432019-10-30 16:30:14 -0700149 };
150
151 const int32_t op_inputs[2] = { 0, 1 };
152 const int32_t op_outputs[1] = { 2 };
153 flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator(
154 builder,
155 0 /* opcode_index */,
156 builder.CreateVector<int32_t>(op_inputs, 2),
157 builder.CreateVector<int32_t>(op_outputs, 1));
158
159 const int32_t graph_inputs[1] = { 0 };
160 const int32_t graph_outputs[1] = { 2 };
161 flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph(
162 builder,
163 builder.CreateVector(tensors, 3),
164 builder.CreateVector<int32_t>(graph_inputs, 1),
165 builder.CreateVector<int32_t>(graph_outputs, 1),
Marat Dukhan1b092292019-11-18 08:46:36 -0800166 builder.CreateVector(&op, 1));
Marat Dukhan95b22432019-10-30 16:30:14 -0700167
168 flatbuffers::Offset<flatbuffers::String> description = builder.CreateString("PReLU model");
169
170 flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder,
171 TFLITE_SCHEMA_VERSION,
172 builder.CreateVector(&operator_code, 1),
173 builder.CreateVector(&subgraph, 1),
174 description,
Marat Dukhan1b092292019-11-18 08:46:36 -0800175 builder.CreateVector(buffers, 2));
Marat Dukhan95b22432019-10-30 16:30:14 -0700176
177 builder.Finish(model_buffer);
178
179 const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer());
180 tflite::ops::builtin::BuiltinOpResolver resolver;
181 tflite::InterpreterBuilder interpreterBuilder(model, resolver);
182 std::unique_ptr<tflite::Interpreter> interpreter;
183 if (interpreterBuilder(&interpreter) != kTfLiteOk) {
184 state.SkipWithError("failed to create TFLite interpreter");
185 return;
186 }
187 if (interpreter == nullptr) {
188 state.SkipWithError("TFLite interpreter is null");
189 return;
190 }
191 interpreter->SetNumThreads(1);
192
193 if (interpreter->AllocateTensors() != kTfLiteOk) {
194 state.SkipWithError("failed to allocate tensors");
195 return;
196 }
197
198 std::generate(
199 interpreter->typed_tensor<float>(0),
200 interpreter->typed_tensor<float>(0) + batch_size * height * width * channels,
201 std::ref(f32irng));
202
203 for (auto _ : state) {
204 if (interpreter->Invoke() != kTfLiteOk) {
205 state.SkipWithError("failed to invoke TFLite interpreter");
206 return;
207 }
208 }
209
Marat Dukhan1b092292019-11-18 08:46:36 -0800210 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
211
Marat Dukhan95b22432019-10-30 16:30:14 -0700212 const size_t elements_per_iteration = batch_size * height * width * channels;
213 state.counters["elements"] =
214 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
215
Marat Dukhan1b092292019-11-18 08:46:36 -0800216 const size_t bytes_per_iteration = (2 * elements_per_iteration + channels) * sizeof(float);
Marat Dukhan95b22432019-10-30 16:30:14 -0700217 state.counters["bytes"] =
218 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
219
220 interpreter.reset();
221}
222#endif // BENCHMARK_TENSORFLOW_LITE
223
224// Characteristic arguments for ImageNet classification models
225static void ImageNet(benchmark::internal::Benchmark* b)
226{
227 b->ArgNames({"N", "H", "W", "C"});
228
229 int32_t c = 16;
230 for (int32_t hw = 224 / 2; hw >= 7; hw /= 2) {
231 b->Args({1, hw, hw, c});
232 b->Args({1, hw, hw, c * 2});
233 c *= 2;
234 }
235}
236
237BENCHMARK_CAPTURE(xnnpack_prelu_f32, imagenet, "ImageNet 224x224")->Apply(ImageNet)->UseRealTime();
238
239#ifdef BENCHMARK_TENSORFLOW_LITE
240 BENCHMARK_CAPTURE(tflite_prelu_f32, imagenet, "ImageNet 224x224")->Apply(ImageNet)->UseRealTime();
241#endif // BENCHMARK_TENSORFLOW_LITE
242
243#ifndef XNNPACK_BENCHMARK_NO_MAIN
244BENCHMARK_MAIN();
245#endif