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()); |
| 25 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), rng); |
| 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 | |
| 71 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 72 | |
| 73 | const size_t elements_per_iteration = batch_size * channels; |
| 74 | state.counters["elements"] = |
| 75 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
| 76 | |
| 77 | const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(float); |
| 78 | state.counters["bytes"] = |
| 79 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
| 80 | } |
| 81 | |
| 82 | static void CharacteristicArguments(benchmark::internal::Benchmark* b) |
| 83 | { |
| 84 | b->ArgNames({"N", "C"}); |
| 85 | |
| 86 | int32_t c = 16; |
| 87 | for (int32_t n = 224; n >= 7; n /= 2) { |
| 88 | b->Args({n * n, c}); |
| 89 | c *= 2; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | BENCHMARK(xnnpack_truncation_f32)->Apply(CharacteristicArguments)->UseRealTime(); |
| 94 | |
| 95 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 96 | BENCHMARK_MAIN(); |
| 97 | #endif |