| 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 | |
| 13 | #include "barrier.hpp" |
| 14 | #include "gemm_lib.hpp" |
| 15 | #include "kai/ops/gemm/kai_ops.hpp" |
| 16 | #include "scheduler.hpp" |
| 17 | #include "utils.hpp" // iceildiv |
| 18 | |
| 19 | #undef TASKMASTER |
| 20 | |
| 21 | #include "kai/ops/conv/winograd.hpp" |
| 22 | |
| 23 | // (Templated) glue to make it work with current infrastructure |
| 24 | template <typename Top, typename Tret> |
| 25 | class winograd_wrapper { |
| 26 | private: |
| 27 | struct free_delete { |
| 28 | void operator()(void *x) { free_aligned_memory(x); } |
| 29 | }; |
| 30 | |
| 31 | const GemmProblem * const p; |
| 32 | |
| 33 | const unsigned int nthreads; |
| 34 | |
| 35 | std::unique_ptr<void, free_delete> winograd_input={}; |
| 36 | std::unique_ptr<void, free_delete> winograd_output={}; |
| 37 | std::unique_ptr<void, free_delete> winograd_weights={}; |
| 38 | std::unique_ptr<void, free_delete> gemm_pretransposed_data={}; |
| 39 | |
| 40 | std::unique_ptr<void, free_delete> working_data={}; |
| 41 | |
| 42 | std::unique_ptr<scheduler> sched; |
| 43 | std::unique_ptr<kai::ops::barrier> barr; |
| 44 | |
| 45 | std::shared_ptr<Matrix<Top>> A; |
| 46 | std::shared_ptr<Matrix<Tret>> C; |
| 47 | std::shared_ptr<Matrix<Tret>> bias; |
| 48 | |
| 49 | kai::ops::ConvolutionArgs conv_args; |
| 50 | kai::ops::winograd::WinogradImpl winograd_impl; |
| 51 | kai::ops::UniqueGemmCommon<Top, Top, Top> gemm; |
| 52 | |
| 53 | public: |
| 54 | static int get_m_block() { return 1; } |
| 55 | static int get_n_block() { return 1; } |
| 56 | static int get_k_block() { return 1; } |
| 57 | |
| 58 | typedef Top lhs_operand_type; |
| 59 | typedef Top rhs_operand_type; |
| 60 | typedef Tret result_type; |
| 61 | |
| 62 | winograd_wrapper(winograd_wrapper &) = delete; |
| 63 | winograd_wrapper(winograd_wrapper &&) = default; |
| 64 | winograd_wrapper & operator=(winograd_wrapper &) = delete; |
| 65 | |
| 66 | // Constructor: non-quantized |
| 67 | winograd_wrapper( |
| 68 | std::shared_ptr<Matrix<Top>> &A, |
| 69 | std::shared_ptr<Matrix<Top>> &B, |
| 70 | std::shared_ptr<Matrix<Tret>> &C, |
| 71 | std::shared_ptr<Matrix<Tret>> &bias, |
| 72 | GemmProblem *p, unsigned int nthreads, int do_init |
| 73 | ) : p(p), nthreads(nthreads), barr(new kai::ops::barrier(nthreads)), |
| 74 | A(A), C(C), bias(bias), |
| 75 | conv_args( |
| 76 | p->batches, |
| 77 | { static_cast<unsigned int>(p->input_height), |
| 78 | static_cast<unsigned int>(p->input_width) }, |
| 79 | p->input_channels, |
| 80 | p->padding_top, p->padding_left, |
| 81 | { static_cast<unsigned int>(p->output_height), |
| 82 | static_cast<unsigned int>(p->output_width) }, |
| 83 | p->output_channels, |
| 84 | { static_cast<unsigned int>(p->kernel_height), |
| 85 | static_cast<unsigned int>(p->kernel_width) }, |
| 86 | p->act |
| 87 | ) |
| 88 | { |
| 89 | const CPUInfo *ci = get_CPUInfo(); |
| 90 | |
| 91 | if (p->groups != 1) { |
| 92 | printf("Winograd: Groups not supported, aborting.\n"); |
| 93 | // TODO |
| 94 | exit(1); |
| 95 | } |
| 96 | |
| 97 | // Get a winograd layer appropriate for the kernel size. |
| 98 | if (p->out_stride_h != 1 || p->out_stride_w != 1) { |
| 99 | printf("Winograd: strided convolution not supported\n"); |
| 100 | exit(1); |
| 101 | } |
| 102 | if (p->in_stride_h != 1 || p->in_stride_w != 1) { |
| 103 | printf("Winograd: dilated convolution not supported\n"); |
| 104 | exit(1); |
| 105 | } |
| 106 | |
| 107 | // Get configuration arguments for Winograd |
| 108 | kai::ops::winograd::WinogradConfig winograd_cfg; |
| 109 | winograd_cfg.output_rows = p->winograd_args.output_tile_rows; |
| 110 | winograd_cfg.output_cols = p->winograd_args.output_tile_cols; |
| 111 | winograd_cfg.input_transform_filter = p->winograd_args.input_transform_filter; |
| 112 | winograd_cfg.weight_transform_filter = p->winograd_args.weight_transform_filter; |
| 113 | winograd_cfg.output_transform_filter = p->winograd_args.output_transform_filter; |
| 114 | |
| 115 | // Get arguments for the GEMM |
| 116 | kai::ops::GemmConfig cfg; |
| 117 | cfg.filter = p->kernel_filter; |
| 118 | cfg.inner_block_size = p->inner_block_size; |
| 119 | cfg.outer_block_size = p->outer_block_size; |
| 120 | |
| 121 | const bool success = kai::ops::winograd::get_implementation<Top>( |
| 122 | winograd_impl, ci, conv_args, nthreads, p->fast_mode, &winograd_cfg, &cfg |
| 123 | ); |
| 124 | |
| 125 | if (!success) |
| 126 | { |
| 127 | printf("Winograd: Unsupported kernel size: %" PRId64 "x%" PRId64 ".\n",p->kernel_height, p->kernel_width); |
| 128 | exit(1); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | printf("Using input transform: %s\n", winograd_impl.input_transform->get_name().c_str()); |
| 133 | printf("Using weight transform: %s\n", winograd_impl.weight_transform->get_name().c_str()); |
| 134 | printf("Using output transform: %s\n", winograd_impl.output_transform->get_name().c_str()); |
| 135 | } |
| 136 | |
| 137 | // Construct the GEMM |
| 138 | gemm = kai::ops::gemm<Top, Top, Top>(*winograd_impl.gemm_args); |
| 139 | |
| 140 | #ifndef SILENT |
| 141 | auto my_cfg = gemm->get_config(); |
| 142 | |
| 143 | printf("GEMM configuration: kernel=%s inner_block=%u outer_block=%u\n", my_cfg.filter.c_str(), my_cfg.inner_block_size, my_cfg.outer_block_size); |
| 144 | #endif |
| 145 | // Determine how much working space is required, allocate it. |
| 146 | size_t working_space_size = std::max({ |
| 147 | winograd_impl.input_transform->get_working_space_size(conv_args, nthreads), |
| 148 | winograd_impl.output_transform->get_working_space_size(conv_args, nthreads), |
| 149 | gemm->get_working_size() |
| 150 | }); |
| 151 | working_data = std::unique_ptr<void, free_delete>(allocate_aligned_memory(64, working_space_size)); |
| 152 | gemm->set_working_space(working_data.get()); |
| 153 | |
| 154 | // Allocate memory for the Winograd-domain representation of the problem |
| 155 | const auto &wds = winograd_impl.winograd_spec; |
| 156 | winograd_input = std::unique_ptr<void, free_delete>(allocate_aligned_memory(64, wds.input_matrix_size_bytes)); |
| 157 | winograd_weights = std::unique_ptr<void, free_delete>(allocate_aligned_memory(64, wds.weight_matrix_size_bytes)); |
| 158 | winograd_output = std::unique_ptr<void, free_delete>(allocate_aligned_memory(64, wds.output_matrix_size_bytes)); |
| 159 | |
| 160 | // Prepare the GEMM |
| 161 | gemm->set_arrays_generic( |
| 162 | winograd_input.get(), wds.input_ld_row, wds.input_ld_batch, wds.input_ld_matrix, |
| 163 | winograd_weights.get(), wds.weight_ld_row, wds.weight_ld_matrix, |
| 164 | winograd_output.get(), wds.output_ld_row, wds.output_ld_batch, wds.output_ld_matrix, |
| 165 | nullptr, 0 // No bias in the GEMM |
| 166 | ); |
| 167 | |
| 168 | // Prepare weights |
| 169 | winograd_impl.weight_transform->execute( |
| 170 | conv_args, |
| 171 | B->data, p->kernel_width * p->input_channels * B->stride, p->input_channels * B->stride, B->stride, |
| 172 | winograd_weights.get(), winograd_impl.winograd_spec, |
| 173 | 0, 1 // Thread 1 of 1 |
| 174 | ); |
| 175 | if (gemm->B_is_pretransposed()) |
| 176 | { |
| 177 | // Pretranspose the weights if required to by the GEMM |
| 178 | gemm_pretransposed_data = std::unique_ptr<void, free_delete>(allocate_aligned_memory(64, gemm->get_B_pretransposed_array_size())); |
| 179 | gemm->pretranspose_B_array_generic( |
| 180 | gemm_pretransposed_data.get(), |
| 181 | winograd_weights.get(), |
| 182 | winograd_impl.winograd_spec.weight_ld_row, |
| 183 | winograd_impl.winograd_spec.weight_ld_matrix, |
| 184 | false |
| 185 | ); |
| 186 | gemm->set_pretransposed_B_data(gemm_pretransposed_data.get()); |
| 187 | } |
| 188 | |
| 189 | sched = std::unique_ptr<scheduler> (new static_scheduler(gemm.get(), nthreads, p->schedule_shape_override)); |
| 190 | } |
| 191 | |
| 192 | __attribute__ ((noinline)) void Run(unsigned int threadid) { |
| 193 | { |
| 194 | // Input transform |
| 195 | winograd_impl.input_transform->execute( |
| 196 | conv_args, |
| 197 | A->data, A->batch_stride, p->input_width * A->stride, A->stride, |
| 198 | winograd_input.get(), winograd_impl.winograd_spec, |
| 199 | working_data.get(), threadid, nthreads |
| 200 | ); |
| 201 | } |
| 202 | barr->arrive_and_wait(); |
| 203 | |
| 204 | // GEMM |
| 205 | { |
| 206 | sched->execute(threadid); |
| 207 | barr->arrive_and_wait(); |
| 208 | } |
| 209 | |
| 210 | // Output transform |
| 211 | { |
| 212 | winograd_impl.output_transform->execute( |
| 213 | conv_args, |
| 214 | winograd_output.get(), winograd_impl.winograd_spec, |
| 215 | (bias == nullptr) ? nullptr : bias->data, |
| 216 | C->data, C->batch_stride, p->output_width * C->stride, C->stride, |
| 217 | working_data.get(), threadid, nthreads |
| 218 | ); |
| 219 | barr->arrive_and_wait(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /* Dump valid kernel name list. */ |
| 224 | static void print_kernels (GemmProblem *p, unsigned int nthreads) { |
| 225 | printf("Kernel list not supported yet.\n"); |
| 226 | } |
| 227 | }; |