blob: 09637dd2d6f4910b514ea7b0d84e9c154687163d [file] [log] [blame]
Georgios Pinitasda953f22019-04-02 17:27:03 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Pablo Tello181e6512017-11-15 13:28:27 +00003 *
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/NEGEMMLowpAssemblyMatrixMultiplyCore.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Pablo Tello181e6512017-11-15 13:28:27 +000029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
33#include "arm_compute/runtime/TensorAllocator.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010034#include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
35#include "src/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
36#include "src/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Matthew Bentham92046462020-03-07 22:15:55 +000037#include "support/MemorySupport.h"
Pablo Tello181e6512017-11-15 13:28:27 +000038
Michalis Spyrouebcebf12020-10-21 00:04:14 +010039namespace arm_compute
40{
41NEGEMMLowpAssemblyMatrixMultiplyCore::~NEGEMMLowpAssemblyMatrixMultiplyCore() = default;
Pablo Tello181e6512017-11-15 13:28:27 +000042
43NEGEMMLowpAssemblyMatrixMultiplyCore::NEGEMMLowpAssemblyMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbiereaefd002018-07-20 17:49:35 +010044 : _memory_group(memory_manager), _asm_glue(memory_manager), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _tmp_a(), _tmp_b()
Pablo Tello181e6512017-11-15 13:28:27 +000045{
46}
47
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010048void NEGEMMLowpAssemblyMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output)
Pablo Tello181e6512017-11-15 13:28:27 +000049{
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000050 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::S8);
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
Pablo Tello181e6512017-11-15 13:28:27 +000052 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
53 ARM_COMPUTE_ERROR_ON_MSG((a)->info()->dimension(0) != (b)->info()->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B");
54 ARM_COMPUTE_ERROR_ON_MSG((a)->info()->dimension(1) != (output)->info()->dimension(1), "The output matrix must have the same number of rows as the matrix A");
55 ARM_COMPUTE_ERROR_ON_MSG((b)->info()->dimension(0) != (output)->info()->dimension(0), "The output matrix must have the same number of columns as the matrix B");
56
Pablo Telloeb82fd22018-02-23 13:43:50 +000057 bool run_optimised = false;
Pablo Telloeb82fd22018-02-23 13:43:50 +000058 switch(a->info()->data_type())
59 {
60 case DataType::S8:
Pablo Tello66c656a2018-03-15 10:34:58 +000061 case DataType::QASYMM8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000062 case DataType::U8:
63 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010064 _asm_glue.configure(a, b, c, output, GEMMInfo(false, false, true));
Anthony Barbiereaefd002018-07-20 17:49:35 +010065 run_optimised = _asm_glue.is_configured();
Pablo Telloeb82fd22018-02-23 13:43:50 +000066 break;
67 }
68 default:
69 {
70 ARM_COMPUTE_ERROR("Datatype not supported");
71 break;
72 }
73 }
Pablo Telloeb82fd22018-02-23 13:43:50 +000074 if(!run_optimised)
Pablo Tello181e6512017-11-15 13:28:27 +000075 {
76 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
77 TensorShape shape_tmp_a = a->info()->tensor_shape();
78 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
79 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.f));
80
81 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
82 TensorShape shape_tmp_b = b->info()->tensor_shape();
83 shape_tmp_b.set(0, b->info()->dimension(1) * 16);
84 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / 16.f));
85
86 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type());
87 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type());
88 _tmp_a.allocator()->init(info_a);
89 _tmp_b.allocator()->init(info_b);
90 _memory_group.manage(&_tmp_a);
91 _memory_group.manage(&_tmp_b);
92
93 // Configure interleave kernel
94 {
95 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
96 k->configure(a, &_tmp_a);
97 _mtx_a_reshape_kernel = std::move(k);
98 }
99
100 // Configure transpose kernel
101 {
102 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
103 k->configure(b, &_tmp_b);
104 _mtx_b_reshape_kernel = std::move(k);
105 }
106
107 // Configure matrix multiply kernel
108 {
109 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
110 k->configure(&_tmp_a, &_tmp_b, output);
111 _mm_kernel = std::move(k);
112 }
113
114 // Allocate tensors
115 _tmp_a.allocator()->allocate();
116 _tmp_b.allocator()->allocate();
117 }
118}
119
120void NEGEMMLowpAssemblyMatrixMultiplyCore::run()
121{
Georgios Pinitasda953f22019-04-02 17:27:03 +0100122 MemoryGroupResourceScope scope_mg(_memory_group);
Pablo Tello181e6512017-11-15 13:28:27 +0000123 if(_mtx_a_reshape_kernel)
124 {
125 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
126 }
127
128 if(_mtx_b_reshape_kernel)
129 {
130 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
131 }
132
Anthony Barbiereaefd002018-07-20 17:49:35 +0100133 if(_asm_glue.is_configured())
Pablo Telloeb82fd22018-02-23 13:43:50 +0000134 {
Anthony Barbiereaefd002018-07-20 17:49:35 +0100135 _asm_glue.run();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000136 }
137 else
138 {
139 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
140 }
Pablo Tello181e6512017-11-15 13:28:27 +0000141}
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100142} // namespace arm_compute