XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 1 | // 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 Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 16 | #include "bench/utils.h" |
Marat Dukhan | c3b9e86 | 2019-11-17 13:18:54 -0800 | [diff] [blame] | 17 | #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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 25 | |
| 26 | |
Marat Dukhan | c3b9e86 | 2019-11-17 13:18:54 -0800 | [diff] [blame] | 27 | static void xnnpack_sigmoid_q8(benchmark::State& state) { |
| 28 | const size_t batch_size = state.range(0); |
| 29 | const size_t channels = state.range(1); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 40 | xnn_status status = xnn_initialize(nullptr /* allocator */); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 41 | if (status != xnn_status_success) { |
| 42 | state.SkipWithError("failed to initialize XNNPACK"); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | xnn_operator_t sigmoid_op = nullptr; |
| 47 | status = xnn_create_sigmoid_nc_q8( |
| 48 | channels, channels /* input stride */, channels /* output stride */, |
| 49 | 127 /* input zero point */, 1.0f /* input scale */, |
| 50 | 0 /* output zero point */, 1.0f / 256.0f /* output scale */, |
| 51 | 0 /* output min */, 255 /* output max */, |
| 52 | 0 /* flags */, &sigmoid_op); |
| 53 | if (status != xnn_status_success || sigmoid_op == nullptr) { |
| 54 | state.SkipWithError("failed to create Sigmoid operator"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | status = xnn_setup_sigmoid_nc_q8( |
| 59 | sigmoid_op, |
| 60 | batch_size, |
| 61 | input.data(), output.data(), |
| 62 | nullptr /* thread pool */); |
| 63 | if (status != xnn_status_success) { |
| 64 | state.SkipWithError("failed to setup Sigmoid operator"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | for (auto _ : state) { |
| 69 | status = xnn_run_operator(sigmoid_op, nullptr /* thread pool */); |
| 70 | if (status != xnn_status_success) { |
| 71 | state.SkipWithError("failed to run Sigmoid operator"); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | status = xnn_delete_operator(sigmoid_op); |
| 77 | if (status != xnn_status_success) { |
| 78 | state.SkipWithError("failed to delete Sigmoid operator"); |
| 79 | return; |
| 80 | } |
| 81 | |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 82 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 83 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 84 | const size_t elements_per_iteration = batch_size * channels; |
| 85 | state.counters["elements"] = |
| 86 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 87 | |
| 88 | const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(uint8_t); |
| 89 | state.counters["bytes"] = |
| 90 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 91 | } |
| 92 | |
Marat Dukhan | c3b9e86 | 2019-11-17 13:18:54 -0800 | [diff] [blame] | 93 | static void xnnpack_sigmoid_f32(benchmark::State& state) { |
| 94 | const size_t batch_size = state.range(0); |
| 95 | const size_t channels = state.range(1); |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 96 | |
| 97 | std::random_device random_device; |
| 98 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | c3b9e86 | 2019-11-17 13:18:54 -0800 | [diff] [blame] | 99 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), rng); |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 100 | |
| 101 | std::vector<float> input(batch_size * channels); |
| 102 | std::vector<float> output(batch_size * channels); |
| 103 | std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 104 | std::fill(output.begin(), output.end(), std::nanf("")); |
| 105 | |
Marat Dukhan | 04f03be | 2019-11-19 12:36:47 -0800 | [diff] [blame] | 106 | xnn_status status = xnn_initialize(nullptr /* allocator */); |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 107 | if (status != xnn_status_success) { |
| 108 | state.SkipWithError("failed to initialize XNNPACK"); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | xnn_operator_t sigmoid_op = nullptr; |
| 113 | status = xnn_create_sigmoid_nc_f32( |
| 114 | channels, channels /* input stride */, channels /* output stride */, |
| 115 | 0 /* flags */, &sigmoid_op); |
| 116 | if (status != xnn_status_success || sigmoid_op == nullptr) { |
| 117 | state.SkipWithError("failed to create Sigmoid operator"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | status = xnn_setup_sigmoid_nc_f32( |
| 122 | sigmoid_op, |
| 123 | batch_size, |
| 124 | input.data(), output.data(), |
| 125 | nullptr /* thread pool */); |
| 126 | if (status != xnn_status_success) { |
| 127 | state.SkipWithError("failed to setup Sigmoid operator"); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | for (auto _ : state) { |
| 132 | status = xnn_run_operator(sigmoid_op, nullptr /* thread pool */); |
| 133 | if (status != xnn_status_success) { |
| 134 | state.SkipWithError("failed to run Sigmoid operator"); |
| 135 | return; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | status = xnn_delete_operator(sigmoid_op); |
| 140 | if (status != xnn_status_success) { |
| 141 | state.SkipWithError("failed to delete Sigmoid operator"); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 146 | |
| 147 | const size_t elements_per_iteration = batch_size * channels; |
| 148 | state.counters["elements"] = |
| 149 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 150 | |
| 151 | const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float); |
| 152 | state.counters["bytes"] = |
| 153 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 154 | } |
| 155 | |
Marat Dukhan | c3b9e86 | 2019-11-17 13:18:54 -0800 | [diff] [blame] | 156 | #ifdef BENCHMARK_TENSORFLOW_LITE |
| 157 | static void tflite_sigmoid_f32(benchmark::State& state) { |
| 158 | const size_t batch_size = state.range(0); |
| 159 | const size_t channels = state.range(1); |
| 160 | |
| 161 | std::random_device random_device; |
| 162 | auto rng = std::mt19937(random_device()); |
| 163 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), rng); |
| 164 | |
| 165 | flatbuffers::FlatBufferBuilder builder; |
| 166 | flatbuffers::Offset<tflite::OperatorCode> operator_code = |
| 167 | CreateOperatorCode(builder, tflite::BuiltinOperator_LOGISTIC); |
| 168 | |
| 169 | flatbuffers::Offset<tflite::Buffer> buffers[1] = { |
| 170 | tflite::CreateBuffer(builder, builder.CreateVector({})), |
| 171 | }; |
| 172 | |
| 173 | const int32_t input_shape[4] = { |
| 174 | static_cast<int32_t>(batch_size), |
| 175 | static_cast<int32_t>(1 /* height */), |
| 176 | static_cast<int32_t>(1 /* width */), |
| 177 | static_cast<int32_t>(channels) |
| 178 | }; |
| 179 | const int32_t output_shape[4] = { |
| 180 | static_cast<int32_t>(batch_size), |
| 181 | static_cast<int32_t>(1 /* height */), |
| 182 | static_cast<int32_t>(1 /* width */), |
| 183 | static_cast<int32_t>(channels) |
| 184 | }; |
| 185 | |
| 186 | flatbuffers::Offset<tflite::Tensor> tensors[2] = { |
| 187 | tflite::CreateTensor(builder, |
| 188 | builder.CreateVector<int32_t>(input_shape, 4), |
| 189 | tflite::TensorType_FLOAT32), |
| 190 | tflite::CreateTensor(builder, |
| 191 | builder.CreateVector<int32_t>(output_shape, 4), |
| 192 | tflite::TensorType_FLOAT32), |
| 193 | }; |
| 194 | |
| 195 | const int32_t op_inputs[1] = { 0 }; |
| 196 | const int32_t op_outputs[1] = { 1 }; |
| 197 | flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator( |
| 198 | builder, |
| 199 | 0 /* opcode_index */, |
| 200 | builder.CreateVector<int32_t>(op_inputs, 1), |
| 201 | builder.CreateVector<int32_t>(op_outputs, 1)); |
| 202 | |
| 203 | const int32_t graph_inputs[1] = { 0 }; |
| 204 | const int32_t graph_outputs[1] = { 1 }; |
| 205 | flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph( |
| 206 | builder, |
| 207 | builder.CreateVector(tensors, 2), |
| 208 | builder.CreateVector<int32_t>(graph_inputs, 1), |
| 209 | builder.CreateVector<int32_t>(graph_outputs, 1), |
| 210 | builder.CreateVector(&op, 1)); |
| 211 | |
| 212 | flatbuffers::Offset<flatbuffers::String> description = builder.CreateString("Sigmoid model"); |
| 213 | |
| 214 | flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder, |
| 215 | TFLITE_SCHEMA_VERSION, |
| 216 | builder.CreateVector(&operator_code, 1), |
| 217 | builder.CreateVector(&subgraph, 1), |
| 218 | description, |
| 219 | builder.CreateVector(buffers, 1)); |
| 220 | |
| 221 | builder.Finish(model_buffer); |
| 222 | |
| 223 | const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer()); |
| 224 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 225 | tflite::InterpreterBuilder interpreterBuilder(model, resolver); |
| 226 | std::unique_ptr<tflite::Interpreter> interpreter; |
| 227 | if (interpreterBuilder(&interpreter) != kTfLiteOk) { |
| 228 | state.SkipWithError("failed to create TFLite interpreter"); |
| 229 | return; |
| 230 | } |
| 231 | if (interpreter == nullptr) { |
| 232 | state.SkipWithError("TFLite interpreter is null"); |
| 233 | return; |
| 234 | } |
| 235 | interpreter->SetNumThreads(1); |
| 236 | |
| 237 | if (interpreter->AllocateTensors() != kTfLiteOk) { |
| 238 | state.SkipWithError("failed to allocate tensors"); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | std::generate( |
| 243 | interpreter->typed_tensor<float>(0), |
| 244 | interpreter->typed_tensor<float>(0) + batch_size * channels, |
| 245 | std::ref(f32rng)); |
| 246 | |
| 247 | for (auto _ : state) { |
| 248 | if (interpreter->Invoke() != kTfLiteOk) { |
| 249 | state.SkipWithError("failed to invoke TFLite interpreter"); |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 255 | |
| 256 | const size_t elements_per_iteration = batch_size * channels; |
| 257 | state.counters["elements"] = |
| 258 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 259 | |
| 260 | const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float); |
| 261 | state.counters["bytes"] = |
| 262 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 263 | |
| 264 | interpreter.reset(); |
| 265 | } |
| 266 | #endif // BENCHMARK_TENSORFLOW_LITE |
| 267 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 268 | static void CharacteristicArguments(benchmark::internal::Benchmark* b) |
| 269 | { |
| 270 | b->ArgNames({"N", "C"}); |
| 271 | |
| 272 | int32_t c = 16; |
| 273 | for (int32_t n = 224; n >= 7; n /= 2) { |
| 274 | b->Args({n * n, c}); |
| 275 | c *= 2; |
| 276 | } |
| 277 | } |
| 278 | |
Marat Dukhan | c3b9e86 | 2019-11-17 13:18:54 -0800 | [diff] [blame] | 279 | BENCHMARK(xnnpack_sigmoid_q8)->Apply(CharacteristicArguments)->UseRealTime(); |
| 280 | BENCHMARK(xnnpack_sigmoid_f32)->Apply(CharacteristicArguments)->UseRealTime(); |
| 281 | |
| 282 | #ifdef BENCHMARK_TENSORFLOW_LITE |
| 283 | BENCHMARK(tflite_sigmoid_f32)->Apply(CharacteristicArguments)->UseRealTime(); |
| 284 | #endif // BENCHMARK_TENSORFLOW_LITE |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 285 | |
| 286 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 287 | BENCHMARK_MAIN(); |
| 288 | #endif |