blob: a3080e7f29b0db93a907faade53f22c8a53cf64b [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
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:
Michalis Spyrou5cb49dc2019-12-03 13:42:25 +000072 FallbackTransform() noexcept {};
73 /** Prevent instances of this class from being copied (As this class contains pointers) */
74 FallbackTransform(const FallbackTransform &) = delete;
75 /** Default move constructor */
76 FallbackTransform(FallbackTransform &&) = default;
77 /** Prevent instances of this class from being copied (As this class contains pointers) */
78 FallbackTransform &operator=(const FallbackTransform &) = delete;
79 /** Default move assignment operator */
80 FallbackTransform &operator=(FallbackTransform &&) = default;
81 void run() override
Michalis Spyrou1a569a32019-09-10 17:20:34 +010082 {
83 _output.allocator()->allocate();
84 ARM_COMPUTE_ERROR_ON(_output.buffer() == nullptr);
85 _gemm_kernel_asm->pretranspose_B_array(_output.buffer(), _in1_ptr, _ldb, _multi_stride_b);
86 _reshape_run = true;
87 }
88
89 void release() override
90 {
91 _output.allocator()->free();
92 }
93
94 ITensor *get_weights() override
95 {
96 return &_output;
97 }
98
99 uint32_t uid() override
100 {
101 uint32_t id = (_B_pretranspose_size | 0x80000000);
102 return id;
103 }
104
105 void configure(size_t B_pretranspose_size, unsigned int alignment)
106 {
107 _output.allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
108 _B_pretranspose_size = B_pretranspose_size;
109 }
110
111 void set_pretranspose(ITensor *tensor)
112 {
113 if(!_reshape_run)
114 {
115 _gemm_kernel_asm->set_pretransposed_B_data(tensor->buffer());
116 }
117 }
118
119 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)
120 {
121 _ldb = ldb;
122 _in1_ptr = in1_ptr;
123 _multi_stride_b = multi_stride_b;
124 _gemm_kernel_asm = gemm_kernel_asm;
125 }
126
127private:
128 Tensor _output{};
129 int _ldb{};
130 const TypeInput *_in1_ptr{};
131 int _multi_stride_b{};
132 size_t _B_pretranspose_size{};
133 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
134};
135
Anthony Barbiereaefd002018-07-20 17:49:35 +0100136/** Fallback in case ACL doesn't have a function */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100137template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing>
Anthony Barbiereaefd002018-07-20 17:49:35 +0100138class Fallback : public NEGEMMAssemblyDispatch::IFallback
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100139{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100140public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100141 /** Destructor */
142 ~Fallback()
143 {
144 // Release memory if we have allocated the memory ourselves
145 if(_pretranspose && !(_weights_manager && _weights_manager->are_weights_managed(_b)))
146 {
147 delete _pretranspose;
148 }
149 }
150
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000151 /** Initialise the functions's input and output.
152 *
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100153 * @param[in] a Input tensor containing the Matrix A.
154 * @param[in] b Input tensor containing the Matrix B.
155 * @param[in] c Input tensor containing the Matrix C.
156 * @param[out] d Output tensor to store the result of matrix multiplication.
157 * @param[in] args Matrix multiplication information.
158 * @param[in] gemm_info GEMM meta-data
159 * @param[in] memory_group Memory group to be used by the function.
160 * @param[in] weights_manager Weights manager to be used by the function.
161 * @param[in] os Output stage meta-data.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000162 */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100163 void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100164 arm_gemm::GemmArgs args, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100165 MemoryGroup &memory_group, IWeightsManager *weights_manager, const OutputStage &os = {});
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000166
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000167 /** Set requantization shifts to be used
168 *
169 * @param[in] shifts Requantization shifts
170 *
171 * @return Pointer to the shift data
172 */
173 /** Set requantization data to be used
174 *
175 *
176 * @param shifts Requantization shifts
177 * @param multipliers Requantization multipliers
178 *
179 * @return A tuple with the pointers to the shift and multiplier data respectively
180 */
181 std::tuple<const int32_t *, const int32_t *> set_requantize_data(const std::vector<int32_t> &shifts,
182 const std::vector<int32_t> &multipliers);
183
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000184 // Inherited methods overridden:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100185 void run() override;
186 void prepare() override;
187 bool is_configured() const override;
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100188
Anthony Barbiereaefd002018-07-20 17:49:35 +0100189private:
190 /** Allocate a workspace tensor.
191 *
192 * @param[in] workspace_size Size to allocate.
193 * @param[in] memory_group Tensor memory group.
194 * @param[in] alignment Workspace memory alignment.
195 */
Anthony Barbier20394d52018-08-02 11:29:09 +0100196 void allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100197
Anthony Barbiereaefd002018-07-20 17:49:35 +0100198 /** Assembly Gemm kernel */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100199 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100200 /** Optimised NEON kernel */
201 std::unique_ptr<INEKernel> _optimised_kernel{ nullptr };
202 /** Input A */
203 const ITensor *_a
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100204 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100205 nullptr
206 };
207 /** Input B */
208 const ITensor *_b
209 {
210 nullptr
211 };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100212 const ITensor *_c
213 {
214 nullptr
215 };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100216 /** Output */
217 ITensor *_d{ nullptr };
218 /** GEMM workspace */
219 Tensor _workspace{};
220 /** Pre-transpose tensor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100221 ITensor *_pretranspose{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100222 /** Prepared flag */
223 bool _is_prepared{ false };
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100224 /** GEMM meta-data */
225 GEMMInfo _gemm_info{};
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100226 /** Weights manager */
227 IWeightsManager *_weights_manager{ nullptr };
228 /** Weights transform object */
229 FallbackTransform<TypeInput, TypeOutput> _weights_transform{};
Georgios Pinitas77d42522019-11-05 13:35:47 +0000230 /** GEMM kernel description */
231 arm_gemm::KernelDescription _kernel_info{};
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000232 /** Per channel quantization shifts */
233 std::vector<int32_t> _shifts{};
234 /** Per channel quantization multipliers */
235 std::vector<int32_t> _multipliers{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100236};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100237
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100238template <typename TypeInput, typename TypeOutput, class OutputStage>
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000239std::tuple<const int32_t *, const int32_t *> Fallback<TypeInput, TypeOutput, OutputStage>::set_requantize_data(const std::vector<int32_t> &shifts,
240 const std::vector<int32_t> &multipliers)
241{
242 _multipliers = multipliers;
243 _shifts = shifts;
Georgios Pinitas47fd61f2020-01-29 12:02:20 +0000244 std::transform(_shifts.begin(), _shifts.end(), _shifts.begin(), std::negate<int32_t>());
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000245 return std::make_tuple(_shifts.data(), _multipliers.data());
246}
247
248template <typename TypeInput, typename TypeOutput, class OutputStage>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100249void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100250 arm_gemm::GemmArgs args, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100251 MemoryGroup &memory_group, IWeightsManager *weights_manager, const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100252{
Georgios Pinitas77d42522019-11-05 13:35:47 +0000253 arm_gemm::GemmConfig gemm_cfg;
254 _kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput, OutputStage>(args, os);
255 _weights_manager = weights_manager;
256 if(_kernel_info.method != arm_gemm::GemmMethod::GEMV_BATCHED)
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000257 {
Georgios Pinitas77d42522019-11-05 13:35:47 +0000258 gemm_cfg.filter = _kernel_info.name;
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000259 args._cfg = &gemm_cfg;
260 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100261 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100262 if(_gemm_kernel_asm == nullptr)
263 {
264 //configuration not supported: Leave function unconfigured:
265 return;
266 }
267
268 // arm_compute wrapper for the Gemm object (see above)
269 std::unique_ptr<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>> acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>>();
270 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000271 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100272 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
273 if(workspace_size > 0)
274 {
275 // Allocate workspace
276 const unsigned int alignment = 4096;
Anthony Barbier20394d52018-08-02 11:29:09 +0100277 allocate_workspace(workspace_size, memory_group, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100278 }
279
280 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
281 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
282 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100283 const int window_size = _gemm_kernel_asm->get_window_size();
284 if(window_size < args._maxthreads)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100285 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100286 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100287 }
288 }
289
290 _optimised_kernel = std::move(acl_gemm_wrapper);
291 _a = a;
292 _b = b;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100293 _c = c;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100294 _d = d;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100295 _gemm_info = gemm_info;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100296 // Check for pre-transposed support
297 if(_gemm_kernel_asm->B_pretranspose_required())
298 {
299 // Forcing 128-byte alignment (required by 32-bit kernels)
300 const unsigned int alignment = 128;
301 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100302 if(weights_manager && _weights_manager->are_weights_managed(b))
303 {
304 _weights_transform.configure(B_pretranspose_size, alignment);
305 _pretranspose = _weights_manager->acquire(b, &_weights_transform);
306 }
307 else
308 {
309 _pretranspose = new Tensor();
310 static_cast<Tensor *>(_pretranspose)->allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
311 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100312 }
313}
314
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100315template <typename TypeInput, typename TypeOutput, class OutputStage>
316void Fallback<TypeInput, TypeOutput, OutputStage>::prepare()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100317{
318 if(!_is_prepared)
319 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100320 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
321 if(_c && _c->info()->data_type() == DataType::S32)
322 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100323 _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 +0100324 }
325
Anthony Barbier71d9b572018-07-06 17:05:59 +0100326 // Pretranspose B if required
327 if(_gemm_kernel_asm->B_pretranspose_required())
328 {
329 const int ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitaseb84d6b2018-07-27 18:28:10 +0100330 const auto in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
Anthony Barbier71d9b572018-07-06 17:05:59 +0100331 const int multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
332
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100333 if(_weights_manager && _weights_manager->are_weights_managed(_b))
334 {
335 _weights_transform.set_args(ldb, in1_ptr, multi_stride_b, _gemm_kernel_asm);
336 _weights_manager->run(_b, &_weights_transform);
337
338 // If we didn't run the reshape function, set the pretransposed buffer
339 if(!_weights_transform.is_reshape_run())
340 {
341 _weights_transform.set_pretranspose(_pretranspose);
342 }
343 }
344 else
345 {
346 static_cast<Tensor *>(_pretranspose)->allocator()->allocate();
347 ARM_COMPUTE_ERROR_ON(_pretranspose->buffer() == nullptr);
348 _gemm_kernel_asm->pretranspose_B_array(_pretranspose->buffer(), in1_ptr, ldb, multi_stride_b);
349 _b->mark_as_unused();
350 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100351 }
352
353 _is_prepared = true;
354 }
355}
356
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100357template <typename TypeInput, typename TypeOutput, class OutputStage>
358void Fallback<TypeInput, TypeOutput, OutputStage>::allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100359{
360 ARM_COMPUTE_ERROR_ON_MSG(workspace_size == 0, "size cannot be 0");
361 _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 +0100362 memory_group.manage(&_workspace);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100363 _workspace.allocator()->allocate();
364}
365
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100366template <typename TypeInput, typename TypeOutput, class OutputStage>
367bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100368{
369 return _optimised_kernel != nullptr;
370}
371
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100372template <typename TypeInput, typename TypeOutput, class OutputStage>
373void Fallback<TypeInput, TypeOutput, OutputStage>::run()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100374{
375 const int lda = _a->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100376 int ldb = 0;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100377 const int ldd = _d->info()->strides_in_bytes().y() / sizeof(TypeOutput);
378
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100379 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d() != 0 ? 3 : 2;
380 const size_t a_multi_idx = a_batch_idx + 1;
381 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d() != 0 ? 3 : 2;
382 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100383
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100384 const int batch_stride_a = _a->info()->strides_in_bytes()[a_batch_idx] / sizeof(TypeInput);
385 const int batch_stride_d = _d->info()->strides_in_bytes()[d_batch_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100386
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100387 const int multi_stride_a = _a->info()->strides_in_bytes()[a_multi_idx] / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100388 int multi_stride_b = 0;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100389 const int multi_stride_d = _d->info()->strides_in_bytes()[d_multi_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100390
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100391 const auto in0_ptr = reinterpret_cast<const TypeInput *>(_a->buffer() + _a->info()->offset_first_element_in_bytes());
392 const TypeInput *in1_ptr = nullptr;
393 auto out_ptr = reinterpret_cast<TypeOutput *>(_d->buffer() + _d->info()->offset_first_element_in_bytes());
394
395 // Check if B is pre-tranposed and de-reference if not
396 if(!_gemm_kernel_asm->B_is_pretransposed())
397 {
398 ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
399 multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
400 in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
401 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100402
403 // Set workspace if needed and reset number of threads as buffer manager gets re-created with max_threads
404 if(_workspace.buffer() != nullptr)
405 {
406 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(_workspace.buffer()));
407 const unsigned int window_size = _gemm_kernel_asm->get_window_size();
408 unsigned int num_threads = NEScheduler::get().num_threads();
409 if(window_size < num_threads)
410 {
411 num_threads = window_size;
412 _gemm_kernel_asm->set_nthreads(num_threads);
413 }
414 }
415
416 // Prepare assembly kernel
417 prepare();
418
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100419 TypeOutput *bias = nullptr;
420 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
421 if(_c && _c->info()->data_type() != DataType::S32)
422 {
423 bias = reinterpret_cast<TypeOutput *>(_c->buffer() + _c->info()->offset_first_element_in_bytes());
424 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100425 // Set gemm parameters
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100426 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a,
427 in1_ptr, ldb, multi_stride_b,
428 out_ptr, ldd, batch_stride_d, multi_stride_d,
429 bias, 0);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100430
431 // Schedule assembly kernel
Georgios Pinitas77d42522019-11-05 13:35:47 +0000432 IScheduler::Hints scheduling_hint = IScheduler::Hints(Window::DimX);
Georgios Pinitas6011f242019-11-15 14:26:44 +0000433 if(_kernel_info.method == arm_gemm::GemmMethod::GEMM_INTERLEAVED && _d->info()->data_type() == DataType::F32)
Georgios Pinitas77d42522019-11-05 13:35:47 +0000434 {
Georgios Pinitas6011f242019-11-15 14:26:44 +0000435 const int granule_threshold = 200;
436 scheduling_hint = IScheduler::Hints(Window::DimX, IScheduler::StrategyHint::DYNAMIC, granule_threshold);
Georgios Pinitas77d42522019-11-05 13:35:47 +0000437 }
438 NEScheduler::get().schedule(_optimised_kernel.get(), scheduling_hint);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100439}
440
Anthony Barbiereaefd002018-07-20 17:49:35 +0100441template <typename TypeInput, typename TypeOutput>
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100442void create_arm_gemm(std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
443 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, arm_gemm::Activation activation, const GEMMInfo &gemm_info,
444 IWeightsManager *weights_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100445{
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100446 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100447 const CPUInfo &ci = NEScheduler::get().cpu_info();
448 unsigned int num_threads = NEScheduler::get().num_threads();
449
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100450 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 +0100451
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100452 // Create arm_gemm fallback
453 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput>>();
454 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager);
455 arm_gemm = std::move(fallback);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100456}
457
458template <typename TypeInput, typename TypeOutput>
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100459void create_arm_gemm_quant(std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
460 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, arm_gemm::Activation activation, const GEMMInfo &gemm_info,
461 IWeightsManager *weights_manager)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100462{
463 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
464 const CPUInfo &ci = NEScheduler::get().cpu_info();
465 unsigned int num_threads = NEScheduler::get().num_threads();
466
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100467 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 +0100468
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000469 // Create arm_gemm fallback
470 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::Requantize32>>();
471
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100472 // Configure requantization info
473 const int32_t a_offset = -a->info()->quantization_info().uniform().offset;
474 const int32_t b_offset = -b->info()->quantization_info().uniform().offset;
475 const GEMMLowpOutputStageInfo os_info = gemm_info.gemmlowp_output_stage();
476
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000477 arm_gemm::Requantize32 gemm_requant_info{};
478 if(os_info.gemmlowp_shifts.size() > 1)
479 {
480 const auto requantize_data = fallback->set_requantize_data(os_info.gemmlowp_shifts, os_info.gemmlowp_multipliers);
481 gemm_requant_info = arm_gemm::Requantize32(nullptr, 0,
482 a_offset, b_offset, os_info.gemmlowp_offset,
483 std::get<0>(requantize_data), std::get<1>(requantize_data),
484 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
485 }
486 else
487 {
488 gemm_requant_info = arm_gemm::Requantize32(nullptr, 0,
489 a_offset, b_offset, os_info.gemmlowp_offset,
490 -os_info.gemmlowp_shift, os_info.gemmlowp_multiplier,
491 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
492 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100493
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000494 // Configure fallback
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100495 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager, gemm_requant_info);
496 arm_gemm = std::move(fallback);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100497}
498
499} //namespace
500
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100501NEGEMMAssemblyDispatch::NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100502 : _arm_gemm(nullptr), _memory_group(std::move(memory_manager)), _weights_manager(weights_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100503{
504}
505
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100506Status 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 +0100507{
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000508 ARM_COMPUTE_UNUSED(gemm_info, c);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100509 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
510 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000511 ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(a);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100512#ifndef __aarch64__
Michele Di Giorgio52556722019-12-23 16:35:12 +0000513 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->element_size() == 1, "8bit integer types only supported for aarch64");
Anthony Barbiereaefd002018-07-20 17:49:35 +0100514#endif /* __aarch64__ */
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100515 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 +0000516 DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100517 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 +0000518 DataType::BFLOAT16, DataType::F16, DataType::F32);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100519 if(is_data_type_quantized_per_channel(b->data_type()))
520 {
521 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8_SIGNED, DataType::S8);
522 }
523 else
524 {
525 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
526 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100527 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32, "Only F32 output supported for F32 input");
528 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 +0000529 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 +0100530 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 +0100531 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 +0100532 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 +0100533 return Status{};
534}
535
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100536bool NEGEMMAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
537{
538 arm_gemm::Activation act = map_to_arm_gemm_activation(activation);
539 return act.type != arm_gemm::Activation::Type::None;
540}
541
542void NEGEMMAssemblyDispatch::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, const GEMMInfo &gemm_info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100543{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100544 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100545 arm_gemm::Activation act = map_to_arm_gemm_activation(gemm_info.activation_info());
Anthony Barbiereaefd002018-07-20 17:49:35 +0100546
547 //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 +0100548 if(!NEGEMMAssemblyDispatch::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, d->info(), gemm_info))
Anthony Barbiereaefd002018-07-20 17:49:35 +0100549 {
550 return;
551 }
552
553 switch(a->info()->data_type())
554 {
555 case DataType::F32:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100556 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 +0100557 break;
558#ifdef __aarch64__
559 case DataType::U8:
560 case DataType::QASYMM8:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100561 if(d->info()->data_type() == DataType::S32)
562 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100563 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 +0100564 }
565 else
566 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100567 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 +0100568 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100569 break;
570 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100571 case DataType::QASYMM8_SIGNED:
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000572 if(d->info()->data_type() == DataType::S32)
573 {
574 create_arm_gemm<int8_t, int32_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
575 }
576 else
577 {
578 create_arm_gemm_quant<int8_t, int8_t>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
579 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100580 break;
581#endif /* __aarch64__ */
Georgios Pinitasc7b183a2020-03-06 18:12:09 +0000582#if defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16)
583 case DataType::BFLOAT16:
584 create_arm_gemm<bfloat16, float>(_arm_gemm, _memory_group, a, b, c, d, act, gemm_info, _weights_manager);
585 break;
586#endif /* defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) || defined(ARM_COMPUTE_FORCE_BF16) */
Anthony Barbiereaefd002018-07-20 17:49:35 +0100587#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
588 case DataType::F16:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100589 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 +0100590 break;
591#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
592 default:
593 break;
594 }
595}
596
597void NEGEMMAssemblyDispatch::prepare()
598{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100599 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
600 _arm_gemm->prepare();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100601}
602
603bool NEGEMMAssemblyDispatch::is_configured() const
604{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100605 return _arm_gemm != nullptr && _arm_gemm->is_configured();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100606}
607
608void NEGEMMAssemblyDispatch::run()
609{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100610 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100611
612 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
613 _arm_gemm->run();
Anthony Barbiereaefd002018-07-20 17:49:35 +0100614}
Anthony Barbier71d9b572018-07-06 17:05:59 +0100615} //namespace arm_compute