Marat Dukhan | 8772714 | 2020-06-24 15:24:10 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 <cmath> |
| 8 | #include <functional> |
| 9 | #include <limits> |
| 10 | #include <random> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <xnnpack.h> |
| 14 | |
| 15 | #include <benchmark/benchmark.h> |
| 16 | #include "bench/utils.h" |
| 17 | |
| 18 | |
| 19 | static void xnnpack_truncation_f32(benchmark::State& state) { |
| 20 | const size_t batch_size = state.range(0); |
| 21 | const size_t channels = state.range(1); |
| 22 | |
| 23 | std::random_device random_device; |
| 24 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 25 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng)); |
Marat Dukhan | 8772714 | 2020-06-24 15:24:10 -0700 | [diff] [blame] | 26 | |
| 27 | std::vector<float> input(batch_size * channels); |
| 28 | std::vector<float> output(batch_size * channels); |
| 29 | std::generate(input.begin(), input.end(), std::ref(f32rng)); |
| 30 | std::fill(output.begin(), output.end(), std::nanf("")); |
| 31 | |
| 32 | xnn_status status = xnn_initialize(nullptr /* allocator */); |
| 33 | if (status != xnn_status_success) { |
| 34 | state.SkipWithError("failed to initialize XNNPACK"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | xnn_operator_t truncation_op = nullptr; |
| 39 | status = xnn_create_truncation_nc_f32( |
| 40 | channels, channels /* input stride */, channels /* output stride */, |
| 41 | 0 /* flags */, &truncation_op); |
| 42 | if (status != xnn_status_success || truncation_op == nullptr) { |
| 43 | state.SkipWithError("failed to create Truncation operator"); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | status = xnn_setup_truncation_nc_f32( |
| 48 | truncation_op, |
| 49 | batch_size, |
| 50 | input.data(), output.data(), |
| 51 | nullptr /* thread pool */); |
| 52 | if (status != xnn_status_success) { |
| 53 | state.SkipWithError("failed to setup Truncation operator"); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | for (auto _ : state) { |
| 58 | status = xnn_run_operator(truncation_op, nullptr /* thread pool */); |
| 59 | if (status != xnn_status_success) { |
| 60 | state.SkipWithError("failed to run Truncation operator"); |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | status = xnn_delete_operator(truncation_op); |
| 66 | if (status != xnn_status_success) { |
| 67 | state.SkipWithError("failed to delete Truncation operator"); |
| 68 | return; |
| 69 | } |
| 70 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 71 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 72 | if (cpu_frequency != 0) { |
| 73 | state.counters["cpufreq"] = cpu_frequency; |
| 74 | } |
Marat Dukhan | 8772714 | 2020-06-24 15:24:10 -0700 | [diff] [blame] | 75 | |
| 76 | const size_t elements_per_iteration = batch_size * channels; |
| 77 | state.counters["elements"] = |
| 78 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 79 | |
| 80 | const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float); |
| 81 | state.counters["bytes"] = |
| 82 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 83 | } |
| 84 | |
| 85 | static void CharacteristicArguments(benchmark::internal::Benchmark* b) |
| 86 | { |
| 87 | b->ArgNames({"N", "C"}); |
| 88 | |
| 89 | int32_t c = 16; |
| 90 | for (int32_t n = 224; n >= 7; n /= 2) { |
| 91 | b->Args({n * n, c}); |
| 92 | c *= 2; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | BENCHMARK(xnnpack_truncation_f32)->Apply(CharacteristicArguments)->UseRealTime(); |
| 97 | |
| 98 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 99 | BENCHMARK_MAIN(); |
| 100 | #endif |