blob: 1fcac58e105408e9d802c315d736507d4ead43d2 [file] [log] [blame]
Anthony Barbier71d9b572018-07-06 17:05:59 +01001/*
Michalis Spyrou71ac9032019-11-14 14:31:44 +00002 * Copyright (c) 2018-2020 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 */
24#include "arm_compute/runtime/NEON/functions/NEGEMMAssemblyDispatch.h"
25
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010026#include "src/core/NEON/kernels/assembly/arm_gemm.hpp"
27
Anthony Barbiereaefd002018-07-20 17:49:35 +010028#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbierc8e84b52018-07-17 16:48:42 +010030#include "arm_compute/runtime/NEON/functions/NESimpleAssemblyFunction.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010031
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +010032#include "src/core/NEON/kernels/assembly/NEGEMMAssemblyWrapperKernel.h"
33
Anthony Barbiereaefd002018-07-20 17:49:35 +010034#include <arm_neon.h>
35
Anthony Barbierc8e84b52018-07-17 16:48:42 +010036namespace arm_compute
37{
Anthony Barbiereaefd002018-07-20 17:49:35 +010038namespace
Anthony Barbier71d9b572018-07-06 17:05:59 +010039{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010040arm_gemm::Activation map_to_arm_gemm_activation(const ActivationLayerInfo &act)
Anthony Barbiereaefd002018-07-20 17:49:35 +010041{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010042 arm_gemm::Activation gemm_act;
43
44 // Early exit in case lower bound is other than 0, as it's not yet supported
45 if(act.b() != 0.f)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010046 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010047 return gemm_act;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010048 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010049
50 switch(act.activation())
51 {
52 case ActivationLayerInfo::ActivationFunction::RELU:
53 gemm_act.type = arm_gemm::Activation::Type::ReLU;
54 break;
55 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
56 gemm_act.type = arm_gemm::Activation::Type::BoundedReLU;
57 gemm_act.param1 = act.a();
58 gemm_act.param2 = 0.f;
59 break;
60 case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
61 gemm_act.type = arm_gemm::Activation::Type::BoundedReLU;
62 gemm_act.param1 = act.a();
63 gemm_act.param2 = act.b();
64 break;
65 default:
66 gemm_act.type = arm_gemm::Activation::Type::None;
67 }
68
69 return gemm_act;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010070}
71
Michalis Spyrou1a569a32019-09-10 17:20:34 +010072template <typename TypeInput, typename TypeOutput>
73class FallbackTransform : public ITransformWeights
74{
75public:
Michalis Spyrou5cb49dc2019-12-03 13:42:25 +000076 FallbackTransform() noexcept {};
77 /** Prevent instances of this class from being copied (As this class contains pointers) */
78 FallbackTransform(const FallbackTransform &) = delete;
79 /** Default move constructor */
80 FallbackTransform(FallbackTransform &&) = default;
81 /** Prevent instances of this class from being copied (As this class contains pointers) */
82 FallbackTransform &operator=(const FallbackTransform &) = delete;
83 /** Default move assignment operator */
84 FallbackTransform &operator=(FallbackTransform &&) = default;
85 void run() override
Michalis Spyrou1a569a32019-09-10 17:20:34 +010086 {
87 _output.allocator()->allocate();
88 ARM_COMPUTE_ERROR_ON(_output.buffer() == nullptr);
89 _gemm_kernel_asm->pretranspose_B_array(_output.buffer(), _in1_ptr, _ldb, _multi_stride_b);
90 _reshape_run = true;
91 }
92
93 void release() override
94 {
95 _output.allocator()->free();
96 }
97
98 ITensor *get_weights() override
99 {
100 return &_output;
101 }
102
103 uint32_t uid() override
104 {
105 uint32_t id = (_B_pretranspose_size | 0x80000000);
106 return id;
107 }
108
109 void configure(size_t B_pretranspose_size, unsigned int alignment)
110 {
111 _output.allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
112 _B_pretranspose_size = B_pretranspose_size;
113 }
114
115 void set_pretranspose(ITensor *tensor)
116 {
117 if(!_reshape_run)
118 {
119 _gemm_kernel_asm->set_pretransposed_B_data(tensor->buffer());
120 }
121 }
122
123 void set_args(const int ldb, const TypeInput *in1_ptr, const int multi_stride_b, std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> gemm_kernel_asm)
124 {
125 _ldb = ldb;
126 _in1_ptr = in1_ptr;
127 _multi_stride_b = multi_stride_b;
128 _gemm_kernel_asm = gemm_kernel_asm;
129 }
130
131private:
132 Tensor _output{};
133 int _ldb{};
134 const TypeInput *_in1_ptr{};
135 int _multi_stride_b{};
136 size_t _B_pretranspose_size{};
137 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
138};
139
Anthony Barbiereaefd002018-07-20 17:49:35 +0100140/** Fallback in case ACL doesn't have a function */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100141template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing>
Anthony Barbiereaefd002018-07-20 17:49:35 +0100142class Fallback : public NEGEMMAssemblyDispatch::IFallback
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100143{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100144public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100145 /** Destructor */
146 ~Fallback()
147 {
148 // Release memory if we have allocated the memory ourselves
149 if(_pretranspose && !(_weights_manager && _weights_manager->are_weights_managed(_b)))
150 {
151 delete _pretranspose;
152 }
153 }
154
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000155 /** Initialise the functions's input and output.
156 *
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100157 * @param[in] a Input tensor containing the Matrix A.
158 * @param[in] b Input tensor containing the Matrix B.
159 * @param[in] c Input tensor containing the Matrix C.
160 * @param[out] d Output tensor to store the result of matrix multiplication.
161 * @param[in] args Matrix multiplication information.
162 * @param[in] gemm_info GEMM meta-data
163 * @param[in] memory_group Memory group to be used by the function.
164 * @param[in] weights_manager Weights manager to be used by the function.
165 * @param[in] os Output stage meta-data.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000166 */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100167 void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100168 arm_gemm::GemmArgs args, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100169 MemoryGroup &memory_group, IWeightsManager *weights_manager, const OutputStage &os = {});
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000170
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000171 /** Set requantization shifts to be used
172 *
173 * @param[in] shifts Requantization shifts
174 *
175 * @return Pointer to the shift data
176 */
177 /** Set requantization data to be used
178 *
179 *
180 * @param shifts Requantization shifts
181 * @param multipliers Requantization multipliers
182 *
183 * @return A tuple with the pointers to the shift and multiplier data respectively
184 */
185 std::tuple<const int32_t *, const int32_t *> set_requantize_data(const std::vector<int32_t> &shifts,
186 const std::vector<int32_t> &multipliers);
187
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000188 // Inherited methods overridden:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100189 void run() override;
190 void prepare() override;
191 bool is_configured() const override;
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100192
Anthony Barbiereaefd002018-07-20 17:49:35 +0100193private:
194 /** Allocate a workspace tensor.
195 *
196 * @param[in] workspace_size Size to allocate.
197 * @param[in] memory_group Tensor memory group.
198 * @param[in] alignment Workspace memory alignment.
199 */
Anthony Barbier20394d52018-08-02 11:29:09 +0100200 void allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100201
Anthony Barbiereaefd002018-07-20 17:49:35 +0100202 /** Assembly Gemm kernel */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100203 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100204 /** Optimised NEON kernel */
205 std::unique_ptr<INEKernel> _optimised_kernel{ nullptr };
206 /** Input A */
207 const ITensor *_a
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100208 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100209 nullptr
210 };
211 /** Input B */
212 const ITensor *_b
213 {
214 nullptr
215 };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100216 const ITensor *_c
217 {
218 nullptr
219 };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100220 /** Output */
221 ITensor *_d{ nullptr };
222 /** GEMM workspace */
223 Tensor _workspace{};
224 /** Pre-transpose tensor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100225 ITensor *_pretranspose{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100226 /** Prepared flag */
227 bool _is_prepared{ false };
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100228 /** GEMM meta-data */
229 GEMMInfo _gemm_info{};
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100230 /** Weights manager */
231 IWeightsManager *_weights_manager{ nullptr };
232 /** Weights transform object */
233 FallbackTransform<TypeInput, TypeOutput> _weights_transform{};
Georgios Pinitas77d42522019-11-05 13:35:47 +0000234 /** GEMM kernel description */
235 arm_gemm::KernelDescription _kernel_info{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000236 /** Per channel quantization shifts */
237 std::vector<int32_t> _shifts{};
238 /** Per channel quantization multipliers */
239 std::vector<int32_t> _multipliers{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100240};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100241
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100242template <typename TypeInput, typename TypeOutput, class OutputStage>
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000243std::tuple<const int32_t *, const int32_t *> Fallback<TypeInput, TypeOutput, OutputStage>::set_requantize_data(const std::vector<int32_t> &shifts,
244 const std::vector<int32_t> &multipliers)
245{
246 _multipliers = multipliers;
247 _shifts = shifts;
Georgios Pinitas47fd61f2020-01-29 12:02:20 +0000248 std::transform(_shifts.begin(), _shifts.end(), _shifts.begin(), std::negate<int32_t>());
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000249 return std::make_tuple(_shifts.data(), _multipliers.data());
250}
251
252template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100253void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100254 arm_gemm::GemmArgs args, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100255 MemoryGroup &memory_group, IWeightsManager *weights_manager, const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100256{
Georgios Pinitas77d42522019-11-05 13:35:47 +0000257 arm_gemm::GemmConfig gemm_cfg;
258 _kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput, OutputStage>(args, os);
259 _weights_manager = weights_manager;
260 if(_kernel_info.method != arm_gemm::GemmMethod::GEMV_BATCHED)
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000261 {
Georgios Pinitas77d42522019-11-05 13:35:47 +0000262 gemm_cfg.filter = _kernel_info.name;
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000263 args._cfg = &gemm_cfg;
264 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100265 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100266 if(_gemm_kernel_asm == nullptr)
267 {
268 //configuration not supported: Leave function unconfigured:
269 return;
270 }
271
272 // arm_compute wrapper for the Gemm object (see above)
273 std::unique_ptr<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>> acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>>();
274 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000275 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100276 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
277 if(workspace_size > 0)
278 {
279 // Allocate workspace
280 const unsigned int alignment = 4096;
Anthony Barbier20394d52018-08-02 11:29:09 +0100281 allocate_workspace(workspace_size, memory_group, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100282 }
283
284 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
285 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
286 {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000287 const unsigned int window_size = get_total_window_size(*_gemm_kernel_asm);
288 if(window_size < static_cast<unsigned int>(args._maxthreads))
Anthony Barbier71d9b572018-07-06 17:05:59 +0100289 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100290 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100291 }
292 }
293
294 _optimised_kernel = std::move(acl_gemm_wrapper);
295 _a = a;
296 _b = b;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100297 _c = c;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100298 _d = d;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100299 _gemm_info = gemm_info;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100300 // Check for pre-transposed support
301 if(_gemm_kernel_asm->B_pretranspose_required())
302 {
303 // Forcing 128-byte alignment (required by 32-bit kernels)
304 const unsigned int alignment = 128;
305 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100306 if(weights_manager && _weights_manager->are_weights_managed(b))
307 {
308 _weights_transform.configure(B_pretranspose_size, alignment);
309 _pretranspose = _weights_manager->acquire(b, &_weights_transform);
310 }
311 else
312 {
313 _pretranspose = new Tensor();
314 static_cast<Tensor *>(_pretranspose)->allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
315 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100316 }
317}
318
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100319template <typename TypeInput, typename TypeOutput, class OutputStage>
320void Fallback<TypeInput, TypeOutput, OutputStage>::prepare()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100321{
322 if(!_is_prepared)
323 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100324 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
325 if(_c && _c->info()->data_type() == DataType::S32)
326 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100327 _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 +0100328 }
329
Anthony Barbier71d9b572018-07-06 17:05:59 +0100330 // Pretranspose B if required
331 if(_gemm_kernel_asm->B_pretranspose_required())
332 {
333 const int ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitaseb84d6b2018-07-27 18:28:10 +0100334 const auto in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
Anthony Barbier71d9b572018-07-06 17:05:59 +0100335 const int multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
336
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100337 if(_weights_manager && _weights_manager->are_weights_managed(_b))
338 {
339 _weights_transform.set_args(ldb, in1_ptr, multi_stride_b, _gemm_kernel_asm);
340 _weights_manager->run(_b, &_weights_transform);
341
342 // If we didn't run the reshape function, set the pretransposed buffer
343 if(!_weights_transform.is_reshape_run())
344 {
345 _weights_transform.set_pretranspose(_pretranspose);
346 }
347 }
348 else
349 {
350 static_cast<Tensor *>(_pretranspose)->allocator()->allocate();
351 ARM_COMPUTE_ERROR_ON(_pretranspose->buffer() == nullptr);
352 _gemm_kernel_asm->pretranspose_B_array(_pretranspose->buffer(), in1_ptr, ldb, multi_stride_b);
353 _b->mark_as_unused();
354 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100355 }
356
357 _is_prepared = true;
358 }
359}
360
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100361template <typename TypeInput, typename TypeOutput, class OutputStage>
362void Fallback<TypeInput, TypeOutput, OutputStage>::allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100363{
364 ARM_COMPUTE_ERROR_ON_MSG(workspace_size == 0, "size cannot be 0");
365 _workspace.allocator()->init(TensorInfo(TensorShape{ (workspace_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
Anthony Barbier20394d52018-08-02 11:29:09 +0100366 memory_group.manage(&_workspace);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100367 _workspace.allocator()->allocate();
368}
369
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100370template <typename TypeInput, typename TypeOutput, class OutputStage>
371bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100372{
373 return _optimised_kernel != nullptr;
374}
375
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100376template <typename TypeInput, typename TypeOutput, class OutputStage>
377void Fallback<TypeInput, TypeOutput, OutputStage>::run()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100378{
379 const int lda = _a->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100380 int ldb = 0;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100381 const int ldd = _d->info()->strides_in_bytes().y() / sizeof(TypeOutput);
382
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100383 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d() != 0 ? 3 : 2;
384 const size_t a_multi_idx = a_batch_idx + 1;
385 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d() != 0 ? 3 : 2;
386 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100387
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100388 const int batch_stride_a = _a->info()->strides_in_bytes()[a_batch_idx] / sizeof(TypeInput);
389 const int batch_stride_d = _d->info()->strides_in_bytes()[d_batch_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100390
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100391 const int multi_stride_a = _a->info()->strides_in_bytes()[a_multi_idx] / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100392 int multi_stride_b = 0;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100393 const int multi_stride_d = _d->info()->strides_in_bytes()[d_multi_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100394
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100395 const auto in0_ptr = reinterpret_cast<const TypeInput *>(_a->buffer() + _a->info()->offset_first_element_in_bytes());
396 const TypeInput *in1_ptr = nullptr;
397 auto out_ptr = reinterpret_cast<TypeOutput *>(_d->buffer() + _d->info()->offset_first_element_in_bytes());
398
399 // Check if B is pre-tranposed and de-reference if not
400 if(!_gemm_kernel_asm->B_is_pretransposed())
401 {
402 ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
403 multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
404 in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
405 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100406
407 // Set workspace if needed and reset number of threads as buffer manager gets re-created with max_threads
408 if(_workspace.buffer() != nullptr)
409 {
410 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(_workspace.buffer()));
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000411 const unsigned int window_size = get_total_window_size(*_gemm_kernel_asm);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100412 unsigned int num_threads = NEScheduler::get().num_threads();
413 if(window_size < num_threads)
414 {
415 num_threads = window_size;
416 _gemm_kernel_asm->set_nthreads(num_threads);
417 }
418 }
419
420 // Prepare assembly kernel
421 prepare();
422
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100423 TypeOutput *bias = nullptr;
424 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
425 if(_c && _c->info()->data_type() != DataType::S32)
426 {
427 bias = reinterpret_cast<TypeOutput *>(_c->buffer() + _c->info()->offset_first_element_in_bytes());
428 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100429 // Set gemm parameters
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100430 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a,
431 in1_ptr, ldb, multi_stride_b,
432 out_ptr, ldd, batch_stride_d, multi_stride_d,
433 bias, 0);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100434 // Schedule assembly kernel
Georgios Pinitas77d42522019-11-05 13:35:47 +0000435 IScheduler::Hints scheduling_hint = IScheduler::Hints(Window::DimX);
Georgios Pinitas6011f242019-11-15 14:26:44 +0000436 if(_kernel_info.method == arm_gemm::GemmMethod::GEMM_INTERLEAVED && _d->info()->data_type() == DataType::F32)
Georgios Pinitas77d42522019-11-05 13:35:47 +0000437 {
Georgios Pinitas6011f242019-11-15 14:26:44 +0000438 const int granule_threshold = 200;
439 scheduling_hint = IScheduler::Hints(Window::DimX, IScheduler::StrategyHint::DYNAMIC, granule_threshold);
Georgios Pinitas77d42522019-11-05 13:35:47 +0000440 }
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000441 else if(_kernel_info.method == arm_gemm::GemmMethod::GEMM_INTERLEAVED_2D && _d->info()->data_type() == DataType::F32)
442 {
443 //GEMM_INTERLEAVED supports 2D parallelism, IScheduler::split_dimensions_all signals to parallelise over all window dimensions
444 const int granule_threshold = 200;
445 scheduling_hint = IScheduler::Hints(IScheduler::split_dimensions_all, IScheduler::StrategyHint::STATIC, granule_threshold);
446 }
447
Georgios Pinitas77d42522019-11-05 13:35:47 +0000448 NEScheduler::get().schedule(_optimised_kernel.get(), scheduling_hint);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100449}
450
Anthony Barbiereaefd002018-07-20 17:49:35 +0100451template <typename TypeInput, typename TypeOutput>
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100452void create_arm_gemm(std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
453 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, arm_gemm::Activation activation, const GEMMInfo &gemm_info,
454 IWeightsManager *weights_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100455{
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100456 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100457 const CPUInfo &ci = NEScheduler::get().cpu_info();
458 unsigned int num_threads = NEScheduler::get().num_threads();
459
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100460 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.batches, p.multis, false, false, activation, num_threads, gemm_info.pretranpose_B());
Anthony Barbiereaefd002018-07-20 17:49:35 +0100461
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100462 // Create arm_gemm fallback
463 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput>>();
464 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager);
465 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100466}
467
468template <typename TypeInput, typename TypeOutput>
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100469void create_arm_gemm_quant(std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
470 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, arm_gemm::Activation activation, const GEMMInfo &gemm_info,
471 IWeightsManager *weights_manager)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100472{
Michele Di Giorgio6ad60af2020-06-09 14:52:15 +0100473 ARM_COMPUTE_UNUSED(activation);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100474 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
475 const CPUInfo &ci = NEScheduler::get().cpu_info();
476 unsigned int num_threads = NEScheduler::get().num_threads();
477
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100478 arm_gemm::GemmArgs args(&ci, p.M, p.N, p.K, p.batches, p.multis, false, false, activation, num_threads, gemm_info.pretranpose_B());
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100479
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000480 // Create arm_gemm fallback
481 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::Requantize32>>();
482
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100483 // Configure requantization info
484 const int32_t a_offset = -a->info()->quantization_info().uniform().offset;
485 const int32_t b_offset = -b->info()->quantization_info().uniform().offset;
486 const GEMMLowpOutputStageInfo os_info = gemm_info.gemmlowp_output_stage();
487
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000488 arm_gemm::Requantize32 gemm_requant_info{};
489 if(os_info.gemmlowp_shifts.size() > 1)
490 {
491 const auto requantize_data = fallback->set_requantize_data(os_info.gemmlowp_shifts, os_info.gemmlowp_multipliers);
492 gemm_requant_info = arm_gemm::Requantize32(nullptr, 0,
493 a_offset, b_offset, os_info.gemmlowp_offset,
494 std::get<0>(requantize_data), std::get<1>(requantize_data),
495 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
496 }
497 else
498 {
499 gemm_requant_info = arm_gemm::Requantize32(nullptr, 0,
500 a_offset, b_offset, os_info.gemmlowp_offset,
501 -os_info.gemmlowp_shift, os_info.gemmlowp_multiplier,
502 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
503 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100504
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000505 // Configure fallback
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100506 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager, gemm_requant_info);
507 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100508}
509
510} //namespace
511
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100512NEGEMMAssemblyDispatch::NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100513 : _arm_gemm(nullptr), _memory_group(std::move(memory_manager)), _weights_manager(weights_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100514{
515}
516
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100517Status NEGEMMAssemblyDispatch::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const GEMMInfo &gemm_info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100518{
Georgios Pinitas0f954eb2020-06-23 17:28:38 +0100519 ARM_COMPUTE_UNUSED(c);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100520 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
521 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000522 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Georgios Pinitas0f954eb2020-06-23 17:28:38 +0100523
524 ARM_COMPUTE_RETURN_ERROR_ON(!gemm_info.pretranpose_B());
Anthony Barbiereaefd002018-07-20 17:49:35 +0100525#ifndef __aarch64__
Michele Di Giorgio52556722019-12-23 16:35:12 +0000526 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->element_size() == 1, "8bit integer types only supported for aarch64");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100527#endif /* __aarch64__ */
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100528 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 +0000529 DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100530 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 +0000531 DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100532 if(is_data_type_quantized_per_channel(b->data_type()))
533 {
534 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
535 }
536 else
537 {
538 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
539 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100540 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32, "Only F32 output supported for F32 input");
541 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 +0000542 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 +0100543 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 +0100544 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 +0100545 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 && d->data_type() != DataType::QASYMM8, "Only QASYMM8 output supported for QASYMM8 input");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100546 return Status{};
547}
548
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100549bool NEGEMMAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
550{
551 arm_gemm::Activation act = map_to_arm_gemm_activation(activation);
552 return act.type != arm_gemm::Activation::Type::None;
553}
554
555void NEGEMMAssemblyDispatch::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, const GEMMInfo &gemm_info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100556{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100557 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100558 arm_gemm::Activation act = map_to_arm_gemm_activation(gemm_info.activation_info());
Anthony Barbiereaefd002018-07-20 17:49:35 +0100559
560 //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()
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100561 if(!NEGEMMAssemblyDispatch::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, d->info(), gemm_info))
Anthony Barbiereaefd002018-07-20 17:49:35 +0100562 {
563 return;
564 }
565
566 switch(a->info()->data_type())
567 {
568 case DataType::F32:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100569 create_arm_gemm<float, float>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100570 break;
571#ifdef __aarch64__
572 case DataType::U8:
573 case DataType::QASYMM8:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100574 if(d->info()->data_type() == DataType::S32)
575 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100576 create_arm_gemm<uint8_t, uint32_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100577 }
578 else
579 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100580 create_arm_gemm_quant<uint8_t, uint8_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100581 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100582 break;
583 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100584 case DataType::QASYMM8_SIGNED:
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000585 if(d->info()->data_type() == DataType::S32)
586 {
587 create_arm_gemm<int8_t, int32_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
588 }
589 else
590 {
591 create_arm_gemm_quant<int8_t, int8_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
592 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100593 break;
594#endif /* __aarch64__ */
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000595#if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
596 case DataType::BFLOAT16:
597 create_arm_gemm<bfloat16, float>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
598 break;
599#endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
Anthony Barbiereaefd002018-07-20 17:49:35 +0100600#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
601 case DataType::F16:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100602 create_arm_gemm<float16_t, float16_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100603 break;
604#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
605 default:
606 break;
607 }
608}
609
610void NEGEMMAssemblyDispatch::prepare()
611{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100612 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
613 _arm_gemm->prepare();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100614}
615
616bool NEGEMMAssemblyDispatch::is_configured() const
617{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100618 return _arm_gemm != nullptr && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100619}
620
621void NEGEMMAssemblyDispatch::run()
622{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100623 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100624
625 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
626 _arm_gemm->run();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100627}
Anthony Barbier71d9b572018-07-06 17:05:59 +0100628} //namespace arm_compute