blob: 8ff81afe5463d5a77ebc97c5ce0e29b780e69202 [file] [log] [blame]
Anthony Barbier71d9b572018-07-06 17:05:59 +01001/*
Francesco.Petrogalli@arm.com193cad32022-03-07 13:39:21 +00002 * Copyright (c) 2018-2022 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"
27#include "src/core/CPP/Validate.h"
Pablo Marquez Tello93581a52022-07-21 13:55:27 +010028#include "src/core/NEON/kernels/arm_gemm/utils.hpp"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010029#include "src/core/helpers/MemoryHelpers.h"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000030#include "src/core/utils/AssemblyUtils.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010031#include "src/cpu/kernels/assembly/CpuGemmAssemblyWrapperKernel.h"
32#include "src/cpu/kernels/assembly/arm_gemm.hpp"
33#include "src/cpu/utils/CpuAuxTensorHandler.h"
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010034
Anthony Barbiereaefd002018-07-20 17:49:35 +010035#include <arm_neon.h>
36
Anthony Barbierc8e84b52018-07-17 16:48:42 +010037namespace arm_compute
38{
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010039namespace cpu
40{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010041using namespace arm_compute::experimental;
42
Anthony Barbiereaefd002018-07-20 17:49:35 +010043namespace
Anthony Barbier71d9b572018-07-06 17:05:59 +010044{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000045struct free_delete
46{
47 void operator()(void *x)
48 {
49 free(x);
50 }
51};
52
53struct Params
54{
55 unsigned int M;
56 unsigned int N;
57 unsigned int K;
58 unsigned int batches;
59 unsigned int multis;
60 unsigned int sections;
61 bool indirect;
62};
63
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010064Params extract_parameters(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *d, const AsmGemmInfo &info)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000065{
66 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000067 Params p;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010068 p.M = d->tensor_shape().y();
69 p.K = a->tensor_shape().x();
70 p.N = d->tensor_shape().x();
Georgios Pinitas4c634e02020-12-01 02:17:19 +000071 p.batches = 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000072 p.multis = 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000073 p.sections = 1;
Georgios Pinitas4c634e02020-12-01 02:17:19 +000074 p.indirect = false;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000075
76 if(info.method == AsmConvMethod::Conv || info.method == AsmConvMethod::Indirect)
77 {
78 p.indirect = true;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010079 p.sections = b->tensor_shape()[2] * b->tensor_shape()[3];
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000080 }
81 else
82 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010083 p.multis = b->tensor_shape().z();
84 p.batches = d->tensor_shape().total_size_upper(2) / p.multis;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000085 }
86
87 // Update M in case of GEMM3D for output
88 if(info.depth_output_gemm3d != 0)
89 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010090 p.M = d->tensor_shape().y() * d->tensor_shape().z();
91 p.batches = d->tensor_shape().total_size_upper(3) / p.multis;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000092 }
93
94 return p;
95}
96
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000097IScheduler::Hints scheduling_hint_heuristic(arm_gemm::GemmMethod method, DataType data_type)
98{
99 // Schedule assembly kernel
100 const int granule_threshold = 200;
101 IScheduler::Hints scheduling_hint = IScheduler::Hints(Window::DimX);
102 if(method == arm_gemm::GemmMethod::GEMM_INTERLEAVED && data_type == DataType::F32)
103 {
104 scheduling_hint = IScheduler::Hints(Window::DimX, IScheduler::StrategyHint::DYNAMIC, granule_threshold);
105 }
106 else if(method == arm_gemm::GemmMethod::GEMM_INTERLEAVED_2D && (data_type == DataType::F32 || data_type == DataType::F16 || data_type == DataType::U8 || data_type == DataType::S8))
107 {
108 //GEMM_INTERLEAVED supports 2D parallelism, IScheduler::split_dimensions_all signals to parallelise over all window dimensions
109 scheduling_hint = IScheduler::Hints(IScheduler::split_dimensions_all, IScheduler::StrategyHint::STATIC, granule_threshold);
110 }
111 else if(method == arm_gemm::GemmMethod::QUANTIZE_WRAPPER_2D && (data_type == DataType::QASYMM8 || data_type == DataType::QASYMM8_SIGNED))
112 {
113 //special case for QASYMM8 to support 2D parallelism, scheduler here may be tweaked differently compared to FP32 case
114 scheduling_hint = IScheduler::Hints(IScheduler::split_dimensions_all, IScheduler::StrategyHint::STATIC, granule_threshold);
115 }
116
117 return scheduling_hint;
118}
119
Anthony Barbiereaefd002018-07-20 17:49:35 +0100120/** Fallback in case ACL doesn't have a function */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100121template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing>
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100122class Fallback : public CpuGemmAssemblyDispatch::IFallback
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100123{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100124public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100125 /** Destructor */
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100126 ~Fallback() = default;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100127
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000128 /** Initialise the functions's input and output.
129 *
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100130 * @param[in] a Input tensor containing the Matrix A.
131 * @param[in] b Input tensor containing the Matrix B.
132 * @param[in] c Input tensor containing the Matrix C.
133 * @param[out] d Output tensor to store the result of matrix multiplication.
134 * @param[in] args Matrix multiplication information.
135 * @param[in] gemm_info GEMM meta-data
136 * @param[in] os Output stage meta-data.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000137 */
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100138 void configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d,
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000139 arm_gemm::GemmArgs args, const AsmGemmInfo &gemm_info,
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100140 const OutputStage &os = {});
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000141
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000142 /** Set requantization shifts to be used
143 *
144 * @param[in] shifts Requantization shifts
145 *
146 * @return Pointer to the shift data
147 */
148 /** Set requantization data to be used
149 *
150 *
151 * @param shifts Requantization shifts
152 * @param multipliers Requantization multipliers
153 *
154 * @return A tuple with the pointers to the shift and multiplier data respectively
155 */
morgolock0bc80da2020-08-10 16:44:18 +0100156 std::tuple<bool, const int32_t *, const int32_t *, const int32_t *> set_requantize_data(const std::vector<int32_t> &shifts,
157 const std::vector<int32_t> &multipliers);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000158
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000159 // Inherited methods overridden:
Mohammed Suhail Munshi4b5f6ef2022-10-21 11:15:54 +0100160 void run(ITensorPack &tensors) override;
161 void prepare(ITensorPack &tensors) override;
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100162 bool is_configured() const override;
163 experimental::MemoryRequirements workspace() const override;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000164 bool isVarWeightsKernel() const override
165 {
166 if(!_gemm_kernel_asm)
167 return false;
Ramy Elgammal91780022022-07-20 14:57:37 +0100168 const arm_compute::WeightFormat wf = assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format);
169 return wf != arm_compute::WeightFormat::UNSPECIFIED && wf != arm_compute::WeightFormat::ANY;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000170 }
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100171
Anthony Barbiereaefd002018-07-20 17:49:35 +0100172private:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100173 enum AuxTensorIdx
174 {
175 AsmGemmWorkspace = 0,
176 Pretranspose,
177 Count
178 };
179
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000180 /** Configure the indirect buffer
181 *
182 * @param[in] a Input tensor containing the Matrix A.
183 * @param[in] b Input tensor containing the Matrix B.
184 * @param[out] d Output tensor to store the result of matrix multiplication.
185 * @param[in] info GEMM meta-data
186 */
187 void configure_indirect(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *d, const AsmGemmInfo &info);
188 /** Prepare the indirect buffer */
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100189 void prepare_indirect_buffer(ITensorPack &tensors);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100190
Anthony Barbiereaefd002018-07-20 17:49:35 +0100191 /** Assembly Gemm kernel */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100192 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000193 /** Optimised Arm® Neon™ kernel */
Anthony Barbiereaefd002018-07-20 17:49:35 +0100194 std::unique_ptr<INEKernel> _optimised_kernel{ nullptr };
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100195 /** Assembly GEMM workspace tensor info */
196 TensorInfo _workspace_info{};
197 /** Pre-transpose tensor info */
198 TensorInfo _pretranspose_info{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100199 /** Prepared flag */
200 bool _is_prepared{ false };
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100201 /** GEMM meta-data */
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000202 AsmGemmInfo _gemm_info{};
Georgios Pinitas77d42522019-11-05 13:35:47 +0000203 /** GEMM kernel description */
204 arm_gemm::KernelDescription _kernel_info{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000205 /** Per channel quantization shifts */
206 std::vector<int32_t> _shifts{};
morgolock0bc80da2020-08-10 16:44:18 +0100207 std::vector<int32_t> right_shifts{};
208 std::vector<int32_t> left_shifts{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000209 /** Per channel quantization multipliers */
210 std::vector<int32_t> _multipliers{};
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000211 /** Indirect buffer */
212 std::unique_ptr<const TypeInput *const *, free_delete> _indirect_arg{};
213 std::unique_ptr<const TypeInput *, free_delete> _indirect_buf{};
Mohammed Suhail Munshi4b5f6ef2022-10-21 11:15:54 +0100214 std::vector<TypeInput> _indirect_pad{};
215 arm_gemm::ConvolutionParameters _cp{};
216 experimental::MemoryRequirements _aux_mem{ Count };
217 bool _B_pretranspose_required{ false };
218 bool _is_b_constant{ true };
219 bool _is_c_constant{ true };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100220};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100221
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100222template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000223std::tuple<bool, const int32_t *, const int32_t *, const int32_t *>
224Fallback<TypeInput, TypeOutput, OutputStage>::set_requantize_data(const std::vector<int32_t> &shifts, const std::vector<int32_t> &multipliers)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000225{
morgolock0bc80da2020-08-10 16:44:18 +0100226 _multipliers = multipliers;
227 _shifts = shifts;
228 bool need_left = false;
229 for(const auto s : _shifts)
230 {
231 left_shifts.push_back(std::max(-s, int32_t(0)));
232 right_shifts.push_back(std::min(-s, int32_t(0)));
morgolockfa269bb2020-09-08 16:00:56 +0100233 if(s < 0 && !need_left)
morgolock0bc80da2020-08-10 16:44:18 +0100234 {
235 need_left = true;
236 }
237 }
238 return std::make_tuple(need_left, left_shifts.data(), right_shifts.data(), _multipliers.data());
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000239}
240
241template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100242void Fallback<TypeInput, TypeOutput, OutputStage>::prepare_indirect_buffer(ITensorPack &tensors)
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000243{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100244 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
245 const TypeInput *A_ptr = reinterpret_cast<TypeInput *>(a->buffer());
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000246 const int multis = 1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100247 const int batches = a->info()->tensor_shape().total_size_upper(3);
248 const size_t stride_A = a->info()->strides_in_bytes().y() / sizeof(TypeInput);
249 const size_t batch_stride_A = a->info()->strides_in_bytes()[3] / sizeof(TypeInput);
250 const size_t multi_stride_A = a->info()->strides_in_bytes()[4] / sizeof(TypeInput);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000251
252 const size_t output_hw = _cp.output_height * _cp.output_width;
253 const int batch_size = _cp.kernel_height * _cp.kernel_width * output_hw * sizeof(TypeInput);
254 const size_t batch_stride = batch_size / sizeof(TypeInput);
255 const int multi_size = batch_size * batches;
256 const size_t multi_stride = multi_size / sizeof(TypeInput);
257
258 for(int64_t m = 0; m < multis; m++)
259 {
260 for(int64_t b = 0; b < batches; b++)
261 {
262 for(int64_t output_y = 0; output_y < _cp.output_height; output_y++)
263 {
264 for(int64_t output_x = 0; output_x < _cp.output_width; output_x++)
265 {
266 int64_t output_xy = (output_y * _cp.output_width) + output_x;
267
268 for(int64_t kernel_y = 0; kernel_y < _cp.kernel_height; kernel_y++)
269 {
270 for(int64_t kernel_x = 0; kernel_x < _cp.kernel_width; kernel_x++)
271 {
272 int64_t input_x = (output_x * _cp.output_stride_w) + kernel_x - _cp.padding_left;
273 int64_t input_y = (output_y * _cp.output_stride_h) + kernel_y - _cp.padding_top;
274 int64_t kernel_xy = (kernel_y * _cp.kernel_width) + kernel_x;
275 int64_t input_xy = (input_y * _cp.input_width) + input_x;
276
277 if(input_x < 0 || input_x >= _cp.input_width || input_y < 0 || input_y >= _cp.input_height)
278 {
279 _indirect_buf.get()[m * multi_stride + b * batch_stride + kernel_xy * output_hw + output_xy] = _indirect_pad.data();
280 }
281 else
282 {
283 _indirect_buf.get()[m * multi_stride + b * batch_stride + kernel_xy * output_hw + output_xy] =
284 A_ptr + (m * multi_stride_A + b * batch_stride_A + input_xy * stride_A);
285 }
286 }
287 }
288 }
289 }
290 }
291 }
292}
293
294template <typename TypeInput, typename TypeOutput, class OutputStage>
295void Fallback<TypeInput, TypeOutput, OutputStage>::configure_indirect(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *d, const AsmGemmInfo &info)
296{
297 ARM_COMPUTE_ERROR_ON(!(info.method == AsmConvMethod::Conv || info.method == AsmConvMethod::Indirect));
298
299 float zeropad = 0.f;
300 if(is_data_type_quantized(a->data_type()))
301 {
302 zeropad = a->quantization_info().uniform().offset;
303 }
304
305 const int64_t input_width = static_cast<int64_t>(a->tensor_shape()[1]);
306 const int64_t input_height = static_cast<int64_t>(a->tensor_shape()[2]);
307 const int64_t input_channels = static_cast<int64_t>(a->tensor_shape()[0]);
308 const int64_t kernel_width = static_cast<int64_t>(b->tensor_shape()[2]);
309 const int64_t kernel_height = static_cast<int64_t>(b->tensor_shape()[3]);
310 const int64_t output_width = static_cast<int64_t>(d->tensor_shape()[1]);
311 const int64_t output_height = static_cast<int64_t>(d->tensor_shape()[2]);
312
313 _cp = { input_width, input_height, input_channels, kernel_width, kernel_height, output_width, output_height,
314 info.ps_info.stride().first, info.ps_info.stride().second, info.padding_top, info.padding_left, zeropad
315 };
316
317 if(info.method == AsmConvMethod::Conv)
318 {
319 _gemm_kernel_asm->set_convolution_parameters(_cp);
320 }
321
322 if(info.method == AsmConvMethod::Indirect)
323 {
324 const unsigned int multis = 1;
325 const unsigned int batches = a->tensor_shape().total_size_upper(3);
326 const unsigned int kernel_hw = _cp.kernel_width * _cp.kernel_height;
327 const unsigned int output_hw = _cp.output_width * _cp.output_height;
328
329 using TypeInputPtr = TypeInput *;
330 const int batch_size = kernel_hw * output_hw * sizeof(TypeInputPtr);
331 const size_t batch_stride = batch_size / sizeof(TypeInputPtr);
332 const int multi_size = batch_size * batches;
333 const size_t multi_stride = multi_size / sizeof(TypeInputPtr);
334
335 _indirect_buf = std::unique_ptr<const TypeInput *, free_delete>(reinterpret_cast<const TypeInput **>(malloc(multi_size * multis)));
336 _indirect_arg = std::unique_ptr<const TypeInput *const *, free_delete>(reinterpret_cast<const TypeInput *const **>(malloc(sizeof(TypeInput **) * kernel_hw * multis * batches)));
Sang-Hoon Park8d5337e2021-01-15 14:36:25 +0000337 _indirect_pad = std::vector<TypeInput>(_cp.input_channels, TypeInput(zeropad));
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000338
339 // Set indirect argument
340 int64_t pos = 0;
341 for(int64_t m = 0; m < multis; m++)
342 {
343 for(int64_t b = 0; b < batches; b++)
344 {
345 for(int64_t kernel_xy = 0; kernel_xy < kernel_hw; kernel_xy++)
346 {
347 (_indirect_arg.get())[pos++] = _indirect_buf.get() + m * multi_stride + b * batch_stride + kernel_xy * output_hw;
348 }
349 }
350 }
351
352 _gemm_kernel_asm->set_indirect_parameters(a->tensor_shape()[0], _indirect_arg.get());
353 }
354}
355
356template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100357void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d,
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000358 arm_gemm::GemmArgs args, const AsmGemmInfo &gemm_info,
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100359 const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100360{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100361 ARM_COMPUTE_UNUSED(c);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100362
363 _is_b_constant = b->are_values_constant();
364 _is_c_constant = c ? c->are_values_constant() : true;
365
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100366 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100367 if(_gemm_kernel_asm == nullptr)
368 {
369 //configuration not supported: Leave function unconfigured:
370 return;
371 }
372
Francesco.Petrogalli@arm.com193cad32022-03-07 13:39:21 +0000373 arm_gemm::GemmConfig gemm_cfg = _gemm_kernel_asm->get_config();
374
Anthony Barbier71d9b572018-07-06 17:05:59 +0100375 // arm_compute wrapper for the Gemm object (see above)
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100376 auto acl_gemm_wrapper = std::make_unique<kernel::CpuGemmAssemblyWrapperKernel<TypeInput, TypeOutput>>();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100377 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000378 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100379 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
380 const unsigned int alignment = 4096;
381 _workspace_info = TensorInfo(TensorShape(workspace_size), 1, DataType::U8);
382 _aux_mem[AsmGemmWorkspace] = MemoryInfo(offset_int_vec(AsmGemmWorkspace), MemoryLifetime::Temporary, workspace_size, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100383
384 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
385 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
386 {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100387 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000388 if(window_size < static_cast<unsigned int>(args._maxthreads))
Anthony Barbier71d9b572018-07-06 17:05:59 +0100389 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100390 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100391 }
392 }
393
394 _optimised_kernel = std::move(acl_gemm_wrapper);
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100395 _gemm_info = gemm_info;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100396 // Check for pre-transposed support
397 if(_gemm_kernel_asm->B_pretranspose_required())
398 {
399 // Forcing 128-byte alignment (required by 32-bit kernels)
400 const unsigned int alignment = 128;
401 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100402 _pretranspose_info = TensorInfo(TensorShape(B_pretranspose_size), 1, DataType::U8);
403 _aux_mem[Pretranspose] = MemoryInfo(offset_int_vec(Pretranspose), MemoryLifetime::Persistent, B_pretranspose_size, alignment);
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100404 _B_pretranspose_required = true;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100405 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000406
407 // Handle indirect GEMM convolution
408 if(gemm_info.method == AsmConvMethod::Conv || gemm_info.method == AsmConvMethod::Indirect)
409 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100410 configure_indirect(a, b, d, gemm_info);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000411 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100412}
413
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100414template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100415void Fallback<TypeInput, TypeOutput, OutputStage>::prepare(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100416{
417 if(!_is_prepared)
418 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100419 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
420 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
421
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100422 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100423 if(c && c->info()->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100424 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100425 _gemm_kernel_asm->set_quantized_bias(reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100426 }
427
Anthony Barbier71d9b572018-07-06 17:05:59 +0100428 // Pretranspose B if required
429 if(_gemm_kernel_asm->B_pretranspose_required())
430 {
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000431 // Fixed format kernels need no pretranspose.
Ramy Elgammal91780022022-07-20 14:57:37 +0100432 ARM_COMPUTE_ERROR_ON(arm_compute::is_fixed_format(assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format)));
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100433 const int ldb = b->info()->strides_in_bytes().y() / sizeof(TypeInput);
434 const auto in1_ptr = reinterpret_cast<const TypeInput *>(b->buffer() + b->info()->offset_first_element_in_bytes());
435 const int multi_stride_b = b->info()->strides_in_bytes().z() / sizeof(TypeInput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100436
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100437 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, false);
438 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
439 _gemm_kernel_asm->pretranspose_B_array(pretranspose.get()->buffer(), in1_ptr, ldb, multi_stride_b);
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100440
441 b->mark_as_unused();
Anthony Barbier71d9b572018-07-06 17:05:59 +0100442 }
443
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000444 if(_gemm_info.method == AsmConvMethod::Indirect)
445 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100446 prepare_indirect_buffer(tensors);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000447 }
448
Anthony Barbier71d9b572018-07-06 17:05:59 +0100449 _is_prepared = true;
450 }
451}
452
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100453template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100454bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100455{
456 return _optimised_kernel != nullptr;
457}
458
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100459template <typename TypeInput, typename TypeOutput, class OutputStage>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100460experimental::MemoryRequirements Fallback<TypeInput, TypeOutput, OutputStage>::workspace() const
461{
462 return _aux_mem;
463}
464
465template <typename TypeInput, typename TypeOutput, class OutputStage>
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100466void Fallback<TypeInput, TypeOutput, OutputStage>::run(ITensorPack &tensors)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100467{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100468 auto a = tensors.get_const_tensor(TensorType::ACL_SRC_0);
469 auto b = tensors.get_const_tensor(TensorType::ACL_SRC_1);
470 auto c = tensors.get_const_tensor(TensorType::ACL_SRC_2);
471 auto d = tensors.get_tensor(TensorType::ACL_DST);
472
473 int lda = a->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100474 int ldb = 0;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100475 const int ldd = d->info()->strides_in_bytes().y() / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100476
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000477 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100478 const size_t a_multi_idx = a_batch_idx + 1;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000479 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d != 0 ? 3 : 2;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100480 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100481
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100482 int batch_stride_a = a->info()->strides_in_bytes()[a_batch_idx] / sizeof(TypeInput);
483 const int batch_stride_d = d->info()->strides_in_bytes()[d_batch_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100484
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100485 int multi_stride_a = a->info()->strides_in_bytes()[a_multi_idx] / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100486 int multi_stride_b = 0;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100487 const int multi_stride_d = d->info()->strides_in_bytes()[d_multi_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100488
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100489 auto in0_ptr = reinterpret_cast<const TypeInput *>(a->buffer() + a->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100490 const TypeInput *in1_ptr = nullptr;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100491 auto out_ptr = reinterpret_cast<TypeOutput *>(d->buffer() + d->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100492
493 // Check if B is pre-tranposed and de-reference if not
494 if(!_gemm_kernel_asm->B_is_pretransposed())
495 {
Ramy Elgammal91780022022-07-20 14:57:37 +0100496 ldb = b->info()->strides_in_bytes().y() / sizeof(TypeInput);
Milos Puzovic13b623e2022-07-27 17:53:21 +0000497 multi_stride_b = b->info()->strides_in_bytes().z() / sizeof(TypeInput);
Ramy Elgammal91780022022-07-20 14:57:37 +0100498 const arm_compute::WeightFormat wf = assembly_utils::map_to_arm_compute_weight_format(_gemm_kernel_asm->get_config().weight_format);
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000499 if(is_fixed_format(wf))
500 {
501 // The 4D tensor of dimension O'HWI' created for the
502 // OHWIo<interleave_by>i<block_by> format is in reality seen
503 // as a 2D tensor at arm_gemm level, where the rows are
504 // O'/<interleave_by> and the columns are <interleave_by> *
505 // H * W * I'.
Milos Puzovic13b623e2022-07-27 17:53:21 +0000506 ITensorInfo *tensor_info = b->info();
507 const DataLayout data_layout = tensor_info->data_layout();
508 const TensorShape tensor_shape = tensor_info->tensor_shape();
509 const int tensor_height = tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)];
510 const int tensor_width = tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)];
Pablo Marquez Tello93581a52022-07-21 13:55:27 +0100511 int tensor_channels = tensor_shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)];
Milos Puzovic13b623e2022-07-27 17:53:21 +0000512 const int interleave_by = arm_compute::interleave_by(wf);
Pablo Marquez Tello93581a52022-07-21 13:55:27 +0100513 const int blocked_by = arm_compute::block_by(wf);
Milos Puzovic13b623e2022-07-27 17:53:21 +0000514 // We need to find a new stride that is distance from the data for one
515 // set of output channels to the next
516 if(ldb == tensor_channels && multi_stride_b == tensor_channels * tensor_width)
517 {
518 // In this case dimensions that are packed are height, width and channel
519 // so we need to stride it by interleave_by
Pablo Marquez Tello93581a52022-07-21 13:55:27 +0100520 if(tensor_channels % blocked_by != 0)
521 {
522 // We need to pad
523 tensor_channels = arm_gemm::iceildiv(tensor_channels, blocked_by) * blocked_by;
524 }
Milos Puzovic13b623e2022-07-27 17:53:21 +0000525 ldb = interleave_by * tensor_height * tensor_width * tensor_channels;
526 }
527 else if(multi_stride_b == 0 || (ldb == tensor_width && multi_stride_b == tensor_height * tensor_width))
528 {
529 // In this case dimension that is packed is only height
530 // so we need to stride only height by interleave_by
531 ldb = interleave_by * tensor_height;
532 }
533 else
534 {
535 // If dimensions are not packed as above error is thrown
536 // as at the moment other forms of packing are not supported
537 ARM_COMPUTE_ERROR("Unsupported packing for fixed format kernel");
538 }
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000539 }
Milos Puzovic13b623e2022-07-27 17:53:21 +0000540 in1_ptr = reinterpret_cast<const TypeInput *>(b->buffer() + b->info()->offset_first_element_in_bytes());
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100541 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100542
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100543 // If necessary, run pretranspose every time if either weights or biases are non-constant
544 if((b && !_is_b_constant) || (c && !_is_c_constant && c->info()->data_type() == DataType::S32))
545 {
546 if(c && c->info()->data_type() == DataType::S32)
547 {
548 _gemm_kernel_asm->set_quantized_bias(reinterpret_cast<const int32_t *>(c->buffer() + c->info()->offset_first_element_in_bytes()), 0);
549 }
550
551 // Pretranspose B if required
552 if(_B_pretranspose_required)
553 {
554 const int ldb = b->info()->strides_in_bytes().y() / sizeof(TypeInput);
555 const auto b_ptr = reinterpret_cast<const TypeInput *>(b->buffer() + b->info()->offset_first_element_in_bytes());
556 const int multi_stride_b = b->info()->strides_in_bytes().z() / sizeof(TypeInput);
557
558 CpuAuxTensorHandler pretranspose(offset_int_vec(Pretranspose), _pretranspose_info, tensors, true);
559 ARM_COMPUTE_ERROR_ON(pretranspose.get()->buffer() == nullptr);
560
561 if(_is_b_constant)
562 {
563 _gemm_kernel_asm->requantize_bias(pretranspose.get()->buffer(), b_ptr, ldb, multi_stride_b);
564 }
565 else
566 {
567 _gemm_kernel_asm->pretranspose_B_array(pretranspose.get()->buffer(), b_ptr, ldb, multi_stride_b);
568 }
569 }
570 }
571
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100572 const auto scheduling_hint = scheduling_hint_heuristic(_kernel_info.method, d->info()->data_type());
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000573
David Mansell9e698d52020-08-25 15:02:02 +0100574 // 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 +0100575 CpuAuxTensorHandler workspace(offset_int_vec(AsmGemmWorkspace), _workspace_info, tensors, false);
576 if(workspace.get()->buffer() != nullptr)
David Mansell9e698d52020-08-25 15:02:02 +0100577 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100578 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(workspace.get()->buffer()));
David Mansell9e698d52020-08-25 15:02:02 +0100579 const unsigned int split_dim = scheduling_hint.split_dimension();
580 const unsigned int window_size = _gemm_kernel_asm->get_window_size().total_size();
581 unsigned int num_threads = NEScheduler::get().num_threads();
582 if(window_size < num_threads)
583 {
584 num_threads = window_size;
585 }
586 if(split_dim != IScheduler::split_dimensions_all)
587 {
588 // Make sure the kernel does not expect more threads than we can actually spawn
589 const unsigned int num_iterations = _optimised_kernel.get()->window().num_iterations(split_dim);
590 num_threads = std::min(num_iterations, num_threads);
591 }
592 _gemm_kernel_asm->set_nthreads(num_threads);
593 }
594
595 // Prepare assembly kernel
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100596 prepare(tensors);
David Mansell9e698d52020-08-25 15:02:02 +0100597
David Mansell9e698d52020-08-25 15:02:02 +0100598 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000599 TypeOutput *bias = nullptr;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100600 if(c && c->info()->data_type() != DataType::S32)
David Mansell9e698d52020-08-25 15:02:02 +0100601 {
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100602 bias = reinterpret_cast<TypeOutput *>(c->buffer() + c->info()->offset_first_element_in_bytes());
David Mansell9e698d52020-08-25 15:02:02 +0100603 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000604
605 if(_gemm_info.method == AsmConvMethod::Indirect)
606 {
607 in0_ptr = nullptr;
608 lda = 0;
609 batch_stride_a = 0;
610 multi_stride_a = 0;
611 }
612
David Mansell9e698d52020-08-25 15:02:02 +0100613 // Set gemm parameters
614 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a,
615 in1_ptr, ldb, multi_stride_b,
616 out_ptr, ldd, batch_stride_d, multi_stride_d,
617 bias, 0);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000618 // Schedule
Georgios Pinitas77d42522019-11-05 13:35:47 +0000619 NEScheduler::get().schedule(_optimised_kernel.get(), scheduling_hint);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100620}
621
Anthony Barbiereaefd002018-07-20 17:49:35 +0100622template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100623void create_arm_gemm(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
624 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d,
625 arm_gemm::Activation activation, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100626{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000627 Params p = extract_parameters(a, b, d, info);
628 const CPUInfo &ci = NEScheduler::get().cpu_info();
629 unsigned int num_threads = NEScheduler::get().num_threads();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100630
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000631 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100632 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000633 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads, info.fixed_format, info.fast_mode, &cfg);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100634
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100635 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000636 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput>>();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100637 fallback->configure(a, b, c, d, args, info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100638 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100639}
640
641template <typename TypeInput, typename TypeOutput>
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100642void create_arm_gemm_quant(std::unique_ptr<CpuGemmAssemblyDispatch::IFallback> &arm_gemm,
643 const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d,
644 arm_gemm::Activation activation, const AsmGemmInfo &info)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100645{
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +0100646 ARM_COMPUTE_UNUSED(activation);
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100647 Params p = extract_parameters(a, b, d, info);
648 const CPUInfo &ci = NEScheduler::get().cpu_info();
649 const unsigned int num_threads = NEScheduler::get().num_threads();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100650
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000651 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100652 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000653 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, activation, num_threads, info.fixed_format, info.fast_mode, &cfg);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100654
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000655 // Create arm_gemm fallback
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000656 auto fallback = std::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::Requantize32>>();
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000657
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100658 // Configure requantization info
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000659 const int32_t negation = info.negated_offsets ? 1 : -1;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100660 const int32_t a_offset = -a->quantization_info().uniform().offset * negation;
661 const int32_t b_offset = -b->quantization_info().uniform().offset * negation;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000662 const GEMMLowpOutputStageInfo os_info = info.output_stage;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100663
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000664 arm_gemm::Requantize32 gemm_requant_info{};
665 if(os_info.gemmlowp_shifts.size() > 1)
666 {
667 const auto requantize_data = fallback->set_requantize_data(os_info.gemmlowp_shifts, os_info.gemmlowp_multipliers);
668 gemm_requant_info = arm_gemm::Requantize32(nullptr, 0,
669 a_offset, b_offset, os_info.gemmlowp_offset,
morgolock0bc80da2020-08-10 16:44:18 +0100670 (std::get<0>(requantize_data)) ? std::get<1>(requantize_data) : nullptr,
671 std::get<2>(requantize_data),
672 std::get<3>(requantize_data),
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000673 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
674 }
675 else
676 {
677 gemm_requant_info = arm_gemm::Requantize32(nullptr, 0,
678 a_offset, b_offset, os_info.gemmlowp_offset,
679 -os_info.gemmlowp_shift, os_info.gemmlowp_multiplier,
680 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
681 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100682
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000683 // Configure fallback
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100684 fallback->configure(a, b, c, d, args, info, gemm_requant_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100685 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100686}
Anthony Barbiereaefd002018-07-20 17:49:35 +0100687} //namespace
688
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100689CpuGemmAssemblyDispatch::CpuGemmAssemblyDispatch()
690 : _arm_gemm(nullptr)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100691{
692}
693
Ramy Elgammal91780022022-07-20 14:57:37 +0100694Status CpuGemmAssemblyDispatch::has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d,
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000695 const AsmGemmInfo &info)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000696{
697 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
698 ARM_COMPUTE_UNUSED(c);
699 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
700 Params p = extract_parameters(a, b, d, info);
701 const CPUInfo &ci = NEScheduler::get().cpu_info();
702 unsigned int num_threads = NEScheduler::get().num_threads();
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000703 arm_gemm::GemmConfig cfg;
Ramy Elgammal91780022022-07-20 14:57:37 +0100704 cfg.weight_format = assembly_utils::map_to_arm_gemm_weight_format(info.weight_format);
705 arm_gemm::WeightFormat arm_gemm_expected_wf = assembly_utils::map_to_arm_gemm_weight_format(expected_weight_format);
706 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.sections, p.batches, p.multis, p.indirect, act, num_threads, info.fixed_format, info.fast_mode, &cfg);
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000707 switch(a->data_type())
708 {
709 case DataType::F32:
Ramy Elgammal91780022022-07-20 14:57:37 +0100710 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(arm_gemm::has_opt_gemm<float, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000711 "We could not find an optimized kernel for F32 input");
712 break;
713#ifdef __aarch64__
714 case DataType::U8:
715 case DataType::QASYMM8:
716 if(d->data_type() == DataType::S32)
717 {
Ramy Elgammal91780022022-07-20 14:57:37 +0100718 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(arm_gemm::has_opt_gemm<uint8_t, uint32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
Ramy Elgammalc8cc0242022-10-05 17:05:20 +0100719 "We could not find an optimized kernel for U8/QASYMM8 input and U32 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000720 }
721 else
722 {
Ramy Elgammal91780022022-07-20 14:57:37 +0100723 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(arm_gemm::has_opt_gemm<uint8_t, uint8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000724 "We could not find an optimized kernel for U8 input and U8 output");
725 }
726 break;
727 case DataType::S8:
728 case DataType::QASYMM8_SIGNED:
729 if(d->data_type() == DataType::S32)
730 {
Ramy Elgammal91780022022-07-20 14:57:37 +0100731 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(arm_gemm::has_opt_gemm<int8_t, int32_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000732 "We could not find an optimized kernel for S8/QASYMM8_SIGNED input and S32 output");
733 }
734 else
735 {
Ramy Elgammal91780022022-07-20 14:57:37 +0100736 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(arm_gemm::has_opt_gemm<int8_t, int8_t, arm_gemm::Requantize32>(arm_gemm_expected_wf, args, {})),
Ramy Elgammalc8cc0242022-10-05 17:05:20 +0100737 "We could not find an optimized kernel for S8 input and S8 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000738 }
739 break;
740#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100741#if defined(ARM_COMPUTE_ENABLE_BF16)
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000742 case DataType::BFLOAT16:
743 {
Ramy Elgammalaa52b7d2022-07-27 10:44:04 +0100744 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(arm_gemm::has_opt_gemm<bfloat16, float, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000745 "We could not find an optimized kernel for BFLOAT16 input and F32 output");
746 break;
747 }
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100748#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000749#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
750 case DataType::F16:
Ramy Elgammal91780022022-07-20 14:57:37 +0100751 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(arm_gemm::has_opt_gemm<float16_t, float16_t, arm_gemm::Nothing>(arm_gemm_expected_wf, args, {})),
Ramy Elgammalc8cc0242022-10-05 17:05:20 +0100752 "We could not find an optimized kernel for F16 input and F16 output");
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000753 break;
754#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
755 default:
756 ARM_COMPUTE_RETURN_ERROR_ON_MSG(true, "Usupported type. Could not find a kernel");
757 break;
758 }
Ramy Elgammal91780022022-07-20 14:57:37 +0100759 expected_weight_format = assembly_utils::map_to_arm_compute_weight_format(arm_gemm_expected_wf);
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000760
761 return Status{};
762}
763
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100764Status CpuGemmAssemblyDispatch::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100765{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000766 ARM_COMPUTE_UNUSED(c, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100767 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
768 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000769 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Mohammed Suhail Munshi4b5f6ef2022-10-21 11:15:54 +0100770 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!(info.reshape_b_only_on_first_run), "Assembly kernel will not be executed when reshape_b_only_on_first_run is false");
Georgios Pinitas0f954eb2020-06-23 17:28:38 +0100771
Anthony Barbiereaefd002018-07-20 17:49:35 +0100772#ifndef __aarch64__
Michele Di Giorgio52556722019-12-23 16:35:12 +0000773 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->element_size() == 1, "8bit integer types only supported for aarch64");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100774#endif /* __aarch64__ */
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100775 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S8,
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000776 DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100777 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(b, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::S8,
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000778 DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100779 if(is_data_type_quantized_per_channel(b->data_type()))
780 {
781 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
782 }
783 else
784 {
785 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
786 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100787 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32, "Only F32 output supported for F32 input");
788 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F16 && d->data_type() != DataType::F16, "Only F16 output supported for F16 input");
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000789 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::BFLOAT16 && d->data_type() != DataType::F32, "Only F32 output supported for BFLOAT16 input");
Anthony Barbier90367492018-08-01 13:56:08 +0100790 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 && d->data_type() != DataType::U32, "Only U32 output supported for U8 input");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100791 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::S8 && d->data_type() != DataType::S32, "Only S32 output supported for S8 input");
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100792 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 && d->data_type() != DataType::QASYMM8, "Only QASYMM8 output supported for QASYMM8 input");
Ramy Elgammal91780022022-07-20 14:57:37 +0100793 arm_compute::WeightFormat expected_weight_format;
794 const Status ret = CpuGemmAssemblyDispatch::has_opt_impl(expected_weight_format, a, b, c, d, info);
795 if((bool)ret && expected_weight_format != arm_compute::WeightFormat::ANY)
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000796 {
797 // Correctness check: if the format expected by the kernel is
798 // not "any", make sure that the one found matches the format
799 // intended by the caller.
800 ARM_COMPUTE_RETURN_ERROR_ON_MSG((expected_weight_format != info.weight_format),
801 "The format expected by the kernel does not correspond with the one requested by the user.");
802 }
803 return ret;
Anthony Barbiereaefd002018-07-20 17:49:35 +0100804}
805
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100806bool CpuGemmAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100807{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000808 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100809 return act.type != arm_gemm::Activation::Type::None;
810}
811
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100812void CpuGemmAssemblyDispatch::configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d, const AsmGemmInfo &info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100813{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100814 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000815 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(info.activation_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100816
817 //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()
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100818 if(!CpuGemmAssemblyDispatch::validate(a, b, c, d, info))
Anthony Barbiereaefd002018-07-20 17:49:35 +0100819 {
820 return;
821 }
822
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100823 switch(a->data_type())
Anthony Barbiereaefd002018-07-20 17:49:35 +0100824 {
825 case DataType::F32:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100826 create_arm_gemm<float, float>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100827 break;
828#ifdef __aarch64__
829 case DataType::U8:
830 case DataType::QASYMM8:
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100831 if(d->data_type() == DataType::S32)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100832 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100833 create_arm_gemm<uint8_t, uint32_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100834 }
835 else
836 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100837 create_arm_gemm_quant<uint8_t, uint8_t>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100838 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100839 break;
840 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100841 case DataType::QASYMM8_SIGNED:
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100842 if(d->data_type() == DataType::S32)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000843 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100844 create_arm_gemm<int8_t, int32_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000845 }
846 else
847 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100848 create_arm_gemm_quant<int8_t, int8_t>(_arm_gemm, a, b, c, d, act, info);
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000849 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100850 break;
851#endif /* __aarch64__ */
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100852#if defined(ARM_COMPUTE_ENABLE_BF16)
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000853 case DataType::BFLOAT16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100854 create_arm_gemm<bfloat16, float>(_arm_gemm, a, b, c, d, act, info);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000855 break;
Pablo Marquez Tellod208f4f2022-07-19 12:19:46 +0100856#endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
Anthony Barbiereaefd002018-07-20 17:49:35 +0100857#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
858 case DataType::F16:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100859 create_arm_gemm<float16_t, float16_t>(_arm_gemm, a, b, c, d, act, info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100860 break;
861#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
862 default:
863 break;
864 }
865}
866
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100867void CpuGemmAssemblyDispatch::prepare(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100868{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100869 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100870 _arm_gemm->prepare(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100871}
872
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100873bool CpuGemmAssemblyDispatch::is_configured() const
Anthony Barbiereaefd002018-07-20 17:49:35 +0100874{
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000875 return _arm_gemm && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100876}
877
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100878void CpuGemmAssemblyDispatch::run(ITensorPack &tensors)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100879{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100880 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100881 _arm_gemm->run(tensors);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100882}
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100883
884experimental::MemoryRequirements CpuGemmAssemblyDispatch::workspace() const
885{
886 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
887 return _arm_gemm->workspace();
888}
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100889} // namespace cpu
890} // namespace arm_compute