blob: 8d63645ec54b694696a571b75456100f989eab04 [file] [log] [blame]
Marat Dukhan87727142020-06-24 15:24:10 -07001// 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
19static 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 Dukhan44f0ca72020-08-02 21:46:58 -070025 auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
Marat Dukhan87727142020-06-24 15:24:10 -070026
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 Dukhand713e8a2020-12-04 14:23:12 -080071 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
72 if (cpu_frequency != 0) {
73 state.counters["cpufreq"] = cpu_frequency;
74 }
Marat Dukhan87727142020-06-24 15:24:10 -070075
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
85static 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
96BENCHMARK(xnnpack_truncation_f32)->Apply(CharacteristicArguments)->UseRealTime();
97
98#ifndef XNNPACK_BENCHMARK_NO_MAIN
99BENCHMARK_MAIN();
100#endif