blob: cc21ccbaa1445502f51589ae2511ac7966cfaf9e [file] [log] [blame]
Anthony Barbier71d9b572018-07-06 17:05:59 +01001/*
Gunes Bayiref637392024-02-12 21:32:51 +00002 * Copyright (c) 2018-2024 Arm Limited.
Anthony Barbier71d9b572018-07-06 17:05:59 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/operators/internal/CpuGemmAssemblyDispatch.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010025
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010026#include "arm_compute/runtime/NEON/NEScheduler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010028#include "src/core/CPP/Validate.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010029#include "src/core/helpers/MemoryHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/core/NEON/kernels/arm_gemm/utils.hpp"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000031#include "src/core/utils/AssemblyUtils.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/cpu/kernels/assembly/arm_gemm.hpp"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "src/cpu/kernels/assembly/CpuGemmAssemblyWrapperKernel.h"
SiCong Lic5ab4df2023-10-17 17:38:57 +010034#include "src/cpu/operators/CpuTranspose.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/cpu/utils/CpuAuxTensorHandler.h"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010036
Anthony Barbiereaefd002018-07-20 17:49:35 +010037#include <arm_neon.h>
38
Anthony Barbierc8e84b52018-07-17 16:48:42 +010039namespace arm_compute
40{
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010041namespace cpu
42{
SiCong Lidba672c2023-04-06 16:30:18 +010043namespace
44{
45/** Run pretranspose_B_array in parallel (1D static scheduling)
46 *
47 * @tparam TypeInput
48 * @tparam TypeOutput
49 *
50 * @param[in] gemm_asm GemmCommon kernel to run
51 * @param[in] dst Pretransposed B array
52 * @param[in] src B array to be pretransposed
53 * @param[in] src_ld Stride in y
54 * @param[in] src_multi_stride Stride in z ("multi")
55 * @param[in] num_threads Number of threads to run this method. Must be >= 1
56 */
57template <typename TypeInput, typename TypeOutput>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058void run_parallel_pretranspose_B_array(arm_gemm::GemmCommon<TypeInput, TypeOutput> *gemm_asm,
59 ITensor *dst,
60 const TypeInput *src,
61 int src_ld,
62 int src_multi_stride,
Gunes Bayiref637392024-02-12 21:32:51 +000063 unsigned int num_threads,
64 bool transpose)
SiCong Lidba672c2023-04-06 16:30:18 +010065{
66 ARM_COMPUTE_ERROR_ON(gemm_asm == nullptr);
67 ARM_COMPUTE_ERROR_ON(num_threads == 0);
68 // The window size is also the total workload size
69 const unsigned int wsize = gemm_asm->get_B_pretranspose_window_size();
70
71 std::vector<IScheduler::Workload> workloads(num_threads);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 for (unsigned int t = 0; t < num_threads; ++t)
SiCong Lidba672c2023-04-06 16:30:18 +010073 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 workloads[t] = [=](const ThreadInfo &info)
SiCong Lidba672c2023-04-06 16:30:18 +010075 {
76 const unsigned int start = (info.thread_id * wsize) / num_threads;
77 const unsigned int end = ((info.thread_id + 1) * wsize) / num_threads;
78
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 if (start < end)
SiCong Lidba672c2023-04-06 16:30:18 +010080 {
Gunes Bayiref637392024-02-12 21:32:51 +000081 gemm_asm->pretranspose_B_array_part(dst->buffer(), src, src_ld, src_multi_stride, transpose, start,
82 end);
SiCong Lidba672c2023-04-06 16:30:18 +010083 }
84 };
85 }
86 NEScheduler::get().run_tagged_workloads(workloads, "CpuGemmAssemblyDispatch/pretranspose_B_array");
87}
88} // namespace
89
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010090using namespace arm_compute::experimental;
91
Anthony Barbiereaefd002018-07-20 17:49:35 +010092namespace
Anthony Barbier71d9b572018-07-06 17:05:59 +010093{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000094struct free_delete
95{
96 void operator()(void *x)
97 {
98 free(x);
99 }
100};
101
102struct Params
103{
104 unsigned int M;
105 unsigned int N;
106 unsigned int K;
107 unsigned int batches;
108 unsigned int multis;
109 unsigned int sections;
110 bool indirect;
111};
112
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100113Params extract_parameters(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *d, const AsmGemmInfo &info)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000114{
115 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000116 Params p;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100117 p.M = d->tensor_shape().y();
118 p.K = a->tensor_shape().x();
119 p.N = d->tensor_shape().x();
Georgios Pinitas4c634e02020-12-01 02:17:19 +0000120 p.batches = 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000121 p.multis = 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000122 p.sections = 1;
Georgios Pinitas4c634e02020-12-01 02:17:19 +0000123 p.indirect = false;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 if (info.method == AsmConvMethod::Conv || info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000126 {
127 p.indirect = true;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100128 p.sections = b->tensor_shape()[2] * b->tensor_shape()[3];
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000129 }
130 else
131 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100132 p.multis = b->tensor_shape().z();
133 p.batches = d->tensor_shape().total_size_upper(2) / p.multis;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000134 }
135
136 // Update M in case of GEMM3D for output
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 if (info.depth_output_gemm3d != 0)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000138 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100139 p.M = d->tensor_shape().y() * d->tensor_shape().z();
140 p.batches = d->tensor_shape().total_size_upper(3) / p.multis;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000141 }
142
143 return p;
144}
145
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000146IScheduler::Hints scheduling_hint_heuristic(arm_gemm::GemmMethod method, DataType data_type)
147{
148 // Schedule assembly kernel
149 const int granule_threshold = 200;
150 IScheduler::Hints scheduling_hint = IScheduler::Hints(Window::DimX);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 if (method == arm_gemm::GemmMethod::GEMM_INTERLEAVED && data_type == DataType::F32)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000152 {
153 scheduling_hint = IScheduler::Hints(Window::DimX, IScheduler::StrategyHint::DYNAMIC, granule_threshold);
154 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 else if (method == arm_gemm::GemmMethod::GEMM_INTERLEAVED_2D &&
156 (data_type == DataType::F32 || data_type == DataType::F16 || data_type == DataType::U8 ||
157 data_type == DataType::S8))
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000158 {
159 //GEMM_INTERLEAVED supports 2D parallelism, IScheduler::split_dimensions_all signals to parallelise over all window dimensions
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 scheduling_hint =
161 IScheduler::Hints(IScheduler::split_dimensions_all, IScheduler::StrategyHint::STATIC, granule_threshold);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000162 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100163 else if (method == arm_gemm::GemmMethod::QUANTIZE_WRAPPER_2D &&
164 (data_type == DataType::QASYMM8 || data_type == DataType::QASYMM8_SIGNED))
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000165 {
166 //special case for QASYMM8 to support 2D parallelism, scheduler here may be tweaked differently compared to FP32 case
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 scheduling_hint =
168 IScheduler::Hints(IScheduler::split_dimensions_all, IScheduler::StrategyHint::STATIC, granule_threshold);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000169 }
170
171 return scheduling_hint;
172}
173
Anthony Barbiereaefd002018-07-20 17:49:35 +0100174/** Fallback in case ACL doesn't have a function */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100175template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing>
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100176class Fallback : public CpuGemmAssemblyDispatch::IFallback
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100177{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100178public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100179 /** Destructor */
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100180 ~Fallback() = default;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100181
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000182 /** Initialise the functions's input and output.
183 *
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100184 * @param[in] a Input tensor containing the Matrix A.
185 * @param[in] b Input tensor containing the Matrix B.
186 * @param[in] c Input tensor containing the Matrix C.
187 * @param[out] d Output tensor to store the result of matrix multiplication.
188 * @param[in] args Matrix multiplication information.
189 * @param[in] gemm_info GEMM meta-data
190 * @param[in] os Output stage meta-data.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000191 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 void configure(const ITensorInfo *a,
193 const ITensorInfo *b,
194 const ITensorInfo *c,
195 ITensorInfo *d,
196 arm_gemm::GemmArgs args,
197 const AsmGemmInfo &gemm_info,
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100198 const OutputStage &os = {});
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000199
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000200 /** Set requantization shifts to be used
201 *
202 * @param[in] shifts Requantization shifts
203 *
204 * @return Pointer to the shift data
205 */
206 /** Set requantization data to be used
207 *
208 *
209 * @param shifts Requantization shifts
210 * @param multipliers Requantization multipliers
211 *
212 * @return A tuple with the pointers to the shift and multiplier data respectively
213 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 std::tuple<bool, const int32_t *, const int32_t *, const int32_t *>
215 set_requantize_data(const std::vector<int32_t> &shifts, const std::vector<int32_t> &multipliers);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000216
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000217 // Inherited methods overridden:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100218 void run(ITensorPack &tensors) override;
219 void prepare(ITensorPack &tensors) override;
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100220 bool is_configured() const override;
221 experimental::MemoryRequirements workspace() const override;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000222 bool isVarWeightsKernel() const override
223 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100224 if (!_gemm_kernel_asm)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000225 return false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 const arm_compute::WeightFormat wf =
227 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format);
Ramy Elgammal91780022022-07-20 14:57:37 +0100228 return wf != arm_compute::WeightFormat::UNSPECIFIED && wf != arm_compute::WeightFormat::ANY;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000229 }
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100230
Anthony Barbiereaefd002018-07-20 17:49:35 +0100231private:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100232 enum AuxTensorIdx
233 {
234 AsmGemmWorkspace = 0,
SiCong Lic5ab4df2023-10-17 17:38:57 +0100235 PrePretransposedB, /* Transposed B (rhs) before being passed to gemm or pretranspose_B_array */
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100236 Pretranspose,
237 Count
238 };
239
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000240 /** Configure the indirect buffer
241 *
242 * @param[in] a Input tensor containing the Matrix A.
243 * @param[in] b Input tensor containing the Matrix B.
244 * @param[out] d Output tensor to store the result of matrix multiplication.
245 * @param[in] info GEMM meta-data
246 */
247 void configure_indirect(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *d, const AsmGemmInfo &info);
248 /** Prepare the indirect buffer */
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100249 void prepare_indirect_buffer(ITensorPack &tensors);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100250
SiCong Lic5ab4df2023-10-17 17:38:57 +0100251 /** Operator to transpose B before gemm or pretranspose_B_array*/
252 std::unique_ptr<CpuTranspose> _pre_pretranspose_b{nullptr};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100253 /** Assembly Gemm kernel */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100254 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{nullptr};
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000255 /** Optimised Arm® Neon™ kernel */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100256 std::unique_ptr<INEKernel> _optimised_kernel{nullptr};
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100257 /** Assembly GEMM workspace tensor info */
258 TensorInfo _workspace_info{};
SiCong Lic5ab4df2023-10-17 17:38:57 +0100259 /** Pre-pre-transposed B tensor info */
260 TensorInfo _pre_pretransposed_b_info{};
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100261 /** Pre-transpose tensor info */
262 TensorInfo _pretranspose_info{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100263 /** Prepared flag */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100264 bool _is_prepared{false};
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100265 /** GEMM meta-data */
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000266 AsmGemmInfo _gemm_info{};
Georgios Pinitas77d42522019-11-05 13:35:47 +0000267 /** GEMM kernel description */
268 arm_gemm::KernelDescription _kernel_info{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000269 /** Per channel quantization shifts */
270 std::vector<int32_t> _shifts{};
morgolock0bc80da2020-08-10 16:44:18 +0100271 std::vector<int32_t> right_shifts{};
272 std::vector<int32_t> left_shifts{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000273 /** Per channel quantization multipliers */
274 std::vector<int32_t> _multipliers{};
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000275 /** Indirect buffer */
276 std::unique_ptr<const TypeInput *const *, free_delete> _indirect_arg{};
277 std::unique_ptr<const TypeInput *, free_delete> _indirect_buf{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100278 std::vector<TypeInput> _indirect_pad{};
279 arm_gemm::ConvolutionParameters _cp{};
280 experimental::MemoryRequirements _aux_mem{Count};
281 bool _B_pretranspose_required{false};
282 bool _is_b_constant{true};
283 bool _is_c_constant{true};
Gunes Bayiref637392024-02-12 21:32:51 +0000284 bool _run_pre_pretranspose_b{false};
285 bool _B_pre_pretranspose_required{false};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100286};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100287
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100288template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000289std::tuple<bool, const int32_t *, const int32_t *, const int32_t *>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100290Fallback<TypeInput, TypeOutput, OutputStage>::set_requantize_data(const std::vector<int32_t> &shifts,
291 const std::vector<int32_t> &multipliers)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000292{
morgolock0bc80da2020-08-10 16:44:18 +0100293 _multipliers = multipliers;
294 _shifts = shifts;
295 bool need_left = false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100296 for (const auto s : _shifts)
morgolock0bc80da2020-08-10 16:44:18 +0100297 {
298 left_shifts.push_back(std::max(-s, int32_t(0)));
299 right_shifts.push_back(std::min(-s, int32_t(0)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100300 if (s < 0 && !need_left)
morgolock0bc80da2020-08-10 16:44:18 +0100301 {
302 need_left = true;
303 }
304 }
305 return std::make_tuple(need_left, left_shifts.data(), right_shifts.data(), _multipliers.data());
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000306}
307
308template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100309void Fallback<TypeInput, TypeOutput, OutputStage>::prepare_indirect_buffer(ITensorPack &tensors)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000310{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100311 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
312 const TypeInput *A_ptr = reinterpret_cast<TypeInput *>(a->buffer());
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000313 const int multis = 1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100314 const int batches = a->info()->tensor_shape().total_size_upper(3);
315 const size_t stride_A = a->info()->strides_in_bytes().y() / sizeof(TypeInput);
316 const size_t batch_stride_A = a->info()->strides_in_bytes()[3] / sizeof(TypeInput);
317 const size_t multi_stride_A = a->info()->strides_in_bytes()[4] / sizeof(TypeInput);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000318
319 const size_t output_hw = _cp.output_height * _cp.output_width;
320 const int batch_size = _cp.kernel_height * _cp.kernel_width * output_hw * sizeof(TypeInput);
321 const size_t batch_stride = batch_size / sizeof(TypeInput);
322 const int multi_size = batch_size * batches;
323 const size_t multi_stride = multi_size / sizeof(TypeInput);
324
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100325 for (int64_t m = 0; m < multis; m++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000326 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100327 for (int64_t b = 0; b < batches; b++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000328 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100329 for (int64_t output_y = 0; output_y < _cp.output_height; output_y++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000330 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100331 for (int64_t output_x = 0; output_x < _cp.output_width; output_x++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000332 {
333 int64_t output_xy = (output_y * _cp.output_width) + output_x;
334
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100335 for (int64_t kernel_y = 0; kernel_y < _cp.kernel_height; kernel_y++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000336 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100337 for (int64_t kernel_x = 0; kernel_x < _cp.kernel_width; kernel_x++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000338 {
339 int64_t input_x = (output_x * _cp.output_stride_w) + kernel_x - _cp.padding_left;
340 int64_t input_y = (output_y * _cp.output_stride_h) + kernel_y - _cp.padding_top;
341 int64_t kernel_xy = (kernel_y * _cp.kernel_width) + kernel_x;
342 int64_t input_xy = (input_y * _cp.input_width) + input_x;
343
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100344 if (input_x < 0 || input_x >= _cp.input_width || input_y < 0 || input_y >= _cp.input_height)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000345 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100346 _indirect_buf
347 .get()[m * multi_stride + b * batch_stride + kernel_xy * output_hw + output_xy] =
348 _indirect_pad.data();
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000349 }
350 else
351 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100352 _indirect_buf
353 .get()[m * multi_stride + b * batch_stride + kernel_xy * output_hw + output_xy] =
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000354 A_ptr + (m * multi_stride_A + b * batch_stride_A + input_xy * stride_A);
355 }
356 }
357 }
358 }
359 }
360 }
361 }
362}
363
364template <typename TypeInput, typename TypeOutput, class OutputStage>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100365void Fallback<TypeInput, TypeOutput, OutputStage>::configure_indirect(const ITensorInfo *a,
366 const ITensorInfo *b,
367 const ITensorInfo *d,
368 const AsmGemmInfo &info)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000369{
370 ARM_COMPUTE_ERROR_ON(!(info.method == AsmConvMethod::Conv || info.method == AsmConvMethod::Indirect));
371
372 float zeropad = 0.f;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100373 if (is_data_type_quantized(a->data_type()))
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000374 {
375 zeropad = a->quantization_info().uniform().offset;
376 }
377
378 const int64_t input_width = static_cast<int64_t>(a->tensor_shape()[1]);
379 const int64_t input_height = static_cast<int64_t>(a->tensor_shape()[2]);
380 const int64_t input_channels = static_cast<int64_t>(a->tensor_shape()[0]);
381 const int64_t kernel_width = static_cast<int64_t>(b->tensor_shape()[2]);
382 const int64_t kernel_height = static_cast<int64_t>(b->tensor_shape()[3]);
383 const int64_t output_width = static_cast<int64_t>(d->tensor_shape()[1]);
384 const int64_t output_height = static_cast<int64_t>(d->tensor_shape()[2]);
385
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100386 _cp = {input_width,
387 input_height,
388 input_channels,
389 kernel_width,
390 kernel_height,
391 output_width,
392 output_height,
393 info.ps_info.stride().first,
394 info.ps_info.stride().second,
395 info.padding_top,
396 info.padding_left,
397 zeropad};
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000398
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100399 if (info.method == AsmConvMethod::Conv)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000400 {
401 _gemm_kernel_asm->set_convolution_parameters(_cp);
402 }
403
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100404 if (info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000405 {
406 const unsigned int multis = 1;
407 const unsigned int batches = a->tensor_shape().total_size_upper(3);
408 const unsigned int kernel_hw = _cp.kernel_width * _cp.kernel_height;
409 const unsigned int output_hw = _cp.output_width * _cp.output_height;
410
411 using TypeInputPtr = TypeInput *;
412 const int batch_size = kernel_hw * output_hw * sizeof(TypeInputPtr);
413 const size_t batch_stride = batch_size / sizeof(TypeInputPtr);
414 const int multi_size = batch_size * batches;
415 const size_t multi_stride = multi_size / sizeof(TypeInputPtr);
416
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100417 _indirect_buf = std::unique_ptr<const TypeInput *, free_delete>(
418 reinterpret_cast<const TypeInput **>(malloc(multi_size * multis)));
419 _indirect_arg = std::unique_ptr<const TypeInput *const *, free_delete>(
420 reinterpret_cast<const TypeInput *const **>(malloc(sizeof(TypeInput **) * kernel_hw * multis * batches)));
Sang-Hoon Park8d5337e2021-01-15 14:36:25 +0000421 _indirect_pad = std::vector<TypeInput>(_cp.input_channels, TypeInput(zeropad));
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000422
423 // Set indirect argument
424 int64_t pos = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100425 for (int64_t m = 0; m < multis; m++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000426 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100427 for (int64_t b = 0; b < batches; b++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000428 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100429 for (int64_t kernel_xy = 0; kernel_xy < kernel_hw; kernel_xy++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000430 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100431 (_indirect_arg.get())[pos++] =
432 _indirect_buf.get() + m * multi_stride + b * batch_stride + kernel_xy * output_hw;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000433 }
434 }
435 }
436
437 _gemm_kernel_asm->set_indirect_parameters(a->tensor_shape()[0], _indirect_arg.get());
438 }
439}
440
441template <typename TypeInput, typename TypeOutput, class OutputStage>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100442void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensorInfo *a,
443 const ITensorInfo *b,
444 const ITensorInfo *c,
445 ITensorInfo *d,
446 arm_gemm::GemmArgs args,
447 const AsmGemmInfo &gemm_info,
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100448 const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100449{
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100450 _is_b_constant = b->are_values_constant();
451 _is_c_constant = c ? c->are_values_constant() : true;
452
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100453 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100454 if (_gemm_kernel_asm == nullptr)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100455 {
456 //configuration not supported: Leave function unconfigured:
457 return;
458 }
459
Francesco.Petrogalli@arm.com193cad32022-03-07 13:39:21 +0000460 arm_gemm::GemmConfig gemm_cfg = _gemm_kernel_asm->get_config();
461
Anthony Barbier71d9b572018-07-06 17:05:59 +0100462 // arm_compute wrapper for the Gemm object (see above)
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100463 auto acl_gemm_wrapper = std::make_unique<kernel::CpuGemmAssemblyWrapperKernel<TypeInput, TypeOutput>>();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100464 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000465 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100466 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
467 const unsigned int alignment = 4096;
468 _workspace_info = TensorInfo(TensorShape(workspace_size), 1, DataType::U8);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100469 _aux_mem[AsmGemmWorkspace] =
470 MemoryInfo(offset_int_vec(AsmGemmWorkspace), MemoryLifetime::Temporary, workspace_size, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100471
472 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
473 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
474 {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100475 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100476 if (window_size < static_cast<unsigned int>(args._maxthreads))
Anthony Barbier71d9b572018-07-06 17:05:59 +0100477 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100478 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100479 }
480 }
481
482 _optimised_kernel = std::move(acl_gemm_wrapper);
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100483 _gemm_info = gemm_info;
Gunes Bayiref637392024-02-12 21:32:51 +0000484
SiCong Lic5ab4df2023-10-17 17:38:57 +0100485 // Check if we need to pre-pretranspose B. Fixed format kernels need no pre-pretranspose.
Gunes Bayiref637392024-02-12 21:32:51 +0000486 _B_pre_pretranspose_required = _gemm_info.transpose_b && !isVarWeightsKernel();
487 _B_pretranspose_required = _gemm_kernel_asm->B_pretranspose_required();
488
489 const bool kernel_supports_transpose = _gemm_kernel_asm->B_pretranspose_supports_transpose();
490 const bool kernel_can_fuse_transpose = _B_pretranspose_required && kernel_supports_transpose;
491 _run_pre_pretranspose_b = _B_pre_pretranspose_required && !kernel_can_fuse_transpose;
492
493 if (_run_pre_pretranspose_b)
SiCong Lic5ab4df2023-10-17 17:38:57 +0100494 {
495 _pre_pretranspose_b = std::make_unique<CpuTranspose>();
496 _pre_pretranspose_b->configure(b, &_pre_pretransposed_b_info);
497 MemoryLifetime lifetime;
498 if (_is_b_constant)
499 {
Gunes Bayiref637392024-02-12 21:32:51 +0000500 if (_B_pretranspose_required)
SiCong Lic5ab4df2023-10-17 17:38:57 +0100501 {
502 // PrePretransposedB tensor is only used in prepare(), but is then succeeded by Pretranspose
503 // So PrePretransposedB can be freed inside prepare()
504 lifetime = MemoryLifetime::Prepare;
505 }
506 else
507 {
508 // PrePretransposedB tensor is only used in prepare(), but is the final transformation of B
509 // So PrePretransposedB needs to persist beyond prepare()
510 lifetime = MemoryLifetime::Persistent;
511 }
512 }
513 else
514 {
515 // PrePretransposedB tensor is always used in run() and doesn't need to persist
516 lifetime = MemoryLifetime::Temporary;
517 }
518 // Forcing 128-byte alignment (required by 32-bit kernels)
519 const unsigned int alignment = 128;
520 _aux_mem[PrePretransposedB] =
521 MemoryInfo(offset_int_vec(PrePretransposedB), lifetime, _pre_pretransposed_b_info.total_size(), alignment);
522 }
523
Anthony Barbier71d9b572018-07-06 17:05:59 +0100524 // Check for pre-transposed support
Gunes Bayiref637392024-02-12 21:32:51 +0000525 if (_B_pretranspose_required)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100526 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100527 // Fixed format kernels need no pretranspose.
528 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
529 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
Anthony Barbier71d9b572018-07-06 17:05:59 +0100530 // Forcing 128-byte alignment (required by 32-bit kernels)
531 const unsigned int alignment = 128;
532 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100533 _pretranspose_info = TensorInfo(TensorShape(B_pretranspose_size), 1, DataType::U8);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100534 _aux_mem[Pretranspose] =
535 MemoryInfo(offset_int_vec(Pretranspose), MemoryLifetime::Persistent, B_pretranspose_size, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100536 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000537
538 // Handle indirect GEMM convolution
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100539 if (gemm_info.method == AsmConvMethod::Conv || gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000540 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100541 configure_indirect(a, b, d, gemm_info);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000542 }
Jonathan Deakina668f9f2024-01-24 09:15:38 +0000543
544 if (std::is_same<OutputStage, arm_gemm::DequantizeFloat>::value)
545 {
546 // Output dequantization is just the two src scales multiplied together
547 _gemm_kernel_asm->set_dequantize_scale(a->quantization_info().uniform().scale *
548 b->quantization_info().uniform().scale);
549 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100550}
551
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100552template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100553void Fallback<TypeInput, TypeOutput, OutputStage>::prepare(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100554{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100555 if (!_is_prepared)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100556 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100557 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
558 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
SiCong Lid4650e92023-11-14 15:17:10 +0000559 ARM_COMPUTE_ERROR_ON_NULLPTR(b);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100560
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100561 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100562 if (c && c->info()->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100563 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100564 _gemm_kernel_asm->set_quantized_bias(
565 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100566 }
SiCong Lic5ab4df2023-10-17 17:38:57 +0100567 const ITensor *b_to_use = b;
Gunes Bayiref637392024-02-12 21:32:51 +0000568
SiCong Lic5ab4df2023-10-17 17:38:57 +0100569 // Pre-pretranspose B if required
SiCong Lic5ab4df2023-10-17 17:38:57 +0100570 CpuAuxTensorHandler pre_pretransposed_b(
571 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
572 /*pack_inject: no need to inject into tensors*/
573 false,
574 /*bypass_alloc: no need to allocate if pre-pretranspose B is not required as this handle will not be used*/
Gunes Bayiref637392024-02-12 21:32:51 +0000575 !_run_pre_pretranspose_b);
576
577 if (_run_pre_pretranspose_b)
SiCong Lic5ab4df2023-10-17 17:38:57 +0100578 {
579 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
580 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
581 _pre_pretranspose_b->run(pre_pretranspose_pack);
582 b_to_use = pre_pretransposed_b.get();
583 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100584
Anthony Barbier71d9b572018-07-06 17:05:59 +0100585 // Pretranspose B if required
Gunes Bayiref637392024-02-12 21:32:51 +0000586 if (_B_pretranspose_required)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100587 {
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000588 // Fixed format kernels need no pretranspose.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100589 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
590 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
SiCong Lic5ab4df2023-10-17 17:38:57 +0100591 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
592 const auto in1_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
593 b_to_use->info()->offset_first_element_in_bytes());
594 const int multi_stride_b = b_to_use->info()->strides_in_bytes().z() / b_to_use->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100595
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100596 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, false);
Gunes Bayiref637392024-02-12 21:32:51 +0000597
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100598 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
Gunes Bayiref637392024-02-12 21:32:51 +0000599
600 const bool kernel_supports_transpose = _gemm_kernel_asm->B_pretranspose_supports_transpose();
601 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(
602 _gemm_kernel_asm.get(), pretranspose.get(), in1_ptr, ldb, multi_stride_b,
603 NEScheduler::get().num_threads(), _B_pre_pretranspose_required && kernel_supports_transpose);
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100604
605 b->mark_as_unused();
Gunes Bayiref637392024-02-12 21:32:51 +0000606 // Note that we don't need to mark b_to_use as unused, as if it's been assigned to pre_pretransposed_b,
607 // its memory will be auto-managed by the handler
Anthony Barbier71d9b572018-07-06 17:05:59 +0100608 }
609
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100610 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000611 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100612 prepare_indirect_buffer(tensors);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000613 }
614
Anthony Barbier71d9b572018-07-06 17:05:59 +0100615 _is_prepared = true;
616 }
617}
618
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100619template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100620bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100621{
622 return _optimised_kernel != nullptr;
623}
624
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100625template <typename TypeInput, typename TypeOutput, class OutputStage>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100626experimental::MemoryRequirements Fallback<TypeInput, TypeOutput, OutputStage>::workspace() const
627{
628 return _aux_mem;
629}
630
631template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100632void Fallback<TypeInput, TypeOutput, OutputStage>::run(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100633{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100634 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
635 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
636 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
637 auto d = tensors.get_tensor(TensorType::ACL_DST);
SiCong Lid4650e92023-11-14 15:17:10 +0000638 ARM_COMPUTE_ERROR_ON_NULLPTR(a, d);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100639
Jonathan Deakina668f9f2024-01-24 09:15:38 +0000640 // Only update at runtime if the src quantization is dynamic
641 if (std::is_same<OutputStage, arm_gemm::DequantizeFloat>::value &&
642 (a->info()->quantization_info().is_dynamic() || b->info()->quantization_info().is_dynamic()))
643 {
644 // Output dequantization is just the two src scales multiplied together
645 _gemm_kernel_asm->set_dequantize_scale(a->info()->quantization_info().uniform().scale *
646 b->info()->quantization_info().uniform().scale);
647 }
648
Jonathan Deakin464ed202023-01-12 11:41:14 +0000649 int lda = a->info()->strides_in_bytes().y() / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100650 int ldb = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000651 const int ldd = d->info()->strides_in_bytes().y() / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100652
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000653 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100654 const size_t a_multi_idx = a_batch_idx + 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000655 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100656 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100657
Jonathan Deakin464ed202023-01-12 11:41:14 +0000658 int batch_stride_a = a->info()->strides_in_bytes()[a_batch_idx] / a->info()->element_size();
659 const int batch_stride_d = d->info()->strides_in_bytes()[d_batch_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100660
Jonathan Deakin464ed202023-01-12 11:41:14 +0000661 int multi_stride_a = a->info()->strides_in_bytes()[a_multi_idx] / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100662 int multi_stride_b = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000663 const int multi_stride_d = d->info()->strides_in_bytes()[d_multi_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100664
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100665 auto in0_ptr = reinterpret_cast<const TypeInput *>(a->buffer() + a->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100666 const TypeInput *in1_ptr = nullptr;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100667 auto out_ptr = reinterpret_cast<TypeOutput *>(d->buffer() + d->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100668
SiCong Lic5ab4df2023-10-17 17:38:57 +0100669 const ITensor *b_to_use = b;
670
671 // Pre-pretranspose B if required
SiCong Lic5ab4df2023-10-17 17:38:57 +0100672 CpuAuxTensorHandler pre_pretransposed_b(
673 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
674 false /*pack_inject: no need to inject into tensors*/,
Gunes Bayiref637392024-02-12 21:32:51 +0000675 !_run_pre_pretranspose_b /*bypass_alloc: no need to allocate if pre-pretranspose B is not required as this handle will not be used*/);
676 if (b_to_use && !_is_b_constant && _run_pre_pretranspose_b)
SiCong Lic5ab4df2023-10-17 17:38:57 +0100677 {
678 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
679 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
680 _pre_pretranspose_b->run(pre_pretranspose_pack);
681 b_to_use = pre_pretransposed_b.get();
682 }
683
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100684 // Check if B is pre-tranposed and de-reference if not
SiCong Lid4650e92023-11-14 15:17:10 +0000685 if (b_to_use && !_gemm_kernel_asm->B_is_pretransposed())
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100686 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100687 ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
688 multi_stride_b = b_to_use->info()->strides_in_bytes().z() / b_to_use->info()->element_size();
689 in1_ptr =
690 reinterpret_cast<const TypeInput *>(b_to_use->buffer() + b_to_use->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100691 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100692
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100693 // If necessary, run pretranspose every time if either weights or biases are non-constant
SiCong Lic5ab4df2023-10-17 17:38:57 +0100694 if ((b_to_use && !_is_b_constant) || (c && !_is_c_constant && c->info()->data_type() == DataType::S32))
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100695 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100696 if (c && c->info()->data_type() == DataType::S32)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100697 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100698 _gemm_kernel_asm->set_quantized_bias(
699 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100700 }
701
702 // Pretranspose B if required
SiCong Lid4650e92023-11-14 15:17:10 +0000703 if (b_to_use && _B_pretranspose_required)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100704 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100705 // Fixed format kernels need no pretranspose.
706 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
707 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
708 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
709 const auto b_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
710 b_to_use->info()->offset_first_element_in_bytes());
711 const int multi_stride_b = b_to_use->info()->strides_in_bytes().z() / b_to_use->info()->element_size();
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100712
713 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, true);
714 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
715
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100716 if (_is_b_constant)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100717 {
718 _gemm_kernel_asm->requantize_bias(pretranspose.get()->buffer(), b_ptr, ldb, multi_stride_b);
719 }
720 else
721 {
Gunes Bayiref637392024-02-12 21:32:51 +0000722 const bool kernel_supports_transpose = _gemm_kernel_asm->B_pretranspose_supports_transpose();
723 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(
724 _gemm_kernel_asm.get(), pretranspose.get(), b_ptr, ldb, multi_stride_b,
725 NEScheduler::get().num_threads(), _B_pre_pretranspose_required && kernel_supports_transpose);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100726 }
727 }
728 }
729
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100730 const auto scheduling_hint = scheduling_hint_heuristic(_kernel_info.method, d->info()->data_type());
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000731
David Mansell9e698d52020-08-25 15:02:02 +0100732 // Set workspace if needed and reset number of threads as buffer manager gets re-created with max_threads
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100733 CpuAuxTensorHandler workspace(offset_int_vec(AsmGemmWorkspace), _workspace_info, tensors, false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100734 if (workspace.get()->buffer() != nullptr)
David Mansell9e698d52020-08-25 15:02:02 +0100735 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100736 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(workspace.get()->buffer()));
David Mansell9e698d52020-08-25 15:02:02 +0100737 const unsigned int split_dim = scheduling_hint.split_dimension();
738 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000739 unsigned int num_threads = NEScheduler::get().num_threads();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100740 if (window_size < num_threads)
David Mansell9e698d52020-08-25 15:02:02 +0100741 {
742 num_threads = window_size;
743 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100744 if (split_dim != IScheduler::split_dimensions_all)
David Mansell9e698d52020-08-25 15:02:02 +0100745 {
746 // Make sure the kernel does not expect more threads than we can actually spawn
747 const unsigned int num_iterations = _optimised_kernel.get()->window().num_iterations(split_dim);
748 num_threads = std::min(num_iterations, num_threads);
749 }
750 _gemm_kernel_asm->set_nthreads(num_threads);
751 }
752
753 // Prepare assembly kernel
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100754 prepare(tensors);
David Mansell9e698d52020-08-25 15:02:02 +0100755
David Mansell9e698d52020-08-25 15:02:02 +0100756 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000757 TypeOutput *bias = nullptr;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100758 if (c && c->info()->data_type() != DataType::S32)
David Mansell9e698d52020-08-25 15:02:02 +0100759 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100760 bias = reinterpret_cast<TypeOutput *>(c->buffer() + c->info()->offset_first_element_in_bytes());
David Mansell9e698d52020-08-25 15:02:02 +0100761 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000762
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100763 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000764 {
765 in0_ptr = nullptr;
766 lda = 0;
767 batch_stride_a = 0;
768 multi_stride_a = 0;
769 }
770
David Mansell9e698d52020-08-25 15:02:02 +0100771 // Set gemm parameters
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100772 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a, in1_ptr, ldb, multi_stride_b, out_ptr,
773 ldd, batch_stride_d, multi_stride_d, bias, 0);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000774 // Schedule
Georgios Pinitas77d42522019-11-05 13:35:47 +0000775 NEScheduler::get().schedule(_optimised_kernel.get(), scheduling_hint);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100776}
777
Anthony Barbiereaefd002018-07-20 17:49:35 +0100778template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100779void create_arm_gemm(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100780 const ITensorInfo *a,
781 const ITensorInfo *b,
782 const ITensorInfo *c,
783 ITensorInfo *d,
784 arm_gemm::Activation activation,
785 const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100786{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000787 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000788 const CPUInfo &ci = NEScheduler::get().cpu_info();
789 unsigned int num_threads = NEScheduler::get().num_threads();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100790
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000791 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100792 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100793 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads,
Radu Salavatf1f1f872024-02-27 18:32:26 +0000794 info.fixed_format, info.fast_mode, info.accumulate, &cfg);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100795
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100796 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000797 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput>>();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100798 fallback->configure(a, b, c, d, args, info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100799 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100800}
801
802template <typename TypeInput, typename TypeOutput>
Jonathan Deakina668f9f2024-01-24 09:15:38 +0000803void create_arm_gemm_dequant(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
804 const ITensorInfo *a,
805 const ITensorInfo *b,
806 const ITensorInfo *c,
807 ITensorInfo *d,
808 arm_gemm::Activation activation,
809 const AsmGemmInfo &info)
810{
811 ARM_COMPUTE_UNUSED(activation);
812
813 Params p = extract_parameters(a, b, d, info);
814 const CPUInfo &ci = NEScheduler::get().cpu_info();
815 const unsigned int num_threads = NEScheduler::get().num_threads();
816
817 arm_gemm::GemmConfig cfg;
818 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
819 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads,
820 info.fixed_format, info.fast_mode, info.accumulate, &cfg);
821
822 // Create arm_gemm fallback
823 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::DequantizeFloat>>();
824
825 // Configure requantization info
826 const GEMMLowpOutputStageInfo os_info = info.output_stage;
827
828 arm_gemm::DequantizeFloat gemm_dequant_info{};
829 gemm_dequant_info = arm_gemm::DequantizeFloat(d->quantization_info().uniform().scale);
830
831 fallback->configure(a, b, c, d, args, info, gemm_dequant_info);
832 arm_gemm = std::move(fallback);
833}
834
835template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100836void create_arm_gemm_quant(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100837 const ITensorInfo *a,
838 const ITensorInfo *b,
839 const ITensorInfo *c,
840 ITensorInfo *d,
841 arm_gemm::Activation activation,
842 const AsmGemmInfo &info)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100843{
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +0100844 ARM_COMPUTE_UNUSED(activation);
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100845 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000846 const CPUInfo &ci = NEScheduler::get().cpu_info();
847 const unsigned int num_threads = NEScheduler::get().num_threads();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100848
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000849 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100850 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100851 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads,
Radu Salavatf1f1f872024-02-27 18:32:26 +0000852 info.fixed_format, info.fast_mode, info.accumulate, &cfg);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100853
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000854 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000855 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::Requantize32>>();
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000856
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100857 // Configure requantization info
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000858 const int32_t negation = info.negated_offsets ? 1 : -1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100859 const int32_t a_offset = -a->quantization_info().uniform().offset * negation;
860 const int32_t b_offset = -b->quantization_info().uniform().offset * negation;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000861 const GEMMLowpOutputStageInfo os_info = info.output_stage;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100862
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000863 arm_gemm::Requantize32 gemm_requant_info{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100864 if (os_info.gemmlowp_shifts.size() > 1)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000865 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100866 const auto requantize_data =
867 fallback->set_requantize_data(os_info.gemmlowp_shifts, os_info.gemmlowp_multipliers);
868 gemm_requant_info = arm_gemm::Requantize32(
869 nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset,
870 (std::get<0>(requantize_data)) ? std::get<1>(requantize_data) : nullptr, std::get<2>(requantize_data),
871 std::get<3>(requantize_data), os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000872 }
873 else
874 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100875 gemm_requant_info =
876 arm_gemm::Requantize32(nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset, -os_info.gemmlowp_shift,
877 os_info.gemmlowp_multiplier, os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000878 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100879
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000880 // Configure fallback
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100881 fallback->configure(a, b, c, d, args, info, gemm_requant_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100882 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100883}
Anthony Barbiereaefd002018-07-20 17:49:35 +0100884} //namespace
885
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100886CpuGemmAssemblyDispatch::CpuGemmAssemblyDispatch() : _arm_gemm(nullptr)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100887{
888}
889
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100890Status CpuGemmAssemblyDispatch::has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
891 const ITensorInfo *a,
892 const ITensorInfo *b,
893 const ITensorInfo *c,
894 const ITensorInfo *d,
895 const AsmGemmInfo &info)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000896{
897 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
898 ARM_COMPUTE_UNUSED(c);
899 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
900 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000901 const CPUInfo &ci = NEScheduler::get().cpu_info();
902 unsigned int num_threads = NEScheduler::get().num_threads();
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000903 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100904 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
905 arm_gemm::WeightFormat arm_gemm_expected_wf = assembly_utils::map_to_arm_gemm_weight_format(expected_weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100906 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, act, num_threads,
Radu Salavatf1f1f872024-02-27 18:32:26 +0000907 info.fixed_format, info.fast_mode, info.accumulate, &cfg);
SiCong Lic5ab4df2023-10-17 17:38:57 +0100908 // TODO: Incorporate info.transpose_b COMPMID-6595
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100909 switch (a->data_type())
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000910 {
911 case DataType::F32:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100912 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
913 !(arm_gemm::has_opt_gemm<float, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
914 "We could not find an optimized kernel for F32 input");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000915 break;
916#ifdef __aarch64__
917 case DataType::U8:
918 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100919 if (d->data_type() == DataType::S32)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000920 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100921 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
922 !(arm_gemm::has_opt_gemm<uint8_t, uint32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
923 "We could not find an optimized kernel for U8/QASYMM8 input and U32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000924 }
925 else
926 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100927 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
928 !(arm_gemm::has_opt_gemm<uint8_t, uint8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
929 "We could not find an optimized kernel for U8 input and U8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000930 }
931 break;
932 case DataType::S8:
933 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100934 if (d->data_type() == DataType::S32)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000935 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100936 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
937 !(arm_gemm::has_opt_gemm<int8_t, int32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
938 "We could not find an optimized kernel for S8/QASYMM8_SIGNED input and S32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000939 }
940 else
941 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100942 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
943 !(arm_gemm::has_opt_gemm<int8_t, int8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
944 "We could not find an optimized kernel for S8 input and S8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000945 }
946 break;
947#endif /* __aarch64__ */
Pablo Marquez Tello2217f1e2024-05-14 07:54:19 +0100948
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100949#if defined(ARM_COMPUTE_ENABLE_BF16)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000950 case DataType::BFLOAT16:
951 {
Renato Arantes36a75da2024-01-26 17:31:18 +0000952 if (d->data_type() == DataType::BFLOAT16)
953 {
954 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
955 !(arm_gemm::has_opt_gemm<bfloat16, bfloat16, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
956 "We could not find an optimized kernel for BFLOAT16 input and BFLOAT16 output");
957 }
958 else
959 {
960 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
961 !(arm_gemm::has_opt_gemm<bfloat16, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
962 "We could not find an optimized kernel for BFLOAT16 input and F32 output");
963 }
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000964 break;
965 }
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100966#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Pablo Marquez Tello2217f1e2024-05-14 07:54:19 +0100967
968#if defined(ARM_COMPUTE_ENABLE_FP16)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000969 case DataType::F16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100970 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
971 !(arm_gemm::has_opt_gemm<float16_t, float16_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
972 "We could not find an optimized kernel for F16 input and F16 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000973 break;
Pablo Marquez Tello2217f1e2024-05-14 07:54:19 +0100974#endif /* ARM_COMPUTE_ENABLE_FP16 */
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000975 default:
976 ARM_COMPUTE_RETURN_ERROR_ON_MSG(true, "Usupported type. Could not find a kernel");
977 break;
978 }
Ramy Elgammal91780022022-07-20 14:57:37 +0100979 expected_weight_format = assembly_utils::map_to_arm_compute_weight_format(arm_gemm_expected_wf);
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000980
981 return Status{};
982}
983
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100984Status CpuGemmAssemblyDispatch::validate(
985 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100986{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000987 ARM_COMPUTE_UNUSED(c, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100988 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
989 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000990 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100991 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(info.reshape_b_only_on_first_run),
992 "Assembly kernel will not be executed when reshape_b_only_on_first_run is false");
Georgios Pinitas0f954eb2020-06-23 17:28:38 +0100993
Anthony Barbiereaefd002018-07-20 17:49:35 +0100994#ifndef __aarch64__
Michele Di Giorgio52556722019-12-23 16:35:12 +0000995 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->element_size() == 1, "8bit integer types only supported for aarch64");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100996#endif /* __aarch64__ */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100997 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::QASYMM8,
998 DataType::QASYMM8_SIGNED, DataType::S8, DataType::BFLOAT16,
999 DataType::F16, DataType::F32);
1000 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
1001 b, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::S8,
1002 DataType::BFLOAT16, DataType::F16, DataType::F32);
1003 if (is_data_type_quantized_per_channel(b->data_type()))
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001004 {
1005 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
1006 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001007 else if (is_fixed_format_fast_math(info.weight_format))
Jonathan Deakin464ed202023-01-12 11:41:14 +00001008 {
1009 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(a, DataType::F32);
1010 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(b, DataType::BFLOAT16);
1011 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001012 else
1013 {
1014 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
1015 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001016 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32,
1017 "Only F32 output supported for F32 input");
1018 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F16 && d->data_type() != DataType::F16,
1019 "Only F16 output supported for F16 input");
Renato Arantes36a75da2024-01-26 17:31:18 +00001020 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::BFLOAT16 &&
1021 (d->data_type() != DataType::F32 && d->data_type() != DataType::BFLOAT16),
1022 "Only F32/BFLOAT16 output supported for BFLOAT16 input");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001023 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 && d->data_type() != DataType::U32,
1024 "Only U32 output supported for U8 input");
1025 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::S8 && d->data_type() != DataType::S32,
1026 "Only S32 output supported for S8 input");
1027 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 &&
1028 (d->data_type() != DataType::QASYMM8 && d->data_type() != DataType::S32),
Ethan Doe1fe48ca2023-03-01 23:19:26 +00001029 "Only QASYMM8/S32 output supported for QASYMM8 input");
Viet-Hoa Do246fe082023-08-16 10:29:00 +01001030 arm_compute::WeightFormat expected_weight_format = arm_compute::WeightFormat::UNSPECIFIED;
Ramy Elgammal91780022022-07-20 14:57:37 +01001031 const Status ret = CpuGemmAssemblyDispatch::has_opt_impl(expected_weight_format, a, b, c, d, info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001032 if ((bool)ret && expected_weight_format != arm_compute::WeightFormat::ANY)
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001033 {
1034 // Correctness check: if the format expected by the kernel is
1035 // not "any", make sure that the one found matches the format
1036 // intended by the caller.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001037 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
1038 (expected_weight_format != info.weight_format),
1039 "The format expected by the kernel does not correspond with the one requested by the user.");
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001040 }
1041 return ret;
Anthony Barbiereaefd002018-07-20 17:49:35 +01001042}
1043
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001044bool CpuGemmAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001045{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001046 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001047 return act.type != arm_gemm::Activation::Type::None;
1048}
1049
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001050void CpuGemmAssemblyDispatch::configure(
1051 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001052{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001053 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001054 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001055
1056 //If we don't support a combination of data types, silently return: it is the caller's responsibility to check if configure() was successful via is_configured()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001057 if (!CpuGemmAssemblyDispatch::validate(a, b, c, d, info))
Anthony Barbiereaefd002018-07-20 17:49:35 +01001058 {
1059 return;
1060 }
1061
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001062 switch (a->data_type())
Anthony Barbiereaefd002018-07-20 17:49:35 +01001063 {
1064 case DataType::F32:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001065 create_arm_gemm<float, float>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001066 break;
1067#ifdef __aarch64__
1068 case DataType::U8:
1069 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001070 if (d->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001071 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001072 create_arm_gemm<uint8_t, uint32_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001073 }
1074 else
1075 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001076 create_arm_gemm_quant<uint8_t, uint8_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001077 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001078 break;
1079 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001080 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001081 if (d->data_type() == DataType::S32)
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001082 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001083 create_arm_gemm<int8_t, int32_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001084 }
Jonathan Deakina668f9f2024-01-24 09:15:38 +00001085 else if (d->data_type() == DataType::F32)
1086 {
1087 create_arm_gemm_dequant<int8_t, float>(_arm_gemm, a, b, c, d, act, info);
1088 }
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001089 else
1090 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001091 create_arm_gemm_quant<int8_t, int8_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001092 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001093 break;
1094#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001095#if defined(ARM_COMPUTE_ENABLE_BF16)
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001096 case DataType::BFLOAT16:
Renato Arantes36a75da2024-01-26 17:31:18 +00001097 if (d->data_type() == DataType::BFLOAT16)
1098 {
1099 create_arm_gemm<bfloat16, bfloat16>(_arm_gemm, a, b, c, d, act, info);
1100 }
1101 else
1102 {
1103 create_arm_gemm<bfloat16, float>(_arm_gemm, a, b, c, d, act, info);
1104 }
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001105 break;
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001106#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Pablo Marquez Tello2217f1e2024-05-14 07:54:19 +01001107#ifdef ARM_COMPUTE_ENABLE_FP16
Anthony Barbiereaefd002018-07-20 17:49:35 +01001108 case DataType::F16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001109 create_arm_gemm<float16_t, float16_t>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001110 break;
Pablo Marquez Tello2217f1e2024-05-14 07:54:19 +01001111#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbiereaefd002018-07-20 17:49:35 +01001112 default:
1113 break;
1114 }
1115}
1116
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001117void CpuGemmAssemblyDispatch::prepare(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001118{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001119 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001120 _arm_gemm->prepare(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001121}
1122
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001123bool CpuGemmAssemblyDispatch::is_configured() const
Anthony Barbiereaefd002018-07-20 17:49:35 +01001124{
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001125 return _arm_gemm && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +01001126}
1127
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001128void CpuGemmAssemblyDispatch::run(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001129{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001130 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001131 _arm_gemm->run(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001132}
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001133
1134experimental::MemoryRequirements CpuGemmAssemblyDispatch::workspace() const
1135{
1136 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
1137 return _arm_gemm->workspace();
1138}
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001139} // namespace cpu
1140} // namespace arm_compute