blob: 01a74a5a5682192073d9b19507997a672c0ffde3 [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 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100543}
544
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100545template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100546void Fallback<TypeInput, TypeOutput, OutputStage>::prepare(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100547{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100548 if (!_is_prepared)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100549 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100550 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
551 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
SiCong Lid4650e92023-11-14 15:17:10 +0000552 ARM_COMPUTE_ERROR_ON_NULLPTR(b);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100553
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100554 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100555 if (c && c->info()->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100556 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100557 _gemm_kernel_asm->set_quantized_bias(
558 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100559 }
SiCong Lic5ab4df2023-10-17 17:38:57 +0100560 const ITensor *b_to_use = b;
Gunes Bayiref637392024-02-12 21:32:51 +0000561
SiCong Lic5ab4df2023-10-17 17:38:57 +0100562 // Pre-pretranspose B if required
SiCong Lic5ab4df2023-10-17 17:38:57 +0100563 CpuAuxTensorHandler pre_pretransposed_b(
564 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
565 /*pack_inject: no need to inject into tensors*/
566 false,
567 /*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 +0000568 !_run_pre_pretranspose_b);
569
570 if (_run_pre_pretranspose_b)
SiCong Lic5ab4df2023-10-17 17:38:57 +0100571 {
572 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
573 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
574 _pre_pretranspose_b->run(pre_pretranspose_pack);
575 b_to_use = pre_pretransposed_b.get();
576 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100577
Anthony Barbier71d9b572018-07-06 17:05:59 +0100578 // Pretranspose B if required
Gunes Bayiref637392024-02-12 21:32:51 +0000579 if (_B_pretranspose_required)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100580 {
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000581 // Fixed format kernels need no pretranspose.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100582 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
583 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
SiCong Lic5ab4df2023-10-17 17:38:57 +0100584 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
585 const auto in1_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
586 b_to_use->info()->offset_first_element_in_bytes());
587 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 +0100588
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100589 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, false);
Gunes Bayiref637392024-02-12 21:32:51 +0000590
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100591 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
Gunes Bayiref637392024-02-12 21:32:51 +0000592
593 const bool kernel_supports_transpose = _gemm_kernel_asm->B_pretranspose_supports_transpose();
594 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(
595 _gemm_kernel_asm.get(), pretranspose.get(), in1_ptr, ldb, multi_stride_b,
596 NEScheduler::get().num_threads(), _B_pre_pretranspose_required && kernel_supports_transpose);
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100597
598 b->mark_as_unused();
Gunes Bayiref637392024-02-12 21:32:51 +0000599 // Note that we don't need to mark b_to_use as unused, as if it's been assigned to pre_pretransposed_b,
600 // its memory will be auto-managed by the handler
Anthony Barbier71d9b572018-07-06 17:05:59 +0100601 }
602
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100603 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000604 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100605 prepare_indirect_buffer(tensors);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000606 }
607
Anthony Barbier71d9b572018-07-06 17:05:59 +0100608 _is_prepared = true;
609 }
610}
611
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100612template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100613bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100614{
615 return _optimised_kernel != nullptr;
616}
617
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100618template <typename TypeInput, typename TypeOutput, class OutputStage>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100619experimental::MemoryRequirements Fallback<TypeInput, TypeOutput, OutputStage>::workspace() const
620{
621 return _aux_mem;
622}
623
624template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100625void Fallback<TypeInput, TypeOutput, OutputStage>::run(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100626{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100627 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
628 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
629 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
630 auto d = tensors.get_tensor(TensorType::ACL_DST);
SiCong Lid4650e92023-11-14 15:17:10 +0000631 ARM_COMPUTE_ERROR_ON_NULLPTR(a, d);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100632
Jonathan Deakin464ed202023-01-12 11:41:14 +0000633 int lda = a->info()->strides_in_bytes().y() / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100634 int ldb = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000635 const int ldd = d->info()->strides_in_bytes().y() / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100636
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000637 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100638 const size_t a_multi_idx = a_batch_idx + 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000639 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100640 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100641
Jonathan Deakin464ed202023-01-12 11:41:14 +0000642 int batch_stride_a = a->info()->strides_in_bytes()[a_batch_idx] / a->info()->element_size();
643 const int batch_stride_d = d->info()->strides_in_bytes()[d_batch_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100644
Jonathan Deakin464ed202023-01-12 11:41:14 +0000645 int multi_stride_a = a->info()->strides_in_bytes()[a_multi_idx] / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100646 int multi_stride_b = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000647 const int multi_stride_d = d->info()->strides_in_bytes()[d_multi_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100648
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100649 auto in0_ptr = reinterpret_cast<const TypeInput *>(a->buffer() + a->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100650 const TypeInput *in1_ptr = nullptr;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100651 auto out_ptr = reinterpret_cast<TypeOutput *>(d->buffer() + d->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100652
SiCong Lic5ab4df2023-10-17 17:38:57 +0100653 const ITensor *b_to_use = b;
654
655 // Pre-pretranspose B if required
SiCong Lic5ab4df2023-10-17 17:38:57 +0100656 CpuAuxTensorHandler pre_pretransposed_b(
657 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
658 false /*pack_inject: no need to inject into tensors*/,
Gunes Bayiref637392024-02-12 21:32:51 +0000659 !_run_pre_pretranspose_b /*bypass_alloc: no need to allocate if pre-pretranspose B is not required as this handle will not be used*/);
660 if (b_to_use && !_is_b_constant && _run_pre_pretranspose_b)
SiCong Lic5ab4df2023-10-17 17:38:57 +0100661 {
662 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
663 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
664 _pre_pretranspose_b->run(pre_pretranspose_pack);
665 b_to_use = pre_pretransposed_b.get();
666 }
667
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100668 // Check if B is pre-tranposed and de-reference if not
SiCong Lid4650e92023-11-14 15:17:10 +0000669 if (b_to_use && !_gemm_kernel_asm->B_is_pretransposed())
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100670 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100671 ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
672 multi_stride_b = b_to_use->info()->strides_in_bytes().z() / b_to_use->info()->element_size();
673 in1_ptr =
674 reinterpret_cast<const TypeInput *>(b_to_use->buffer() + b_to_use->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100675 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100676
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100677 // If necessary, run pretranspose every time if either weights or biases are non-constant
SiCong Lic5ab4df2023-10-17 17:38:57 +0100678 if ((b_to_use && !_is_b_constant) || (c && !_is_c_constant && c->info()->data_type() == DataType::S32))
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100679 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100680 if (c && c->info()->data_type() == DataType::S32)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100681 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100682 _gemm_kernel_asm->set_quantized_bias(
683 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100684 }
685
686 // Pretranspose B if required
SiCong Lid4650e92023-11-14 15:17:10 +0000687 if (b_to_use && _B_pretranspose_required)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100688 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100689 // Fixed format kernels need no pretranspose.
690 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
691 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
692 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
693 const auto b_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
694 b_to_use->info()->offset_first_element_in_bytes());
695 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 +0100696
697 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, true);
698 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
699
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100700 if (_is_b_constant)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100701 {
702 _gemm_kernel_asm->requantize_bias(pretranspose.get()->buffer(), b_ptr, ldb, multi_stride_b);
703 }
704 else
705 {
Gunes Bayiref637392024-02-12 21:32:51 +0000706 const bool kernel_supports_transpose = _gemm_kernel_asm->B_pretranspose_supports_transpose();
707 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(
708 _gemm_kernel_asm.get(), pretranspose.get(), b_ptr, ldb, multi_stride_b,
709 NEScheduler::get().num_threads(), _B_pre_pretranspose_required && kernel_supports_transpose);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100710 }
711 }
712 }
713
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100714 const auto scheduling_hint = scheduling_hint_heuristic(_kernel_info.method, d->info()->data_type());
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000715
David Mansell9e698d52020-08-25 15:02:02 +0100716 // 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 +0100717 CpuAuxTensorHandler workspace(offset_int_vec(AsmGemmWorkspace), _workspace_info, tensors, false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100718 if (workspace.get()->buffer() != nullptr)
David Mansell9e698d52020-08-25 15:02:02 +0100719 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100720 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(workspace.get()->buffer()));
David Mansell9e698d52020-08-25 15:02:02 +0100721 const unsigned int split_dim = scheduling_hint.split_dimension();
722 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000723 unsigned int num_threads = NEScheduler::get().num_threads();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100724 if (window_size < num_threads)
David Mansell9e698d52020-08-25 15:02:02 +0100725 {
726 num_threads = window_size;
727 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100728 if (split_dim != IScheduler::split_dimensions_all)
David Mansell9e698d52020-08-25 15:02:02 +0100729 {
730 // Make sure the kernel does not expect more threads than we can actually spawn
731 const unsigned int num_iterations = _optimised_kernel.get()->window().num_iterations(split_dim);
732 num_threads = std::min(num_iterations, num_threads);
733 }
734 _gemm_kernel_asm->set_nthreads(num_threads);
735 }
736
737 // Prepare assembly kernel
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100738 prepare(tensors);
David Mansell9e698d52020-08-25 15:02:02 +0100739
David Mansell9e698d52020-08-25 15:02:02 +0100740 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000741 TypeOutput *bias = nullptr;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100742 if (c && c->info()->data_type() != DataType::S32)
David Mansell9e698d52020-08-25 15:02:02 +0100743 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100744 bias = reinterpret_cast<TypeOutput *>(c->buffer() + c->info()->offset_first_element_in_bytes());
David Mansell9e698d52020-08-25 15:02:02 +0100745 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000746
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100747 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000748 {
749 in0_ptr = nullptr;
750 lda = 0;
751 batch_stride_a = 0;
752 multi_stride_a = 0;
753 }
754
David Mansell9e698d52020-08-25 15:02:02 +0100755 // Set gemm parameters
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100756 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a, in1_ptr, ldb, multi_stride_b, out_ptr,
757 ldd, batch_stride_d, multi_stride_d, bias, 0);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000758 // Schedule
Georgios Pinitas77d42522019-11-05 13:35:47 +0000759 NEScheduler::get().schedule(_optimised_kernel.get(), scheduling_hint);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100760}
761
Anthony Barbiereaefd002018-07-20 17:49:35 +0100762template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100763void create_arm_gemm(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100764 const ITensorInfo *a,
765 const ITensorInfo *b,
766 const ITensorInfo *c,
767 ITensorInfo *d,
768 arm_gemm::Activation activation,
769 const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100770{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000771 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000772 const CPUInfo &ci = NEScheduler::get().cpu_info();
773 unsigned int num_threads = NEScheduler::get().num_threads();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100774
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000775 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100776 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100777 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 +0000778 info.fixed_format, info.fast_mode, info.accumulate, &cfg);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100779
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100780 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000781 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput>>();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100782 fallback->configure(a, b, c, d, args, info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100783 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100784}
785
786template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100787void create_arm_gemm_quant(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100788 const ITensorInfo *a,
789 const ITensorInfo *b,
790 const ITensorInfo *c,
791 ITensorInfo *d,
792 arm_gemm::Activation activation,
793 const AsmGemmInfo &info)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100794{
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +0100795 ARM_COMPUTE_UNUSED(activation);
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100796 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000797 const CPUInfo &ci = NEScheduler::get().cpu_info();
798 const unsigned int num_threads = NEScheduler::get().num_threads();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100799
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000800 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100801 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100802 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 +0000803 info.fixed_format, info.fast_mode, info.accumulate, &cfg);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100804
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000805 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000806 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::Requantize32>>();
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000807
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100808 // Configure requantization info
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000809 const int32_t negation = info.negated_offsets ? 1 : -1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100810 const int32_t a_offset = -a->quantization_info().uniform().offset * negation;
811 const int32_t b_offset = -b->quantization_info().uniform().offset * negation;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000812 const GEMMLowpOutputStageInfo os_info = info.output_stage;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100813
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000814 arm_gemm::Requantize32 gemm_requant_info{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100815 if (os_info.gemmlowp_shifts.size() > 1)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000816 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100817 const auto requantize_data =
818 fallback->set_requantize_data(os_info.gemmlowp_shifts, os_info.gemmlowp_multipliers);
819 gemm_requant_info = arm_gemm::Requantize32(
820 nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset,
821 (std::get<0>(requantize_data)) ? std::get<1>(requantize_data) : nullptr, std::get<2>(requantize_data),
822 std::get<3>(requantize_data), os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000823 }
824 else
825 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100826 gemm_requant_info =
827 arm_gemm::Requantize32(nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset, -os_info.gemmlowp_shift,
828 os_info.gemmlowp_multiplier, os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000829 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100830
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000831 // Configure fallback
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100832 fallback->configure(a, b, c, d, args, info, gemm_requant_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100833 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100834}
Anthony Barbiereaefd002018-07-20 17:49:35 +0100835} //namespace
836
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100837CpuGemmAssemblyDispatch::CpuGemmAssemblyDispatch() : _arm_gemm(nullptr)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100838{
839}
840
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100841Status CpuGemmAssemblyDispatch::has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
842 const ITensorInfo *a,
843 const ITensorInfo *b,
844 const ITensorInfo *c,
845 const ITensorInfo *d,
846 const AsmGemmInfo &info)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000847{
848 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
849 ARM_COMPUTE_UNUSED(c);
850 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
851 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000852 const CPUInfo &ci = NEScheduler::get().cpu_info();
853 unsigned int num_threads = NEScheduler::get().num_threads();
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000854 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100855 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
856 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 +0100857 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 +0000858 info.fixed_format, info.fast_mode, info.accumulate, &cfg);
SiCong Lic5ab4df2023-10-17 17:38:57 +0100859 // TODO: Incorporate info.transpose_b COMPMID-6595
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100860 switch (a->data_type())
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000861 {
862 case DataType::F32:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100863 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
864 !(arm_gemm::has_opt_gemm<float, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
865 "We could not find an optimized kernel for F32 input");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000866 break;
867#ifdef __aarch64__
868 case DataType::U8:
869 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100870 if (d->data_type() == DataType::S32)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000871 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100872 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
873 !(arm_gemm::has_opt_gemm<uint8_t, uint32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
874 "We could not find an optimized kernel for U8/QASYMM8 input and U32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000875 }
876 else
877 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100878 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
879 !(arm_gemm::has_opt_gemm<uint8_t, uint8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
880 "We could not find an optimized kernel for U8 input and U8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000881 }
882 break;
883 case DataType::S8:
884 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100885 if (d->data_type() == DataType::S32)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000886 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100887 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
888 !(arm_gemm::has_opt_gemm<int8_t, int32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
889 "We could not find an optimized kernel for S8/QASYMM8_SIGNED input and S32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000890 }
891 else
892 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100893 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
894 !(arm_gemm::has_opt_gemm<int8_t, int8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
895 "We could not find an optimized kernel for S8 input and S8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000896 }
897 break;
898#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100899#if defined(ARM_COMPUTE_ENABLE_BF16)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000900 case DataType::BFLOAT16:
901 {
Renato Arantes36a75da2024-01-26 17:31:18 +0000902 if (d->data_type() == DataType::BFLOAT16)
903 {
904 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
905 !(arm_gemm::has_opt_gemm<bfloat16, bfloat16, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
906 "We could not find an optimized kernel for BFLOAT16 input and BFLOAT16 output");
907 }
908 else
909 {
910 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
911 !(arm_gemm::has_opt_gemm<bfloat16, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
912 "We could not find an optimized kernel for BFLOAT16 input and F32 output");
913 }
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000914 break;
915 }
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100916#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000917#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
918 case DataType::F16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100919 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
920 !(arm_gemm::has_opt_gemm<float16_t, float16_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
921 "We could not find an optimized kernel for F16 input and F16 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000922 break;
923#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
924 default:
925 ARM_COMPUTE_RETURN_ERROR_ON_MSG(true, "Usupported type. Could not find a kernel");
926 break;
927 }
Ramy Elgammal91780022022-07-20 14:57:37 +0100928 expected_weight_format = assembly_utils::map_to_arm_compute_weight_format(arm_gemm_expected_wf);
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000929
930 return Status{};
931}
932
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100933Status CpuGemmAssemblyDispatch::validate(
934 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100935{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000936 ARM_COMPUTE_UNUSED(c, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100937 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
938 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000939 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100940 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(info.reshape_b_only_on_first_run),
941 "Assembly kernel will not be executed when reshape_b_only_on_first_run is false");
Georgios Pinitas0f954eb2020-06-23 17:28:38 +0100942
Anthony Barbiereaefd002018-07-20 17:49:35 +0100943#ifndef __aarch64__
Michele Di Giorgio52556722019-12-23 16:35:12 +0000944 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->element_size() == 1, "8bit integer types only supported for aarch64");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100945#endif /* __aarch64__ */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100946 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::QASYMM8,
947 DataType::QASYMM8_SIGNED, DataType::S8, DataType::BFLOAT16,
948 DataType::F16, DataType::F32);
949 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
950 b, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::S8,
951 DataType::BFLOAT16, DataType::F16, DataType::F32);
952 if (is_data_type_quantized_per_channel(b->data_type()))
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100953 {
954 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
955 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100956 else if (is_fixed_format_fast_math(info.weight_format))
Jonathan Deakin464ed202023-01-12 11:41:14 +0000957 {
958 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(a, DataType::F32);
959 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(b, DataType::BFLOAT16);
960 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100961 else
962 {
963 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
964 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100965 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32,
966 "Only F32 output supported for F32 input");
967 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F16 && d->data_type() != DataType::F16,
968 "Only F16 output supported for F16 input");
Renato Arantes36a75da2024-01-26 17:31:18 +0000969 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::BFLOAT16 &&
970 (d->data_type() != DataType::F32 && d->data_type() != DataType::BFLOAT16),
971 "Only F32/BFLOAT16 output supported for BFLOAT16 input");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100972 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 && d->data_type() != DataType::U32,
973 "Only U32 output supported for U8 input");
974 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::S8 && d->data_type() != DataType::S32,
975 "Only S32 output supported for S8 input");
976 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 &&
977 (d->data_type() != DataType::QASYMM8 && d->data_type() != DataType::S32),
Ethan Doe1fe48ca2023-03-01 23:19:26 +0000978 "Only QASYMM8/S32 output supported for QASYMM8 input");
Viet-Hoa Do246fe082023-08-16 10:29:00 +0100979 arm_compute::WeightFormat expected_weight_format = arm_compute::WeightFormat::UNSPECIFIED;
Ramy Elgammal91780022022-07-20 14:57:37 +0100980 const Status ret = CpuGemmAssemblyDispatch::has_opt_impl(expected_weight_format, a, b, c, d, info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100981 if ((bool)ret && expected_weight_format != arm_compute::WeightFormat::ANY)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000982 {
983 // Correctness check: if the format expected by the kernel is
984 // not "any", make sure that the one found matches the format
985 // intended by the caller.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100986 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
987 (expected_weight_format != info.weight_format),
988 "The format expected by the kernel does not correspond with the one requested by the user.");
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000989 }
990 return ret;
Anthony Barbiereaefd002018-07-20 17:49:35 +0100991}
992
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100993bool CpuGemmAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100994{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000995 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100996 return act.type != arm_gemm::Activation::Type::None;
997}
998
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100999void CpuGemmAssemblyDispatch::configure(
1000 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001001{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001002 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001003 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001004
1005 //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 +01001006 if (!CpuGemmAssemblyDispatch::validate(a, b, c, d, info))
Anthony Barbiereaefd002018-07-20 17:49:35 +01001007 {
1008 return;
1009 }
1010
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001011 switch (a->data_type())
Anthony Barbiereaefd002018-07-20 17:49:35 +01001012 {
1013 case DataType::F32:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001014 create_arm_gemm<float, float>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001015 break;
1016#ifdef __aarch64__
1017 case DataType::U8:
1018 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001019 if (d->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001020 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001021 create_arm_gemm<uint8_t, uint32_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001022 }
1023 else
1024 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001025 create_arm_gemm_quant<uint8_t, uint8_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001026 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001027 break;
1028 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001029 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001030 if (d->data_type() == DataType::S32)
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001031 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001032 create_arm_gemm<int8_t, int32_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001033 }
1034 else
1035 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001036 create_arm_gemm_quant<int8_t, int8_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001037 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001038 break;
1039#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001040#if defined(ARM_COMPUTE_ENABLE_BF16)
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001041 case DataType::BFLOAT16:
Renato Arantes36a75da2024-01-26 17:31:18 +00001042 if (d->data_type() == DataType::BFLOAT16)
1043 {
1044 create_arm_gemm<bfloat16, bfloat16>(_arm_gemm, a, b, c, d, act, info);
1045 }
1046 else
1047 {
1048 create_arm_gemm<bfloat16, float>(_arm_gemm, a, b, c, d, act, info);
1049 }
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001050 break;
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001051#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Anthony Barbiereaefd002018-07-20 17:49:35 +01001052#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1053 case DataType::F16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001054 create_arm_gemm<float16_t, float16_t>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001055 break;
1056#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1057 default:
1058 break;
1059 }
1060}
1061
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001062void CpuGemmAssemblyDispatch::prepare(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001063{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001064 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001065 _arm_gemm->prepare(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001066}
1067
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001068bool CpuGemmAssemblyDispatch::is_configured() const
Anthony Barbiereaefd002018-07-20 17:49:35 +01001069{
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001070 return _arm_gemm && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +01001071}
1072
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001073void CpuGemmAssemblyDispatch::run(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001074{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001075 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001076 _arm_gemm->run(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001077}
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001078
1079experimental::MemoryRequirements CpuGemmAssemblyDispatch::workspace() const
1080{
1081 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
1082 return _arm_gemm->workspace();
1083}
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001084} // namespace cpu
1085} // namespace arm_compute