| 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 | #include "dwconv_registry.hpp" |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
| 12 | #include <optional> |
| 13 | #include <test/common/cpu_info.hpp> |
| 14 | #include <test/common/data_type.hpp> |
| 15 | |
| 16 | #include "dwconv_benchmark_logic.hpp" |
| 17 | #include "dwconv_interface.hpp" |
| 18 | |
| 19 | #ifdef __GNUC__ |
| 20 | #pragma GCC diagnostic push |
| 21 | #pragma GCC diagnostic ignored "-Wswitch-default" |
| 22 | #endif // __GNUC__ |
| 23 | |
| 24 | #include <benchmark/benchmark.h> |
| 25 | |
| 26 | #ifdef __GNUC__ |
| 27 | #pragma GCC diagnostic pop |
| 28 | #endif // __GNUC__ |
| 29 | |
| 30 | // Micro-kernels to register for benchmarking |
| 31 | #include "kai/ukernels/dwconv/dwconv_f16_f16_f16p/kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla.h" |
| 32 | #include "kai/ukernels/dwconv/dwconv_f32_f32_f32p/kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla.h" |
| 33 | #include "kai/ukernels/dwconv/pack/kai_rhs_dwconv_pack_x16p1vlx1b_x16_x16_sme.h" |
| 34 | #include "kai/ukernels/dwconv/pack/kai_rhs_dwconv_pack_x32p1vlx1b_x32_x32_sme.h" |
| 35 | |
| 36 | namespace kai::benchmark { |
| 37 | using DataType = test::DataType; |
| 38 | |
| 39 | inline constexpr DwConvPackedDepthfirstFloatInterface kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla_iface{ |
| 40 | .run_dwconv = kai_run_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla, |
| 41 | }; |
| 42 | |
| 43 | inline constexpr DwConvPackedFloatInterface kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla_iface{ |
| 44 | .run_dwconv = kai_run_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla, |
| 45 | }; |
| 46 | |
| 47 | struct DwConvBenchmarkCase { |
| 48 | ::benchmark::internal::Benchmark* benchmark; |
| 49 | const DwConvTraits* traits; |
| 50 | }; |
| 51 | |
| 52 | inline constexpr DwConvRhsConfig kai_dwconv_packed_fp16_rhs_cfg{ |
| 53 | .layout = DwConvRhsLayout::Packed, |
| 54 | .weights_elem_bits = 16, |
| 55 | .bias_elem_bits = 16, |
| 56 | .get_packed_rhs_size = kai_rhs_get_dst_size_dwconv_pack_x16p1vlx1b_x16_x16_sme, |
| 57 | }; |
| 58 | |
| 59 | inline constexpr DwConvRhsConfig kai_dwconv_packed_fp32_rhs_cfg{ |
| 60 | .layout = DwConvRhsLayout::Packed, |
| 61 | .weights_elem_bits = 32, |
| 62 | .bias_elem_bits = 32, |
| 63 | .get_packed_rhs_size = kai_rhs_get_dst_size_dwconv_pack_x32p1vlx1b_x32_x32_sme, |
| 64 | }; |
| 65 | |
| 66 | // Helper function to bundle traits |
| 67 | template < |
| 68 | typename GetMStep, typename GetFilterHeight, typename GetFilterWidth, typename GetKr, typename GetDstSize, |
| 69 | typename GetDstOffset, typename GetSrcOffset> |
| 70 | constexpr DwConvTraits BundleTraits( |
| 71 | GetMStep get_m_step, GetFilterHeight get_filter_height, GetFilterWidth get_filter_width, GetKr get_kr, |
| 72 | GetDstSize get_dst_size, GetDstOffset get_dst_offset, GetSrcOffset get_src_offset) { |
| 73 | DwConvTraits traits{}; |
| 74 | traits.get_m_step = get_m_step; |
| 75 | traits.get_filter_height = get_filter_height; |
| 76 | traits.get_filter_width = get_filter_width; |
| 77 | traits.get_kr = get_kr; |
| 78 | traits.get_dst_size = get_dst_size; |
| 79 | traits.get_dst_offset = get_dst_offset; |
| 80 | traits.get_src_offset = get_src_offset; |
| 81 | return traits; |
| 82 | } |
| 83 | |
| 84 | template <typename GetFilterHeight, typename GetFilterWidth, typename GetDstSize> |
| 85 | constexpr DwConvTraits BundleDepthfirstTraits( |
| 86 | GetFilterHeight get_filter_height, GetFilterWidth get_filter_width, GetDstSize get_dst_size) { |
| 87 | DwConvTraits traits{}; |
| 88 | traits.get_filter_height = get_filter_height; |
| 89 | traits.get_filter_width = get_filter_width; |
| 90 | traits.get_dst_size = get_dst_size; |
| 91 | return traits; |
| 92 | } |
| 93 | |
| 94 | inline constexpr DwConvTraits kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla_traits = BundleDepthfirstTraits( |
| 95 | kai_get_filter_height_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla, |
| 96 | kai_get_filter_width_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla, |
| 97 | kai_get_dst_size_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla); |
| 98 | |
| 99 | inline constexpr DwConvTraits kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla_traits = BundleTraits( |
| 100 | kai_get_m_step_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla, |
| 101 | kai_get_filter_height_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla, |
| 102 | kai_get_filter_width_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla, |
| 103 | kai_get_kr_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla, |
| 104 | kai_get_dst_size_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla, |
| 105 | kai_get_dst_offset_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla, |
| 106 | kai_get_src_offset_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla); |
| 107 | |
| 108 | inline std::array<DwConvBenchmarkCase, 2> dwconv_benchmarks{{ |
| 109 | { |
| 110 | ::benchmark::RegisterBenchmark( |
| 111 | "kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla", kai_benchmark_dwconv, |
| 112 | RunnerFactory{[](const DwConvTraits& tr, DataType sdt, DataType ddt) { |
| 113 | return std::make_unique<DwConvPackedFloatRunner>( |
| 114 | kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla_iface, tr, sdt, ddt); |
| 115 | }}, |
| 116 | kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla_traits, DataType::FP32, DataType::FP32, |
| 117 | kai_dwconv_packed_fp32_rhs_cfg, test::cpu_has_sme2), |
| 118 | &kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla_traits, |
| 119 | }, |
| 120 | { |
| 121 | ::benchmark::RegisterBenchmark( |
| 122 | "kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla", kai_benchmark_dwconv, |
| 123 | RunnerFactory{[](const DwConvTraits& tr, DataType sdt, DataType ddt) { |
| 124 | return std::make_unique<DwConvPackedDepthfirstFloatRunner>( |
| 125 | kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla_iface, tr, sdt, ddt); |
| 126 | }}, |
| 127 | kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla_traits, DataType::FP16, DataType::FP16, |
| 128 | kai_dwconv_packed_fp16_rhs_cfg, test::cpu_has_sme2), |
| 129 | &kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla_traits, |
| 130 | }, |
| 131 | }}; |
| 132 | |
| 133 | void RegisterDwConvBenchmarks(const DwConvShape& shape) { |
| 134 | if (!supports_unit_stride_and_dilation(shape)) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | for (auto& entry : dwconv_benchmarks) { |
| 139 | const size_t filter_height = entry.traits->get_filter_height(); |
| 140 | const size_t filter_width = entry.traits->get_filter_width(); |
| 141 | |
| 142 | const auto [out_h, out_w] = compute_dwconv_output_dims(shape, filter_height, filter_width); |
| 143 | if (out_h == 0 || out_w == 0) { |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | entry.benchmark |
| 148 | ->Args({ |
| 149 | static_cast<int64_t>(shape.num_channels), |
| 150 | static_cast<int64_t>(shape.input_height), |
| 151 | static_cast<int64_t>(shape.input_width), |
| 152 | static_cast<int64_t>(shape.stride[0]), |
| 153 | static_cast<int64_t>(shape.stride[1]), |
| 154 | static_cast<int64_t>(shape.padding[0]), |
| 155 | static_cast<int64_t>(shape.padding[1]), |
| 156 | static_cast<int64_t>(shape.padding[2]), |
| 157 | static_cast<int64_t>(shape.padding[3]), |
| 158 | static_cast<int64_t>(shape.dilation[0]), |
| 159 | static_cast<int64_t>(shape.dilation[1]), |
| 160 | }) |
| 161 | ->ArgNames({ |
| 162 | "channels", |
| 163 | "input_height", |
| 164 | "input_width", |
| 165 | "stride_h", |
| 166 | "stride_w", |
| 167 | "pad_top", |
| 168 | "pad_bottom", |
| 169 | "pad_left", |
| 170 | "pad_right", |
| 171 | "dilation_h", |
| 172 | "dilation_w", |
| 173 | }); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | std::optional<DwConvOutputShape> InferDwConvOutputDims(const DwConvShape& shape) { |
| 178 | if (dwconv_benchmarks.empty()) { |
| 179 | return std::nullopt; |
| 180 | } |
| 181 | |
| 182 | if (!supports_unit_stride_and_dilation(shape)) { |
| 183 | return std::nullopt; |
| 184 | } |
| 185 | |
| 186 | const DwConvTraits* traits = dwconv_benchmarks.front().traits; |
| 187 | const size_t filter_height = traits->get_filter_height(); |
| 188 | const size_t filter_width = traits->get_filter_width(); |
| 189 | |
| 190 | const auto dims = compute_dwconv_output_dims(shape, filter_height, filter_width); |
| 191 | if (dims.height == 0 || dims.width == 0) { |
| 192 | return std::nullopt; |
| 193 | } |
| 194 | |
| 195 | return dims; |
| 196 | } |
| 197 | |
| 198 | } // namespace kai::benchmark |