|
| 1 | +// Copyright 2025 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 <cstddef> |
| 7 | +#include <cstdint> |
| 8 | + |
| 9 | +#include "bench/utils.h" |
| 10 | +#include "src/xnnpack/buffer.h" |
| 11 | +#include "src/xnnpack/common.h" |
| 12 | +#include "src/xnnpack/hardware-config.h" // IWYU pragma: keep |
| 13 | +#include "src/xnnpack/reduce.h" // IWYU pragma: keep |
| 14 | +#include "test/replicable_random_device.h" |
| 15 | +#include <benchmark/benchmark.h> |
| 16 | + |
| 17 | +// Microkernel function, templated on the `params` type. |
| 18 | +template <typename Input, typename Output, typename UKernelParams> |
| 19 | +using UKernelFn = void (*)(size_t, size_t, size_t, size_t, const Input*, size_t, |
| 20 | + size_t, size_t, const Input*, Output*, |
| 21 | + const UKernelParams*); |
| 22 | + |
| 23 | +template <typename Input, typename Output, typename UKernelParams> |
| 24 | +static void reduce(benchmark::State& state, uint64_t arch_flags, |
| 25 | + UKernelFn<Input, Output, UKernelParams> ukernel) { |
| 26 | + if (!benchmark::utils::CheckArchFlags(state, arch_flags)) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const size_t channels = state.range(0); |
| 31 | + const size_t rows = state.range(1); |
| 32 | + |
| 33 | + xnnpack::ReplicableRandomDevice rng; |
| 34 | + |
| 35 | + xnnpack::Buffer<Input, XNN_ALLOCATION_ALIGNMENT> input( |
| 36 | + channels * rows, xnnpack::XnnExtraBytes); |
| 37 | + xnnpack::Buffer<Input, XNN_ALLOCATION_ALIGNMENT> zero(channels, 0, |
| 38 | + xnnpack::XnnExtraBytes); |
| 39 | + xnnpack::fill_uniform_random_bits(input.data(), input.size(), rng); |
| 40 | + xnnpack::Buffer<Output, XNN_ALLOCATION_ALIGNMENT> output(channels); |
| 41 | + |
| 42 | + UKernelParams params; |
| 43 | + memset(¶ms, 0, sizeof(params)); |
| 44 | + |
| 45 | + for (auto _ : state) { |
| 46 | + ukernel(channels, rows, 1, 1, input.data(), channels * sizeof(Input), 0, 0, |
| 47 | + zero.data(), output.data(), ¶ms); |
| 48 | + } |
| 49 | + |
| 50 | + const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 51 | + if (cpu_frequency != 0) { |
| 52 | + state.counters["cpufreq"] = cpu_frequency; |
| 53 | + } |
| 54 | + |
| 55 | + const size_t elements_per_iteration = channels * rows; |
| 56 | + state.counters["elements"] = benchmark::Counter( |
| 57 | + static_cast<uint64_t>(state.iterations()) * elements_per_iteration, |
| 58 | + benchmark::Counter::kIsRate); |
| 59 | + |
| 60 | + const size_t bytes_per_iteration = channels * rows * sizeof(Input); |
| 61 | + state.counters["bytes"] = benchmark::Counter( |
| 62 | + static_cast<uint64_t>(state.iterations()) * bytes_per_iteration, |
| 63 | + benchmark::Counter::kIsRate); |
| 64 | +} |
| 65 | + |
| 66 | +#define XNN_UKERNEL(arch_flags, ukernel, row_tile, batch_tile, vector_tile, \ |
| 67 | + datatype_in, datatype_out, params_type, init_params) \ |
| 68 | + BENCHMARK_CAPTURE(reduce, ukernel, arch_flags, ukernel) \ |
| 69 | + ->Apply(benchmark::utils::ReduceDiscontiguousParameters<datatype_in>) \ |
| 70 | + ->UseRealTime(); |
| 71 | +// #include "src/f16-f32acc-rdsum/f16-f32acc-rdsum.inc" |
| 72 | +#include "src/f32-rdsum/f32-rdsum.inc" |
| 73 | +#undef XNN_UKERNEL |
| 74 | + |
| 75 | +#ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 76 | +XNN_BENCHMARK_MAIN(); |
| 77 | +#endif |
0 commit comments