blob: eb43af76380bc879a82bedd6440af73c48233b44 [file] [log] [blame]
Marat Dukhan5c7fd892021-12-30 16:04:23 -08001// 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
28static void xnnpack_leaky_relu_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>(-5.0f, 5.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 leaky_relu_op = nullptr;
47 status = xnn_create_leaky_relu_nc_f32(
48 1 /* channels */, 1 /* input stride */, 1 /* output stride */,
49 0.01f /* negative slope */,
50 0 /* flags */, &leaky_relu_op);
51 if (status != xnn_status_success || leaky_relu_op == nullptr) {
52 state.SkipWithError("failed to create Leaky ReLU operator");
53 return;
54 }
55
56 status = xnn_setup_leaky_relu_nc_f32(
57 leaky_relu_op, batch_size,
58 input.data(), output.data(),
59 nullptr /* thread pool */);
60 if (status != xnn_status_success) {
61 state.SkipWithError("failed to setup Leaky ReLU operator");
62 return;
63 }
64
65 for (auto _ : state) {
66 status = xnn_run_operator(leaky_relu_op, nullptr /* thread pool */);
67 if (status != xnn_status_success) {
68 state.SkipWithError("failed to run Leaky ReLU operator");
69 return;
70 }
71 }
72
73 status = xnn_delete_operator(leaky_relu_op);
74 if (status != xnn_status_success) {
75 state.SkipWithError("failed to delete Leaky ReLU operator");
76 return;
77 }
78
79 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
80 if (cpu_frequency != 0) {
81 state.counters["cpufreq"] = cpu_frequency;
82 }
83
84 state.counters["elements"] =
85 benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
86
87 const size_t bytes_per_iteration = 2 * batch_size * sizeof(float);
88 state.counters["bytes"] =
89 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
90}
91
92#ifdef BENCHMARK_TENSORFLOW_LITE
93static void tflite_leaky_relu_f32(benchmark::State& state) {
94 const size_t batch_size = state.range(0);
95
96 std::random_device random_device;
97 auto rng = std::mt19937(random_device());
98 auto f32rng = std::bind(std::uniform_real_distribution<float>(-5.0f, 5.0f), std::ref(rng));
99
100 flatbuffers::FlatBufferBuilder builder;
101 const flatbuffers::Offset<tflite::OperatorCode> operator_code =
102 CreateOperatorCode(builder, tflite::BuiltinOperator_LEAKY_RELU);
103
104 flatbuffers::Offset<tflite::LeakyReluOptions> leaky_relu_options =
105 tflite::CreateLeakyReluOptions(builder, 0.01f /* alpha */);
106
107 const std::array<flatbuffers::Offset<tflite::Buffer>, 1> buffers{{
108 tflite::CreateBuffer(builder, builder.CreateVector({})),
109 }};
110
111 const std::array<int32_t, 1> shape{{
112 static_cast<int32_t>(batch_size)
113 }};
114
115 const std::array<flatbuffers::Offset<tflite::Tensor>, 2> tensors{{
116 tflite::CreateTensor(builder,
117 builder.CreateVector<int32_t>(shape.data(), shape.size()),
118 tflite::TensorType_FLOAT32),
119 tflite::CreateTensor(builder,
120 builder.CreateVector<int32_t>(shape.data(), shape.size()),
121 tflite::TensorType_FLOAT32),
122 }};
123
124 const std::array<int32_t, 1> op_inputs{{ 0 }};
125 const std::array<int32_t, 1> op_outputs{{ 1 }};
126 flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator(
127 builder,
128 0 /* opcode_index */,
129 builder.CreateVector<int32_t>(op_inputs.data(), op_inputs.size()),
130 builder.CreateVector<int32_t>(op_outputs.data(), op_outputs.size()),
131 tflite::BuiltinOptions_LeakyReluOptions, leaky_relu_options.Union());
132
133 const std::array<int32_t, 1> graph_inputs{{ 0 }};
134 const std::array<int32_t, 1> graph_outputs{{ 1 }};
135 const flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph(
136 builder,
137 builder.CreateVector(tensors.data(), tensors.size()),
138 builder.CreateVector<int32_t>(graph_inputs.data(), graph_inputs.size()),
139 builder.CreateVector<int32_t>(graph_outputs.data(), graph_outputs.size()),
140 builder.CreateVector(&op, 1));
141
142 const flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder,
143 TFLITE_SCHEMA_VERSION,
144 builder.CreateVector(&operator_code, 1),
145 builder.CreateVector(&subgraph, 1),
146 builder.CreateString("Leaky ReLU model"),
147 builder.CreateVector(buffers.data(), buffers.size()));
148
149 builder.Finish(model_buffer);
150
151 const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer());
152 tflite::ops::builtin::BuiltinOpResolverWithoutDefaultDelegates resolver;
153 tflite::InterpreterBuilder interpreterBuilder(model, resolver);
154 std::unique_ptr<tflite::Interpreter> interpreter;
155 if (interpreterBuilder(&interpreter) != kTfLiteOk || interpreter == nullptr) {
156 state.SkipWithError("failed to create TFLite interpreter");
157 return;
158 }
159 interpreter->SetNumThreads(1);
160
161 if (interpreter->AllocateTensors() != kTfLiteOk) {
162 state.SkipWithError("failed to allocate tensors");
163 return;
164 }
165
166 std::generate(
167 interpreter->typed_tensor<float>(0),
168 interpreter->typed_tensor<float>(0) + batch_size,
169 std::ref(f32rng));
170
171 for (auto _ : state) {
172 if (interpreter->Invoke() != kTfLiteOk) {
173 state.SkipWithError("failed to invoke TFLite interpreter");
174 return;
175 }
176 }
177
178 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
179 if (cpu_frequency != 0) {
180 state.counters["cpufreq"] = cpu_frequency;
181 }
182
183 state.counters["elements"] =
184 benchmark::Counter(uint64_t(state.iterations()) * batch_size, benchmark::Counter::kIsRate);
185
186 const size_t bytes_per_iteration = 2 * batch_size * sizeof(float);
187 state.counters["bytes"] =
188 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
189
190 interpreter.reset();
191}
192#endif // BENCHMARK_TENSORFLOW_LITE
193
194BENCHMARK(xnnpack_leaky_relu_f32)
195 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
196 ->UseRealTime();
197
198#ifdef BENCHMARK_TENSORFLOW_LITE
199 BENCHMARK(tflite_leaky_relu_f32)
200 ->Apply(benchmark::utils::UnaryElementwiseParameters<float, float>)
201 ->UseRealTime();
202#endif // BENCHMARK_TENSORFLOW_LITE
203
204#ifndef XNNPACK_BENCHMARK_NO_MAIN
205BENCHMARK_MAIN();
206#endif