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 | 9c0db96 | 2020-01-28 12:30:14 -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 | |
Chao Mei | c664027 | 2020-07-23 09:35:11 -0700 | [diff] [blame] | 26 | #ifndef XNN_NO_QU8_OPERATORS |
Marat Dukhan | 08b7a97 | 2020-07-14 18:17:29 -0700 | [diff] [blame] | 27 | static void xnnpack_softmax_qu8(benchmark::State& state) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 28 | 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()); |
Marat Dukhan | 5ce30d9 | 2020-04-14 03:31:26 -0700 | [diff] [blame] | 33 | auto u8rng = std::bind(std::uniform_int_distribution<uint32_t>(0, std::numeric_limits<uint8_t>::max()), rng); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 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 | |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 46 | xnn_operator_t softmax_op = nullptr; |
Marat Dukhan | 08b7a97 | 2020-07-14 18:17:29 -0700 | [diff] [blame] | 47 | status = xnn_create_softmax_nc_qu8( |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 48 | channels, channels /* input stride */, channels /* output stride */, |
| 49 | 1.0f /* input scale */, |
| 50 | 0 /* output zero point */, 1.0f / 256.0f /* output scale */, |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 51 | 0 /* flags */, &softmax_op); |
| 52 | if (status != xnn_status_success || softmax_op == nullptr) { |
| 53 | state.SkipWithError("failed to create SoftMax operator"); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
Marat Dukhan | 08b7a97 | 2020-07-14 18:17:29 -0700 | [diff] [blame] | 57 | status = xnn_setup_softmax_nc_qu8( |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 58 | softmax_op, |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 59 | batch_size, |
| 60 | input.data(), output.data(), |
| 61 | nullptr /* thread pool */); |
| 62 | if (status != xnn_status_success) { |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 63 | state.SkipWithError("failed to setup SoftMax operator"); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | |
| 67 | for (auto _ : state) { |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 68 | status = xnn_run_operator(softmax_op, nullptr /* thread pool */); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 69 | if (status != xnn_status_success) { |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 70 | state.SkipWithError("failed to run SoftMax operator"); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | } |
| 74 | |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 75 | status = xnn_delete_operator(softmax_op); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 76 | if (status != xnn_status_success) { |
Marat Dukhan | fd8e689 | 2020-01-27 15:25:25 -0800 | [diff] [blame] | 77 | state.SkipWithError("failed to delete SoftMax operator"); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 81 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 82 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 83 | 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 Dukhan | 9c0db96 | 2020-01-28 12:30:14 -0800 | [diff] [blame] | 92 | static 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 | } |
Chao Mei | c664027 | 2020-07-23 09:35:11 -0700 | [diff] [blame] | 154 | #endif // XNN_NO_QU8_OPERATORS |
Marat Dukhan | 9c0db96 | 2020-01-28 12:30:14 -0800 | [diff] [blame] | 155 | |
| 156 | #ifdef BENCHMARK_TENSORFLOW_LITE |
| 157 | static void tflite_softmax_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>(-100.0f, 100.0f), rng); |
| 164 | |
| 165 | flatbuffers::FlatBufferBuilder builder; |
| 166 | flatbuffers::Offset<tflite::OperatorCode> operator_code = |
| 167 | tflite::CreateOperatorCode(builder, tflite::BuiltinOperator_SOFTMAX); |
| 168 | |
| 169 | flatbuffers::Offset<tflite::SoftmaxOptions> softmax_options = |
| 170 | tflite::CreateSoftmaxOptions(builder, 1.0f /* beta */); |
| 171 | |
| 172 | flatbuffers::Offset<tflite::Buffer> buffers[1] = { |
| 173 | tflite::CreateBuffer(builder, builder.CreateVector({})), |
| 174 | }; |
| 175 | |
| 176 | const int32_t input_shape[4] = { |
| 177 | static_cast<int32_t>(batch_size), |
| 178 | static_cast<int32_t>(1 /* height */), |
| 179 | static_cast<int32_t>(1 /* width */), |
| 180 | static_cast<int32_t>(channels) |
| 181 | }; |
| 182 | const int32_t output_shape[4] = { |
| 183 | static_cast<int32_t>(batch_size), |
| 184 | static_cast<int32_t>(1 /* height */), |
| 185 | static_cast<int32_t>(1 /* width */), |
| 186 | static_cast<int32_t>(channels) |
| 187 | }; |
| 188 | |
| 189 | flatbuffers::Offset<tflite::Tensor> tensors[2] = { |
| 190 | tflite::CreateTensor(builder, |
| 191 | builder.CreateVector<int32_t>(input_shape, 4), |
| 192 | tflite::TensorType_FLOAT32), |
| 193 | tflite::CreateTensor(builder, |
| 194 | builder.CreateVector<int32_t>(output_shape, 4), |
| 195 | tflite::TensorType_FLOAT32), |
| 196 | }; |
| 197 | |
| 198 | const int32_t op_inputs[1] = { 0 }; |
| 199 | const int32_t op_outputs[1] = { 1 }; |
| 200 | flatbuffers::Offset<tflite::Operator> op = tflite::CreateOperator( |
| 201 | builder, |
| 202 | 0 /* opcode_index */, |
| 203 | builder.CreateVector<int32_t>(op_inputs, 1), |
| 204 | builder.CreateVector<int32_t>(op_outputs, 1), |
| 205 | tflite::BuiltinOptions_SoftmaxOptions, softmax_options.Union()); |
| 206 | |
| 207 | const int32_t graph_inputs[1] = { 0 }; |
| 208 | const int32_t graph_outputs[1] = { 1 }; |
| 209 | flatbuffers::Offset<tflite::SubGraph> subgraph = tflite::CreateSubGraph( |
| 210 | builder, |
| 211 | builder.CreateVector(tensors, 2), |
| 212 | builder.CreateVector<int32_t>(graph_inputs, 1), |
| 213 | builder.CreateVector<int32_t>(graph_outputs, 1), |
| 214 | builder.CreateVector(&op, 1)); |
| 215 | |
| 216 | flatbuffers::Offset<flatbuffers::String> description = builder.CreateString("Softmax model"); |
| 217 | |
| 218 | flatbuffers::Offset<tflite::Model> model_buffer = tflite::CreateModel(builder, |
| 219 | TFLITE_SCHEMA_VERSION, |
| 220 | builder.CreateVector(&operator_code, 1), |
| 221 | builder.CreateVector(&subgraph, 1), |
| 222 | description, |
| 223 | builder.CreateVector(buffers, 1)); |
| 224 | |
| 225 | builder.Finish(model_buffer); |
| 226 | |
| 227 | const tflite::Model* model = tflite::GetModel(builder.GetBufferPointer()); |
| 228 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 229 | tflite::InterpreterBuilder interpreterBuilder(model, resolver); |
| 230 | std::unique_ptr<tflite::Interpreter> interpreter; |
| 231 | if (interpreterBuilder(&interpreter) != kTfLiteOk) { |
| 232 | state.SkipWithError("failed to create TFLite interpreter"); |
| 233 | return; |
| 234 | } |
| 235 | if (interpreter == nullptr) { |
| 236 | state.SkipWithError("TFLite interpreter is null"); |
| 237 | return; |
| 238 | } |
| 239 | interpreter->SetNumThreads(1); |
| 240 | |
| 241 | if (interpreter->AllocateTensors() != kTfLiteOk) { |
| 242 | state.SkipWithError("failed to allocate tensors"); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | std::generate( |
| 247 | interpreter->typed_tensor<float>(0), |
| 248 | interpreter->typed_tensor<float>(0) + batch_size * channels, |
| 249 | std::ref(f32rng)); |
| 250 | |
| 251 | for (auto _ : state) { |
| 252 | if (interpreter->Invoke() != kTfLiteOk) { |
| 253 | state.SkipWithError("failed to invoke TFLite interpreter"); |
| 254 | return; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 259 | |
| 260 | const size_t elements_per_iteration = batch_size * channels; |
| 261 | state.counters["elements"] = |
| 262 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 263 | |
| 264 | const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float); |
| 265 | state.counters["bytes"] = |
| 266 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 267 | |
| 268 | interpreter.reset(); |
| 269 | } |
| 270 | #endif // BENCHMARK_TENSORFLOW_LITE |
| 271 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 272 | static void CharacteristicArguments(benchmark::internal::Benchmark* b) |
| 273 | { |
| 274 | b->ArgNames({"N", "C"}); |
| 275 | |
| 276 | // CIFAR-10 |
| 277 | b->Args({1, 10}); |
| 278 | // CIFAR-100 */ |
| 279 | b->Args({1, 100}); |
| 280 | // ImageNet-1K |
| 281 | b->Args({1, 1000}); |
| 282 | // ImageNet-1K+1 |
| 283 | b->Args({1, 1001}); |
| 284 | // ImageNet-22K |
| 285 | b->Args({1, 21841}); |
| 286 | } |
| 287 | |
Chao Mei | c664027 | 2020-07-23 09:35:11 -0700 | [diff] [blame] | 288 | #ifndef XNN_NO_QU8_OPERATORS |
Marat Dukhan | 08b7a97 | 2020-07-14 18:17:29 -0700 | [diff] [blame] | 289 | BENCHMARK(xnnpack_softmax_qu8)->Apply(CharacteristicArguments)->UseRealTime(); |
Chao Mei | c664027 | 2020-07-23 09:35:11 -0700 | [diff] [blame] | 290 | #endif // XNN_NO_QU8_OPERATORS |
| 291 | |
Marat Dukhan | 9c0db96 | 2020-01-28 12:30:14 -0800 | [diff] [blame] | 292 | BENCHMARK(xnnpack_softmax_f32)->Apply(CharacteristicArguments)->UseRealTime(); |
| 293 | #ifdef BENCHMARK_TENSORFLOW_LITE |
| 294 | BENCHMARK(tflite_softmax_f32)->Apply(CharacteristicArguments)->UseRealTime(); |
| 295 | #endif // BENCHMARK_TENSORFLOW_LITE |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 296 | |
| 297 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 298 | BENCHMARK_MAIN(); |
| 299 | #endif |