| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
| 12 | #include <functional> |
| 13 | #include <limits> |
| 14 | #include <memory> |
| 15 | #include <test/common/cpu_info.hpp> |
| 16 | #include <test/common/data_type.hpp> |
| 17 | #include <tuple> |
| 18 | #include <utility> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "benchmark/cycle_counter.hpp" |
| 22 | #include "dwconv_interface.hpp" |
| 23 | #include "dwconv_runner.hpp" |
| 24 | #include "kai/kai_common.h" |
| 25 | |
| 26 | #ifdef __GNUC__ |
| 27 | #pragma GCC diagnostic push |
| 28 | #pragma GCC diagnostic ignored "-Wswitch-default" |
| 29 | #endif // __GNUC__ |
| 30 | |
| 31 | #include <benchmark/benchmark.h> |
| 32 | |
| 33 | #ifdef __GNUC__ |
| 34 | #pragma GCC diagnostic pop |
| 35 | #endif // __GNUC__ |
| 36 | |
| 37 | namespace kai::benchmark { |
| 38 | using Buffer = std::vector<uint8_t>; |
| 39 | using CpuRequirement = std::function<bool()>; |
| 40 | using DataType = test::DataType; |
| 41 | |
| 42 | struct DwConvShape { |
| 43 | size_t input_height; |
| 44 | size_t input_width; |
| 45 | size_t num_channels; |
| 46 | std::array<size_t, 2> stride{{1, 1}}; // {stride_height, stride_width} |
| 47 | std::array<size_t, 4> padding{{0, 0, 0, 0}}; // {pad_top, pad_bottom, pad_left, pad_right} |
| 48 | std::array<size_t, 2> dilation{{1, 1}}; // {dilation_height, dilation_width} |
| 49 | }; |
| 50 | |
| 51 | struct DwConvOutputShape { |
| 52 | size_t height; |
| 53 | size_t width; |
| 54 | }; |
| 55 | |
| 56 | inline bool supports_unit_stride_and_dilation(size_t stride_h, size_t stride_w, size_t dilation_h, size_t dilation_w) { |
| 57 | return stride_h == 1 && stride_w == 1 && dilation_h == 1 && dilation_w == 1; |
| 58 | } |
| 59 | |
| 60 | inline bool supports_unit_stride_and_dilation(const DwConvShape& shape) { |
| 61 | return supports_unit_stride_and_dilation(shape.stride[0], shape.stride[1], shape.dilation[0], shape.dilation[1]); |
| 62 | } |
| 63 | |
| 64 | inline DwConvOutputShape compute_dwconv_output_dims( |
| 65 | const DwConvShape& shape, size_t filter_height, size_t filter_width) { |
| 66 | const auto compute_dim = [&](size_t idx) -> size_t { // 0: height, 1: width |
| 67 | const size_t input = (idx == 0) ? shape.input_height : shape.input_width; |
| 68 | const size_t filter = (idx == 0) ? filter_height : filter_width; |
| 69 | const size_t stride = shape.stride[idx]; |
| 70 | const size_t dilation = shape.dilation[idx]; |
| 71 | const size_t pad_before = shape.padding[idx * 2]; |
| 72 | const size_t pad_after = shape.padding[idx * 2 + 1]; |
| 73 | const size_t effective_kernel = (filter - 1) * dilation + 1; |
| 74 | const size_t input_plus_pad = input + pad_before + pad_after; |
| 75 | |
| 76 | if (stride == 0 || filter == 0 || effective_kernel == 0 || input_plus_pad < effective_kernel) { |
| 77 | return 0; |
| 78 | } |
| 79 | const size_t numerator = input + pad_before + pad_after - effective_kernel; |
| 80 | return numerator / stride + 1; |
| 81 | }; |
| 82 | |
| 83 | return DwConvOutputShape{compute_dim(0), compute_dim(1)}; |
| 84 | } |
| 85 | |
| 86 | // Factory to construct a runner matching the registered micro-kernel |
| 87 | using RunnerFactory = std::function<std::unique_ptr<DwConvRunner>(const DwConvTraits&, DataType, DataType)>; |
| 88 | |
| 89 | /// Benchmarks a depthwise convolution micro-kernel using a provided runner factory |
| 90 | inline void kai_benchmark_dwconv( |
| 91 | ::benchmark::State& state, const RunnerFactory& runner_factory, const DwConvTraits& traits, const DataType src_type, |
| 92 | const DataType dst_type, const DwConvRhsConfig& rhs_cfg, const CpuRequirement& is_cpu_supported) { |
| 93 | if (!is_cpu_supported()) { |
| 94 | state.SkipWithMessage("Unsupported CPU feature"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | const size_t num_channels = static_cast<size_t>(state.range(0)); |
| 99 | const size_t input_height = static_cast<size_t>(state.range(1)); |
| 100 | const size_t input_width = static_cast<size_t>(state.range(2)); |
| 101 | const size_t stride_h = static_cast<size_t>(state.range(3)); |
| 102 | const size_t stride_w = static_cast<size_t>(state.range(4)); |
| 103 | const size_t pad_top = static_cast<size_t>(state.range(5)); |
| 104 | const size_t pad_bottom = static_cast<size_t>(state.range(6)); |
| 105 | const size_t pad_left = static_cast<size_t>(state.range(7)); |
| 106 | const size_t pad_right = static_cast<size_t>(state.range(8)); |
| 107 | const size_t dilation_h = static_cast<size_t>(state.range(9)); |
| 108 | const size_t dilation_w = static_cast<size_t>(state.range(10)); |
| 109 | |
| 110 | if (!supports_unit_stride_and_dilation(stride_h, stride_w, dilation_h, dilation_w)) { |
| 111 | state.SkipWithMessage("Current DWConv micro-kernels only support stride=1 and dilation=1"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // Buffer sizes |
| 116 | const size_t filter_height = traits.get_filter_height(); |
| 117 | const size_t filter_width = traits.get_filter_width(); |
| 118 | DwConvShape runtime_shape{}; |
| 119 | runtime_shape.input_height = input_height; |
| 120 | runtime_shape.input_width = input_width; |
| 121 | runtime_shape.num_channels = num_channels; |
| 122 | runtime_shape.stride = {stride_h, stride_w}; |
| 123 | runtime_shape.padding = {pad_top, pad_bottom, pad_left, pad_right}; |
| 124 | runtime_shape.dilation = {dilation_h, dilation_w}; |
| 125 | const auto [output_height, output_width] = compute_dwconv_output_dims(runtime_shape, filter_height, filter_width); |
| 126 | |
| 127 | if (output_height == 0 || output_width == 0) { |
| 128 | state.SkipWithMessage("Invalid DWConv dimensions derived from CLI flags"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | size_t input_size = input_height * input_width * num_channels * data_type_size_bytes(src_type); |
| 133 | size_t output_size = output_height * output_width * num_channels * data_type_size_bytes(dst_type); |
| 134 | |
| 135 | // SME/SVE scaling for bandwidth accounting |
| 136 | #if defined(__ARM_FEATURE_SVE2) || defined(_M_ARM64) |
| 137 | if (test::cpu_has_sme() || test::cpu_has_sme2()) { |
| 138 | const size_t vl = kai_get_sme_vector_length_u32(); |
| 139 | input_size *= vl; |
| 140 | output_size *= vl; |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | // RHS sizes by layout |
| 145 | size_t rhs_packed_size = 0, rhs_weights_size = 0, rhs_bias_size = 0; |
| 146 | if (rhs_cfg.layout == DwConvRhsLayout::Packed) { |
| 147 | KAI_ASSERT_ALWAYS_MSG( |
| 148 | rhs_cfg.get_packed_rhs_size, "Packed DWConv benchmarks must provide get_packed_rhs_size callback"); |
| 149 | rhs_packed_size = rhs_cfg.get_packed_rhs_size(filter_height, filter_width, num_channels); |
| 150 | } else { |
| 151 | rhs_weights_size = num_channels * (filter_height * filter_width) * (rhs_cfg.weights_elem_bits / 8); |
| 152 | rhs_bias_size = num_channels * (rhs_cfg.bias_elem_bits / 8); |
| 153 | } |
| 154 | |
| 155 | const Buffer src(input_size); |
| 156 | Buffer dst(output_size); |
| 157 | |
| 158 | // Construct runner and configure common parameters |
| 159 | auto runner = runner_factory(traits, src_type, dst_type); |
| 160 | runner->set_input_dims(input_height, input_width); |
| 161 | runner->set_output_dims(output_height, output_width); |
| 162 | runner->set_channels(num_channels); |
| 163 | runner->set_padding(pad_top, pad_bottom, pad_left, pad_right); |
| 164 | runner->set_clamp(-std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()); |
| 165 | |
| 166 | Buffer rhs_packed, rhs_weights, rhs_bias; |
| 167 | if (rhs_cfg.layout == DwConvRhsLayout::Packed) { |
| 168 | rhs_packed = Buffer(rhs_packed_size); |
| 169 | runner->prepare(rhs_packed.data(), nullptr, nullptr, nullptr); |
| 170 | } else { |
| 171 | rhs_weights = Buffer(rhs_weights_size); |
| 172 | rhs_bias = Buffer(rhs_bias_size); |
| 173 | runner->prepare(nullptr, rhs_weights.data(), rhs_bias.data(), nullptr); |
| 174 | } |
| 175 | |
| 176 | const bool cycle_counter_available = cycle_counter_init(); |
| 177 | uint64_t total_cycles = 0; |
| 178 | uint64_t start_cycles = 0; |
| 179 | bool cycle_measurement_valid = false; |
| 180 | if (cycle_counter_available) { |
| 181 | cycle_counter_start(); |
| 182 | cycle_measurement_valid = cycle_counter_read(start_cycles); |
| 183 | } |
| 184 | |
| 185 | for (auto _ : state) { |
| 186 | runner->run(src.data(), dst.data()); |
| 187 | } |
| 188 | |
| 189 | if (cycle_counter_available) { |
| 190 | uint64_t end_cycles = 0; |
| 191 | cycle_measurement_valid = |
| 192 | cycle_measurement_valid && cycle_counter_read(end_cycles) && end_cycles >= start_cycles; |
| 193 | cycle_counter_stop(); |
| 194 | if (cycle_measurement_valid) { |
| 195 | total_cycles += (end_cycles - start_cycles); |
| 196 | } |
| 197 | cycle_counter_shutdown(); |
| 198 | } |
| 199 | |
| 200 | state.counters["cpu_cycles"] = ::benchmark::Counter( |
| 201 | static_cast<double>(total_cycles), ::benchmark::Counter::kAvgIterations, ::benchmark::Counter::OneK::kIs1000); |
| 202 | |
| 203 | const size_t num_ops = output_height * output_width * num_channels * filter_height * filter_width * 2; // MACs |
| 204 | const size_t rhs_bytes = |
| 205 | (rhs_cfg.layout == DwConvRhsLayout::Packed) ? rhs_packed_size : (rhs_weights_size + rhs_bias_size); |
| 206 | state.SetItemsProcessed(state.iterations() * num_ops); |
| 207 | state.SetBytesProcessed(state.iterations() * (input_size + rhs_bytes + output_size)); |
| 208 | } |
| 209 | |
| 210 | } // namespace kai::benchmark |