blob: 82bd465c9970856a574d213f7aff60ae621b5ec1 [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);
544
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100545 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100546 if (c && c->info()->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100547 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100548 _gemm_kernel_asm->set_quantized_bias(
549 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100550 }
SiCong Lic5ab4df2023-10-17 17:38:57 +0100551 const ITensor *b_to_use = b;
552 // Pre-pretranspose B if required
553 const bool run_pre_pretranspose_b = _gemm_info.transpose_b && !isVarWeightsKernel();
554 CpuAuxTensorHandler pre_pretransposed_b(
555 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
556 /*pack_inject: no need to inject into tensors*/
557 false,
558 /*bypass_alloc: no need to allocate if pre-pretranspose B is not required as this handle will not be used*/
559 !run_pre_pretranspose_b);
560 if (run_pre_pretranspose_b)
561 {
562 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
563 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
564 _pre_pretranspose_b->run(pre_pretranspose_pack);
565 b_to_use = pre_pretransposed_b.get();
566 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100567
Anthony Barbier71d9b572018-07-06 17:05:59 +0100568 // Pretranspose B if required
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100569 if (_gemm_kernel_asm->B_pretranspose_required())
Anthony Barbier71d9b572018-07-06 17:05:59 +0100570 {
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000571 // Fixed format kernels need no pretranspose.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100572 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
573 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
SiCong Lic5ab4df2023-10-17 17:38:57 +0100574 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
575 const auto in1_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
576 b_to_use->info()->offset_first_element_in_bytes());
577 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 +0100578
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100579 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, false);
580 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100581 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(_gemm_kernel_asm.get(), pretranspose.get(),
582 in1_ptr, ldb, multi_stride_b,
583 NEScheduler::get().num_threads());
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100584
585 b->mark_as_unused();
SiCong Lic5ab4df2023-10-17 17:38:57 +0100586 // 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 +0100587 }
588
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100589 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000590 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100591 prepare_indirect_buffer(tensors);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000592 }
593
Anthony Barbier71d9b572018-07-06 17:05:59 +0100594 _is_prepared = true;
595 }
596}
597
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100598template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100599bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100600{
601 return _optimised_kernel != nullptr;
602}
603
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100604template <typename TypeInput, typename TypeOutput, class OutputStage>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100605experimental::MemoryRequirements Fallback<TypeInput, TypeOutput, OutputStage>::workspace() const
606{
607 return _aux_mem;
608}
609
610template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100611void Fallback<TypeInput, TypeOutput, OutputStage>::run(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100612{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100613 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
614 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
615 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
616 auto d = tensors.get_tensor(TensorType::ACL_DST);
617
Jonathan Deakin464ed202023-01-12 11:41:14 +0000618 int lda = a->info()->strides_in_bytes().y() / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100619 int ldb = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000620 const int ldd = d->info()->strides_in_bytes().y() / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100621
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000622 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100623 const size_t a_multi_idx = a_batch_idx + 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000624 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100625 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100626
Jonathan Deakin464ed202023-01-12 11:41:14 +0000627 int batch_stride_a = a->info()->strides_in_bytes()[a_batch_idx] / a->info()->element_size();
628 const int batch_stride_d = d->info()->strides_in_bytes()[d_batch_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100629
Jonathan Deakin464ed202023-01-12 11:41:14 +0000630 int multi_stride_a = a->info()->strides_in_bytes()[a_multi_idx] / a->info()->element_size();
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100631 int multi_stride_b = 0;
Jonathan Deakin464ed202023-01-12 11:41:14 +0000632 const int multi_stride_d = d->info()->strides_in_bytes()[d_multi_idx] / d->info()->element_size();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100633
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100634 auto in0_ptr = reinterpret_cast<const TypeInput *>(a->buffer() + a->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100635 const TypeInput *in1_ptr = nullptr;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100636 auto out_ptr = reinterpret_cast<TypeOutput *>(d->buffer() + d->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100637
SiCong Lic5ab4df2023-10-17 17:38:57 +0100638 const ITensor *b_to_use = b;
639
640 // Pre-pretranspose B if required
641 const bool run_pre_pretranspose_b = _gemm_info.transpose_b && !isVarWeightsKernel();
642 CpuAuxTensorHandler pre_pretransposed_b(
643 offset_int_vec(PrePretransposedB), _pre_pretransposed_b_info, tensors,
644 false /*pack_inject: no need to inject into tensors*/,
645 !run_pre_pretranspose_b /*bypass_alloc: no need to allocate if pre-pretranspose B is not required as this handle will not be used*/);
646 if (b_to_use && !_is_b_constant && run_pre_pretranspose_b)
647 {
648 ARM_COMPUTE_ERROR_ON(_pre_pretranspose_b == nullptr);
649 ITensorPack pre_pretranspose_pack{{ACL_SRC, b_to_use}, {ACL_DST, pre_pretransposed_b.get()}};
650 _pre_pretranspose_b->run(pre_pretranspose_pack);
651 b_to_use = pre_pretransposed_b.get();
652 }
653
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100654 // Check if B is pre-tranposed and de-reference if not
Anitha Raj69766d62023-11-21 11:19:50 +0000655 if (!_gemm_kernel_asm->B_is_pretransposed())
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100656 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100657 ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
658 multi_stride_b = b_to_use->info()->strides_in_bytes().z() / b_to_use->info()->element_size();
659 in1_ptr =
660 reinterpret_cast<const TypeInput *>(b_to_use->buffer() + b_to_use->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100661 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100662
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100663 // If necessary, run pretranspose every time if either weights or biases are non-constant
SiCong Lic5ab4df2023-10-17 17:38:57 +0100664 if ((b_to_use && !_is_b_constant) || (c && !_is_c_constant && c->info()->data_type() == DataType::S32))
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100665 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100666 if (c && c->info()->data_type() == DataType::S32)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100667 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100668 _gemm_kernel_asm->set_quantized_bias(
669 reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100670 }
671
672 // Pretranspose B if required
Anitha Raj69766d62023-11-21 11:19:50 +0000673 if (_B_pretranspose_required)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100674 {
SiCong Lic5ab4df2023-10-17 17:38:57 +0100675 // Fixed format kernels need no pretranspose.
676 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(
677 assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
678 const int ldb = b_to_use->info()->strides_in_bytes().y() / b_to_use->info()->element_size();
679 const auto b_ptr = reinterpret_cast<const TypeInput *>(b_to_use->buffer() +
680 b_to_use->info()->offset_first_element_in_bytes());
681 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 +0100682
683 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, true);
684 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
685
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100686 if (_is_b_constant)
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100687 {
688 _gemm_kernel_asm->requantize_bias(pretranspose.get()->buffer(), b_ptr, ldb, multi_stride_b);
689 }
690 else
691 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100692 run_parallel_pretranspose_B_array<TypeInput, TypeOutput>(_gemm_kernel_asm.get(), pretranspose.get(),
693 b_ptr, ldb, multi_stride_b,
694 NEScheduler::get().num_threads());
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100695 }
696 }
697 }
698
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100699 const auto scheduling_hint = scheduling_hint_heuristic(_kernel_info.method, d->info()->data_type());
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000700
David Mansell9e698d52020-08-25 15:02:02 +0100701 // 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 +0100702 CpuAuxTensorHandler workspace(offset_int_vec(AsmGemmWorkspace), _workspace_info, tensors, false);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100703 if (workspace.get()->buffer() != nullptr)
David Mansell9e698d52020-08-25 15:02:02 +0100704 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100705 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(workspace.get()->buffer()));
David Mansell9e698d52020-08-25 15:02:02 +0100706 const unsigned int split_dim = scheduling_hint.split_dimension();
707 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
708 unsigned int num_threads = NEScheduler::get().num_threads();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100709 if (window_size < num_threads)
David Mansell9e698d52020-08-25 15:02:02 +0100710 {
711 num_threads = window_size;
712 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100713 if (split_dim != IScheduler::split_dimensions_all)
David Mansell9e698d52020-08-25 15:02:02 +0100714 {
715 // Make sure the kernel does not expect more threads than we can actually spawn
716 const unsigned int num_iterations = _optimised_kernel.get()->window().num_iterations(split_dim);
717 num_threads = std::min(num_iterations, num_threads);
718 }
719 _gemm_kernel_asm->set_nthreads(num_threads);
720 }
721
722 // Prepare assembly kernel
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100723 prepare(tensors);
David Mansell9e698d52020-08-25 15:02:02 +0100724
David Mansell9e698d52020-08-25 15:02:02 +0100725 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000726 TypeOutput *bias = nullptr;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100727 if (c && c->info()->data_type() != DataType::S32)
David Mansell9e698d52020-08-25 15:02:02 +0100728 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100729 bias = reinterpret_cast<TypeOutput *>(c->buffer() + c->info()->offset_first_element_in_bytes());
David Mansell9e698d52020-08-25 15:02:02 +0100730 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000731
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100732 if (_gemm_info.method == AsmConvMethod::Indirect)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000733 {
734 in0_ptr = nullptr;
735 lda = 0;
736 batch_stride_a = 0;
737 multi_stride_a = 0;
738 }
739
David Mansell9e698d52020-08-25 15:02:02 +0100740 // Set gemm parameters
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100741 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a, in1_ptr, ldb, multi_stride_b, out_ptr,
742 ldd, batch_stride_d, multi_stride_d, bias, 0);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000743 // Schedule
Georgios Pinitas77d42522019-11-05 13:35:47 +0000744 NEScheduler::get().schedule(_optimised_kernel.get(), scheduling_hint);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100745}
746
Anthony Barbiereaefd002018-07-20 17:49:35 +0100747template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100748void create_arm_gemm(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100749 const ITensorInfo *a,
750 const ITensorInfo *b,
751 const ITensorInfo *c,
752 ITensorInfo *d,
753 arm_gemm::Activation activation,
754 const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100755{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000756 Params p = extract_parameters(a, b, d, info);
757 const CPUInfo &ci = NEScheduler::get().cpu_info();
758 unsigned int num_threads = NEScheduler::get().num_threads();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100759
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000760 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100761 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100762 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads,
763 info.fixed_format, info.fast_mode, &cfg);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100764
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100765 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000766 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput>>();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100767 fallback->configure(a, b, c, d, args, info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100768 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100769}
770
771template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100772void create_arm_gemm_quant(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100773 const ITensorInfo *a,
774 const ITensorInfo *b,
775 const ITensorInfo *c,
776 ITensorInfo *d,
777 arm_gemm::Activation activation,
778 const AsmGemmInfo &info)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100779{
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +0100780 ARM_COMPUTE_UNUSED(activation);
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100781 Params p = extract_parameters(a, b, d, info);
782 const CPUInfo &ci = NEScheduler::get().cpu_info();
783 const unsigned int num_threads = NEScheduler::get().num_threads();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100784
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000785 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100786 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100787 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads,
788 info.fixed_format, info.fast_mode, &cfg);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100789
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000790 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000791 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::Requantize32>>();
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000792
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100793 // Configure requantization info
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000794 const int32_t negation = info.negated_offsets ? 1 : -1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100795 const int32_t a_offset = -a->quantization_info().uniform().offset * negation;
796 const int32_t b_offset = -b->quantization_info().uniform().offset * negation;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000797 const GEMMLowpOutputStageInfo os_info = info.output_stage;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100798
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000799 arm_gemm::Requantize32 gemm_requant_info{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100800 if (os_info.gemmlowp_shifts.size() > 1)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000801 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100802 const auto requantize_data =
803 fallback->set_requantize_data(os_info.gemmlowp_shifts, os_info.gemmlowp_multipliers);
804 gemm_requant_info = arm_gemm::Requantize32(
805 nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset,
806 (std::get<0>(requantize_data)) ? std::get<1>(requantize_data) : nullptr, std::get<2>(requantize_data),
807 std::get<3>(requantize_data), os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000808 }
809 else
810 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100811 gemm_requant_info =
812 arm_gemm::Requantize32(nullptr, 0, a_offset, b_offset, os_info.gemmlowp_offset, -os_info.gemmlowp_shift,
813 os_info.gemmlowp_multiplier, os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000814 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100815
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000816 // Configure fallback
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100817 fallback->configure(a, b, c, d, args, info, gemm_requant_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100818 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100819}
Anthony Barbiereaefd002018-07-20 17:49:35 +0100820} //namespace
821
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100822CpuGemmAssemblyDispatch::CpuGemmAssemblyDispatch() : _arm_gemm(nullptr)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100823{
824}
825
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100826Status CpuGemmAssemblyDispatch::has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
827 const ITensorInfo *a,
828 const ITensorInfo *b,
829 const ITensorInfo *c,
830 const ITensorInfo *d,
831 const AsmGemmInfo &info)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000832{
833 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
834 ARM_COMPUTE_UNUSED(c);
835 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
836 Params p = extract_parameters(a, b, d, info);
837 const CPUInfo &ci = NEScheduler::get().cpu_info();
838 unsigned int num_threads = NEScheduler::get().num_threads();
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000839 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100840 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
841 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 +0100842 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, act, num_threads,
843 info.fixed_format, info.fast_mode, &cfg);
SiCong Lic5ab4df2023-10-17 17:38:57 +0100844 // TODO: Incorporate info.transpose_b COMPMID-6595
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100845 switch (a->data_type())
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000846 {
847 case DataType::F32:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100848 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
849 !(arm_gemm::has_opt_gemm<float, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
850 "We could not find an optimized kernel for F32 input");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000851 break;
852#ifdef __aarch64__
853 case DataType::U8:
854 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100855 if (d->data_type() == DataType::S32)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000856 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100857 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
858 !(arm_gemm::has_opt_gemm<uint8_t, uint32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
859 "We could not find an optimized kernel for U8/QASYMM8 input and U32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000860 }
861 else
862 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100863 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
864 !(arm_gemm::has_opt_gemm<uint8_t, uint8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
865 "We could not find an optimized kernel for U8 input and U8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000866 }
867 break;
868 case DataType::S8:
869 case DataType::QASYMM8_SIGNED:
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<int8_t, int32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
874 "We could not find an optimized kernel for S8/QASYMM8_SIGNED input and S32 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<int8_t, int8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
880 "We could not find an optimized kernel for S8 input and S8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000881 }
882 break;
883#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100884#if defined(ARM_COMPUTE_ENABLE_BF16)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000885 case DataType::BFLOAT16:
886 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100887 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
888 !(arm_gemm::has_opt_gemm<bfloat16, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
889 "We could not find an optimized kernel for BFLOAT16 input and F32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000890 break;
891 }
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100892#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000893#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
894 case DataType::F16:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100895 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
896 !(arm_gemm::has_opt_gemm<float16_t, float16_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
897 "We could not find an optimized kernel for F16 input and F16 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000898 break;
899#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
900 default:
901 ARM_COMPUTE_RETURN_ERROR_ON_MSG(true, "Usupported type. Could not find a kernel");
902 break;
903 }
Ramy Elgammal91780022022-07-20 14:57:37 +0100904 expected_weight_format = assembly_utils::map_to_arm_compute_weight_format(arm_gemm_expected_wf);
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000905
906 return Status{};
907}
908
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100909Status CpuGemmAssemblyDispatch::validate(
910 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100911{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000912 ARM_COMPUTE_UNUSED(c, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100913 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
914 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000915 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100916 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(info.reshape_b_only_on_first_run),
917 "Assembly kernel will not be executed when reshape_b_only_on_first_run is false");
Georgios Pinitas0f954eb2020-06-23 17:28:38 +0100918
Anthony Barbiereaefd002018-07-20 17:49:35 +0100919#ifndef __aarch64__
Michele Di Giorgio52556722019-12-23 16:35:12 +0000920 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->element_size() == 1, "8bit integer types only supported for aarch64");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100921#endif /* __aarch64__ */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100922 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::QASYMM8,
923 DataType::QASYMM8_SIGNED, DataType::S8, DataType::BFLOAT16,
924 DataType::F16, DataType::F32);
925 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
926 b, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::S8,
927 DataType::BFLOAT16, DataType::F16, DataType::F32);
928 if (is_data_type_quantized_per_channel(b->data_type()))
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100929 {
930 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
931 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100932 else if (is_fixed_format_fast_math(info.weight_format))
Jonathan Deakin464ed202023-01-12 11:41:14 +0000933 {
934 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(a, DataType::F32);
935 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(b, DataType::BFLOAT16);
936 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100937 else
938 {
939 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
940 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100941 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32,
942 "Only F32 output supported for F32 input");
943 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F16 && d->data_type() != DataType::F16,
944 "Only F16 output supported for F16 input");
945 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::BFLOAT16 && d->data_type() != DataType::F32,
946 "Only F32 output supported for BFLOAT16 input");
947 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 && d->data_type() != DataType::U32,
948 "Only U32 output supported for U8 input");
949 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::S8 && d->data_type() != DataType::S32,
950 "Only S32 output supported for S8 input");
951 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 &&
952 (d->data_type() != DataType::QASYMM8 && d->data_type() != DataType::S32),
Ethan Doe1fe48ca2023-03-01 23:19:26 +0000953 "Only QASYMM8/S32 output supported for QASYMM8 input");
Viet-Hoa Do246fe082023-08-16 10:29:00 +0100954 arm_compute::WeightFormat expected_weight_format = arm_compute::WeightFormat::UNSPECIFIED;
Ramy Elgammal91780022022-07-20 14:57:37 +0100955 const Status ret = CpuGemmAssemblyDispatch::has_opt_impl(expected_weight_format, a, b, c, d, info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100956 if ((bool)ret && expected_weight_format != arm_compute::WeightFormat::ANY)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000957 {
958 // Correctness check: if the format expected by the kernel is
959 // not "any", make sure that the one found matches the format
960 // intended by the caller.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100961 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
962 (expected_weight_format != info.weight_format),
963 "The format expected by the kernel does not correspond with the one requested by the user.");
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000964 }
965 return ret;
Anthony Barbiereaefd002018-07-20 17:49:35 +0100966}
967
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100968bool CpuGemmAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100969{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000970 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100971 return act.type != arm_gemm::Activation::Type::None;
972}
973
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100974void CpuGemmAssemblyDispatch::configure(
975 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100976{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100977 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000978 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100979
980 //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 +0100981 if (!CpuGemmAssemblyDispatch::validate(a, b, c, d, info))
Anthony Barbiereaefd002018-07-20 17:49:35 +0100982 {
983 return;
984 }
985
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100986 switch (a->data_type())
Anthony Barbiereaefd002018-07-20 17:49:35 +0100987 {
988 case DataType::F32:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100989 create_arm_gemm<float, float>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100990 break;
991#ifdef __aarch64__
992 case DataType::U8:
993 case DataType::QASYMM8:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100994 if (d->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100995 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100996 create_arm_gemm<uint8_t, uint32_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100997 }
998 else
999 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001000 create_arm_gemm_quant<uint8_t, uint8_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001001 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001002 break;
1003 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001004 case DataType::QASYMM8_SIGNED:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001005 if (d->data_type() == DataType::S32)
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001006 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001007 create_arm_gemm<int8_t, int32_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001008 }
1009 else
1010 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001011 create_arm_gemm_quant<int8_t, int8_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +00001012 }
Anthony Barbiereaefd002018-07-20 17:49:35 +01001013 break;
1014#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001015#if defined(ARM_COMPUTE_ENABLE_BF16)
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001016 case DataType::BFLOAT16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001017 create_arm_gemm<bfloat16, float>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00001018 break;
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +01001019#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Anthony Barbiereaefd002018-07-20 17:49:35 +01001020#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1021 case DataType::F16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001022 create_arm_gemm<float16_t, float16_t>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001023 break;
1024#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
1025 default:
1026 break;
1027 }
1028}
1029
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001030void CpuGemmAssemblyDispatch::prepare(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001031{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001032 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001033 _arm_gemm->prepare(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001034}
1035
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001036bool CpuGemmAssemblyDispatch::is_configured() const
Anthony Barbiereaefd002018-07-20 17:49:35 +01001037{
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001038 return _arm_gemm && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +01001039}
1040
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001041void CpuGemmAssemblyDispatch::run(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +01001042{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +01001043 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +01001044 _arm_gemm->run(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +01001045}
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +01001046
1047experimental::MemoryRequirements CpuGemmAssemblyDispatch::workspace() const
1048{
1049 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
1050 return _arm_gemm->workspace();
1051}
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01001052} // namespace cpu
1053} // namespace arm_compute