blob: 611bc764633fdf480d416e18ba358cac2320648b [file] [log] [blame]
Anthony Barbier71d9b572018-07-06 17:05:59 +01001/*
Jonathan Deakin464ed202023-01-12 11:41:14 +00002 * Copyright (c) 2018-2023 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,
63 unsigned int num_threads)
SiCong Lidba672c2023-04-06 16:30:18 +010064{
65 ARM_COMPUTE_ERROR_ON(gemm_asm == nullptr);
66 ARM_COMPUTE_ERROR_ON(num_threads == 0);
67 // The window size is also the total workload size
68 const unsigned int wsize = gemm_asm->get_B_pretranspose_window_size();
69
70 std::vector<IScheduler::Workload> workloads(num_threads);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 for (unsigned int t = 0; t < num_threads; ++t)
SiCong Lidba672c2023-04-06 16:30:18 +010072 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 workloads[t] = [=](const ThreadInfo &info)
SiCong Lidba672c2023-04-06 16:30:18 +010074 {
75 const unsigned int start = (info.thread_id * wsize) / num_threads;
76 const unsigned int end = ((info.thread_id + 1) * wsize) / num_threads;
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (start < end)
SiCong Lidba672c2023-04-06 16:30:18 +010079 {
80 gemm_asm->pretranspose_B_array_part(dst->buffer(), src, src_ld, src_multi_stride, start, end);
81 }
82 };
83 }
84 NEScheduler::get().run_tagged_workloads(workloads, "CpuGemmAssemblyDispatch/pretranspose_B_array");
85}
86} // namespace
87
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010088using namespace arm_compute::experimental;
89
Anthony Barbiereaefd002018-07-20 17:49:35 +010090namespace
Anthony Barbier71d9b572018-07-06 17:05:59 +010091{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000092struct free_delete
93{
94 void operator()(void *x)
95 {
96 free(x);
97 }
98};
99
100struct Params
101{
102 unsigned int M;
103 unsigned int N;
104 unsigned int K;
105 unsigned int batches;
106 unsigned int multis;
107 unsigned int sections;
108 bool indirect;
109};
110
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100111Params extract_parameters(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *d, const AsmGemmInfo &info)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000112{
113 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000114 Params p;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100115 p.M = d->tensor_shape().y();
116 p.K = a->tensor_shape().x();
117 p.N = d->tensor_shape().x();
Georgios Pinitas4c634e02020-12-01 02:17:19 +0000118 p.batches = 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000119 p.multis = 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000120 p.sections = 1;
Georgios Pinitas4c634e02020-12-01 02:17:19 +0000121 p.indirect = false;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000122
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 if (info.method == AsmConvMethod::Conv || info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000124 {
125 p.indirect = true;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100126 p.sections = b->tensor_shape()[2] * b->tensor_shape()[3];
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000127 }
128 else
129 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100130 p.multis = b->tensor_shape().z();
131 p.batches = d->tensor_shape().total_size_upper(2) / p.multis;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000132 }
133
134 // Update M in case of GEMM3D for output
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 if (info.depth_output_gemm3d != 0)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000136 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100137 p.M = d->tensor_shape().y() * d->tensor_shape().z();
138 p.batches = d->tensor_shape().total_size_upper(3) / p.multis;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000139 }
140
141 return p;
142}
143
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000144IScheduler::Hints scheduling_hint_heuristic(arm_gemm::GemmMethod method, DataType data_type)
145{
146 // Schedule assembly kernel
147 const int granule_threshold = 200;
148 IScheduler::Hints scheduling_hint = IScheduler::Hints(Window::DimX);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 if (method == arm_gemm::GemmMethod::GEMM_INTERLEAVED && data_type == DataType::F32)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000150 {
151 scheduling_hint = IScheduler::Hints(Window::DimX, IScheduler::StrategyHint::DYNAMIC, granule_threshold);
152 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 else if (method == arm_gemm::GemmMethod::GEMM_INTERLEAVED_2D &&
154 (data_type == DataType::F32 || data_type == DataType::F16 || data_type == DataType::U8 ||
155 data_type == DataType::S8))
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000156 {
157 //GEMM_INTERLEAVED supports 2D parallelism, IScheduler::split_dimensions_all signals to parallelise over all window dimensions
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 scheduling_hint =
159 IScheduler::Hints(IScheduler::split_dimensions_all, IScheduler::StrategyHint::STATIC, granule_threshold);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000160 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100161 else if (method == arm_gemm::GemmMethod::QUANTIZE_WRAPPER_2D &&
162 (data_type == DataType::QASYMM8 || data_type == DataType::QASYMM8_SIGNED))
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000163 {
164 //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 +0100165 scheduling_hint =
166 IScheduler::Hints(IScheduler::split_dimensions_all, IScheduler::StrategyHint::STATIC, granule_threshold);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000167 }
168
169 return scheduling_hint;
170}
171
Anthony Barbiereaefd002018-07-20 17:49:35 +0100172/** Fallback in case ACL doesn't have a function */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100173template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing>
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100174class Fallback : public CpuGemmAssemblyDispatch::IFallback
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100175{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100176public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100177 /** Destructor */
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100178 ~Fallback() = default;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100179
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000180 /** Initialise the functions's input and output.
181 *
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100182 * @param[in] a Input tensor containing the Matrix A.
183 * @param[in] b Input tensor containing the Matrix B.
184 * @param[in] c Input tensor containing the Matrix C.
185 * @param[out] d Output tensor to store the result of matrix multiplication.
186 * @param[in] args Matrix multiplication information.
187 * @param[in] gemm_info GEMM meta-data
188 * @param[in] os Output stage meta-data.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000189 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100190 void configure(const ITensorInfo *a,
191 const ITensorInfo *b,
192 const ITensorInfo *c,
193 ITensorInfo *d,
194 arm_gemm::GemmArgs args,
195 const AsmGemmInfo &gemm_info,
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100196 const OutputStage &os = {});
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000197
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000198 /** Set requantization shifts to be used
199 *
200 * @param[in] shifts Requantization shifts
201 *
202 * @return Pointer to the shift data
203 */
204 /** Set requantization data to be used
205 *
206 *
207 * @param shifts Requantization shifts
208 * @param multipliers Requantization multipliers
209 *
210 * @return A tuple with the pointers to the shift and multiplier data respectively
211 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100212 std::tuple<bool, const int32_t *, const int32_t *, const int32_t *>
213 set_requantize_data(const std::vector<int32_t> &shifts, const std::vector<int32_t> &multipliers);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000214
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000215 // Inherited methods overridden:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 void run(ITensorPack &tensors) override;
217 void prepare(ITensorPack &tensors) override;
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100218 bool is_configured() const override;
219 experimental::MemoryRequirements workspace() const override;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000220 bool isVarWeightsKernel() const override
221 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100222 if (!_gemm_kernel_asm)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000223 return false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100224 const arm_compute::WeightFormat wf =
225 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format);
Ramy Elgammal91780022022-07-20 14:57:37 +0100226 return wf != arm_compute::WeightFormat::UNSPECIFIED && wf != arm_compute::WeightFormat::ANY;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000227 }
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100228
Anthony Barbiereaefd002018-07-20 17:49:35 +0100229private:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100230 enum AuxTensorIdx
231 {
232 AsmGemmWorkspace = 0,
SiCong Lic5ab4df2023-10-17 17:38:57 +0100233 PrePretransposedB, /* Transposed B (rhs) before being passed to gemm or pretranspose_B_array */
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100234 Pretranspose,
235 Count
236 };
237
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000238 /** Configure the indirect buffer
239 *
240 * @param[in] a Input tensor containing the Matrix A.
241 * @param[in] b Input tensor containing the Matrix B.
242 * @param[out] d Output tensor to store the result of matrix multiplication.
243 * @param[in] info GEMM meta-data
244 */
245 void configure_indirect(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *d, const AsmGemmInfo &info);
246 /** Prepare the indirect buffer */
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100247 void prepare_indirect_buffer(ITensorPack &tensors);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100248
SiCong Lic5ab4df2023-10-17 17:38:57 +0100249 /** Operator to transpose B before gemm or pretranspose_B_array*/
250 std::unique_ptr<CpuTranspose> _pre_pretranspose_b{nullptr};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100251 /** Assembly Gemm kernel */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100252 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{nullptr};
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000253 /** Optimised Arm® Neon™ kernel */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100254 std::unique_ptr<INEKernel> _optimised_kernel{nullptr};
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100255 /** Assembly GEMM workspace tensor info */
256 TensorInfo _workspace_info{};
SiCong Lic5ab4df2023-10-17 17:38:57 +0100257 /** Pre-pre-transposed B tensor info */
258 TensorInfo _pre_pretransposed_b_info{};
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100259 /** Pre-transpose tensor info */
260 TensorInfo _pretranspose_info{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100261 /** Prepared flag */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100262 bool _is_prepared{false};
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100263 /** GEMM meta-data */
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000264 AsmGemmInfo _gemm_info{};
Georgios Pinitas77d42522019-11-05 13:35:47 +0000265 /** GEMM kernel description */
266 arm_gemm::KernelDescription _kernel_info{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000267 /** Per channel quantization shifts */
268 std::vector<int32_t> _shifts{};
morgolock0bc80da2020-08-10 16:44:18 +0100269 std::vector<int32_t> right_shifts{};
270 std::vector<int32_t> left_shifts{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000271 /** Per channel quantization multipliers */
272 std::vector<int32_t> _multipliers{};
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000273 /** Indirect buffer */
274 std::unique_ptr<const TypeInput *const *, free_delete> _indirect_arg{};
275 std::unique_ptr<const TypeInput *, free_delete> _indirect_buf{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100276 std::vector<TypeInput> _indirect_pad{};
277 arm_gemm::ConvolutionParameters _cp{};
278 experimental::MemoryRequirements _aux_mem{Count};
279 bool _B_pretranspose_required{false};
280 bool _is_b_constant{true};
281 bool _is_c_constant{true};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100282};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100283
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100284template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000285std::tuple<bool, const int32_t *, const int32_t *, const int32_t *>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100286Fallback<TypeInput, TypeOutput, OutputStage>::set_requantize_data(const std::vector<int32_t> &shifts,
287 const std::vector<int32_t> &multipliers)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000288{
morgolock0bc80da2020-08-10 16:44:18 +0100289 _multipliers = multipliers;
290 _shifts = shifts;
291 bool need_left = false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100292 for (const auto s : _shifts)
morgolock0bc80da2020-08-10 16:44:18 +0100293 {
294 left_shifts.push_back(std::max(-s, int32_t(0)));
295 right_shifts.push_back(std::min(-s, int32_t(0)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100296 if (s < 0 && !need_left)
morgolock0bc80da2020-08-10 16:44:18 +0100297 {
298 need_left = true;
299 }
300 }
301 return std::make_tuple(need_left, left_shifts.data(), right_shifts.data(), _multipliers.data());
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000302}
303
304template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100305void Fallback<TypeInput, TypeOutput, OutputStage>::prepare_indirect_buffer(ITensorPack &tensors)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000306{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100307 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
308 const TypeInput *A_ptr = reinterpret_cast<TypeInput *>(a->buffer());
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000309 const int multis = 1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100310 const int batches = a->info()->tensor_shape().total_size_upper(3);
311 const size_t stride_A = a->info()->strides_in_bytes().y() / sizeof(TypeInput);
312 const size_t batch_stride_A = a->info()->strides_in_bytes()[3] / sizeof(TypeInput);
313 const size_t multi_stride_A = a->info()->strides_in_bytes()[4] / sizeof(TypeInput);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000314
315 const size_t output_hw = _cp.output_height * _cp.output_width;
316 const int batch_size = _cp.kernel_height * _cp.kernel_width * output_hw * sizeof(TypeInput);
317 const size_t batch_stride = batch_size / sizeof(TypeInput);
318 const int multi_size = batch_size * batches;
319 const size_t multi_stride = multi_size / sizeof(TypeInput);
320
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100321 for (int64_t m = 0; m < multis; m++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000322 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100323 for (int64_t b = 0; b < batches; b++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000324 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100325 for (int64_t output_y = 0; output_y < _cp.output_height; output_y++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000326 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100327 for (int64_t output_x = 0; output_x < _cp.output_width; output_x++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000328 {
329 int64_t output_xy = (output_y * _cp.output_width) + output_x;
330
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100331 for (int64_t kernel_y = 0; kernel_y < _cp.kernel_height; kernel_y++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000332 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100333 for (int64_t kernel_x = 0; kernel_x < _cp.kernel_width; kernel_x++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000334 {
335 int64_t input_x = (output_x * _cp.output_stride_w) + kernel_x - _cp.padding_left;
336 int64_t input_y = (output_y * _cp.output_stride_h) + kernel_y - _cp.padding_top;
337 int64_t kernel_xy = (kernel_y * _cp.kernel_width) + kernel_x;
338 int64_t input_xy = (input_y * _cp.input_width) + input_x;
339
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100340 if (input_x < 0 || input_x >= _cp.input_width || input_y < 0 || input_y >= _cp.input_height)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000341 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100342 _indirect_buf
343 .get()[m * multi_stride + b * batch_stride + kernel_xy * output_hw + output_xy] =
344 _indirect_pad.data();
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000345 }
346 else
347 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100348 _indirect_buf
349 .get()[m * multi_stride + b * batch_stride + kernel_xy * output_hw + output_xy] =
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000350 A_ptr + (m * multi_stride_A + b * batch_stride_A + input_xy * stride_A);
351 }
352 }
353 }
354 }
355 }
356 }
357 }
358}
359
360template <typename TypeInput, typename TypeOutput, class OutputStage>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100361void Fallback<TypeInput, TypeOutput, OutputStage>::configure_indirect(const ITensorInfo *a,
362 const ITensorInfo *b,
363 const ITensorInfo *d,
364 const AsmGemmInfo &info)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000365{
366 ARM_COMPUTE_ERROR_ON(!(info.method == AsmConvMethod::Conv || info.method == AsmConvMethod::Indirect));
367
368 float zeropad = 0.f;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100369 if (is_data_type_quantized(a->data_type()))
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000370 {
371 zeropad = a->quantization_info().uniform().offset;
372 }
373
374 const int64_t input_width = static_cast<int64_t>(a->tensor_shape()[1]);
375 const int64_t input_height = static_cast<int64_t>(a->tensor_shape()[2]);
376 const int64_t input_channels = static_cast<int64_t>(a->tensor_shape()[0]);
377 const int64_t kernel_width = static_cast<int64_t>(b->tensor_shape()[2]);
378 const int64_t kernel_height = static_cast<int64_t>(b->tensor_shape()[3]);
379 const int64_t output_width = static_cast<int64_t>(d->tensor_shape()[1]);
380 const int64_t output_height = static_cast<int64_t>(d->tensor_shape()[2]);
381
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100382 _cp = {input_width,
383 input_height,
384 input_channels,
385 kernel_width,
386 kernel_height,
387 output_width,
388 output_height,
389 info.ps_info.stride().first,
390 info.ps_info.stride().second,
391 info.padding_top,
392 info.padding_left,
393 zeropad};
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000394
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100395 if (info.method == AsmConvMethod::Conv)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000396 {
397 _gemm_kernel_asm->set_convolution_parameters(_cp);
398 }
399
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100400 if (info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000401 {
402 const unsigned int multis = 1;
403 const unsigned int batches = a->tensor_shape().total_size_upper(3);
404 const unsigned int kernel_hw = _cp.kernel_width * _cp.kernel_height;
405 const unsigned int output_hw = _cp.output_width * _cp.output_height;
406
407 using TypeInputPtr = TypeInput *;
408 const int batch_size = kernel_hw * output_hw * sizeof(TypeInputPtr);
409 const size_t batch_stride = batch_size / sizeof(TypeInputPtr);
410 const int multi_size = batch_size * batches;
411 const size_t multi_stride = multi_size / sizeof(TypeInputPtr);
412
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100413 _indirect_buf = std::unique_ptr<const TypeInput *, free_delete>(
414 reinterpret_cast<const TypeInput **>(malloc(multi_size * multis)));
415 _indirect_arg = std::unique_ptr<const TypeInput *const *, free_delete>(
416 reinterpret_cast<const TypeInput *const **>(malloc(sizeof(TypeInput **) * kernel_hw * multis * batches)));
Sang-Hoon Park8d5337e2021-01-15 14:36:25 +0000417 _indirect_pad = std::vector<TypeInput>(_cp.input_channels, TypeInput(zeropad));
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000418
419 // Set indirect argument
420 int64_t pos = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100421 for (int64_t m = 0; m < multis; m++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000422 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100423 for (int64_t b = 0; b < batches; b++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000424 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100425 for (int64_t kernel_xy = 0; kernel_xy < kernel_hw; kernel_xy++)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000426 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100427 (_indirect_arg.get())[pos++] =
428 _indirect_buf.get() + m * multi_stride + b * batch_stride + kernel_xy * output_hw;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000429 }
430 }
431 }
432
433 _gemm_kernel_asm->set_indirect_parameters(a->tensor_shape()[0], _indirect_arg.get());
434 }
435}
436
437template <typename TypeInput, typename TypeOutput, class OutputStage>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100438void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensorInfo *a,
439 const ITensorInfo *b,
440 const ITensorInfo *c,
441 ITensorInfo *d,
442 arm_gemm::GemmArgs args,
443 const AsmGemmInfo &gemm_info,
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100444 const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100445{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100446 ARM_COMPUTE_UNUSED(c);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100447
448 _is_b_constant = b->are_values_constant();
449 _is_c_constant = c ? c->are_values_constant() : true;
450
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100451 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100452 if (_gemm_kernel_asm == nullptr)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100453 {
454 //configuration not supported: Leave function unconfigured:
455 return;
456 }
457
Francesco.Petrogalli@arm.com193cad32022-03-07 13:39:21 +0000458 arm_gemm::GemmConfig gemm_cfg = _gemm_kernel_asm->get_config();
459
Anthony Barbier71d9b572018-07-06 17:05:59 +0100460 // arm_compute wrapper for the Gemm object (see above)
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100461 auto acl_gemm_wrapper = std::make_unique<kernel::CpuGemmAssemblyWrapperKernel<TypeInput, TypeOutput>>();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100462 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000463 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100464 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
465 const unsigned int alignment = 4096;
466 _workspace_info = TensorInfo(TensorShape(workspace_size), 1, DataType::U8);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100467 _aux_mem[AsmGemmWorkspace] =
468 MemoryInfo(offset_int_vec(AsmGemmWorkspace), MemoryLifetime::Temporary, workspace_size, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100469
470 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
471 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
472 {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100473 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100474 if (window_size < static_cast<unsigned int>(args._maxthreads))
Anthony Barbier71d9b572018-07-06 17:05:59 +0100475 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100476 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100477 }
478 }
479
480 _optimised_kernel = std::move(acl_gemm_wrapper);
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100481 _gemm_info = gemm_info;
SiCong Lic5ab4df2023-10-17 17:38:57 +0100482 // Check if we need to pre-pretranspose B. Fixed format kernels need no pre-pretranspose.
483 const bool run_pre_pretranspose_b = _gemm_info.transpose_b && !isVarWeightsKernel();
484 if (run_pre_pretranspose_b)
485 {
486 _pre_pretranspose_b = std::make_unique<CpuTranspose>();
487 _pre_pretranspose_b->configure(b, &_pre_pretransposed_b_info);
488 MemoryLifetime lifetime;
489 if (_is_b_constant)
490 {
491 if (_gemm_kernel_asm->B_pretranspose_required())
492 {
493 // PrePretransposedB tensor is only used in prepare(), but is then succeeded by Pretranspose
494 // So PrePretransposedB can be freed inside prepare()
495 lifetime = MemoryLifetime::Prepare;
496 }
497 else
498 {
499 // PrePretransposedB tensor is only used in prepare(), but is the final transformation of B
500 // So PrePretransposedB needs to persist beyond prepare()
501 lifetime = MemoryLifetime::Persistent;
502 }
503 }
504 else
505 {
506 // PrePretransposedB tensor is always used in run() and doesn't need to persist
507 lifetime = MemoryLifetime::Temporary;
508 }
509 // Forcing 128-byte alignment (required by 32-bit kernels)
510 const unsigned int alignment = 128;
511 _aux_mem[PrePretransposedB] =
512 MemoryInfo(offset_int_vec(PrePretransposedB), lifetime, _pre_pretransposed_b_info.total_size(), alignment);
513 }
514
Anthony Barbier71d9b572018-07-06 17:05:59 +0100515 // Check for pre-transposed support
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100516 if (_gemm_kernel_asm->B_pretranspose_required())
Anthony Barbier71d9b572018-07-06 17:05:59 +0100517 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100518 // Fixed format kernels need no pretranspose.
519 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
520 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
Anthony Barbier71d9b572018-07-06 17:05:59 +0100521 // Forcing 128-byte alignment (required by 32-bit kernels)
522 const unsigned int alignment = 128;
523 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100524 _pretranspose_info = TensorInfo(TensorShape(B_pretranspose_size), 1, DataType::U8);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100525 _aux_mem[Pretranspose] =
526 MemoryInfo(offset_int_vec(Pretranspose), MemoryLifetime::Persistent, B_pretranspose_size, alignment);
527 _B_pretranspose_required = true;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100528 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000529
530 // Handle indirect GEMM convolution
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100531 if (gemm_info.method == AsmConvMethod::Conv || gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000532 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100533 configure_indirect(a, b, d, gemm_info);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000534 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100535}
536
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100537template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100538void Fallback<TypeInput, TypeOutput, OutputStage>::prepare(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100539{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100540 if (!_is_prepared)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100541 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100542 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
543 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
SiCong Lid4650e92023-11-14 15:17:10 +0000544 ARM_COMPUTE_ERROR_ON_NULLPTR(b);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100545
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100546 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100547 if (c && c->info()->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100548 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100549 _gemm_kernel_asm->set_quantized_bias(
550 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100551 }
SiCong Lic5ab4df2023-10-17 17:38:57 +0100552 const ITensor *b_to_use = b;
553 // Pre-pretranspose B if required
554 const bool run_pre_pretranspose_b = _gemm_info.transpose_b && !isVarWeightsKernel();
555 CpuAuxTensorHandler pre_pretransposed_b(
556 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
557 /*pack_inject: no need to inject into tensors*/
558 false,
559 /*bypass_alloc: no need to allocate if pre-pretranspose B is not required as this handle will not be used*/
560 !run_pre_pretranspose_b);
561 if (run_pre_pretranspose_b)
562 {
563 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
564 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
565 _pre_pretranspose_b->run(pre_pretranspose_pack);
566 b_to_use = pre_pretransposed_b.get();
567 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100568
Anthony Barbier71d9b572018-07-06 17:05:59 +0100569 // Pretranspose B if required
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100570 if (_gemm_kernel_asm->B_pretranspose_required())
Anthony Barbier71d9b572018-07-06 17:05:59 +0100571 {
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000572 // Fixed format kernels need no pretranspose.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100573 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
574 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
SiCong Lic5ab4df2023-10-17 17:38:57 +0100575 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
576 const auto in1_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
577 b_to_use->info()->offset_first_element_in_bytes());
578 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 +0100579
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100580 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, false);
581 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000582 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(_gemm_kernel_asm.get(), pretranspose.get(),
583 in1_ptr, ldb, multi_stride_b,
584 NEScheduler::get().num_threads());
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100585
586 b->mark_as_unused();
SiCong Lic5ab4df2023-10-17 17:38:57 +0100587 // Note that we don't need to mark b_to_use as unused, as if it's been assigned to pre_pretransposed_b, its memory will be auto-managed by the handler
Anthony Barbier71d9b572018-07-06 17:05:59 +0100588 }
589
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100590 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000591 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100592 prepare_indirect_buffer(tensors);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000593 }
594
Anthony Barbier71d9b572018-07-06 17:05:59 +0100595 _is_prepared = true;
596 }
597}
598
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100599template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100600bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100601{
602 return _optimised_kernel != nullptr;
603}
604
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100605template <typename TypeInput, typename TypeOutput, class OutputStage>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100606experimental::MemoryRequirements Fallback<TypeInput, TypeOutput, OutputStage>::workspace() const
607{
608 return _aux_mem;
609}
610
611template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100612void Fallback<TypeInput, TypeOutput, OutputStage>::run(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100613{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100614 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
615 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
616 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
617 auto d = tensors.get_tensor(TensorType::ACL_DST);
SiCong Lid4650e92023-11-14 15:17:10 +0000618 ARM_COMPUTE_ERROR_ON_NULLPTR(a, d);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100619
Jonathan Deakin464ed202023-01-12 11:41:14 +0000620 int lda = a->info()->strides_in_bytes().y() / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100621 int ldb = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000622 const int ldd = d->info()->strides_in_bytes().y() / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100623
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000624 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100625 const size_t a_multi_idx = a_batch_idx + 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000626 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100627 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100628
Jonathan Deakin464ed202023-01-12 11:41:14 +0000629 int batch_stride_a = a->info()->strides_in_bytes()[a_batch_idx] / a->info()->element_size();
630 const int batch_stride_d = d->info()->strides_in_bytes()[d_batch_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100631
Jonathan Deakin464ed202023-01-12 11:41:14 +0000632 int multi_stride_a = a->info()->strides_in_bytes()[a_multi_idx] / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100633 int multi_stride_b = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000634 const int multi_stride_d = d->info()->strides_in_bytes()[d_multi_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100635
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100636 auto in0_ptr = reinterpret_cast<const TypeInput *>(a->buffer() + a->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100637 const TypeInput *in1_ptr = nullptr;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100638 auto out_ptr = reinterpret_cast<TypeOutput *>(d->buffer() + d->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100639
SiCong Lic5ab4df2023-10-17 17:38:57 +0100640 const ITensor *b_to_use = b;
641
642 // Pre-pretranspose B if required
643 const bool run_pre_pretranspose_b = _gemm_info.transpose_b && !isVarWeightsKernel();
644 CpuAuxTensorHandler pre_pretransposed_b(
645 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
646 false /*pack_inject: no need to inject into tensors*/,
647 !run_pre_pretranspose_b /*bypass_alloc: no need to allocate if pre-pretranspose B is not required as this handle will not be used*/);
648 if (b_to_use && !_is_b_constant && run_pre_pretranspose_b)
649 {
650 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
651 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
652 _pre_pretranspose_b->run(pre_pretranspose_pack);
653 b_to_use = pre_pretransposed_b.get();
654 }
655
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100656 // Check if B is pre-tranposed and de-reference if not
SiCong Lid4650e92023-11-14 15:17:10 +0000657 if (b_to_use && !_gemm_kernel_asm->B_is_pretransposed())
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100658 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100659 ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
660 multi_stride_b = b_to_use->info()->strides_in_bytes().z() / b_to_use->info()->element_size();
661 in1_ptr =
662 reinterpret_cast<const TypeInput *>(b_to_use->buffer() + b_to_use->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100663 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100664
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100665 // If necessary, run pretranspose every time if either weights or biases are non-constant
SiCong Lic5ab4df2023-10-17 17:38:57 +0100666 if ((b_to_use && !_is_b_constant) || (c && !_is_c_constant && c->info()->data_type() == DataType::S32))
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100667 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100668 if (c && c->info()->data_type() == DataType::S32)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100669 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100670 _gemm_kernel_asm->set_quantized_bias(
671 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100672 }
673
674 // Pretranspose B if required
SiCong Lid4650e92023-11-14 15:17:10 +0000675 if (b_to_use && _B_pretranspose_required)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100676 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100677 // Fixed format kernels need no pretranspose.
678 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
679 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
680 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
681 const auto b_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
682 b_to_use->info()->offset_first_element_in_bytes());
683 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 +0100684
685 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, true);
686 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
687
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100688 if (_is_b_constant)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100689 {
690 _gemm_kernel_asm->requantize_bias(pretranspose.get()->buffer(), b_ptr, ldb, multi_stride_b);
691 }
692 else
693 {
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000694 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(_gemm_kernel_asm.get(), pretranspose.get(),
695 b_ptr, ldb, multi_stride_b,
696 NEScheduler::get().num_threads());
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100697 }
698 }
699 }
700
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100701 const auto scheduling_hint = scheduling_hint_heuristic(_kernel_info.method, d->info()->data_type());
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000702
David Mansell9e698d52020-08-25 15:02:02 +0100703 // 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 +0100704 CpuAuxTensorHandler workspace(offset_int_vec(AsmGemmWorkspace), _workspace_info, tensors, false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100705 if (workspace.get()->buffer() != nullptr)
David Mansell9e698d52020-08-25 15:02:02 +0100706 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100707 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(workspace.get()->buffer()));
David Mansell9e698d52020-08-25 15:02:02 +0100708 const unsigned int split_dim = scheduling_hint.split_dimension();
709 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000710 unsigned int num_threads = NEScheduler::get().num_threads();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100711 if (window_size < num_threads)
David Mansell9e698d52020-08-25 15:02:02 +0100712 {
713 num_threads = window_size;
714 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100715 if (split_dim != IScheduler::split_dimensions_all)
David Mansell9e698d52020-08-25 15:02:02 +0100716 {
717 // Make sure the kernel does not expect more threads than we can actually spawn
718 const unsigned int num_iterations = _optimised_kernel.get()->window().num_iterations(split_dim);
719 num_threads = std::min(num_iterations, num_threads);
720 }
721 _gemm_kernel_asm->set_nthreads(num_threads);
722 }
723
724 // Prepare assembly kernel
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100725 prepare(tensors);
David Mansell9e698d52020-08-25 15:02:02 +0100726
David Mansell9e698d52020-08-25 15:02:02 +0100727 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000728 TypeOutput *bias = nullptr;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100729 if (c && c->info()->data_type() != DataType::S32)
David Mansell9e698d52020-08-25 15:02:02 +0100730 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100731 bias = reinterpret_cast<TypeOutput *>(c->buffer() + c->info()->offset_first_element_in_bytes());
David Mansell9e698d52020-08-25 15:02:02 +0100732 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000733
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100734 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000735 {
736 in0_ptr = nullptr;
737 lda = 0;
738 batch_stride_a = 0;
739 multi_stride_a = 0;
740 }
741
David Mansell9e698d52020-08-25 15:02:02 +0100742 // Set gemm parameters
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100743 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a, in1_ptr, ldb, multi_stride_b, out_ptr,
744 ldd, batch_stride_d, multi_stride_d, bias, 0);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000745 // Schedule
Georgios Pinitas77d42522019-11-05 13:35:47 +0000746 NEScheduler::get().schedule(_optimised_kernel.get(), scheduling_hint);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100747}
748
Anthony Barbiereaefd002018-07-20 17:49:35 +0100749template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100750void create_arm_gemm(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100751 const ITensorInfo *a,
752 const ITensorInfo *b,
753 const ITensorInfo *c,
754 ITensorInfo *d,
755 arm_gemm::Activation activation,
756 const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100757{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000758 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000759 const CPUInfo &ci = NEScheduler::get().cpu_info();
760 unsigned int num_threads = NEScheduler::get().num_threads();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100761
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000762 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100763 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100764 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads,
765 info.fixed_format, info.fast_mode, &cfg);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100766
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100767 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000768 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput>>();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100769 fallback->configure(a, b, c, d, args, info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100770 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100771}
772
773template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100774void create_arm_gemm_quant(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100775 const ITensorInfo *a,
776 const ITensorInfo *b,
777 const ITensorInfo *c,
778 ITensorInfo *d,
779 arm_gemm::Activation activation,
780 const AsmGemmInfo &info)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100781{
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +0100782 ARM_COMPUTE_UNUSED(activation);
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100783 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000784 const CPUInfo &ci = NEScheduler::get().cpu_info();
785 const unsigned int num_threads = NEScheduler::get().num_threads();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100786
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000787 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100788 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100789 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads,
790 info.fixed_format, info.fast_mode, &cfg);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100791
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000792 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000793 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::Requantize32>>();
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000794
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100795 // Configure requantization info
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000796 const int32_t negation = info.negated_offsets ? 1 : -1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100797 const int32_t a_offset = -a->quantization_info().uniform().offset * negation;
798 const int32_t b_offset = -b->quantization_info().uniform().offset * negation;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000799 const GEMMLowpOutputStageInfo os_info = info.output_stage;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100800
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000801 arm_gemm::Requantize32 gemm_requant_info{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100802 if (os_info.gemmlowp_shifts.size() > 1)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000803 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100804 const auto requantize_data =
805 fallback->set_requantize_data(os_info.gemmlowp_shifts, os_info.gemmlowp_multipliers);
806 gemm_requant_info = arm_gemm::Requantize32(
807 nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset,
808 (std::get<0>(requantize_data)) ? std::get<1>(requantize_data) : nullptr, std::get<2>(requantize_data),
809 std::get<3>(requantize_data), os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000810 }
811 else
812 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100813 gemm_requant_info =
814 arm_gemm::Requantize32(nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset, -os_info.gemmlowp_shift,
815 os_info.gemmlowp_multiplier, os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000816 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100817
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000818 // Configure fallback
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100819 fallback->configure(a, b, c, d, args, info, gemm_requant_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100820 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100821}
Anthony Barbiereaefd002018-07-20 17:49:35 +0100822} //namespace
823
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100824CpuGemmAssemblyDispatch::CpuGemmAssemblyDispatch() : _arm_gemm(nullptr)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100825{
826}
827
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100828Status CpuGemmAssemblyDispatch::has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
829 const ITensorInfo *a,
830 const ITensorInfo *b,
831 const ITensorInfo *c,
832 const ITensorInfo *d,
833 const AsmGemmInfo &info)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000834{
835 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
836 ARM_COMPUTE_UNUSED(c);
837 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
838 Params p = extract_parameters(a, b, d, info);
Pablo Marquez Tello17e116e2023-12-05 15:44:50 +0000839 const CPUInfo &ci = NEScheduler::get().cpu_info();
840 unsigned int num_threads = NEScheduler::get().num_threads();
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000841 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100842 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
843 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 +0100844 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, act, num_threads,
845 info.fixed_format, info.fast_mode, &cfg);
SiCong Lic5ab4df2023-10-17 17:38:57 +0100846 // TODO: Incorporate info.transpose_b COMPMID-6595
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100847 switch (a->data_type())
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000848 {
849 case DataType::F32:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100850 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
851 !(arm_gemm::has_opt_gemm<float, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
852 "We could not find an optimized kernel for F32 input");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000853 break;
854#ifdef __aarch64__
855 case DataType::U8:
856 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100857 if (d->data_type() == DataType::S32)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000858 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100859 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
860 !(arm_gemm::has_opt_gemm<uint8_t, uint32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
861 "We could not find an optimized kernel for U8/QASYMM8 input and U32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000862 }
863 else
864 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100865 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
866 !(arm_gemm::has_opt_gemm<uint8_t, uint8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
867 "We could not find an optimized kernel for U8 input and U8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000868 }
869 break;
870 case DataType::S8:
871 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100872 if (d->data_type() == DataType::S32)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000873 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100874 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
875 !(arm_gemm::has_opt_gemm<int8_t, int32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
876 "We could not find an optimized kernel for S8/QASYMM8_SIGNED input and S32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000877 }
878 else
879 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100880 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
881 !(arm_gemm::has_opt_gemm<int8_t, int8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
882 "We could not find an optimized kernel for S8 input and S8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000883 }
884 break;
885#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100886#if defined(ARM_COMPUTE_ENABLE_BF16)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000887 case DataType::BFLOAT16:
888 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100889 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
890 !(arm_gemm::has_opt_gemm<bfloat16, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
891 "We could not find an optimized kernel for BFLOAT16 input and F32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000892 break;
893 }
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100894#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000895#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
896 case DataType::F16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100897 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
898 !(arm_gemm::has_opt_gemm<float16_t, float16_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
899 "We could not find an optimized kernel for F16 input and F16 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000900 break;
901#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
902 default:
903 ARM_COMPUTE_RETURN_ERROR_ON_MSG(true, "Usupported type. Could not find a kernel");
904 break;
905 }
Ramy Elgammal91780022022-07-20 14:57:37 +0100906 expected_weight_format = assembly_utils::map_to_arm_compute_weight_format(arm_gemm_expected_wf);
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000907
908 return Status{};
909}
910
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100911Status CpuGemmAssemblyDispatch::validate(
912 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100913{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000914 ARM_COMPUTE_UNUSED(c, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100915 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
916 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000917 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100918 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(info.reshape_b_only_on_first_run),
919 "Assembly kernel will not be executed when reshape_b_only_on_first_run is false");
Georgios Pinitas0f954eb2020-06-23 17:28:38 +0100920
Anthony Barbiereaefd002018-07-20 17:49:35 +0100921#ifndef __aarch64__
Michele Di Giorgio52556722019-12-23 16:35:12 +0000922 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->element_size() == 1, "8bit integer types only supported for aarch64");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100923#endif /* __aarch64__ */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100924 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::QASYMM8,
925 DataType::QASYMM8_SIGNED, DataType::S8, DataType::BFLOAT16,
926 DataType::F16, DataType::F32);
927 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
928 b, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::S8,
929 DataType::BFLOAT16, DataType::F16, DataType::F32);
930 if (is_data_type_quantized_per_channel(b->data_type()))
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100931 {
932 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
933 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100934 else if (is_fixed_format_fast_math(info.weight_format))
Jonathan Deakin464ed202023-01-12 11:41:14 +0000935 {
936 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(a, DataType::F32);
937 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(b, DataType::BFLOAT16);
938 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100939 else
940 {
941 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
942 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100943 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32,
944 "Only F32 output supported for F32 input");
945 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F16 && d->data_type() != DataType::F16,
946 "Only F16 output supported for F16 input");
947 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::BFLOAT16 && d->data_type() != DataType::F32,
948 "Only F32 output supported for BFLOAT16 input");
949 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 && d->data_type() != DataType::U32,
950 "Only U32 output supported for U8 input");
951 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::S8 && d->data_type() != DataType::S32,
952 "Only S32 output supported for S8 input");
953 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 &&
954 (d->data_type() != DataType::QASYMM8 && d->data_type() != DataType::S32),
Ethan Doe1fe48ca2023-03-01 23:19:26 +0000955 "Only QASYMM8/S32 output supported for QASYMM8 input");
Viet-Hoa Do246fe082023-08-16 10:29:00 +0100956 arm_compute::WeightFormat expected_weight_format = arm_compute::WeightFormat::UNSPECIFIED;
Ramy Elgammal91780022022-07-20 14:57:37 +0100957 const Status ret = CpuGemmAssemblyDispatch::has_opt_impl(expected_weight_format, a, b, c, d, info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100958 if ((bool)ret && expected_weight_format != arm_compute::WeightFormat::ANY)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000959 {
960 // Correctness check: if the format expected by the kernel is
961 // not "any", make sure that the one found matches the format
962 // intended by the caller.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100963 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
964 (expected_weight_format != info.weight_format),
965 "The format expected by the kernel does not correspond with the one requested by the user.");
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000966 }
967 return ret;
Anthony Barbiereaefd002018-07-20 17:49:35 +0100968}
969
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100970bool CpuGemmAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100971{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000972 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100973 return act.type != arm_gemm::Activation::Type::None;
974}
975
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100976void CpuGemmAssemblyDispatch::configure(
977 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100978{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100979 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000980 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100981
982 //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 +0100983 if (!CpuGemmAssemblyDispatch::validate(a, b, c, d, info))
Anthony Barbiereaefd002018-07-20 17:49:35 +0100984 {
985 return;
986 }
987
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100988 switch (a->data_type())
Anthony Barbiereaefd002018-07-20 17:49:35 +0100989 {
990 case DataType::F32:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100991 create_arm_gemm<float, float>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100992 break;
993#ifdef __aarch64__
994 case DataType::U8:
995 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100996 if (d->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100997 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100998 create_arm_gemm<uint8_t, uint32_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100999 }
1000 else
1001 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001002 create_arm_gemm_quant<uint8_t, uint8_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001003 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001004 break;
1005 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001006 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001007 if (d->data_type() == DataType::S32)
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001008 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001009 create_arm_gemm<int8_t, int32_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001010 }
1011 else
1012 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001013 create_arm_gemm_quant<int8_t, int8_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001014 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001015 break;
1016#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001017#if defined(ARM_COMPUTE_ENABLE_BF16)
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001018 case DataType::BFLOAT16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001019 create_arm_gemm<bfloat16, float>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001020 break;
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001021#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Anthony Barbiereaefd002018-07-20 17:49:35 +01001022#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1023 case DataType::F16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001024 create_arm_gemm<float16_t, float16_t>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001025 break;
1026#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1027 default:
1028 break;
1029 }
1030}
1031
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001032void CpuGemmAssemblyDispatch::prepare(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001033{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001034 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001035 _arm_gemm->prepare(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001036}
1037
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001038bool CpuGemmAssemblyDispatch::is_configured() const
Anthony Barbiereaefd002018-07-20 17:49:35 +01001039{
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001040 return _arm_gemm && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +01001041}
1042
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001043void CpuGemmAssemblyDispatch::run(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001044{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001045 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001046 _arm_gemm->run(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001047}
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001048
1049experimental::MemoryRequirements CpuGemmAssemblyDispatch::workspace() const
1050{
1051 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
1052 return _arm_gemm->workspace();
1053}
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001054} // namespace cpu
1055} // namespace arm_compute