blob: 2a4498b0a94cd0beeb5f90592badb5db844f7324 [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,
Anthony Barbier3d677cc2018-07-23 16:42:59 +010041 std::shared_ptr<IMemoryManager> memory_manager)
42
Anthony Barbiereaefd002018-07-20 17:49:35 +010043{
Georgios Pinitas37d080f2019-06-21 18:43:12 +010044 // 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 +000045 switch(gemm_kernel_info.method)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010046 {
Anthony Barbier3d677cc2018-07-23 16:42:59 +010047 case arm_gemm::GemmMethod::GEMM_INTERLEAVED:
48 {
Georgios Pinitas37d080f2019-06-21 18:43:12 +010049 if(!gemm_info.pretranpose_B())
Anthony Barbier3d677cc2018-07-23 16:42:59 +010050 {
51 return nullptr;
52 }
53 auto function = support::cpp14::make_unique<NEGEMMInterleavedWrapper>(memory_manager);
Georgios Pinitas37d080f2019-06-21 18:43:12 +010054 function->configure(a, b, d, alpha, beta, gemm_info);
Anthony Barbier3d677cc2018-07-23 16:42:59 +010055 return std::move(function);
56 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000057#if defined(__aarch64__)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010058 case arm_gemm::GemmMethod::GEMM_NATIVE:
59 {
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000060 if(gemm_kernel_info.name.find("sgemm_native_16x4") != std::string::npos)
61 {
62 auto kernel = support::cpp14::make_unique<NEGEMMNativeWrapperKernel<float, float>>();
Georgios Pinitas37d080f2019-06-21 18:43:12 +010063 kernel->configure(a, b, d, alpha, beta, gemm_info);
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000064 auto function = support::cpp14::make_unique<NESimpleAssemblyFunction>();
65 function->configure(std::move(kernel));
66 return std::move(function);
67 }
68 return nullptr;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010069 }
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000070#endif // defined(__aarch64__)
Anthony Barbierc8e84b52018-07-17 16:48:42 +010071 default:
Anthony Barbiereaefd002018-07-20 17:49:35 +010072 return nullptr;
Anthony Barbierc8e84b52018-07-17 16:48:42 +010073 }
74}
75
Anthony Barbiereaefd002018-07-20 17:49:35 +010076/** Fallback in case ACL doesn't have a function */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010077template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing>
Anthony Barbiereaefd002018-07-20 17:49:35 +010078class Fallback : public NEGEMMAssemblyDispatch::IFallback
Anthony Barbierc8e84b52018-07-17 16:48:42 +010079{
Anthony Barbiereaefd002018-07-20 17:49:35 +010080public:
Georgios Pinitas3dbfd232019-01-30 17:17:16 +000081 /** Initialise the functions's input and output.
82 *
83 * @param[in] a Input tensor containing the Matrix A.
84 * @param[in] b Input tensor containing the Matrix B.
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010085 * @param[in] c Input tensor containing the Matrix C.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +000086 * @param[out] d Output tensor to store the result of matrix multiplication.
87 * @param[in] args Matrix multiplication information.
Georgios Pinitas37d080f2019-06-21 18:43:12 +010088 * @param[in] gemm_info GEMM meta-data
Georgios Pinitas3dbfd232019-01-30 17:17:16 +000089 * @param[in] memory_group Memory group to be used by the function.
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010090 * @param[in] os Output stage meta-data.
Georgios Pinitas3dbfd232019-01-30 17:17:16 +000091 */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010092 void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
93 arm_gemm::GemmArgs<TypeOutput> args, const GEMMInfo &gemm_info,
94 MemoryGroup &memory_group, const OutputStage &os = {});
Georgios Pinitas3dbfd232019-01-30 17:17:16 +000095
96 // Inherited methods overridden:
Anthony Barbiereaefd002018-07-20 17:49:35 +010097 void run() override;
98 void prepare() override;
99 bool is_configured() const override;
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100100
Anthony Barbiereaefd002018-07-20 17:49:35 +0100101private:
102 /** Allocate a workspace tensor.
103 *
104 * @param[in] workspace_size Size to allocate.
105 * @param[in] memory_group Tensor memory group.
106 * @param[in] alignment Workspace memory alignment.
107 */
Anthony Barbier20394d52018-08-02 11:29:09 +0100108 void allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100109
Anthony Barbiereaefd002018-07-20 17:49:35 +0100110 /** Assembly Gemm kernel */
111 std::unique_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
112 /** Optimised NEON kernel */
113 std::unique_ptr<INEKernel> _optimised_kernel{ nullptr };
114 /** Input A */
115 const ITensor *_a
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100116 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100117 nullptr
118 };
119 /** Input B */
120 const ITensor *_b
121 {
122 nullptr
123 };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100124 const ITensor *_c
125 {
126 nullptr
127 };
Anthony Barbiereaefd002018-07-20 17:49:35 +0100128 /** Output */
129 ITensor *_d{ nullptr };
130 /** GEMM workspace */
131 Tensor _workspace{};
132 /** Pre-transpose tensor */
133 Tensor _pretranspose{};
134 /** Prepared flag */
135 bool _is_prepared{ false };
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100136 /** GEMM meta-data */
137 GEMMInfo _gemm_info{};
Anthony Barbiereaefd002018-07-20 17:49:35 +0100138};
Anthony Barbier71d9b572018-07-06 17:05:59 +0100139
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100140template <typename TypeInput, typename TypeOutput, class OutputStage>
141void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d,
142 arm_gemm::GemmArgs<TypeOutput> args, const GEMMInfo &gemm_info,
143 MemoryGroup &memory_group, const OutputStage &os)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100144{
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000145 arm_gemm::GemmConfig gemm_cfg;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100146 const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput, OutputStage>(args, os);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000147 if(gemm_kernel_info.method != arm_gemm::GemmMethod::GEMV_BATCHED)
148 {
149 gemm_cfg.filter = gemm_kernel_info.name;
150 args._cfg = &gemm_cfg;
151 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100152 _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100153 if(_gemm_kernel_asm == nullptr)
154 {
155 //configuration not supported: Leave function unconfigured:
156 return;
157 }
158
159 // arm_compute wrapper for the Gemm object (see above)
160 std::unique_ptr<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>> acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>>();
161 ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr);
Georgios Pinitas3dbfd232019-01-30 17:17:16 +0000162 acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100163 const size_t workspace_size = _gemm_kernel_asm->get_working_size();
164 if(workspace_size > 0)
165 {
166 // Allocate workspace
167 const unsigned int alignment = 4096;
Anthony Barbier20394d52018-08-02 11:29:09 +0100168 allocate_workspace(workspace_size, memory_group, alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100169 }
170
171 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
172 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
173 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100174 const int window_size = _gemm_kernel_asm->get_window_size();
175 if(window_size < args._maxthreads)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100176 {
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100177 _gemm_kernel_asm->set_nthreads(window_size);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100178 }
179 }
180
181 _optimised_kernel = std::move(acl_gemm_wrapper);
182 _a = a;
183 _b = b;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100184 _c = c;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100185 _d = d;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100186 _gemm_info = gemm_info;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100187 // Check for pre-transposed support
188 if(_gemm_kernel_asm->B_pretranspose_required())
189 {
190 // Forcing 128-byte alignment (required by 32-bit kernels)
191 const unsigned int alignment = 128;
192 const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size();
193 _pretranspose.allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100194 }
195}
196
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100197template <typename TypeInput, typename TypeOutput, class OutputStage>
198void Fallback<TypeInput, TypeOutput, OutputStage>::prepare()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100199{
200 if(!_is_prepared)
201 {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100202 // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C.
203 if(_c && _c->info()->data_type() == DataType::S32)
204 {
205 _gemm_kernel_asm->set_quantized_bias(reinterpret_cast<const int32_t *>(_c->buffer() + _c->info()->offset_first_element_in_bytes()));
206 }
207
Anthony Barbier71d9b572018-07-06 17:05:59 +0100208 // Pretranspose B if required
209 if(_gemm_kernel_asm->B_pretranspose_required())
210 {
Georgios Pinitasca1250d2018-11-22 19:38:27 +0000211 _pretranspose.allocator()->allocate();
Anthony Barbier3d677cc2018-07-23 16:42:59 +0100212 ARM_COMPUTE_ERROR_ON(_pretranspose.buffer() == nullptr);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100213 const int ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitaseb84d6b2018-07-27 18:28:10 +0100214 const auto in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
Anthony Barbier71d9b572018-07-06 17:05:59 +0100215 const int multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
216
Anthony Barbier71d9b572018-07-06 17:05:59 +0100217 _gemm_kernel_asm->pretranspose_B_array(_pretranspose.buffer(), in1_ptr, ldb, multi_stride_b);
218 _b->mark_as_unused();
219 }
220
221 _is_prepared = true;
222 }
223}
224
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100225template <typename TypeInput, typename TypeOutput, class OutputStage>
226void Fallback<TypeInput, TypeOutput, OutputStage>::allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment)
Anthony Barbier71d9b572018-07-06 17:05:59 +0100227{
228 ARM_COMPUTE_ERROR_ON_MSG(workspace_size == 0, "size cannot be 0");
229 _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 +0100230 memory_group.manage(&_workspace);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100231 _workspace.allocator()->allocate();
232}
233
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100234template <typename TypeInput, typename TypeOutput, class OutputStage>
235bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const
Anthony Barbier71d9b572018-07-06 17:05:59 +0100236{
237 return _optimised_kernel != nullptr;
238}
239
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100240template <typename TypeInput, typename TypeOutput, class OutputStage>
241void Fallback<TypeInput, TypeOutput, OutputStage>::run()
Anthony Barbier71d9b572018-07-06 17:05:59 +0100242{
243 const int lda = _a->info()->strides_in_bytes().y() / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100244 int ldb = 0;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100245 const int ldd = _d->info()->strides_in_bytes().y() / sizeof(TypeOutput);
246
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100247 const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d() != 0 ? 3 : 2;
248 const size_t a_multi_idx = a_batch_idx + 1;
249 const size_t d_batch_idx = _gemm_info.depth_output_gemm3d() != 0 ? 3 : 2;
250 const size_t d_multi_idx = d_batch_idx + 1;
Anthony Barbier71d9b572018-07-06 17:05:59 +0100251
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100252 const int batch_stride_a = _a->info()->strides_in_bytes()[a_batch_idx] / sizeof(TypeInput);
253 const int batch_stride_d = _d->info()->strides_in_bytes()[d_batch_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100254
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100255 const int multi_stride_a = _a->info()->strides_in_bytes()[a_multi_idx] / sizeof(TypeInput);
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100256 int multi_stride_b = 0;
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100257 const int multi_stride_d = _d->info()->strides_in_bytes()[d_multi_idx] / sizeof(TypeOutput);
Anthony Barbier71d9b572018-07-06 17:05:59 +0100258
Georgios Pinitas40ed6d82018-07-31 17:22:11 +0100259 const auto in0_ptr = reinterpret_cast<const TypeInput *>(_a->buffer() + _a->info()->offset_first_element_in_bytes());
260 const TypeInput *in1_ptr = nullptr;
261 auto out_ptr = reinterpret_cast<TypeOutput *>(_d->buffer() + _d->info()->offset_first_element_in_bytes());
262
263 // Check if B is pre-tranposed and de-reference if not
264 if(!_gemm_kernel_asm->B_is_pretransposed())
265 {
266 ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
267 multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
268 in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes());
269 }
Anthony Barbier71d9b572018-07-06 17:05:59 +0100270
271 // Set workspace if needed and reset number of threads as buffer manager gets re-created with max_threads
272 if(_workspace.buffer() != nullptr)
273 {
274 _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(_workspace.buffer()));
275 const unsigned int window_size = _gemm_kernel_asm->get_window_size();
276 unsigned int num_threads = NEScheduler::get().num_threads();
277 if(window_size < num_threads)
278 {
279 num_threads = window_size;
280 _gemm_kernel_asm->set_nthreads(num_threads);
281 }
282 }
283
284 // Prepare assembly kernel
285 prepare();
286
287 // Set gemm parameters
288 _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);
289
290 // Schedule assembly kernel
291 NEScheduler::get().schedule(_optimised_kernel.get(), Window::DimX);
292}
293
Anthony Barbiereaefd002018-07-20 17:49:35 +0100294template <typename TypeInput, typename TypeOutput>
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100295void create_function_or_arm_gemm(std::unique_ptr<IFunction> &acl_function, std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
296 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info,
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100297 std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100298{
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100299 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100300 const CPUInfo &ci = NEScheduler::get().cpu_info();
301 unsigned int num_threads = NEScheduler::get().num_threads();
302
Georgios Pinitas37d080f2019-06-21 18:43:12 +0100303 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 +0100304
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100305 // Try to create an ACL function:
306 const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput>(args);
307 acl_function = create_function_all_types(gemm_kernel_info, a, b, d, alpha, beta, gemm_info, std::move(memory_manager));
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000308
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100309 // If we still don't have an ACL function:
Anthony Barbiereaefd002018-07-20 17:49:35 +0100310 if(acl_function == nullptr)
311 {
312 //Fallback onto arm_gemm function if ACL doesn't support this method.
313 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput>>();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100314 fallback->configure(a, b, c, d, args, gemm_info, memory_group);
315 arm_gemm = std::move(fallback);
316 }
317}
318
319template <typename TypeInput, typename TypeOutput>
320void create_function_or_arm_gemm_quant(std::unique_ptr<IFunction> &acl_function, std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group,
321 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info,
322 std::shared_ptr<IMemoryManager> memory_manager)
323{
324 INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info);
325 const CPUInfo &ci = NEScheduler::get().cpu_info();
326 unsigned int num_threads = NEScheduler::get().num_threads();
327
328 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());
329
330 // Configure requantization info
331 const int32_t a_offset = -a->info()->quantization_info().uniform().offset;
332 const int32_t b_offset = -b->info()->quantization_info().uniform().offset;
333 const GEMMLowpOutputStageInfo os_info = gemm_info.gemmlowp_output_stage();
334
335 const arm_gemm::ARequantizeLayer32 gemm_requant_info(nullptr,
336 a_offset, b_offset, os_info.gemmlowp_offset,
337 -os_info.gemmlowp_shift, os_info.gemmlowp_multiplier,
338 os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound);
339
340 // Try to create an ACL function:
341 const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput>(args, gemm_requant_info);
342 acl_function = create_function_all_types(gemm_kernel_info, a, b, d, alpha, beta, gemm_info, std::move(memory_manager));
343
344 // If we still don't have an ACL function:
345 if(acl_function == nullptr)
346 {
347 // Fallback onto arm_gemm function if ACL doesn't support this method.
348 auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::ARequantizeLayer32>>();
349 fallback->configure(a, b, c, d, args, gemm_info, memory_group, gemm_requant_info);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100350 arm_gemm = std::move(fallback);
351 }
352}
353
354} //namespace
355
356NEGEMMAssemblyDispatch::NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier3d677cc2018-07-23 16:42:59 +0100357 : _function(nullptr), _arm_gemm(nullptr), _memory_group(memory_manager), _memory_manager(memory_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +0100358{
359}
360
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100361Status 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 +0100362{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100363 ARM_COMPUTE_UNUSED(alpha, beta, gemm_info);
364 ARM_COMPUTE_UNUSED(c);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100365 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d);
366 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a);
367#ifndef __aarch64__
368 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");
369#endif /* __aarch64__ */
370 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::U8, DataType::QASYMM8, DataType::S8, DataType::F16);
371 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
372 ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32, "Only F32 output supported for F32 input");
373 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 +0100374 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 +0100375 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 +0100376 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 +0100377 return Status{};
378}
379
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100380void 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 +0100381{
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100382 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100383
384 //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 +0100385 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 +0100386 {
387 return;
388 }
389
390 switch(a->info()->data_type())
391 {
392 case DataType::F32:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100393 create_function_or_arm_gemm<float, float>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100394 break;
395#ifdef __aarch64__
396 case DataType::U8:
397 case DataType::QASYMM8:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100398 if(d->info()->data_type() == DataType::S32)
399 {
400 create_function_or_arm_gemm<uint8_t, uint32_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager);
401 }
402 else
403 {
404 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);
405 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100406 break;
407 case DataType::S8:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100408 create_function_or_arm_gemm<int8_t, int32_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100409 break;
410#endif /* __aarch64__ */
411#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
412 case DataType::F16:
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100413 create_function_or_arm_gemm<float16_t, float16_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100414 break;
415#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
416 default:
417 break;
418 }
419}
420
421void NEGEMMAssemblyDispatch::prepare()
422{
423 if(_function != nullptr)
424 {
425 _function->prepare();
426 }
427 else
428 {
429 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
430 _arm_gemm->prepare();
431 }
432}
433
434bool NEGEMMAssemblyDispatch::is_configured() const
435{
436 return (_arm_gemm != nullptr && _arm_gemm->is_configured()) || _function != nullptr;
437}
438
439void NEGEMMAssemblyDispatch::run()
440{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100441 MemoryGroupResourceScope scope_mg(_memory_group);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100442 if(_function != nullptr)
443 {
444 _function->run();
445 }
446 else
447 {
448 ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr);
449 _arm_gemm->run();
450 }
Anthony Barbiereaefd002018-07-20 17:49:35 +0100451}
Anthony Barbier71d9b572018-07-06 17:05:59 +0100452} //namespace arm_compute