| 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 <cassert> |
| 10 | #include <cinttypes> |
| 11 | #include <cstring> |
| 12 | #include <type_traits> |
| 13 | |
| 14 | #include "kai/ops/gemm/kai_ops.hpp" |
| 15 | |
| 16 | #include "scheduler.hpp" |
| 17 | #include "utils.hpp" // iceildiv |
| 18 | #include "gemm_lib.hpp" |
| 19 | |
| 20 | #undef TASKMASTER |
| 21 | |
| 22 | #include "kai/ops/conv/depthwise.hpp" |
| 23 | |
| 24 | // (Templated) glue to make it work with current infrastructure |
| 25 | template <typename Ta, typename Tb, typename Tret, QuantizationType quantized=QuantizationType::NONE> |
| 26 | class depthwise_wrapper { |
| 27 | struct free_delete { |
| 28 | void operator()(void *x) { free_aligned_memory(x); } |
| 29 | }; |
| 30 | |
| 31 | // Quantized support |
| 32 | std::shared_ptr<Matrix<int32_t>> bias; |
| 33 | Requantize32 qp; |
| 34 | |
| 35 | const GemmProblem *p; |
| 36 | const unsigned int n_threads; |
| 37 | |
| 38 | std::shared_ptr<Matrix<Ta>> input; |
| 39 | std::shared_ptr<Matrix<Tret>> output; |
| 40 | |
| 41 | kai::ops::depthwise::UniqueDepthwiseCommon<Ta, Tb, Tret> depthwise_operator = {}; |
| 42 | |
| 43 | std::unique_ptr<void, free_delete> working_data = {}; |
| 44 | std::unique_ptr<void, free_delete> packed_params = {}; |
| 45 | |
| 46 | public: |
| 47 | static int get_m_block(void) { return 1; } |
| 48 | static int get_n_block(void) { return 1; } |
| 49 | static int get_k_block(void) { return 1; } |
| 50 | |
| 51 | typedef Ta lhs_operand_type; |
| 52 | typedef Tb rhs_operand_type; |
| 53 | typedef Tret result_type; |
| 54 | |
| 55 | depthwise_wrapper(depthwise_wrapper &) = delete; |
| 56 | depthwise_wrapper(depthwise_wrapper &&) = default; |
| 57 | depthwise_wrapper& operator=(depthwise_wrapper &) = delete; |
| 58 | |
| 59 | // Constructor (non-quantized) |
| 60 | depthwise_wrapper( |
| 61 | std::shared_ptr<Matrix<Ta>> &input, |
| 62 | std::shared_ptr<Matrix<Tb>> &weights, |
| 63 | std::shared_ptr<Matrix<Tret>> &output, |
| 64 | std::shared_ptr<Matrix<Tret>> &bias, |
| 65 | GemmProblem *p, |
| 66 | unsigned int n_threads, |
| 67 | int do_init |
| 68 | ) : p(p), n_threads(n_threads), input(input), output(output) { |
| 69 | const CPUInfo *ci = get_CPUInfo(); |
| 70 | |
| 71 | // Check for a valid problem |
| 72 | if (p->input_channels != p->groups) |
| 73 | { |
| 74 | printf("Depthwise: Input channels (%" PRId64 ") != number of groups (%" PRId64 ")\n", p->input_channels, p->groups); |
| 75 | exit(1); |
| 76 | } |
| 77 | |
| 78 | if (p->output_channels % p->groups) |
| 79 | { |
| 80 | printf("Depthwise: Output channels not a multiple of the number of groups.\n"); |
| 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | if (p->multis != 1) |
| 85 | { |
| 86 | printf("Depthwise: Multis not supported.\n"); |
| 87 | exit(1); |
| 88 | } |
| 89 | |
| 90 | if (p->accumulate) |
| 91 | { |
| 92 | printf("Depthwise: Accumulation not supported.\n"); |
| 93 | exit(1); |
| 94 | } |
| 95 | |
| 96 | if (p->kernel_height==1 && p->kernel_width==1) { |
| 97 | printf("Depthwise: 1x1 doesn't look like a real depthwise problem.\n"); |
| 98 | exit(1); |
| 99 | } |
| 100 | |
| 101 | // Construct the depthwise operator. |
| 102 | const int pad_rows = std::max<int>( |
| 103 | 0, (p->output_height - 1) * p->out_stride_h + p->kernel_height - p->input_height |
| 104 | ); |
| 105 | const int pad_cols = std::max<int>( |
| 106 | 0, (p->output_width - 1) * p->out_stride_w + p->kernel_width - p->input_width |
| 107 | ); |
| 108 | int padding_bottom = pad_rows - p->padding_top; |
| 109 | int padding_right = pad_cols - p->padding_left; |
| 110 | |
| 111 | kai::ops::depthwise::DepthwiseConfig cfg = {}; |
| 112 | cfg.filter = p->kernel_filter; |
| 113 | |
| 114 | kai::ops::depthwise::DepthwiseArgs args( |
| 115 | ci, |
| 116 | p->kernel_height, p->kernel_width, |
| 117 | p->out_stride_h, p->out_stride_w, |
| 118 | p->in_stride_h, p->in_stride_w, |
| 119 | p->batches, p->input_height, p->input_width, |
| 120 | p->input_channels, |
| 121 | p->output_height, p->output_width, |
| 122 | p->output_channels / p->groups, |
| 123 | { static_cast<unsigned int>(p->padding_left), |
| 124 | static_cast<unsigned int>(p->padding_top), |
| 125 | static_cast<unsigned int>(padding_right), |
| 126 | static_cast<unsigned int>(padding_bottom) }, |
| 127 | p->act, |
| 128 | &cfg |
| 129 | ); |
| 130 | |
| 131 | args.fast_mode = p->fast_mode; |
| 132 | |
| 133 | depthwise_operator = kai::ops::depthwise::depthwise<Ta, Tb, Tret>(args); |
| 134 | if (depthwise_operator == nullptr) |
| 135 | { |
| 136 | printf("Depthwise: no valid kernel implementation found.\n"); |
| 137 | exit(1); |
| 138 | } |
| 139 | |
| 140 | #ifndef SILENT |
| 141 | printf("Using kernel: %s\n", depthwise_operator->name().c_str()); |
| 142 | #endif |
| 143 | |
| 144 | // Initialise working space, and space for packed weights |
| 145 | working_data = std::unique_ptr<void, free_delete>( |
| 146 | allocate_aligned_memory(64, depthwise_operator->get_working_size(n_threads)) |
| 147 | ); |
| 148 | packed_params = std::unique_ptr<void, free_delete>( |
| 149 | allocate_aligned_memory(64, depthwise_operator->get_storage_size()) |
| 150 | ); |
| 151 | |
| 152 | // Pack parameters |
| 153 | depthwise_operator->pack_parameters( |
| 154 | packed_params.get(), |
| 155 | (bias.get() == nullptr) ? nullptr : bias->data, |
| 156 | weights->data |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | // Constructor: quantized |
| 161 | template<typename Tfloat> |
| 162 | depthwise_wrapper( |
| 163 | std::shared_ptr<Matrix<Ta>> &input, |
| 164 | std::shared_ptr<Matrix<Tb>> &weights, |
| 165 | std::shared_ptr<Matrix<Tret>> &output, |
| 166 | std::shared_ptr<Matrix<int32_t>> &bias, |
| 167 | QuantizeParameters<Ta, Tfloat> &A_qp, |
| 168 | std::vector<QuantizeParameters<Tb, Tfloat> > &B_qp, |
| 169 | QuantizeParameters<Ta, Tfloat> &C_qp, |
| 170 | GemmProblem *p, |
| 171 | unsigned int n_threads, |
| 172 | int do_init |
| 173 | ) : p(p), n_threads(n_threads), input(input), output(output) |
| 174 | { |
| 175 | assert(quantized==QuantizationType::INTEGER); |
| 176 | const CPUInfo *ci = get_CPUInfo(); |
| 177 | |
| 178 | // Check for a valid problem |
| 179 | if (p->input_channels != p->groups) |
| 180 | { |
| 181 | printf("Depthwise: Input channels != number of groups\n"); |
| 182 | exit(1); |
| 183 | } |
| 184 | |
| 185 | if (p->output_channels % p->groups) |
| 186 | { |
| 187 | printf("Depthwise: Output channels not a multiple of the number of groups.\n"); |
| 188 | exit(1); |
| 189 | } |
| 190 | |
| 191 | if (p->multis != 1) |
| 192 | { |
| 193 | printf("Depthwise: Multis not supported.\n"); |
| 194 | exit(1); |
| 195 | } |
| 196 | |
| 197 | if (p->accumulate) |
| 198 | { |
| 199 | printf("Depthwise: Accumulation not supported.\n"); |
| 200 | exit(1); |
| 201 | } |
| 202 | |
| 203 | // Construct the depthwise operator. |
| 204 | const int pad_rows = std::max<int>( |
| 205 | 0, (p->output_height - 1) * p->out_stride_h + p->kernel_height - p->input_height |
| 206 | ); |
| 207 | const int pad_cols = std::max<int>( |
| 208 | 0, (p->output_width - 1) * p->out_stride_w + p->kernel_width - p->input_width |
| 209 | ); |
| 210 | int padding_bottom = pad_rows - p->padding_top; |
| 211 | int padding_right = pad_cols - p->padding_left; |
| 212 | |
| 213 | kai::ops::depthwise::DepthwiseConfig cfg = {}; |
| 214 | cfg.filter = p->kernel_filter; |
| 215 | |
| 216 | kai::ops::depthwise::DepthwiseArgs args( |
| 217 | ci, |
| 218 | p->kernel_height, p->kernel_width, |
| 219 | p->out_stride_h, p->out_stride_w, |
| 220 | p->in_stride_h, p->in_stride_w, |
| 221 | p->batches, p->input_height, p->input_width, |
| 222 | p->input_channels, |
| 223 | p->output_height, p->output_width, |
| 224 | p->output_channels / p->groups, |
| 225 | { static_cast<unsigned int>(p->padding_left), |
| 226 | static_cast<unsigned int>(p->padding_top), |
| 227 | static_cast<unsigned int>(padding_right), |
| 228 | static_cast<unsigned int>(padding_bottom) }, |
| 229 | p->act, |
| 230 | &cfg |
| 231 | ); |
| 232 | |
| 233 | qp.bias = (bias ? bias->data : nullptr); |
| 234 | qp.bias_multi_stride = bias ? bias->multi_stride : 0; |
| 235 | qp.a_offset = A_qp.m_zeropt; |
| 236 | qp.b_offset = B_qp[0].m_zeropt; |
| 237 | qp.c_offset = C_qp.m_zeropt; |
| 238 | qp.minval = C_qp.m_minval; |
| 239 | qp.maxval = C_qp.m_maxval; |
| 240 | qp.set_multipliers(A_qp.m_scale / C_qp.m_scale, B_qp); |
| 241 | |
| 242 | depthwise_operator = kai::ops::depthwise::depthwise<Ta, Tb, Tret, kai::ops::Requantize32>(args, qp); |
| 243 | if (depthwise_operator == nullptr) |
| 244 | { |
| 245 | printf("Depthwise: no valid kernel implementation found.\n"); |
| 246 | exit(1); |
| 247 | } |
| 248 | |
| 249 | #ifndef SILENT |
| 250 | const auto kernel_list = kai::ops::depthwise::get_compatible_kernels<Ta, Tb, Tret, kai::ops::Requantize32>(args, qp); |
| 251 | if (kernel_list.size() > 0) { |
| 252 | for (auto &&i : kernel_list) { |
| 253 | // Print the selected kernel |
| 254 | if (i.is_default) { |
| 255 | printf("Using kernel: %s\n", i.name.c_str()); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | // Initialise working space, and space for packed weights |
| 263 | working_data = std::unique_ptr<void, free_delete>( |
| 264 | allocate_aligned_memory(64, depthwise_operator->get_working_size(n_threads)) |
| 265 | ); |
| 266 | packed_params = std::unique_ptr<void, free_delete>( |
| 267 | allocate_aligned_memory(64, depthwise_operator->get_storage_size()) |
| 268 | ); |
| 269 | |
| 270 | // Pack parameters |
| 271 | depthwise_operator->pack_parameters( |
| 272 | packed_params.get(), |
| 273 | (bias.get() == nullptr) ? nullptr : bias->data, |
| 274 | weights->data |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | __attribute__ ((noinline)) void Run(const unsigned int thread_id) |
| 279 | { |
| 280 | depthwise_operator->execute( |
| 281 | input->data, input->stride, input->stride * p->input_width, input->batch_stride, |
| 282 | packed_params.get(), |
| 283 | output->data, output->stride, output->stride * p->output_width, output->batch_stride, |
| 284 | working_data.get(), |
| 285 | thread_id, n_threads |
| 286 | ); |
| 287 | } |
| 288 | |
| 289 | static void print_kernels(const GemmProblem *const p, const unsigned int n_threads) |
| 290 | { |
| 291 | const CPUInfo *ci = get_CPUInfo(); |
| 292 | |
| 293 | // Check for a valid problem |
| 294 | if (p->input_channels != p->groups) |
| 295 | { |
| 296 | printf("Depthwise: Input channels != number of groups\n"); |
| 297 | exit(1); |
| 298 | } |
| 299 | |
| 300 | if (p->output_channels % p->groups) |
| 301 | { |
| 302 | printf("Depthwise: Output channels not a multiple of the number of groups.\n"); |
| 303 | exit(1); |
| 304 | } |
| 305 | |
| 306 | if (p->multis != 1) |
| 307 | { |
| 308 | printf("Depthwise: Multis not supported.\n"); |
| 309 | exit(1); |
| 310 | } |
| 311 | |
| 312 | if (p->accumulate) |
| 313 | { |
| 314 | printf("Depthwise: Accumulation not supported.\n"); |
| 315 | exit(1); |
| 316 | } |
| 317 | |
| 318 | // Construct the depthwise operator. |
| 319 | const int pad_rows = std::max<int>( |
| 320 | 0, (p->output_height - 1) * p->out_stride_h + p->kernel_height - p->input_height |
| 321 | ); |
| 322 | const int pad_cols = std::max<int>( |
| 323 | 0, (p->output_width - 1) * p->out_stride_w + p->kernel_width - p->input_width |
| 324 | ); |
| 325 | int padding_bottom = pad_rows - p->padding_top; |
| 326 | int padding_right = pad_cols - p->padding_left; |
| 327 | |
| 328 | kai::ops::depthwise::DepthwiseConfig cfg = {}; |
| 329 | cfg.filter = p->kernel_filter; |
| 330 | |
| 331 | kai::ops::depthwise::DepthwiseArgs args( |
| 332 | ci, |
| 333 | p->kernel_height, p->kernel_width, |
| 334 | p->out_stride_h, p->out_stride_w, |
| 335 | p->in_stride_h, p->in_stride_w, |
| 336 | p->batches, p->input_height, p->input_width, |
| 337 | p->input_channels, |
| 338 | p->output_height, p->output_width, |
| 339 | p->output_channels / p->groups, |
| 340 | { static_cast<unsigned int>(p->padding_left), |
| 341 | static_cast<unsigned int>(p->padding_top), |
| 342 | static_cast<unsigned int>(padding_right), |
| 343 | static_cast<unsigned int>(padding_bottom) }, |
| 344 | p->act, |
| 345 | &cfg |
| 346 | ); |
| 347 | |
| 348 | args.fast_mode = p->fast_mode; |
| 349 | |
| 350 | using OutputStage = typename std::conditional<quantized == QuantizationType::NONE, kai::ops::Nothing, kai::ops::Requantize32>::type; |
| 351 | const auto kerns = kai::ops::depthwise::get_compatible_kernels<Ta, Tb, Tret, OutputStage>(args); |
| 352 | |
| 353 | if (kerns.empty()) |
| 354 | { |
| 355 | printf("No kernels available for selected parameters.\n"); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | printf("Available kernels:\n"); |
| 360 | for (auto &&impl : kerns) |
| 361 | { |
| 362 | if (cfg.filter != "" && !std::strstr(impl.name.c_str(), cfg.filter.c_str())) |
| 363 | { |
| 364 | // Don't print filtered kernels |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | if (impl.cycle_estimate == UINT32_MAX) |
| 369 | { |
| 370 | printf("\t%s%-35s (not preferred)\n", impl.is_default ? "*" : " ", impl.name.c_str()); |
| 371 | } |
| 372 | else if (impl.cycle_estimate == 0) |
| 373 | { |
| 374 | printf("\t%s%-35s (preferred)\n", impl.is_default ? "*" : " ", impl.name.c_str()); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | printf("\t%s%-35s (%llu estimated cycles)\n", impl.is_default ? "*" : " ", impl.name.c_str(), (unsigned long long) impl.cycle_estimate); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | }; |