| 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 <time.h> |
| 10 | |
| 11 | #include <cassert> |
| 12 | #include <cstring> |
| 13 | #include <type_traits> |
| 14 | |
| 15 | #include "barrier.hpp" |
| 16 | #include "gemm_lib.hpp" |
| 17 | #include "kai/ops/gemm/kai_ops.hpp" |
| 18 | #include "scheduler.hpp" |
| 19 | #include "utils.hpp" // iceildiv |
| 20 | |
| 21 | #include "barrier.hpp" |
| 22 | |
| 23 | #undef TASKMASTER |
| 24 | |
| 25 | // (Templated) glue to make it work with current infrastructure |
| 26 | template <typename Tlop, typename Trop, typename Tret, QuantizationType quantized=QuantizationType::NONE> |
| 27 | class kai_ops_wrapper { |
| 28 | private: |
| 29 | struct free_delete { |
| 30 | void operator()(void *x) { free_aligned_memory(x); } |
| 31 | }; |
| 32 | |
| 33 | // Quantized support |
| 34 | std::shared_ptr<Matrix<int32_t>> bias; |
| 35 | Requantize32 qp; |
| 36 | |
| 37 | const GemmProblem * const p; |
| 38 | |
| 39 | const unsigned int nthreads; |
| 40 | |
| 41 | std::unique_ptr<void, free_delete> pretransposed_data={}; |
| 42 | std::unique_ptr<void, free_delete> working_data={}; |
| 43 | |
| 44 | /* im2row support */ |
| 45 | std::shared_ptr<Matrix<Tlop>> im2row_buf; |
| 46 | |
| 47 | /* indirect support */ |
| 48 | std::shared_ptr<Matrix<const Tlop *>> indirect_buf; |
| 49 | std::vector<Tlop> padding_buf; |
| 50 | |
| 51 | std::unique_ptr<const Tlop * const *, free_delete> indirect_arg; |
| 52 | |
| 53 | kai::ops::UniqueGemmCommon<Tlop, Trop, Tret> gemm; |
| 54 | std::unique_ptr<scheduler> sched; |
| 55 | |
| 56 | // Need to keep this for timing transform. |
| 57 | std::shared_ptr<Matrix<Trop>> B; |
| 58 | |
| 59 | // Need these for fixed format cases - B_transformed also used for transpose B cases |
| 60 | std::shared_ptr<Matrix<Trop>> B_transformed; |
| 61 | std::shared_ptr<Matrix<bfloat16>> B_transformed_bf16; |
| 62 | |
| 63 | // Either B or B_transformed, depending on whether transpose |
| 64 | std::shared_ptr<Matrix<Trop>> B_pt; |
| 65 | |
| 66 | bool B_pt_is_transposed = false; |
| 67 | |
| 68 | kai::ops::barrier barrier; |
| 69 | |
| 70 | public: |
| 71 | static int get_m_block() { return 1; } |
| 72 | static int get_n_block() { return 1; } |
| 73 | static int get_k_block() { return 1; } |
| 74 | |
| 75 | typedef Tlop lhs_operand_type; |
| 76 | typedef Trop rhs_operand_type; |
| 77 | typedef Tret result_type; |
| 78 | |
| 79 | kai_ops_wrapper(kai_ops_wrapper &) = delete; |
| 80 | kai_ops_wrapper(kai_ops_wrapper &&) = default; |
| 81 | kai_ops_wrapper & operator=(kai_ops_wrapper &) = delete; |
| 82 | |
| 83 | // Constructor: non-quantized |
| 84 | kai_ops_wrapper(std::shared_ptr<Matrix<Tlop>> &A, |
| 85 | std::shared_ptr<Matrix<Trop>> &B, |
| 86 | std::shared_ptr<Matrix<Tret>> &C, |
| 87 | std::shared_ptr<Matrix<Tret>> &bias, |
| 88 | GemmProblem *p, unsigned int nthreads, int do_init) : p(p), nthreads(nthreads), B(B), barrier(nthreads) { |
| 89 | assert(quantized==QuantizationType::NONE || quantized==QuantizationType::FLOAT); |
| 90 | |
| 91 | if (p->groups != 1) { |
| 92 | printf("kai_ops_wrapper: Groups not supported, aborting.\n"); |
| 93 | exit(1); |
| 94 | } |
| 95 | |
| 96 | const unsigned int output_hw = p->output_height * p->output_width; |
| 97 | |
| 98 | // K parameter for the GEMM - input channels is correct for non-convolution cases, indirect and convolution |
| 99 | // GEMMs. For im2row we update this later. |
| 100 | unsigned int gemm_k = p->input_channels; |
| 101 | unsigned int gemm_sections = 1; |
| 102 | bool indirect_flag = false; |
| 103 | |
| 104 | const CPUInfo *ci = get_CPUInfo(); |
| 105 | |
| 106 | kai::ops::GemmConfig cfg; |
| 107 | |
| 108 | cfg.filter = p->kernel_filter; |
| 109 | cfg.inner_block_size = p->inner_block_size; |
| 110 | cfg.outer_block_size = p->outer_block_size; |
| 111 | cfg.weight_format = p->weight_format; |
| 112 | |
| 113 | if (!p->is_basic_gemm()) { |
| 114 | if (p->strategy == ConvStrategy::indirect || p->strategy == ConvStrategy::convolution) { |
| 115 | indirect_flag = true; |
| 116 | gemm_sections = p->kernel_height * p->kernel_width; |
| 117 | } else { |
| 118 | // For im2row GEMMs, we pass in the "full" K value. |
| 119 | gemm_k = p->kernel_height * p->kernel_width * p->input_channels; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | auto args = kai::ops::GemmArgs(ci, output_hw, p->output_channels, gemm_k, gemm_sections, p->batches, p->multis, indirect_flag, p->act, nthreads, p->fixed_format, p->fast_mode, p->accumulate, &cfg); |
| 124 | |
| 125 | gemm = kai::ops::gemm<Tlop, Trop, Tret>(args); |
| 126 | if (gemm.get() == nullptr) { |
| 127 | printf("kai_ops_wrapper: No GEMM matching stated requirements, aborting.\n"); |
| 128 | exit(1); |
| 129 | } |
| 130 | |
| 131 | do_common_setup(A, B, C, bias, p, nthreads, do_init, 0); |
| 132 | } |
| 133 | |
| 134 | // Constructor: dequantized |
| 135 | kai_ops_wrapper(std::shared_ptr<Matrix<Tlop>> &A, |
| 136 | std::shared_ptr<Matrix<Trop>> &B, |
| 137 | std::shared_ptr<Matrix<Tret>> &C, |
| 138 | std::shared_ptr<Matrix<Tret>> &bias, |
| 139 | kai::ops::DequantizeFloat &scale, |
| 140 | GemmProblem *p, unsigned int nthreads, int do_init) : p(p), nthreads(nthreads), B(B), barrier(nthreads) { |
| 141 | assert(quantized==QuantizationType::FLOAT); |
| 142 | |
| 143 | if (p->groups != 1) { |
| 144 | printf("kai_ops_wrapper: Groups not supported, aborting.\n"); |
| 145 | exit(1); |
| 146 | } |
| 147 | |
| 148 | const unsigned int output_hw = p->output_height * p->output_width; |
| 149 | |
| 150 | // K parameter for the GEMM - input channels is correct for non-convolution cases, indirect and convolution |
| 151 | // GEMMs. For im2row we update this later. |
| 152 | unsigned int gemm_k = p->input_channels; |
| 153 | unsigned int gemm_sections = 1; |
| 154 | bool indirect_flag = false; |
| 155 | |
| 156 | const CPUInfo *ci = get_CPUInfo(); |
| 157 | |
| 158 | kai::ops::GemmConfig cfg; |
| 159 | |
| 160 | cfg.filter = p->kernel_filter; |
| 161 | cfg.inner_block_size = p->inner_block_size; |
| 162 | cfg.outer_block_size = p->outer_block_size; |
| 163 | cfg.weight_format = p->weight_format; |
| 164 | |
| 165 | if (!p->is_basic_gemm()) { |
| 166 | if (p->strategy == ConvStrategy::indirect || p->strategy == ConvStrategy::convolution) { |
| 167 | indirect_flag = true; |
| 168 | gemm_sections = p->kernel_height * p->kernel_width; |
| 169 | } else { |
| 170 | // For im2row GEMMs, we pass in the "full" K value. |
| 171 | gemm_k = p->kernel_height * p->kernel_width * p->input_channels; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | auto args = kai::ops::GemmArgs(ci, output_hw, p->output_channels, gemm_k, gemm_sections, p->batches, p->multis, indirect_flag, p->act, nthreads, p->fixed_format, p->fast_mode, p->accumulate, &cfg); |
| 176 | |
| 177 | gemm = kai::ops::gemm<Tlop, Trop, Tret, kai::ops::DequantizeFloat>(args, scale); |
| 178 | |
| 179 | if (gemm.get() == nullptr) { |
| 180 | printf("kai_ops_wrapper: No GEMM matching stated requirements, aborting.\n"); |
| 181 | exit(1); |
| 182 | } |
| 183 | |
| 184 | do_common_setup(A, B, C, bias, p, nthreads, do_init, 0); |
| 185 | } |
| 186 | |
| 187 | // Constructor: quantized |
| 188 | template<typename Tfloat> |
| 189 | kai_ops_wrapper(std::shared_ptr<Matrix<Tlop>> &A, |
| 190 | std::shared_ptr<Matrix<Trop>> &B, |
| 191 | std::shared_ptr<Matrix<Tlop>> &C, |
| 192 | std::shared_ptr<Matrix<int32_t>> &bias, |
| 193 | QuantizeParameters<Tlop, Tfloat> &A_qp, |
| 194 | std::vector<QuantizeParameters<Trop, Tfloat> > &B_qp, |
| 195 | QuantizeParameters<Tlop, Tfloat> &C_qp, |
| 196 | GemmProblem *p, unsigned int nthreads, int do_init) : bias(bias), p(p), nthreads(nthreads), B(B), barrier(nthreads) { |
| 197 | assert(quantized==QuantizationType::INTEGER); |
| 198 | |
| 199 | if (p->groups != 1) { |
| 200 | printf("kai_ops_wrapper: Groups not supported, aborting.\n"); |
| 201 | exit(1); |
| 202 | } |
| 203 | |
| 204 | if (p->in_stride_h != 1 || p->in_stride_w != 1) { |
| 205 | printf("kai_ops_wrapper: in_stride/dilation not supported, aborting.\n"); |
| 206 | exit(1); |
| 207 | } |
| 208 | |
| 209 | if (p->accumulate) { |
| 210 | printf("kai_ops_wrapper: Accumulation not supported, aborting.\n"); |
| 211 | exit(1); |
| 212 | } |
| 213 | |
| 214 | const CPUInfo *ci = get_CPUInfo(); |
| 215 | |
| 216 | const unsigned int output_hw = p->output_height * p->output_width; |
| 217 | |
| 218 | // K parameter for the GEMM - input channels is correct for non-convolution cases, indirect and convolution |
| 219 | // GEMMs. For im2row we update this later. |
| 220 | unsigned int gemm_k = p->input_channels; |
| 221 | unsigned int gemm_sections = 1; |
| 222 | bool indirect_flag = false; |
| 223 | |
| 224 | kai::ops::GemmConfig cfg; |
| 225 | |
| 226 | cfg.filter = p->kernel_filter; |
| 227 | cfg.inner_block_size = p->inner_block_size; |
| 228 | cfg.outer_block_size = p->outer_block_size; |
| 229 | cfg.weight_format = p->weight_format; |
| 230 | |
| 231 | /* Set quantization parameters */ |
| 232 | qp.bias = (bias ? bias->data : nullptr); |
| 233 | qp.bias_multi_stride = bias ? bias->multi_stride : 0; |
| 234 | qp.a_offset = A_qp.m_zeropt; |
| 235 | qp.b_offset = B_qp[0].m_zeropt; |
| 236 | qp.c_offset = C_qp.m_zeropt; |
| 237 | qp.minval = C_qp.m_minval; |
| 238 | qp.maxval = C_qp.m_maxval; |
| 239 | |
| 240 | qp.set_multipliers(A_qp.m_scale / C_qp.m_scale, B_qp); |
| 241 | |
| 242 | if (!p->is_basic_gemm()) { |
| 243 | if (p->strategy == ConvStrategy::indirect || p->strategy == ConvStrategy::convolution) { |
| 244 | indirect_flag = true; |
| 245 | gemm_sections = p->kernel_height * p->kernel_width; |
| 246 | } else { |
| 247 | // For im2row GEMMs, we pass in the "full" K value. |
| 248 | gemm_k = p->kernel_height * p->kernel_width * p->input_channels; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Don't (currently) apply activation or accumulation to quantized GEMMs. |
| 253 | auto args = kai::ops::GemmArgs(ci, output_hw, p->output_channels, gemm_k, gemm_sections, p->batches, p->multis, indirect_flag, Activation(), nthreads, p->fixed_format, p->fast_mode, false, &cfg); |
| 254 | |
| 255 | gemm = kai::ops::gemm<Tlop, Trop, Tret, kai::ops::Requantize32>(args, qp); |
| 256 | |
| 257 | if (gemm.get() == nullptr) { |
| 258 | printf("kai_ops_wrapper: No GEMM matching stated requirements, aborting.\n"); |
| 259 | exit(1); |
| 260 | } |
| 261 | |
| 262 | // The "normal" bias is always null in quantized cases - need a null |
| 263 | // one to pass on to do_common_setup() |
| 264 | auto null_bias_ptr = std::shared_ptr<Matrix<Tret>>(nullptr); |
| 265 | |
| 266 | do_common_setup(A, B, C, null_bias_ptr, p, nthreads, do_init, A_qp.m_zeropt); |
| 267 | } |
| 268 | |
| 269 | void do_common_setup(std::shared_ptr<Matrix<Tlop>> &A, |
| 270 | std::shared_ptr<Matrix<Trop>> &B, |
| 271 | std::shared_ptr<Matrix<Tret>> &C, |
| 272 | std::shared_ptr<Matrix<Tret>> &bias, |
| 273 | GemmProblem *p, unsigned int nthreads, int do_init, Tlop zeropt) |
| 274 | { |
| 275 | auto my_cfg = gemm->get_config(); |
| 276 | |
| 277 | #ifndef SILENT |
| 278 | printf("GEMM configuration: kernel=%s inner_block=%u outer_block=%u weight_format=%x\n", my_cfg.filter.c_str(), my_cfg.inner_block_size, my_cfg.outer_block_size, static_cast<uint32_t>(my_cfg.weight_format)); |
| 279 | #endif |
| 280 | const unsigned int output_hw = p->output_height * p->output_width; |
| 281 | const unsigned int kernel_hwi = p->kernel_height * p->kernel_width * p->input_channels; |
| 282 | const unsigned int kernel_hw = p->kernel_height * p->kernel_width; |
| 283 | |
| 284 | // Values to pass in to describe the B argument in set_arrays() |
| 285 | // For pretransposed cases these don't matter, but we need to populate them for fixed format. |
| 286 | // This is done this way because in BF16 cases we need to do some icky casting. |
| 287 | const Trop *setargs_B_ptr = nullptr; |
| 288 | size_t setargs_B_stride = 0; |
| 289 | size_t setargs_B_multi_stride = 0; |
| 290 | |
| 291 | // Do the B transform if needed |
| 292 | if (p->fixed_format) { |
| 293 | // If we requested a fixed format kernel, it is required that a valid weight_format is provided. |
| 294 | if (my_cfg.weight_format == kai::ops::WeightFormat::UNSPECIFIED) { |
| 295 | printf("Error: Requested fixed format GEMM but no weight format specified. Aborting.\n"); |
| 296 | exit(1); |
| 297 | } |
| 298 | |
| 299 | // Weight formats are bitfield encoded, so we can extract the interleave/block parameters by casting and |
| 300 | // masking. |
| 301 | uint32_t i_wf = static_cast<uint32_t>(my_cfg.weight_format); |
| 302 | |
| 303 | auto interleave_by = (i_wf >> 8) & 0xFFF; |
| 304 | auto block_by = (i_wf >> 20) & 0xF; |
| 305 | |
| 306 | // By default, assume we will be packing data per kernel point - |
| 307 | // so we will need to round up the channel count according to |
| 308 | // "block_by" for each point. Basic GEMM cases only have a |
| 309 | // single point so this makes no difference. |
| 310 | auto kernel_points = p->kernel_height * p->kernel_width; |
| 311 | auto rounded_input_channels = roundup<unsigned int>(p->input_channels, block_by); |
| 312 | auto copy_input_channels = p->input_channels; |
| 313 | auto rounded_height = iceildiv(B->N, interleave_by); |
| 314 | |
| 315 | // Except that if we are going to use "im2row" style |
| 316 | // convolution, we shouldn't do the per-kernel-point packing |
| 317 | // after all - adjust values accordingly so we pack all the |
| 318 | // points at once. |
| 319 | if (!p->is_basic_gemm() && p->strategy==ConvStrategy::im2row) { |
| 320 | kernel_points = 1; |
| 321 | rounded_input_channels = roundup<unsigned int>(p->input_channels * p->kernel_height * p->kernel_width, block_by); |
| 322 | copy_input_channels = p->input_channels * p->kernel_height * p->kernel_width; |
| 323 | } |
| 324 | |
| 325 | // Each row of transformed output contains "interleave_by" channels and also needs to allow for each |
| 326 | // kernel point being padded based on the input channel blacking. |
| 327 | auto transformed_width = kernel_points * rounded_input_channels * interleave_by; |
| 328 | |
| 329 | // If the bf16 flag is set, the weights need to be prepared in bf16. |
| 330 | if (((i_wf >> 4) & 0xf) == 1) { |
| 331 | B_transformed_bf16 = std::make_shared<Matrix<bfloat16>>(rounded_height, transformed_width, transformed_width, 1, B->multis); |
| 332 | |
| 333 | // To make sure we get per-kernel point padding right, slice the input and output by kernel point and |
| 334 | // transform each one individually. |
| 335 | for(int i=0; i<kernel_points; i++) { |
| 336 | auto input_slice = B->split_rows(i * copy_input_channels, copy_input_channels); |
| 337 | auto output_slice = B_transformed_bf16->split_cols(i * rounded_input_channels * interleave_by, rounded_input_channels * interleave_by); |
| 338 | |
| 339 | output_slice->Interleave_Blocked_Transposed(input_slice, interleave_by, block_by); |
| 340 | } |
| 341 | |
| 342 | // In BF16 cases, this pointer is immediately cast back to a bf16 * on the other side. |
| 343 | setargs_B_ptr = reinterpret_cast<Trop *>(B_transformed_bf16->data); |
| 344 | setargs_B_stride = B_transformed_bf16->stride; |
| 345 | setargs_B_multi_stride = B_transformed_bf16->multi_stride; |
| 346 | } else { |
| 347 | B_transformed = std::make_shared<Matrix<Trop>>(rounded_height, transformed_width, transformed_width, 1, B->multis); |
| 348 | |
| 349 | // To make sure we get per-kernel point padding right, slice the input and output by kernel point and |
| 350 | // transform each one individually. |
| 351 | for(int i=0; i<kernel_points; i++) { |
| 352 | auto input_slice = B->split_rows(i * copy_input_channels, copy_input_channels); |
| 353 | auto output_slice = B_transformed->split_cols(i * rounded_input_channels * interleave_by, rounded_input_channels * interleave_by); |
| 354 | |
| 355 | output_slice->Interleave_Blocked_Transposed(input_slice, interleave_by, block_by); |
| 356 | } |
| 357 | |
| 358 | setargs_B_ptr = B_transformed->data; |
| 359 | setargs_B_stride = B_transformed->stride; |
| 360 | setargs_B_multi_stride = B_transformed->multi_stride; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Set the arrays for the GEMM, or for the convolution type GEMMs do |
| 365 | // whatever handling is necessary. |
| 366 | if (p->is_basic_gemm()) { |
| 367 | gemm->set_arrays(A->data, A->stride, A->batch_stride, A->multi_stride, |
| 368 | setargs_B_ptr, setargs_B_stride, setargs_B_multi_stride, |
| 369 | C->data, C->stride, C->batch_stride, C->multi_stride, |
| 370 | (bias ? bias->data : nullptr), (bias ? bias->multi_stride : 0)); |
| 371 | } else { |
| 372 | switch (p->strategy) { |
| 373 | default: |
| 374 | case ConvStrategy::fail: |
| 375 | printf("Convolution problem requested but no strategy specified - aborting.\n"); |
| 376 | exit(1); |
| 377 | break; |
| 378 | |
| 379 | case ConvStrategy::im2row: |
| 380 | #ifndef SILENT |
| 381 | printf("Configuring im2row GEMM.\n"); |
| 382 | #endif |
| 383 | { |
| 384 | im2row_buf = std::make_shared<Matrix <Tlop> >(output_hw, kernel_hwi, kernel_hwi, p->batches, p->multis); |
| 385 | |
| 386 | if (im2row_buf.get() == nullptr) { |
| 387 | printf("Unable to allocate im2row buffer.\n"); |
| 388 | exit(1); |
| 389 | } |
| 390 | |
| 391 | for (int64_t m=0; m<p->multis; m++) { |
| 392 | for (int64_t b=0; b<p->batches; b++) { |
| 393 | for (int64_t output_y=0; output_y<p->output_height; output_y++) { |
| 394 | for (int64_t output_x=0; output_x<p->output_width; output_x++) { |
| 395 | int64_t output_xy = (output_y * p->output_width) + output_x; |
| 396 | int64_t output_pos = 0; |
| 397 | |
| 398 | for (int64_t kernel_y=0; kernel_y<p->kernel_height; kernel_y++) { |
| 399 | for (int64_t kernel_x=0; kernel_x<p->kernel_width; kernel_x++) { |
| 400 | int64_t input_x = (output_x * p->out_stride_w) + (kernel_x * p->in_stride_w) - p->padding_left; |
| 401 | int64_t input_y = (output_y * p->out_stride_h) + (kernel_y * p->in_stride_h) - p->padding_top; |
| 402 | |
| 403 | if (input_x < 0 || input_x >= p->input_width || input_y < 0 || input_y >= p->input_height) { |
| 404 | for (int64_t inch=0; inch < p->input_channels; inch++) { |
| 405 | im2row_buf->safe_set(m, b, output_xy, output_pos, zeropt); |
| 406 | output_pos++; |
| 407 | } |
| 408 | } else { |
| 409 | int64_t input_xy = (input_y * p->input_width) + input_x; |
| 410 | |
| 411 | for (int64_t inch = 0; inch < p->input_channels; inch++) { |
| 412 | im2row_buf->safe_set(m, b, output_xy, output_pos, A->safe_read(m, b, input_xy, inch)); |
| 413 | output_pos++; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | gemm->set_arrays(im2row_buf->data, im2row_buf->stride, im2row_buf->batch_stride, im2row_buf->multi_stride, |
| 424 | setargs_B_ptr, setargs_B_stride, setargs_B_multi_stride, |
| 425 | C->data, C->stride, C->batch_stride, C->multi_stride, |
| 426 | (bias ? bias->data : nullptr), (bias ? bias->multi_stride : 0)); |
| 427 | } |
| 428 | break; |
| 429 | |
| 430 | case ConvStrategy::indirect: |
| 431 | #ifndef SILENT |
| 432 | printf("Configuring indirect GEMM.\n"); |
| 433 | #endif |
| 434 | { |
| 435 | indirect_buf = std::make_shared<Matrix <const Tlop *> >(kernel_hw, output_hw, output_hw, p->batches, p->multis); |
| 436 | padding_buf = std::vector<Tlop>(p->input_channels, zeropt); |
| 437 | |
| 438 | for (int64_t m=0; m<p->multis; m++) { |
| 439 | for (int64_t b=0; b<p->batches; b++) { |
| 440 | for (int64_t output_y=0; output_y<p->output_height; output_y++) { |
| 441 | for (int64_t output_x=0; output_x<p->output_width; output_x++) { |
| 442 | int64_t output_xy = (output_y * p->output_width) + output_x; |
| 443 | |
| 444 | for (int64_t kernel_y=0; kernel_y<p->kernel_height; kernel_y++) { |
| 445 | for (int64_t kernel_x=0; kernel_x<p->kernel_width; kernel_x++) { |
| 446 | int64_t input_x = (output_x * p->out_stride_w) + (kernel_x * p->in_stride_w) - p->padding_left; |
| 447 | int64_t input_y = (output_y * p->out_stride_h) + (kernel_y * p->in_stride_h) - p->padding_top; |
| 448 | int64_t kernel_xy = (kernel_y * p->kernel_width) + kernel_x; |
| 449 | int64_t input_xy = (input_y * p->input_width) + input_x; |
| 450 | |
| 451 | if (input_x < 0 || input_x >= p->input_width || input_y < 0 || input_y >= p->input_height) { |
| 452 | indirect_buf->safe_set(m, b, kernel_xy, output_xy, padding_buf.data()); |
| 453 | } else { |
| 454 | indirect_buf->safe_set(m, b, kernel_xy, output_xy, A->data + (m * A->multi_stride + b * A->batch_stride + input_xy * A->stride)); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | indirect_arg = std::unique_ptr<const Tlop * const *, free_delete>(reinterpret_cast<const Tlop * const **>(allocate_aligned_memory(64, sizeof(Tlop **) * p->kernel_height * p->kernel_width * p->multis * p->batches))); |
| 464 | |
| 465 | int64_t pos=0; |
| 466 | for (int64_t m=0;m<p->multis;m++) { |
| 467 | for (int64_t b=0;b<p->batches;b++) { |
| 468 | for (int64_t kernel_xy=0; kernel_xy<p->kernel_width * p->kernel_height; kernel_xy++) { |
| 469 | (indirect_arg.get())[pos++] = indirect_buf->data + m * indirect_buf->multi_stride + b * indirect_buf->batch_stride + kernel_xy * indirect_buf->stride; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | gemm->set_arrays(nullptr, 0, 0, 0, |
| 475 | setargs_B_ptr, setargs_B_stride, setargs_B_multi_stride, |
| 476 | C->data, C->stride, C->batch_stride, C->multi_stride, |
| 477 | (bias ? bias->data : nullptr), (bias ? bias->multi_stride : 0)); |
| 478 | |
| 479 | gemm->set_indirect_parameters( p->input_channels, indirect_arg.get() ); |
| 480 | } |
| 481 | break; |
| 482 | |
| 483 | case ConvStrategy::convolution: |
| 484 | #ifndef SILENT |
| 485 | printf("Configuring convolution GEMM.\n"); |
| 486 | #endif |
| 487 | gemm->set_arrays(A->data, A->stride, A->batch_stride, A->multi_stride, |
| 488 | setargs_B_ptr, setargs_B_stride, setargs_B_multi_stride, |
| 489 | C->data, C->stride, C->batch_stride, C->multi_stride, |
| 490 | (bias ? bias->data : nullptr), (bias ? bias->multi_stride : 0)); |
| 491 | |
| 492 | kai::ops::ConvolutionParameters cp = { |
| 493 | p->input_width, p->input_height, p->input_channels, |
| 494 | p->kernel_width, p->kernel_height, p->output_width, p->output_height, |
| 495 | p->out_stride_w, p->out_stride_h, p->in_stride_w, p->in_stride_h, |
| 496 | p->padding_top, p->padding_left, static_cast<float>(zeropt) }; |
| 497 | |
| 498 | gemm->set_convolution_parameters(cp); |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | |
| 504 | if (gemm->B_is_pretransposed()) { |
| 505 | assert(!p->fixed_format); // Fixed format and pretransposed makes no sense. |
| 506 | |
| 507 | if (p->transposed_b && gemm->B_pretranspose_supports_transpose()) { |
| 508 | #ifndef SILENT |
| 509 | printf("Generating transposed form of B matrix.\n"); |
| 510 | #endif |
| 511 | B_transformed = std::make_shared<Matrix<Trop>>(B->N, B->M, std::max<int>(B->M, p->b_stride), 1, B->multis); |
| 512 | B_transformed->Transpose(B); |
| 513 | |
| 514 | B_pt = B_transformed; |
| 515 | B_pt_is_transposed = true; |
| 516 | } else { |
| 517 | B_pt = B; |
| 518 | B_pt_is_transposed = false; |
| 519 | } |
| 520 | |
| 521 | #ifndef SILENT |
| 522 | printf("Allocating pretranspose data: %zu bytes\n",gemm->get_B_pretransposed_array_size()); |
| 523 | #endif |
| 524 | pretransposed_data = std::unique_ptr<void, free_delete>(allocate_aligned_memory(64, gemm->get_B_pretransposed_array_size())); |
| 525 | if (!p->time_weight_transform) { |
| 526 | #ifndef BARE_METAL |
| 527 | struct timespec ts1, ts2; |
| 528 | clock_gettime(CLOCK_MONOTONIC, &ts1); |
| 529 | gemm->pretranspose_B_array(pretransposed_data.get(), B_pt->data, B_pt->stride, B_pt->multi_stride, B_pt_is_transposed); |
| 530 | clock_gettime(CLOCK_MONOTONIC, &ts2); |
| 531 | #ifndef SILENT |
| 532 | int64_t res=(ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec); |
| 533 | printf("Pretranspose: %" PRId64 " ns.\n", res); |
| 534 | #endif |
| 535 | #else // BARE_METAL |
| 536 | gemm->pretranspose_B_array(pretransposed_data.get(), B_pt->data, B_pt->stride, B_pt->multi_stride, B_pt_is_transposed); |
| 537 | if (p->cache_flush) { |
| 538 | do_cache_flush(pretransposed_data.get(), gemm->get_B_pretransposed_array_size()); |
| 539 | } |
| 540 | #endif |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | if (gemm->get_working_size()) { |
| 545 | #ifndef SILENT |
| 546 | printf("Allocating working data: %zu bytes\n",gemm->get_working_size()); |
| 547 | #endif |
| 548 | working_data = std::unique_ptr<void, free_delete>(allocate_aligned_memory(64, gemm->get_working_size())); |
| 549 | gemm->set_working_space(working_data.get()); |
| 550 | } |
| 551 | |
| 552 | auto winsize = gemm->get_window_size().total_size(); |
| 553 | |
| 554 | if (winsize < nthreads) { |
| 555 | gemm->set_nthreads(winsize); |
| 556 | nthreads = winsize; |
| 557 | } |
| 558 | |
| 559 | #ifndef NO_MULTI_THREADING |
| 560 | if (p->dynamic_scheduling && gemm->supports_dynamic_scheduling()) { |
| 561 | // printf("Setting up dynamic scheduling (granule=%u)\n", p->dynamic_granule_count); |
| 562 | #ifdef TASKMASTER |
| 563 | sched = std::unique_ptr<scheduler> (new taskmaster_scheduler(gemm.get(), nthreads, p->dynamic_granule_count)); |
| 564 | #else |
| 565 | sched = std::unique_ptr<scheduler> (new dynamic_scheduler(gemm.get(), nthreads, p->dynamic_granule_count)); |
| 566 | #endif |
| 567 | } else { |
| 568 | #else |
| 569 | if (1) { |
| 570 | #endif |
| 571 | // printf("Setting up static scheduling.\n"); |
| 572 | sched = std::unique_ptr<scheduler> (new static_scheduler(gemm.get(), nthreads, p->schedule_shape_override)); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | __attribute__ ((noinline)) void Run(unsigned int threadid) { |
| 577 | if (gemm->B_is_pretransposed() && p->time_weight_transform) { |
| 578 | const size_t wsize = gemm->get_B_pretranspose_window_size(); |
| 579 | const size_t start = (threadid * wsize) / nthreads; |
| 580 | const size_t end = ((threadid + 1) * wsize) / nthreads; |
| 581 | |
| 582 | if (start < end) { |
| 583 | gemm->pretranspose_B_array_part(pretransposed_data.get(), B_pt->data, B_pt->stride, B_pt->multi_stride, B_pt_is_transposed, start, end); |
| 584 | } |
| 585 | barrier.arrive_and_wait(); |
| 586 | } |
| 587 | sched->execute(threadid); |
| 588 | } |
| 589 | |
| 590 | static std::vector<kai::ops::KernelDescription> get_kernels(kai::ops::GemmArgs args) { |
| 591 | using OutputStage = typename std::conditional<quantized == QuantizationType::NONE, kai::ops::Nothing, typename std::conditional<quantized == QuantizationType::INTEGER, kai::ops::Requantize32, kai::ops::DequantizeFloat>::type>::type; |
| 592 | return kai::ops::get_compatible_kernels<Tlop, Trop, Tret, OutputStage>(args); |
| 593 | } |
| 594 | |
| 595 | /* Dump valid kernel name list. */ |
| 596 | static void print_kernels (GemmProblem *p, unsigned int nthreads) { |
| 597 | const unsigned int output_hw = p->output_height * p->output_width; |
| 598 | |
| 599 | // K parameter for the GEMM - input channels is correct for non-convolution cases, indirect and convolution |
| 600 | // GEMMs. For im2row we update this later. |
| 601 | unsigned int gemm_k = p->input_channels; |
| 602 | unsigned int gemm_sections = 1; |
| 603 | bool indirect_flag = false; |
| 604 | |
| 605 | const CPUInfo *ci = get_CPUInfo(); |
| 606 | |
| 607 | kai::ops::GemmConfig cfg; |
| 608 | |
| 609 | cfg.filter = p->kernel_filter; |
| 610 | cfg.inner_block_size = p->inner_block_size; |
| 611 | cfg.outer_block_size = p->outer_block_size; |
| 612 | cfg.weight_format = p->weight_format; |
| 613 | |
| 614 | if (p->groups != 1) { |
| 615 | printf("kai_ops_wrapper: Groups not supported, aborting.\n"); |
| 616 | exit(1); |
| 617 | } |
| 618 | |
| 619 | // Accumulation mode is not supported for integer requantizing GEMMS. |
| 620 | if (p->accumulate && quantized==QuantizationType::INTEGER) { |
| 621 | printf("kai_ops_wrapper: Accumulation not supported, aborting.\n"); |
| 622 | exit(1); |
| 623 | } |
| 624 | |
| 625 | if (!p->is_basic_gemm()) { |
| 626 | if (p->strategy == ConvStrategy::indirect || p->strategy == ConvStrategy::convolution) { |
| 627 | indirect_flag = true; |
| 628 | gemm_sections = p->kernel_height * p->kernel_width; |
| 629 | } else { |
| 630 | // For im2row GEMMs, we pass in the "full" K value. |
| 631 | gemm_k = p->kernel_height * p->kernel_width * p->input_channels; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | auto args = kai::ops::GemmArgs(ci, output_hw, p->output_channels, gemm_k, gemm_sections, p->batches, p->multis, indirect_flag, p->act, nthreads, p->fixed_format, p->fast_mode, p->accumulate, &cfg); |
| 636 | |
| 637 | auto kernel_list = get_kernels(args); |
| 638 | |
| 639 | if (kernel_list.size() > 0) { |
| 640 | printf("Available kernels:\n"); |
| 641 | for (auto &&i : kernel_list) { |
| 642 | // Only print kernels which match the filter (if one has been provided) |
| 643 | if (cfg.filter != "" && !strstr(i.name.c_str(), cfg.filter.c_str())) { |
| 644 | continue; |
| 645 | } |
| 646 | if (i.cycle_estimate == UINT64_MAX) { |
| 647 | printf("\t%s%-35s (not preferred)\n", i.is_default?"*":" ", i.name.c_str()); |
| 648 | } else if (i.cycle_estimate == 0) { |
| 649 | printf("\t%s%-35s (preferred)\n", i.is_default?"*":" ", i.name.c_str()); |
| 650 | } else { |
| 651 | printf("\t%s%-35s (%llu estimated cycles)\n", i.is_default?"*":" ", i.name.c_str(), (unsigned long long)i.cycle_estimate); |
| 652 | } |
| 653 | } |
| 654 | } else { |
| 655 | printf("No kernels available for selected parameters.\n"); |
| 656 | } |
| 657 | } |
| 658 | }; |
| 659 | |
| 660 | template<typename Tlop, typename Trop, typename Tret> |
| 661 | using kai_ops_quantized = kai_ops_wrapper<Tlop, Trop, Tret, QuantizationType::INTEGER>; |