blob: 3f705ac0a73932afcdb3fcfa7ed3fe8a28fe8bb6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco7b4d5472018-01-10 15:56:30 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/core/CL/kernels/CLGEMMLowpMatrixMultiplyKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
Gian Marco19835e52018-01-30 13:35:54 +000027#include "arm_compute/core/AccessWindowTranspose.h"
Gian Marco7b4d5472018-01-10 15:56:30 +000028#include "arm_compute/core/CL/CLHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/CLKernelLibrary.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/Helpers.h"
Isabella Gottardid56e7702018-02-28 14:29:36 +000034#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Utils.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
Gian Marco19835e52018-01-30 13:35:54 +000039#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +010040#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
42#include <cstddef>
43#include <cstdint>
44#include <tuple>
45
46using namespace arm_compute;
Gian Marco19835e52018-01-30 13:35:54 +000047using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048
49namespace arm_compute
50{
51class Coordinates;
52} // namespace arm_compute
53
Georgios Pinitas358ca202017-12-07 16:47:52 +000054namespace
55{
56using ElementsProcessed = Steps;
57
Gian Marco19835e52018-01-30 13:35:54 +000058Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +000059{
60 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8);
Gian Marco19835e52018-01-30 13:35:54 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
62
Georgios Pinitas358ca202017-12-07 16:47:52 +000063 if(!is_interleaved_transposed)
64 {
65 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != input1->dimension(1));
Gian Marco19835e52018-01-30 13:35:54 +000066
67 if(output->total_size() != 0)
68 {
69 ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(0) != output->dimension(0));
70 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != output->dimension(1));
71 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
72 }
73 }
74 else
75 {
76 const int m = reshape_info.m();
77 const int n = reshape_info.n();
78 const int k = reshape_info.k();
79 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
80 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
81
82 TensorShape tensor_shape0{ input0->tensor_shape() };
83 tensor_shape0.set(0, k);
84 tensor_shape0.set(1, m);
85
86 TensorShape tensor_shape1{ input1->tensor_shape() };
87 tensor_shape1.set(0, n);
88 tensor_shape1.set(1, k);
89
90 const TensorInfo tensor_info0 = input0->clone()->set_tensor_shape(tensor_shape0);
91 const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
92
93 const TensorInfo tensor_info_reshaped0 = input0->clone()->set_tensor_shape(compute_interleaved_shape(tensor_info0, mult_interleave4x4_height));
94 const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(tensor_info1, mult_transpose1xW_width));
95
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input0, &tensor_info_reshaped0);
97 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
98
99 if(output->total_size() != 0)
100 {
101 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != static_cast<size_t>(n));
102 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != static_cast<size_t>(m));
103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
104 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000105 }
106
107 return Status{};
108}
109
110std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, bool is_interleaved_transposed,
111 ElementsProcessed &num_elements_processed)
112{
113 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
114 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
115
116 Window win{};
117 bool window_changed = false;
118
119 // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication
120 if(is_interleaved_transposed)
121 {
Gian Marco19835e52018-01-30 13:35:54 +0000122 // Configure kernel window
123 num_elems_processed_per_iteration_x = 4;
124 num_elems_processed_per_iteration_y = 4;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000125
126 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
127
Gian Marco19835e52018-01-30 13:35:54 +0000128 AccessWindowRectangle input0_access(input0, 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f);
129 AccessWindowTranspose input1_access(input1, 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000130 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
131
132 window_changed = update_window_and_padding(win, input0_access, input1_access, output_access);
133
134 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
135 }
136 else
137 {
138 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor. num_elems_processed_per_iteration_x
Gian Marco7b4d5472018-01-10 15:56:30 +0000139 num_elems_processed_per_iteration_x = 4;
140 num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 5);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000141
142 // Configure window
143 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
144
145 AccessWindowStatic input0_access(input0, 0, 0, input0->dimension(0), ceil_to_multiple(input0->dimension(1), num_elems_processed_per_iteration_y));
146 AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1));
147 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
148
149 window_changed = update_window_and_padding(win, input0_access, input1_access, output_access);
150
151 Coordinates coord;
152 coord.set_num_dimensions(output->num_dimensions());
153 output_access.set_valid_region(win, ValidRegion(coord, output->tensor_shape()));
154 }
155
156 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
157 return std::make_pair(err, win);
158}
159} // namespace
160
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161CLGEMMLowpMatrixMultiplyKernel::CLGEMMLowpMatrixMultiplyKernel()
162 : _input0(nullptr), _input1(nullptr), _output(nullptr)
163{
164}
165
Gian Marco19835e52018-01-30 13:35:54 +0000166void CLGEMMLowpMatrixMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000168 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Gian Marco19835e52018-01-30 13:35:54 +0000169
170 // Output tensor auto inizialitation if not yet initialized
171 TensorShape tensor_shape{ input0->info()->tensor_shape() };
172 tensor_shape.set(0, is_interleaved_transposed ? reshape_info.n() : input1->info()->dimension(0));
173 tensor_shape.set(1, is_interleaved_transposed ? reshape_info.m() : input0->info()->dimension(1));
174
175 auto_init_if_empty(*output->info(), tensor_shape, 1, DataType::S32, 1, QuantizationInfo());
176
177 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info));
Gian Marco05288a22017-11-21 10:57:50 +0000178
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 _input0 = input0;
180 _input1 = input1;
181 _output = output;
182
Georgios Pinitas358ca202017-12-07 16:47:52 +0000183 ElementsProcessed num_elements_processed{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
Gian Marco7b4d5472018-01-10 15:56:30 +0000185 // Get target architecture
186 GPUTarget arch_target = get_arch_from_target(get_target());
187
Georgios Pinitas358ca202017-12-07 16:47:52 +0000188 // Configure kernel window
189 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info(), is_interleaved_transposed, num_elements_processed);
190 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
191 ICLKernel::configure(win_config.second);
192
193 // Create build options
194 CLBuildOptions build_opts;
195 std::string kernel_name(" ");
Gian Marco05288a22017-11-21 10:57:50 +0000196 if(is_interleaved_transposed)
197 {
Gian Marco19835e52018-01-30 13:35:54 +0000198 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
199 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
200
201 // Note: The computation tile has the x dimension equal to 4 which is less than the transpose_width (16)
202 // In order to access correctly the elements from the transposed matrix B, we need to pass
203 // the correct step which is calculated as (16 * mult_transpose1xW_width) / 4)
204
Gian Marco05288a22017-11-21 10:57:50 +0000205 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(input1->info()->dimension(0)));
Gian Marco19835e52018-01-30 13:35:54 +0000206 build_opts.add_option("-DTRANSPOSE1XW_WIDTH_STEP=" + support::cpp11::to_string(4 * mult_transpose1xW_width));
207 build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height));
208
209 kernel_name = "gemmlowp_mm_interleaved_transposed_" + string_from_target(arch_target);
Gian Marco05288a22017-11-21 10:57:50 +0000210 }
211 else
212 {
Gian Marco05288a22017-11-21 10:57:50 +0000213 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(input0->info()->dimension(0)));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000214 build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_X=" + support::cpp11::to_string(num_elements_processed.x()));
215 build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_Y=" + support::cpp11::to_string(num_elements_processed.y()));
Gian Marco7b4d5472018-01-10 15:56:30 +0000216 kernel_name = "gemmlowp_mm_" + string_from_target(arch_target);
Gian Marco05288a22017-11-21 10:57:50 +0000217 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000218 // Create kernel
219 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Gian Marco05288a22017-11-21 10:57:50 +0000220
221 // Set config_id for enabling LWS tuning
222 _config_id = "gemmlowp_";
223 _config_id += (is_interleaved_transposed ? "reshaped_" : "");
224 _config_id += lower_string(string_from_data_type(input0->info()->data_type()));
225 _config_id += "_";
226 _config_id += support::cpp11::to_string(output->info()->dimension(1));
227 _config_id += "_";
228 _config_id += support::cpp11::to_string(output->info()->dimension(0));
229 _config_id += "_";
230 _config_id += (is_interleaved_transposed ? support::cpp11::to_string(input1->info()->dimension(0)) : support::cpp11::to_string(input1->info()->dimension(1)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231}
232
Gian Marco19835e52018-01-30 13:35:54 +0000233Status CLGEMMLowpMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000234{
235 ElementsProcessed num_elements_processed{};
Gian Marco19835e52018-01-30 13:35:54 +0000236 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, is_interleaved_transposed, reshape_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000237 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
238 input1->clone().get(),
239 output->clone().get(),
240 is_interleaved_transposed,
241 num_elements_processed)
242 .first);
243
244 return Status{};
245}
246
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247void CLGEMMLowpMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue)
248{
249 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
250 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
251
252 Window slice = window.first_slice_window_2D();
253 Window slice_matrix_b = slice;
254 slice_matrix_b.set(Window::DimX, Window::Dimension(0, _input1->info()->dimension(0), 1));
255 slice_matrix_b.set(Window::DimY, Window::Dimension(0, _input1->info()->dimension(1), 1));
256 slice_matrix_b.set(Window::DimZ, Window::Dimension(0, 1, 1));
257
258 do
259 {
260 Window slice_b = slice;
261 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
262 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
263 if(_input1->info()->num_dimensions() < 3)
264 {
265 slice_b = slice_matrix_b;
266 }
267
268 unsigned int idx = 0;
269 add_2D_tensor_argument(idx, _input0, slice);
270 add_2D_tensor_argument(idx, _input1, slice_b);
271 add_2D_tensor_argument(idx, _output, slice);
Gian Marco05288a22017-11-21 10:57:50 +0000272 enqueue(queue, *this, slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273 }
274 while(window.slide_window_slice_2D(slice));
275}