blob: 956ded55d2aaa412d3bbb34dd504c7b88d48b4ef [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 Barbierc8e84b52018-07-17 16:48:42 +010027#include "arm_compute/core/NEON/kernels/assembly/NEGEMMNativeWrapperKernel.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010028#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbierc8e84b52018-07-17 16:48:42 +010029#include "arm_compute/runtime/NEON/functions/NESimpleAssemblyFunction.h"
Anthony Barbier3d677cc2018-07-23 16:42:59 +010030#include "arm_compute/runtime/NEON/functions/assembly/NEGEMMInterleavedWrapper.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010031
Anthony Barbiereaefd002018-07-20 17:49:35 +010032#include <arm_neon.h>
33
Anthony Barbierc8e84b52018-07-17 16:48:42 +010034namespace arm_compute
35{
Anthony Barbiereaefd002018-07-20 17:49:35 +010036namespace
Anthony Barbier71d9b572018-07-06 17:05:59 +010037{
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010038std::unique_ptr<IFunction> create_function_all_types(const arm_gemm::KernelDescription &gemm_kernel_info,
Georgios Pinitas37d080f2019-06-21 18:43:12 +010039 const ITensor *a, const ITensor *b, ITensor *d,
40 float alpha, float beta, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +010041 std::shared_ptr<IMemoryManager> memory_manager,
42 IWeightsManager *weights_manager)
Anthony Barbier3d677cc2018-07-23 16:42:59 +010043
Anthony Barbiereaefd002018-07-20 17:49:35 +010044{
Georgios Pinitas37d080f2019-06-21 18:43:12 +010045 // Note: It's safe to not check for FP16 support because this was already checked in NEGEMMAssemblyDispatch::configure()
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000046 switch(gemm_kernel_info.method)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010047 {
Anthony Barbier3d677cc2018-07-23 16:42:59 +010048 case arm_gemm::GemmMethod::GEMM_INTERLEAVED:
49 {
Georgios Pinitas37d080f2019-06-21 18:43:12 +010050 if(!gemm_info.pretranpose_B())
Anthony Barbier3d677cc2018-07-23 16:42:59 +010051 {
52 return nullptr;
53 }
Michalis Spyrou1a569a32019-09-10 17:20:34 +010054 auto function = support::cpp14::make_unique<NEGEMMInterleavedWrapper>(memory_manager, weights_manager);
Georgios Pinitas37d080f2019-06-21 18:43:12 +010055 function->configure(a, b, d, alpha, beta, gemm_info);
Anthony Barbier3d677cc2018-07-23 16:42:59 +010056 return std::move(function);
57 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000058#if defined(__aarch64__)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010059 case arm_gemm::GemmMethod::GEMM_NATIVE:
60 {
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000061 if(gemm_kernel_info.name.find("sgemm_native_16x4") != std::string::npos)
62 {
63 auto kernel = support::cpp14::make_unique<NEGEMMNativeWrapperKernel<float, float>>();
Georgios Pinitas37d080f2019-06-21 18:43:12 +010064 kernel->configure(a, b, d, alpha, beta, gemm_info);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000065 auto function = support::cpp14::make_unique<NESimpleAssemblyFunction>();
66 function->configure(std::move(kernel));
67 return std::move(function);
68 }
69 return nullptr;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010070 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000071#endif // defined(__aarch64__)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010072 default:
Anthony Barbiereaefd002018-07-20 17:49:35 +010073 return nullptr;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010074 }
75}
76
Michalis Spyrou1a569a32019-09-10 17:20:34 +010077template <typename TypeInput, typename TypeOutput>
78class FallbackTransform : public ITransformWeights
79{
80public:
81 void run() override
82 {
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,
164 arm_gemm::GemmArgs<TypeOutput> 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
167 // Inherited methods overridden:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100168 void run() override;
169 void prepare() override;
170 bool is_configured() const override;
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100171
Anthony Barbiereaefd002018-07-20 17:49:35 +0100172private:
173 /** Allocate a workspace tensor.
174 *
175 * @param[in] workspace_size Size to allocate.
176 * @param[in] memory_group Tensor memory group.
177 * @param[in] alignment Workspace memory alignment.
178 */
Anthony Barbier20394d52018-08-02 11:29:09 +0100179 void allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100180
Anthony Barbiereaefd002018-07-20 17:49:35 +0100181 /** Assembly Gemm kernel */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100182 std::shared_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100183 /** Optimised NEON kernel */
184 std::unique_ptr<INEKernel> _optimised_kernel{ nullptr };
185 /** Input A */
186 const ITensor *_a
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100187 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100188 nullptr
189 };
190 /** Input B */
191 const ITensor *_b
192 {
193 nullptr
194 };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100195 const ITensor *_c
196 {
197 nullptr
198 };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100199 /** Output */
200 ITensor *_d{ nullptr };
201 /** GEMM workspace */
202 Tensor _workspace{};
203 /** Pre-transpose tensor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100204 ITensor *_pretranspose{ nullptr };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100205 /** Prepared flag */
206 bool _is_prepared{ false };
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100207 /** GEMM meta-data */
208 GEMMInfo _gemm_info{};
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100209 /** Weights manager */
210 IWeightsManager *_weights_manager{ nullptr };
211 /** Weights transform object */
212 FallbackTransform<TypeInput, TypeOutput> _weights_transform{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100213};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100214
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100215template <typename TypeInput, typename TypeOutput, class OutputStage>
216void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
217 arm_gemm::GemmArgs<TypeOutput> args, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100218 MemoryGroup &memory_group, IWeightsManager *weights_manager, const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100219{
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000220 arm_gemm::GemmConfig gemm_cfg;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100221 const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput, OutputStage>(args, os);
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100222 _weights_manager = weights_manager;
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000223 if(gemm_kernel_info.method != arm_gemm::GemmMethod::GEMV_BATCHED)
224 {
225 gemm_cfg.filter = gemm_kernel_info.name;
226 args._cfg = &gemm_cfg;
227 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100228 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100229 if(_gemm_kernel_asm == nullptr)
230 {
231 //configuration not supported: Leave function unconfigured:
232 return;
233 }
234
235 // arm_compute wrapper for the Gemm object (see above)
236 std::unique_ptr<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>> acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>>();
237 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000238 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100239 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
240 if(workspace_size > 0)
241 {
242 // Allocate workspace
243 const unsigned int alignment = 4096;
Anthony Barbier20394d52018-08-02 11:29:09 +0100244 allocate_workspace(workspace_size, memory_group, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100245 }
246
247 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
248 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
249 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100250 const int window_size = _gemm_kernel_asm->get_window_size();
251 if(window_size < args._maxthreads)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100252 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100253 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100254 }
255 }
256
257 _optimised_kernel = std::move(acl_gemm_wrapper);
258 _a = a;
259 _b = b;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100260 _c = c;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100261 _d = d;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100262 _gemm_info = gemm_info;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100263 // Check for pre-transposed support
264 if(_gemm_kernel_asm->B_pretranspose_required())
265 {
266 // Forcing 128-byte alignment (required by 32-bit kernels)
267 const unsigned int alignment = 128;
268 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100269 if(weights_manager && _weights_manager->are_weights_managed(b))
270 {
271 _weights_transform.configure(B_pretranspose_size, alignment);
272 _pretranspose = _weights_manager->acquire(b, &_weights_transform);
273 }
274 else
275 {
276 _pretranspose = new Tensor();
277 static_cast<Tensor *>(_pretranspose)->allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
278 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100279 }
280}
281
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100282template <typename TypeInput, typename TypeOutput, class OutputStage>
283void Fallback<TypeInput, TypeOutput, OutputStage>::prepare()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100284{
285 if(!_is_prepared)
286 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100287 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
288 if(_c && _c->info()->data_type() == DataType::S32)
289 {
290 _gemm_kernel_asm->set_quantized_bias(reinterpret_cast<const int32_t *>(_c->buffer() + _c->info()->offset_first_element_in_bytes()));
291 }
292
Anthony Barbier71d9b572018-07-06 17:05:59 +0100293 // Pretranspose B if required
294 if(_gemm_kernel_asm->B_pretranspose_required())
295 {
296 const int ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitaseb84d6b2018-07-27 18:28:10 +0100297 const auto in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
Anthony Barbier71d9b572018-07-06 17:05:59 +0100298 const int multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
299
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100300 if(_weights_manager && _weights_manager->are_weights_managed(_b))
301 {
302 _weights_transform.set_args(ldb, in1_ptr, multi_stride_b, _gemm_kernel_asm);
303 _weights_manager->run(_b, &_weights_transform);
304
305 // If we didn't run the reshape function, set the pretransposed buffer
306 if(!_weights_transform.is_reshape_run())
307 {
308 _weights_transform.set_pretranspose(_pretranspose);
309 }
310 }
311 else
312 {
313 static_cast<Tensor *>(_pretranspose)->allocator()->allocate();
314 ARM_COMPUTE_ERROR_ON(_pretranspose->buffer() == nullptr);
315 _gemm_kernel_asm->pretranspose_B_array(_pretranspose->buffer(), in1_ptr, ldb, multi_stride_b);
316 _b->mark_as_unused();
317 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100318 }
319
320 _is_prepared = true;
321 }
322}
323
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100324template <typename TypeInput, typename TypeOutput, class OutputStage>
325void Fallback<TypeInput, TypeOutput, OutputStage>::allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100326{
327 ARM_COMPUTE_ERROR_ON_MSG(workspace_size == 0, "size cannot be 0");
328 _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 +0100329 memory_group.manage(&_workspace);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100330 _workspace.allocator()->allocate();
331}
332
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100333template <typename TypeInput, typename TypeOutput, class OutputStage>
334bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100335{
336 return _optimised_kernel != nullptr;
337}
338
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100339template <typename TypeInput, typename TypeOutput, class OutputStage>
340void Fallback<TypeInput, TypeOutput, OutputStage>::run()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100341{
342 const int lda = _a->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100343 int ldb = 0;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100344 const int ldd = _d->info()->strides_in_bytes().y() / sizeof(TypeOutput);
345
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100346 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d() != 0 ? 3 : 2;
347 const size_t a_multi_idx = a_batch_idx + 1;
348 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d() != 0 ? 3 : 2;
349 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100350
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100351 const int batch_stride_a = _a->info()->strides_in_bytes()[a_batch_idx] / sizeof(TypeInput);
352 const int batch_stride_d = _d->info()->strides_in_bytes()[d_batch_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100353
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100354 const int multi_stride_a = _a->info()->strides_in_bytes()[a_multi_idx] / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100355 int multi_stride_b = 0;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100356 const int multi_stride_d = _d->info()->strides_in_bytes()[d_multi_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100357
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100358 const auto in0_ptr = reinterpret_cast<const TypeInput *>(_a->buffer() + _a->info()->offset_first_element_in_bytes());
359 const TypeInput *in1_ptr = nullptr;
360 auto out_ptr = reinterpret_cast<TypeOutput *>(_d->buffer() + _d->info()->offset_first_element_in_bytes());
361
362 // Check if B is pre-tranposed and de-reference if not
363 if(!_gemm_kernel_asm->B_is_pretransposed())
364 {
365 ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
366 multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
367 in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
368 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100369
370 // Set workspace if needed and reset number of threads as buffer manager gets re-created with max_threads
371 if(_workspace.buffer() != nullptr)
372 {
373 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(_workspace.buffer()));
374 const unsigned int window_size = _gemm_kernel_asm->get_window_size();
375 unsigned int num_threads = NEScheduler::get().num_threads();
376 if(window_size < num_threads)
377 {
378 num_threads = window_size;
379 _gemm_kernel_asm->set_nthreads(num_threads);
380 }
381 }
382
383 // Prepare assembly kernel
384 prepare();
385
386 // Set gemm parameters
387 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a, in1_ptr, ldb, multi_stride_b, out_ptr, ldd, batch_stride_d, multi_stride_d);
388
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 Pinitascfa2bba2019-06-27 17:00:52 +0100394void create_function_or_arm_gemm(std::unique_ptr<IFunction> &acl_function, std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
395 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100396 std::shared_ptr<IMemoryManager> memory_manager, 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 Pinitas37d080f2019-06-21 18:43:12 +0100402 arm_gemm::GemmArgs<TypeOutput> args(&ci, p.M, p.N, p.K, p.batches, p.multis, false, false, alpha, beta, num_threads, gemm_info.pretranpose_B());
Anthony Barbiereaefd002018-07-20 17:49:35 +0100403
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100404 // Try to create an ACL function:
405 const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput>(args);
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100406 acl_function = create_function_all_types(gemm_kernel_info, a, b, d, alpha, beta, gemm_info, std::move(memory_manager), weights_manager);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000407
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100408 // If we still don't have an ACL function:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100409 if(acl_function == nullptr)
410 {
411 //Fallback onto arm_gemm function if ACL doesn't support this method.
412 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput>>();
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100413 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100414 arm_gemm = std::move(fallback);
415 }
416}
417
418template <typename TypeInput, typename TypeOutput>
419void create_function_or_arm_gemm_quant(std::unique_ptr<IFunction> &acl_function, std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
420 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info,
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100421 std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100422{
423 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
424 const CPUInfo &ci = NEScheduler::get().cpu_info();
425 unsigned int num_threads = NEScheduler::get().num_threads();
426
427 arm_gemm::GemmArgs<TypeOutput> args(&ci, p.M, p.N, p.K, p.batches, p.multis, false, false, alpha, beta, num_threads, gemm_info.pretranpose_B());
428
429 // Configure requantization info
430 const int32_t a_offset = -a->info()->quantization_info().uniform().offset;
431 const int32_t b_offset = -b->info()->quantization_info().uniform().offset;
432 const GEMMLowpOutputStageInfo os_info = gemm_info.gemmlowp_output_stage();
433
434 const arm_gemm::ARequantizeLayer32 gemm_requant_info(nullptr,
435 a_offset, b_offset, os_info.gemmlowp_offset,
436 -os_info.gemmlowp_shift, os_info.gemmlowp_multiplier,
437 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
438
439 // Try to create an ACL function:
440 const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput>(args, gemm_requant_info);
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100441 acl_function = create_function_all_types(gemm_kernel_info, a, b, d, alpha, beta, gemm_info, std::move(memory_manager), weights_manager);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100442
443 // If we still don't have an ACL function:
444 if(acl_function == nullptr)
445 {
446 // Fallback onto arm_gemm function if ACL doesn't support this method.
447 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::ARequantizeLayer32>>();
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100448 fallback->configure(a, b, c, d, args, gemm_info, memory_group, weights_manager, gemm_requant_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100449 arm_gemm = std::move(fallback);
450 }
451}
452
453} //namespace
454
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100455NEGEMMAssemblyDispatch::NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
456 : _function(nullptr), _arm_gemm(nullptr), _memory_group(memory_manager), _memory_manager(memory_manager), _weights_manager(weights_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100457{
458}
459
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100460Status NEGEMMAssemblyDispatch::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100461{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100462 ARM_COMPUTE_UNUSED(alpha, beta, gemm_info);
463 ARM_COMPUTE_UNUSED(c);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100464 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
465 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
466#ifndef __aarch64__
467 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");
468#endif /* __aarch64__ */
469 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::U8, DataType::QASYMM8, DataType::S8, DataType::F16);
470 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
471 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32, "Only F32 output supported for F32 input");
472 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 +0100473 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 +0100474 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 +0100475 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 +0100476 return Status{};
477}
478
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100479void NEGEMMAssemblyDispatch::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100480{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100481 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100482
483 //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 Pinitascfa2bba2019-06-27 17:00:52 +0100484 if(!NEGEMMAssemblyDispatch::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, d->info(), alpha, beta, gemm_info))
Anthony Barbiereaefd002018-07-20 17:49:35 +0100485 {
486 return;
487 }
488
489 switch(a->info()->data_type())
490 {
491 case DataType::F32:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100492 create_function_or_arm_gemm<float, float>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager, _weights_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100493 break;
494#ifdef __aarch64__
495 case DataType::U8:
496 case DataType::QASYMM8:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100497 if(d->info()->data_type() == DataType::S32)
498 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100499 create_function_or_arm_gemm<uint8_t, uint32_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager, _weights_manager);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100500 }
501 else
502 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100503 create_function_or_arm_gemm_quant<uint8_t, uint8_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager, _weights_manager);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100504 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100505 break;
506 case DataType::S8:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100507 create_function_or_arm_gemm<int8_t, int32_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager, _weights_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100508 break;
509#endif /* __aarch64__ */
510#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
511 case DataType::F16:
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100512 create_function_or_arm_gemm<float16_t, float16_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager, _weights_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100513 break;
514#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
515 default:
516 break;
517 }
518}
519
520void NEGEMMAssemblyDispatch::prepare()
521{
522 if(_function != nullptr)
523 {
524 _function->prepare();
525 }
526 else
527 {
528 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
529 _arm_gemm->prepare();
530 }
531}
532
533bool NEGEMMAssemblyDispatch::is_configured() const
534{
535 return (_arm_gemm != nullptr && _arm_gemm->is_configured()) || _function != nullptr;
536}
537
538void NEGEMMAssemblyDispatch::run()
539{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100540 MemoryGroupResourceScope scope_mg(_memory_group);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100541 if(_function != nullptr)
542 {
543 _function->run();
544 }
545 else
546 {
547 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
548 _arm_gemm->run();
549 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100550}
Anthony Barbier71d9b572018-07-06 17:05:59 +0100551} //namespace arm_compute