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 | // Copyright 2019 Google LLC |
| 5 | // |
| 6 | // This source code is licensed under the BSD-style license found in the |
| 7 | // LICENSE file in the root directory of this source tree. |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <cfloat> |
| 11 | #include <cmath> |
| 12 | #include <functional> |
| 13 | #include <random> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include <xnnpack.h> |
| 17 | |
| 18 | #include <benchmark/benchmark.h> |
| 19 | |
| 20 | |
| 21 | void max_pooling_u8(benchmark::State& state, const char* net) { |
| 22 | const size_t batch_size = state.range(0); |
| 23 | const size_t input_height = state.range(1); |
| 24 | const size_t input_width = state.range(2); |
| 25 | const size_t pooling_size = state.range(3); |
| 26 | const size_t padding_size = state.range(4); |
| 27 | const size_t stride = state.range(5); |
| 28 | const size_t channels = state.range(6); |
| 29 | |
| 30 | std::random_device random_device; |
| 31 | auto rng = std::mt19937(random_device()); |
| 32 | auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng); |
| 33 | |
| 34 | const size_t output_height = (2 * padding_size + input_height - pooling_size) / stride + 1; |
| 35 | const size_t output_width = (2 * padding_size + input_width - pooling_size) / stride + 1; |
| 36 | |
| 37 | std::vector<uint8_t> input(batch_size * input_height * input_width * channels); |
| 38 | std::generate(input.begin(), input.end(), std::ref(u8rng)); |
| 39 | std::vector<uint8_t> output(batch_size * output_height * output_width * channels); |
| 40 | std::fill(output.begin(), output.end(), 0xA5); |
| 41 | |
| 42 | xnn_status status = xnn_initialize(); |
| 43 | if (status != xnn_status_success) { |
| 44 | state.SkipWithError("failed to initialize XNNPACK"); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | xnn_operator_t pooling_op = nullptr; |
| 49 | status = xnn_create_max_pooling2d_nhwc_u8( |
| 50 | padding_size, padding_size, padding_size, padding_size, |
| 51 | pooling_size, pooling_size, |
| 52 | stride, stride, |
| 53 | 1 /* dilation height */, 1 /* dilation width */, |
| 54 | channels, channels /* input pixel stride */, channels /* output pixel stride */, |
| 55 | 0, 255, |
| 56 | 0 /* flags */, &pooling_op); |
| 57 | if (status != xnn_status_success) { |
| 58 | state.SkipWithError("failed to create Max Pooling operator"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | status = xnn_setup_max_pooling2d_nhwc_u8( |
| 63 | pooling_op, |
| 64 | batch_size, input_height, input_width, |
| 65 | input.data(), output.data(), |
| 66 | nullptr /* thread pool */); |
| 67 | if (status != xnn_status_success) { |
| 68 | state.SkipWithError("failed to setup Max Pooling operator"); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | for (auto _ : state) { |
| 73 | status = xnn_run_operator(pooling_op, nullptr /* thread pool */); |
| 74 | if (status != xnn_status_success) { |
| 75 | state.SkipWithError("failed to run Max Pooling operator"); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | status = xnn_delete_operator(pooling_op); |
| 81 | if (status != xnn_status_success) { |
| 82 | state.SkipWithError("failed to delete Max Pooling operator"); |
| 83 | return; |
| 84 | } |
| 85 | pooling_op = nullptr; |
| 86 | |
| 87 | state.counters["bytes"] = benchmark::Counter( |
| 88 | uint64_t(state.iterations()) * |
| 89 | batch_size * (input_height * input_width + output_height * output_width) * channels * sizeof(uint8_t), |
| 90 | benchmark::Counter::kIsRate); |
| 91 | } |
| 92 | |
| 93 | void max_pooling_f32(benchmark::State& state, const char* net) { |
| 94 | const size_t batch_size = state.range(0); |
| 95 | const size_t input_height = state.range(1); |
| 96 | const size_t input_width = state.range(2); |
| 97 | const size_t pooling_size = state.range(3); |
| 98 | const size_t padding_size = state.range(4); |
| 99 | const size_t stride = state.range(5); |
| 100 | const size_t channels = state.range(6); |
| 101 | |
| 102 | std::random_device random_device; |
| 103 | auto rng = std::mt19937(random_device()); |
| 104 | auto f32rng = std::bind(std::uniform_real_distribution<float>(0.0f, 1.0f), rng); |
| 105 | |
| 106 | const size_t output_height = (2 * padding_size + input_height - pooling_size) / stride + 1; |
| 107 | const size_t output_width = (2 * padding_size + input_width - pooling_size) / stride + 1; |
| 108 | |
| 109 | std::vector<float> input(batch_size * input_height * input_width * channels); |
| 110 | std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 111 | std::vector<float> output(batch_size * output_height * output_width * channels); |
| 112 | std::fill(output.begin(), output.end(), nanf("")); |
| 113 | |
| 114 | xnn_status status = xnn_initialize(); |
| 115 | if (status != xnn_status_success) { |
| 116 | state.SkipWithError("failed to initialize XNNPACK"); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | xnn_operator_t pooling_op = nullptr; |
| 121 | status = xnn_create_max_pooling2d_nhwc_f32( |
| 122 | padding_size, padding_size, padding_size, padding_size, |
| 123 | pooling_size, pooling_size, |
| 124 | stride, stride, |
| 125 | 1 /* dilation height */, 1 /* dilation width */, |
| 126 | channels, channels /* input pixel stride */, channels /* output pixel stride */, |
| 127 | -std::numeric_limits<float>::infinity(), +std::numeric_limits<float>::infinity(), |
| 128 | 0 /* flags */, &pooling_op); |
| 129 | if (status != xnn_status_success) { |
| 130 | state.SkipWithError("failed to create Max Pooling operator"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | status = xnn_setup_max_pooling2d_nhwc_f32( |
| 135 | pooling_op, |
| 136 | batch_size, input_height, input_width, |
| 137 | input.data(), output.data(), |
| 138 | nullptr /* thread pool */); |
| 139 | if (status != xnn_status_success) { |
| 140 | state.SkipWithError("failed to setup Max Pooling operator"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | for (auto _ : state) { |
| 145 | status = xnn_run_operator(pooling_op, nullptr /* thread pool */); |
| 146 | if (status != xnn_status_success) { |
| 147 | state.SkipWithError("failed to run Max Pooling operator"); |
| 148 | return; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | status = xnn_delete_operator(pooling_op); |
| 153 | if (status != xnn_status_success) { |
| 154 | state.SkipWithError("failed to delete Max Pooling operator"); |
| 155 | return; |
| 156 | } |
| 157 | pooling_op = nullptr; |
| 158 | |
| 159 | state.counters["bytes"] = benchmark::Counter( |
| 160 | uint64_t(state.iterations()) * |
| 161 | batch_size * (input_height * input_width + output_height * output_width) * channels * sizeof(float), |
| 162 | benchmark::Counter::kIsRate); |
| 163 | } |
| 164 | |
| 165 | // ShuffleNet v1/v2. |
| 166 | static void ShuffleNet(benchmark::internal::Benchmark* b) { |
| 167 | b->ArgNames({"N", "H", "W", "K", "P", "S", "C"}); |
| 168 | |
| 169 | /* N H W K P S C */ |
| 170 | b->Args({1, 112, 112, 3, 1, 2, 24}); |
| 171 | } |
| 172 | |
| 173 | // SqueezeNet 1.0 |
| 174 | static void SqueezeNetV10(benchmark::internal::Benchmark* b) { |
| 175 | b->ArgNames({"N", "H", "W", "K", "P", "S", "C"}); |
| 176 | |
| 177 | /*********** MaxPool 1 ************/ |
| 178 | /* N H W K P S C */ |
| 179 | b->Args({1, 111, 111, 3, 0, 2, 96}); |
| 180 | /*********** MaxPool 4 ************/ |
| 181 | /* N H W K P S C */ |
| 182 | b->Args({1, 27, 27, 3, 0, 2, 256}); |
| 183 | /*********** MaxPool 8 ************/ |
| 184 | /* N H W K P S C */ |
| 185 | b->Args({1, 13, 13, 3, 0, 2, 512}); |
| 186 | } |
| 187 | |
| 188 | // SqueezeNet 1.1 |
| 189 | static void SqueezeNetV11(benchmark::internal::Benchmark* b) { |
| 190 | b->ArgNames({"N", "H", "W", "K", "P", "S", "C"}); |
| 191 | |
| 192 | /*********** MaxPool 1 ***********/ |
| 193 | /* N H W K P S C */ |
| 194 | b->Args({1, 111, 111, 3, 0, 2, 64}); |
| 195 | /*********** MaxPool 3 ************/ |
| 196 | /* N H W K P S C */ |
| 197 | b->Args({1, 55, 55, 3, 0, 2, 128}); |
| 198 | /*********** MaxPool 5 ************/ |
| 199 | /* N H W K P S C */ |
| 200 | b->Args({1, 13, 13, 3, 0, 2, 256}); |
| 201 | } |
| 202 | |
| 203 | static void VGG(benchmark::internal::Benchmark* b) { |
| 204 | b->ArgNames({"N", "H", "W", "K", "P", "S", "C"}); |
| 205 | |
| 206 | /* N H W K P S C */ |
| 207 | b->Args({1, 224, 224, 2, 1, 2, 64}); |
| 208 | b->Args({1, 112, 112, 2, 1, 2, 128}); |
| 209 | b->Args({1, 56, 56, 2, 1, 2, 256}); |
| 210 | b->Args({1, 28, 28, 2, 1, 2, 512}); |
| 211 | b->Args({1, 14, 14, 2, 1, 2, 512}); |
| 212 | } |
| 213 | |
| 214 | BENCHMARK_CAPTURE(max_pooling_f32, shufflenet, "ShuffleNet v1/v2")->Apply(ShuffleNet)->UseRealTime(); |
| 215 | BENCHMARK_CAPTURE(max_pooling_f32, squeezenet_v10, "SqueezeNet v1.0")->Apply(SqueezeNetV10)->UseRealTime(); |
| 216 | BENCHMARK_CAPTURE(max_pooling_f32, squeezenet_v11, "SqueezeNet v1.1")->Apply(SqueezeNetV11)->UseRealTime(); |
| 217 | BENCHMARK_CAPTURE(max_pooling_f32, vgg, "VGG")->Apply(VGG); |
| 218 | |
| 219 | BENCHMARK_CAPTURE(max_pooling_u8, shufflenet, "ShuffleNet v1/v2")->Apply(ShuffleNet)->UseRealTime(); |
| 220 | BENCHMARK_CAPTURE(max_pooling_u8, squeezenet_v10, "SqueezeNet v1.0")->Apply(SqueezeNetV10)->UseRealTime(); |
| 221 | BENCHMARK_CAPTURE(max_pooling_u8, squeezenet_v11, "SqueezeNet v1.1")->Apply(SqueezeNetV11)->UseRealTime(); |
| 222 | BENCHMARK_CAPTURE(max_pooling_u8, vgg, "VGG")->Apply(VGG); |
| 223 | |
| 224 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 225 | BENCHMARK_MAIN(); |
| 226 | #endif |