blob: 0fff6c9ca1c940e08b6eb6020c7d963dc1cfbd8d [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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/NEGEMMLowpMatrixMultiplyCore.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Pablo Tello6ff12a02017-11-02 16:09:35 +000029#include "arm_compute/core/NEON/kernels/NEGEMMAssemblyBaseKernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010030#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010031#include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
32#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
33#include "arm_compute/core/NEON/kernels/arm64/NEGEMMLowpAArch64V8P4Kernel.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/runtime/NEON/NEScheduler.h"
38#include "arm_compute/runtime/TensorAllocator.h"
39#include "support/ToolchainSupport.h"
40
Pablo Tello6ff12a02017-11-02 16:09:35 +000041namespace arm_compute
42{
43#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
Pablo Tello6681d242017-11-13 16:44:08 +000044#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_gemm_u8_12x8.hpp"
Pablo Tello6ff12a02017-11-02 16:09:35 +000045} // namespace arm_compute
46
Gian Marco Iodiceab182122017-10-09 15:05:40 +010047using namespace arm_compute;
48
49NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Gian Marcoe75a02b2017-11-08 12:24:09 +000050 : _memory_group(std::move(memory_manager)), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _mtx_a_reduction_kernel(), _mtx_b_reduction_kernel(),
51 _offset_contribution_kernel(), _vector_sum_col(), _vector_sum_row(), _tmp_a(), _tmp_b(), _workspace(), _a_offset(0), _b_offset(0)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010052{
53}
54
55void NEGEMMLowpMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, ITensor *output)
56{
Gian Marcoe75a02b2017-11-08 12:24:09 +000057 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QASYMM8);
Gian Marco Iodiceab182122017-10-09 15:05:40 +010058 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
59 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
60 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");
61 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");
62 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");
63
Gian Marcoe75a02b2017-11-08 12:24:09 +000064 bool dot_product_path = false;
65
66 _a_offset = a->info()->quantization_info().offset;
67 _b_offset = b->info()->quantization_info().offset;
68
Gian Marco Iodiceab182122017-10-09 15:05:40 +010069#ifdef ARM_COMPUTE_AARCH64_V8_2
70 // Check for DOT product instruction
71 const struct CPUInfo ci = NEScheduler::get().cpu_info();
72 const int cpu_has_dotprod = static_cast<int>(ci.CPU) & static_cast<int>(CPUTarget::DOT);
73
74 if(cpu_has_dotprod != 0)
75 {
Gian Marcoe75a02b2017-11-08 12:24:09 +000076 dot_product_path = true;
77
Gian Marco Iodiceab182122017-10-09 15:05:40 +010078 // Configure matrix multiply kernel
Pablo Tello6ff12a02017-11-02 16:09:35 +000079 struct CPUInfo ci = NEScheduler::get().cpu_info();
80 const int M = output->info()->tensor_shape().y();
81 const int N = output->info()->tensor_shape().x();
82 const int K = a->info()->tensor_shape().x();
83
Pablo Tello6681d242017-11-13 16:44:08 +000084 const size_t workbench_size = GemmInterleaved<gemm_u8_12x8, gemm_u8_12x8::operand_type, gemm_u8_12x8::result_type>(&ci, M, N, K, false, false).get_working_size();
85 constexpr size_t alignment = 4096;
86 _workspace.allocator()->init(TensorInfo(TensorShape{ (workbench_size + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8));
Pablo Tello6ff12a02017-11-02 16:09:35 +000087 _memory_group.manage(&_workspace);
Gian Marcoe75a02b2017-11-08 12:24:09 +000088
Pablo Tello6ff12a02017-11-02 16:09:35 +000089 // Configure matrix multiplication kernel
90 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpAArch64V8P4Kernel>();
91 k->configure(a, b, output, &_workspace, 1.f, 1.f);
92 _mm_kernel = std::move(k);
Gian Marco Iodiceab182122017-10-09 15:05:40 +010093 }
94 else
95#endif /* ARM_COMPUTE_AARCH64_V8_2 */
96 {
97 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
98 TensorShape shape_tmp_a = a->info()->tensor_shape();
99 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
100 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.f));
101
102 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
103 TensorShape shape_tmp_b = b->info()->tensor_shape();
104 shape_tmp_b.set(0, b->info()->dimension(1) * 16);
105 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / 16.f));
106
107 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type());
108 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type());
109 _tmp_a.allocator()->init(info_a);
110 _tmp_b.allocator()->init(info_b);
111 _memory_group.manage(&_tmp_a);
112 _memory_group.manage(&_tmp_b);
113
114 // Configure interleave kernel
115 {
116 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
117 k->configure(a, &_tmp_a);
118 _mtx_a_reshape_kernel = std::move(k);
119 }
120
121 // Configure transpose kernel
122 {
123 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
124 k->configure(b, &_tmp_b);
125 _mtx_b_reshape_kernel = std::move(k);
126 }
127
128 // Configure matrix multiply kernel
129 {
130 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
131 k->configure(&_tmp_a, &_tmp_b, output);
132 _mm_kernel = std::move(k);
133 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000134 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100135
Gian Marcoe75a02b2017-11-08 12:24:09 +0000136 // Initialize matrix B reduction kernel only if _a_offset is not equal to 0
137 if(_a_offset != 0)
138 {
139 TensorShape shape_vector_sum_col = b->info()->tensor_shape();
140 shape_vector_sum_col.remove_dimension(1);
141 TensorInfo info_vector_sum_col(shape_vector_sum_col, 1, DataType::S32);
142 _vector_sum_col.allocator()->init(info_vector_sum_col);
143 _memory_group.manage(&_vector_sum_col);
144
145 // Configure Matrix B reduction kernel
146 _mtx_b_reduction_kernel.configure(b, &_vector_sum_col, a->info()->dimension(0), false);
147 }
148
149 // Initialize Matrix A reduction kernel only if _b_offset is not equal to 0
150 if(_b_offset != 0)
151 {
152 TensorShape shape_vector_sum_row = a->info()->tensor_shape();
153 shape_vector_sum_row.set(Window::DimX, a->info()->dimension(1));
154 shape_vector_sum_row.remove_dimension(1);
155 TensorInfo info_vector_sum_row(shape_vector_sum_row, 1, DataType::S32);
156 _vector_sum_row.allocator()->init(info_vector_sum_row);
157 _memory_group.manage(&_vector_sum_row);
158
159 // Configure matrix A reduction kernel
160 _mtx_a_reduction_kernel.configure(a, &_vector_sum_row, a->info()->dimension(0), false);
161 }
162
163 // Configure offset contribution kernel
164 _offset_contribution_kernel.configure(output, _a_offset == 0 ? nullptr : &_vector_sum_col, _b_offset == 0 ? nullptr : &_vector_sum_row, a->info()->dimension(0), _a_offset, _b_offset);
165
166 // Allocate tensors
167 if(!dot_product_path)
168 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000169 _tmp_a.allocator()->allocate();
170 _tmp_b.allocator()->allocate();
171 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000172 else
173 {
174 _workspace.allocator()->allocate();
175 }
176
177 if(_a_offset != 0)
178 {
179 _vector_sum_col.allocator()->allocate();
180 }
181
182 if(_b_offset != 0)
183 {
184 _vector_sum_row.allocator()->allocate();
185 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100186}
187
188void NEGEMMLowpMatrixMultiplyCore::run()
189{
190 _memory_group.acquire();
191
Pablo Tello6ff12a02017-11-02 16:09:35 +0000192 if(_mtx_a_reshape_kernel)
193 {
194 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
195 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100196
Pablo Tello6ff12a02017-11-02 16:09:35 +0000197 if(_mtx_b_reshape_kernel)
198 {
199 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
200 }
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100201
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100202 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
203
Gian Marcoe75a02b2017-11-08 12:24:09 +0000204 // Run matrix A reduction kernel only if _b_offset is not equal to 0
205 if(_b_offset != 0)
206 {
207 NEScheduler::get().schedule(&_mtx_a_reduction_kernel, Window::DimX);
208 }
209
210 // Run matrix B reduction kernel only if _a_offset is not equal to 0
211 if(_a_offset != 0)
212 {
213 NEScheduler::get().schedule(&_mtx_b_reduction_kernel, Window::DimX);
214 }
215
216 // Run offset contribution kernel
217 NEScheduler::get().schedule(&_offset_contribution_kernel, Window::DimY);
218
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100219 _memory_group.release();
Pablo Tello6ff12a02017-11-02 16:09:35 +0000220}