blob: 43e531579a8e85500617d25ec70ab900fca16bf9 [file] [log] [blame]
Anthony Barbier71d9b572018-07-06 17:05:59 +01001/*
Georgios Pinitas7cd26d42019-01-09 18:35:17 +00002 * Copyright (c) 2018-2019 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
Anthony Barbiereaefd002018-07-20 17:49:35 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010027#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbierc8e84b52018-07-17 16:48:42 +010028#include "arm_compute/runtime/NEON/functions/NESimpleAssemblyFunction.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010029
Anthony Barbiereaefd002018-07-20 17:49:35 +010030#include <arm_neon.h>
31
Anthony Barbierc8e84b52018-07-17 16:48:42 +010032namespace arm_compute
33{
Anthony Barbiereaefd002018-07-20 17:49:35 +010034namespace
Anthony Barbier71d9b572018-07-06 17:05:59 +010035{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010036arm_gemm::Activation map_to_arm_gemm_activation(const ActivationLayerInfo &act)
Anthony Barbiereaefd002018-07-20 17:49:35 +010037{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010038 arm_gemm::Activation gemm_act;
39
40 // Early exit in case lower bound is other than 0, as it's not yet supported
41 if(act.b() != 0.f)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010042 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010043 return gemm_act;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010044 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010045
46 switch(act.activation())
47 {
48 case ActivationLayerInfo::ActivationFunction::RELU:
49 gemm_act.type = arm_gemm::Activation::Type::ReLU;
50 break;
51 case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
52 gemm_act.type = arm_gemm::Activation::Type::BoundedReLU;
53 gemm_act.param1 = act.a();
54 gemm_act.param2 = 0.f;
55 break;
56 case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
57 gemm_act.type = arm_gemm::Activation::Type::BoundedReLU;
58 gemm_act.param1 = act.a();
59 gemm_act.param2 = act.b();
60 break;
61 default:
62 gemm_act.type = arm_gemm::Activation::Type::None;
63 }
64
65 return gemm_act;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010066}
67
Michalis Spyrou1a569a32019-09-10 17:20:34 +010068template <typename TypeInput, typename TypeOutput>
69class FallbackTransform : public ITransformWeights
70{
71public:
72 void run() override
73 {
74 _output.allocator()->allocate();
75 ARM_COMPUTE_ERROR_ON(_output.buffer() == nullptr);
76 _gemm_kernel_asm->pretranspose_B_array(_output.buffer(), _in1_ptr, _ldb, _multi_stride_b);
77 _reshape_run = true;
78 }
79
80 void release() override
81 {
82 _output.allocator()->free();
83 }
84
85 ITensor *get_weights() override
86 {
87 return &_output;
88 }
89
90 uint32_t uid() override
91 {
92 uint32_t id = (_B_pretranspose_size | 0x80000000);
93 return id;
94 }
95
96 void configure(size_t B_pretranspose_size, unsigned int alignment)
97 {
98 _output.allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
99 _B_pretranspose_size = B_pretranspose_size;
100 }
101
102 void set_pretranspose(ITensor *tensor)
103 {
104 if(!_reshape_run)
105 {
106 _gemm_kernel_asm->set_pretransposed_B_data(tensor->buffer());
107 }
108 }
109
110 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)
111 {
112 _ldb = ldb;
113 _in1_ptr = in1_ptr;
114 _multi_stride_b = multi_stride_b;
115 _gemm_kernel_asm = gemm_kernel_asm;
116 }
117
118private:
119 Tensor _output{};
120 int _ldb{};
121 const TypeInput *_in1_ptr{};
122 int _multi_stride_b{};
123 size_t _B_pretranspose_size{};
124 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
125};
126
Anthony Barbiereaefd002018-07-20 17:49:35 +0100127/** Fallback in case ACL doesn't have a function */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100128template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing>
Anthony Barbiereaefd002018-07-20 17:49:35 +0100129class Fallback : public NEGEMMAssemblyDispatch::IFallback
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100130{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100131public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100132 /** Destructor */
133 ~Fallback()
134 {
135 // Release memory if we have allocated the memory ourselves
136 if(_pretranspose && !(_weights_manager && _weights_manager->are_weights_managed(_b)))
137 {
138 delete _pretranspose;
139 }
140 }
141
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000142 /** Initialise the functions's input and output.
143 *
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100144 * @param[in] a Input tensor containing the Matrix A.
145 * @param[in] b Input tensor containing the Matrix B.
146 * @param[in] c Input tensor containing the Matrix C.
147 * @param[out] d Output tensor to store the result of matrix multiplication.
148 * @param[in] args Matrix multiplication information.
149 * @param[in] gemm_info GEMM meta-data
150 * @param[in] memory_group Memory group to be used by the function.
151 * @param[in] weights_manager Weights manager to be used by the function.
152 * @param[in] os Output stage meta-data.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000153 */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100154 void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100155 arm_gemm::GemmArgs args, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100156 MemoryGroup &memory_group, IWeightsManager *weights_manager, const OutputStage &os = {});
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000157
158 // Inherited methods overridden:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100159 void run() override;
160 void prepare() override;
161 bool is_configured() const override;
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100162
Anthony Barbiereaefd002018-07-20 17:49:35 +0100163private:
164 /** Allocate a workspace tensor.
165 *
166 * @param[in] workspace_size Size to allocate.
167 * @param[in] memory_group Tensor memory group.
168 * @param[in] alignment Workspace memory alignment.
169 */
Anthony Barbier20394d52018-08-02 11:29:09 +0100170 void allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100171
Anthony Barbiereaefd002018-07-20 17:49:35 +0100172 /** Assembly Gemm kernel */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100173 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100174 /** Optimised NEON kernel */
175 std::unique_ptr<INEKernel> _optimised_kernel{ nullptr };
176 /** Input A */
177 const ITensor *_a
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100178 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100179 nullptr
180 };
181 /** Input B */
182 const ITensor *_b
183 {
184 nullptr
185 };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100186 const ITensor *_c
187 {
188 nullptr
189 };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100190 /** Output */
191 ITensor *_d{ nullptr };
192 /** GEMM workspace */
193 Tensor _workspace{};
194 /** Pre-transpose tensor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100195 ITensor *_pretranspose{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100196 /** Prepared flag */
197 bool _is_prepared{ false };
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100198 /** GEMM meta-data */
199 GEMMInfo _gemm_info{};
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100200 /** Weights manager */
201 IWeightsManager *_weights_manager{ nullptr };
202 /** Weights transform object */
203 FallbackTransform<TypeInput, TypeOutput> _weights_transform{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100204};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100205
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100206template <typename TypeInput, typename TypeOutput, class OutputStage>
207void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100208 arm_gemm::GemmArgs args, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100209 MemoryGroup &memory_group, IWeightsManager *weights_manager, const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100210{
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000211 arm_gemm::GemmConfig gemm_cfg;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100212 const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput, OutputStage>(args, os);
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100213 _weights_manager = weights_manager;
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000214 if(gemm_kernel_info.method != arm_gemm::GemmMethod::GEMV_BATCHED)
215 {
216 gemm_cfg.filter = gemm_kernel_info.name;
217 args._cfg = &gemm_cfg;
218 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100219 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100220 if(_gemm_kernel_asm == nullptr)
221 {
222 //configuration not supported: Leave function unconfigured:
223 return;
224 }
225
226 // arm_compute wrapper for the Gemm object (see above)
227 std::unique_ptr<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>> acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>>();
228 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000229 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100230 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
231 if(workspace_size > 0)
232 {
233 // Allocate workspace
234 const unsigned int alignment = 4096;
Anthony Barbier20394d52018-08-02 11:29:09 +0100235 allocate_workspace(workspace_size, memory_group, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100236 }
237
238 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
239 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
240 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100241 const int window_size = _gemm_kernel_asm->get_window_size();
242 if(window_size < args._maxthreads)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100243 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100244 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100245 }
246 }
247
248 _optimised_kernel = std::move(acl_gemm_wrapper);
249 _a = a;
250 _b = b;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100251 _c = c;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100252 _d = d;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100253 _gemm_info = gemm_info;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100254 // Check for pre-transposed support
255 if(_gemm_kernel_asm->B_pretranspose_required())
256 {
257 // Forcing 128-byte alignment (required by 32-bit kernels)
258 const unsigned int alignment = 128;
259 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100260 if(weights_manager && _weights_manager->are_weights_managed(b))
261 {
262 _weights_transform.configure(B_pretranspose_size, alignment);
263 _pretranspose = _weights_manager->acquire(b, &_weights_transform);
264 }
265 else
266 {
267 _pretranspose = new Tensor();
268 static_cast<Tensor *>(_pretranspose)->allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
269 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100270 }
271}
272
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100273template <typename TypeInput, typename TypeOutput, class OutputStage>
274void Fallback<TypeInput, TypeOutput, OutputStage>::prepare()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100275{
276 if(!_is_prepared)
277 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100278 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
279 if(_c && _c->info()->data_type() == DataType::S32)
280 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100281 _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 +0100282 }
283
Anthony Barbier71d9b572018-07-06 17:05:59 +0100284 // Pretranspose B if required
285 if(_gemm_kernel_asm->B_pretranspose_required())
286 {
287 const int ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitaseb84d6b2018-07-27 18:28:10 +0100288 const auto in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
Anthony Barbier71d9b572018-07-06 17:05:59 +0100289 const int multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
290
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100291 if(_weights_manager && _weights_manager->are_weights_managed(_b))
292 {
293 _weights_transform.set_args(ldb, in1_ptr, multi_stride_b, _gemm_kernel_asm);
294 _weights_manager->run(_b, &_weights_transform);
295
296 // If we didn't run the reshape function, set the pretransposed buffer
297 if(!_weights_transform.is_reshape_run())
298 {
299 _weights_transform.set_pretranspose(_pretranspose);
300 }
301 }
302 else
303 {
304 static_cast<Tensor *>(_pretranspose)->allocator()->allocate();
305 ARM_COMPUTE_ERROR_ON(_pretranspose->buffer() == nullptr);
306 _gemm_kernel_asm->pretranspose_B_array(_pretranspose->buffer(), in1_ptr, ldb, multi_stride_b);
307 _b->mark_as_unused();
308 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100309 }
310
311 _is_prepared = true;
312 }
313}
314
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100315template <typename TypeInput, typename TypeOutput, class OutputStage>
316void Fallback<TypeInput, TypeOutput, OutputStage>::allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100317{
318 ARM_COMPUTE_ERROR_ON_MSG(workspace_size == 0, "size cannot be 0");
319 _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 +0100320 memory_group.manage(&_workspace);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100321 _workspace.allocator()->allocate();
322}
323
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100324template <typename TypeInput, typename TypeOutput, class OutputStage>
325bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100326{
327 return _optimised_kernel != nullptr;
328}
329
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100330template <typename TypeInput, typename TypeOutput, class OutputStage>
331void Fallback<TypeInput, TypeOutput, OutputStage>::run()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100332{
333 const int lda = _a->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100334 int ldb = 0;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100335 const int ldd = _d->info()->strides_in_bytes().y() / sizeof(TypeOutput);
336
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100337 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d() != 0 ? 3 : 2;
338 const size_t a_multi_idx = a_batch_idx + 1;
339 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d() != 0 ? 3 : 2;
340 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100341
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100342 const int batch_stride_a = _a->info()->strides_in_bytes()[a_batch_idx] / sizeof(TypeInput);
343 const int batch_stride_d = _d->info()->strides_in_bytes()[d_batch_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100344
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100345 const int multi_stride_a = _a->info()->strides_in_bytes()[a_multi_idx] / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100346 int multi_stride_b = 0;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100347 const int multi_stride_d = _d->info()->strides_in_bytes()[d_multi_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100348
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100349 const auto in0_ptr = reinterpret_cast<const TypeInput *>(_a->buffer() + _a->info()->offset_first_element_in_bytes());
350 const TypeInput *in1_ptr = nullptr;
351 auto out_ptr = reinterpret_cast<TypeOutput *>(_d->buffer() + _d->info()->offset_first_element_in_bytes());
352
353 // Check if B is pre-tranposed and de-reference if not
354 if(!_gemm_kernel_asm->B_is_pretransposed())
355 {
356 ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
357 multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
358 in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
359 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100360
361 // Set workspace if needed and reset number of threads as buffer manager gets re-created with max_threads
362 if(_workspace.buffer() != nullptr)
363 {
364 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(_workspace.buffer()));
365 const unsigned int window_size = _gemm_kernel_asm->get_window_size();
366 unsigned int num_threads = NEScheduler::get().num_threads();
367 if(window_size < num_threads)
368 {
369 num_threads = window_size;
370 _gemm_kernel_asm->set_nthreads(num_threads);
371 }
372 }
373
374 // Prepare assembly kernel
375 prepare();
376
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100377 TypeOutput *bias = nullptr;
378 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
379 if(_c && _c->info()->data_type() != DataType::S32)
380 {
381 bias = reinterpret_cast<TypeOutput *>(_c->buffer() + _c->info()->offset_first_element_in_bytes());
382 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100383 // Set gemm parameters
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100384 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a,
385 in1_ptr, ldb, multi_stride_b,
386 out_ptr, ldd, batch_stride_d, multi_stride_d,
387 bias, 0);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100388
389 // Schedule assembly kernel
390 NEScheduler::get().schedule(_optimised_kernel.get(), Window::DimX);
391}
392
Anthony Barbiereaefd002018-07-20 17:49:35 +0100393template <typename TypeInput, typename TypeOutput>
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100394void create_arm_gemm(std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
395 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, arm_gemm::Activation activation, const GEMMInfo &gemm_info,
396 IWeightsManager *weights_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100397{
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100398 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100399 const CPUInfo &ci = NEScheduler::get().cpu_info();
400 unsigned int num_threads = NEScheduler::get().num_threads();
401
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100402 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 +0100403
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100404 // Create arm_gemm fallback
405 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput>>();
406 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager);
407 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100408}
409
410template <typename TypeInput, typename TypeOutput>
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100411void create_arm_gemm_quant(std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
412 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, arm_gemm::Activation activation, const GEMMInfo &gemm_info,
413 IWeightsManager *weights_manager)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100414{
415 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
416 const CPUInfo &ci = NEScheduler::get().cpu_info();
417 unsigned int num_threads = NEScheduler::get().num_threads();
418
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100419 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 +0100420
421 // Configure requantization info
422 const int32_t a_offset = -a->info()->quantization_info().uniform().offset;
423 const int32_t b_offset = -b->info()->quantization_info().uniform().offset;
424 const GEMMLowpOutputStageInfo os_info = gemm_info.gemmlowp_output_stage();
425
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100426 const arm_gemm::ARequantizeLayer32 gemm_requant_info(nullptr, 0,
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100427 a_offset, b_offset, os_info.gemmlowp_offset,
428 -os_info.gemmlowp_shift, os_info.gemmlowp_multiplier,
429 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
430
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100431 // Create arm_gemm fallback
432 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::ARequantizeLayer32>>();
433 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager, gemm_requant_info);
434 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100435}
436
437} //namespace
438
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100439NEGEMMAssemblyDispatch::NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100440 : _arm_gemm(nullptr), _memory_group(std::move(memory_manager)), _weights_manager(weights_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100441{
442}
443
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100444Status 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 +0100445{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100446 ARM_COMPUTE_UNUSED(gemm_info);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100447 ARM_COMPUTE_UNUSED(c);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100448 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
449 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
450#ifndef __aarch64__
451 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 || a->data_type() == DataType::S8 || a->data_type() == DataType::QASYMM8, "8bit integer types only supported for aarch64");
452#endif /* __aarch64__ */
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100453 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S8,
454 DataType::F16, DataType::F32);
455 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,
456 DataType::F16, DataType::F32);
457 if(is_data_type_quantized_per_channel(b->data_type()))
458 {
459 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
460 }
461 else
462 {
463 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
464 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100465 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32, "Only F32 output supported for F32 input");
466 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F16 && d->data_type() != DataType::F16, "Only F16 output supported for F16 input");
Anthony Barbier90367492018-08-01 13:56:08 +0100467 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 +0100468 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 +0100469 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 && d->data_type() != DataType::QASYMM8, "Only QASYMM8 output supported for QASYMM8 input");
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100470 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8_SIGNED && d->data_type() != DataType::S32, "Only S32 output supported for QASYMM8_SIGNED input");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100471 return Status{};
472}
473
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100474bool NEGEMMAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
475{
476 arm_gemm::Activation act = map_to_arm_gemm_activation(activation);
477 return act.type != arm_gemm::Activation::Type::None;
478}
479
480void NEGEMMAssemblyDispatch::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, const GEMMInfo &gemm_info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100481{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100482 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100483 arm_gemm::Activation act = map_to_arm_gemm_activation(gemm_info.activation_info());
Anthony Barbiereaefd002018-07-20 17:49:35 +0100484
485 //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 +0100486 if(!NEGEMMAssemblyDispatch::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, d->info(), gemm_info))
Anthony Barbiereaefd002018-07-20 17:49:35 +0100487 {
488 return;
489 }
490
491 switch(a->info()->data_type())
492 {
493 case DataType::F32:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100494 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 +0100495 break;
496#ifdef __aarch64__
497 case DataType::U8:
498 case DataType::QASYMM8:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100499 if(d->info()->data_type() == DataType::S32)
500 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100501 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 +0100502 }
503 else
504 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100505 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 +0100506 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100507 break;
508 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100509 case DataType::QASYMM8_SIGNED:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100510 create_arm_gemm<int8_t, int32_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100511 break;
512#endif /* __aarch64__ */
513#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
514 case DataType::F16:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100515 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 +0100516 break;
517#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
518 default:
519 break;
520 }
521}
522
523void NEGEMMAssemblyDispatch::prepare()
524{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100525 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
526 _arm_gemm->prepare();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100527}
528
529bool NEGEMMAssemblyDispatch::is_configured() const
530{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100531 return _arm_gemm != nullptr && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100532}
533
534void NEGEMMAssemblyDispatch::run()
535{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100536 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100537
538 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
539 _arm_gemm->run();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100540}
Anthony Barbier71d9b572018-07-06 17:05:59 +0100541} //namespace arm_compute