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" |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 17 | |
| 18 | |
| 19 | static void softargmax_q8(benchmark::State& state) { |
| 20 | const size_t batch_size = static_cast<size_t>(state.range(0)); |
| 21 | const size_t channels = static_cast<size_t>(state.range(1)); |
| 22 | |
| 23 | std::random_device random_device; |
| 24 | auto rng = std::mt19937(random_device()); |
| 25 | auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng); |
| 26 | |
| 27 | std::vector<uint8_t> input(batch_size * channels); |
| 28 | std::vector<uint8_t> output(batch_size * channels); |
| 29 | std::generate(input.begin(), input.end(), std::ref(u8rng)); |
| 30 | std::fill(output.begin(), output.end(), 0xA5); |
| 31 | |
| 32 | xnn_status status = xnn_initialize(); |
| 33 | if (status != xnn_status_success) { |
| 34 | state.SkipWithError("failed to initialize XNNPACK"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | xnn_operator_t softargmax_op = nullptr; |
| 39 | status = xnn_create_softargmax_nc_q8( |
| 40 | channels, channels /* input stride */, channels /* output stride */, |
| 41 | 1.0f /* input scale */, |
| 42 | 0 /* output zero point */, 1.0f / 256.0f /* output scale */, |
| 43 | 0 /* flags */, &softargmax_op); |
| 44 | if (status != xnn_status_success || softargmax_op == nullptr) { |
| 45 | state.SkipWithError("failed to create SoftArgMax operator"); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | status = xnn_setup_softargmax_nc_q8( |
| 50 | softargmax_op, |
| 51 | batch_size, |
| 52 | input.data(), output.data(), |
| 53 | nullptr /* thread pool */); |
| 54 | if (status != xnn_status_success) { |
| 55 | state.SkipWithError("failed to setup SoftArgMax operator"); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | for (auto _ : state) { |
| 60 | status = xnn_run_operator(softargmax_op, nullptr /* thread pool */); |
| 61 | if (status != xnn_status_success) { |
| 62 | state.SkipWithError("failed to run SoftArgMax operator"); |
| 63 | return; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | status = xnn_delete_operator(softargmax_op); |
| 68 | if (status != xnn_status_success) { |
| 69 | state.SkipWithError("failed to delete SoftArgMax operator"); |
| 70 | return; |
| 71 | } |
| 72 | |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame^] | 73 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 74 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 75 | const size_t elements_per_iteration = batch_size * channels; |
| 76 | state.counters["elements"] = |
| 77 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 78 | |
| 79 | const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(uint8_t); |
| 80 | state.counters["bytes"] = |
| 81 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 82 | } |
| 83 | |
| 84 | static void CharacteristicArguments(benchmark::internal::Benchmark* b) |
| 85 | { |
| 86 | b->ArgNames({"N", "C"}); |
| 87 | |
| 88 | // CIFAR-10 |
| 89 | b->Args({1, 10}); |
| 90 | // CIFAR-100 */ |
| 91 | b->Args({1, 100}); |
| 92 | // ImageNet-1K |
| 93 | b->Args({1, 1000}); |
| 94 | // ImageNet-1K+1 |
| 95 | b->Args({1, 1001}); |
| 96 | // ImageNet-22K |
| 97 | b->Args({1, 21841}); |
| 98 | } |
| 99 | |
| 100 | BENCHMARK(softargmax_q8)->Apply(CharacteristicArguments)->UseRealTime(); |
| 101 | |
| 102 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 103 | BENCHMARK_MAIN(); |
| 104 | #endif |