blob: 99e184050e247e696c1708ea1eb6659d66fddc99 [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{
Isabella Gottardic4f582e2018-10-11 19:14:55 +010060 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000061 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8);
Gian Marco19835e52018-01-30 13:35:54 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
Isabella Gottardif02e5272018-10-01 12:26:28 +010063 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input0->num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
64 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the matrix B must be <= 3");
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_interleaved_transposed && reshape_info.reinterpret_input_as_3d(), "The input tensor cannot be reinterpreted as 3D if is_interleaved_transposed is true");
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 2 && reshape_info.reinterpret_input_as_3d(), "The input1 tensor cannot have more than 2 dimensions if input0 has to be reinterpreted as 3D");
Gian Marco19835e52018-01-30 13:35:54 +000067
Georgios Pinitas358ca202017-12-07 16:47:52 +000068 if(!is_interleaved_transposed)
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != input1->dimension(1));
Gian Marco19835e52018-01-30 13:35:54 +000071 }
72 else
73 {
74 const int m = reshape_info.m();
75 const int n = reshape_info.n();
76 const int k = reshape_info.k();
77 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
78 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
79
80 TensorShape tensor_shape0{ input0->tensor_shape() };
81 tensor_shape0.set(0, k);
82 tensor_shape0.set(1, m);
83
84 TensorShape tensor_shape1{ input1->tensor_shape() };
85 tensor_shape1.set(0, n);
86 tensor_shape1.set(1, k);
87
88 const TensorInfo tensor_info0 = input0->clone()->set_tensor_shape(tensor_shape0);
89 const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
90
Isabella Gottardic4f582e2018-10-11 19:14:55 +010091 const TensorInfo tensor_info_reshaped0 = input0->clone()->set_tensor_shape(compute_interleaved_shape(tensor_info0, mult_interleave4x4_height));
Gian Marco19835e52018-01-30 13:35:54 +000092 const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(tensor_info1, mult_transpose1xW_width));
93
94 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input0, &tensor_info_reshaped0);
95 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010096 }
Gian Marco19835e52018-01-30 13:35:54 +000097
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +010098 if(output->total_size() != 0)
99 {
100 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, is_interleaved_transposed, reshape_info));
101 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
102 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000103 }
104
105 return Status{};
106}
107
108std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, bool is_interleaved_transposed,
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100109 const GEMMReshapeInfo &reshape_info, ElementsProcessed &num_elements_processed)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000110{
111 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
112 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100113 bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
114 bool reinterpret_output_as_3d = (reshape_info.depth_output_gemm3d() != 1);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000115
116 Window win{};
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100117 Window win_out{};
Georgios Pinitas358ca202017-12-07 16:47:52 +0000118 bool window_changed = false;
119
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100120 // In case both input and output have to be reinterpreted as 3D tensors,
121 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
122 if(reinterpret_input_as_3d == reinterpret_output_as_3d)
123 {
124 reinterpret_input_as_3d = false;
125 reinterpret_output_as_3d = false;
126 }
127
128 // Output tensor auto inizialitation if not yet initialized
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100129 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, is_interleaved_transposed, reshape_info)));
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100130
131 TensorInfo tmp_info(*output);
132
133 if(reinterpret_output_as_3d)
134 {
135 // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
136 // the window needs to be constructed on the 2D collapsed version of the tensor
137 TensorShape tmp_shape(output->tensor_shape());
138 tmp_shape.collapse(2U, 1U);
139 tmp_info.set_tensor_shape(tmp_shape);
140 }
141
Georgios Pinitas358ca202017-12-07 16:47:52 +0000142 // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication
143 if(is_interleaved_transposed)
144 {
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100145 // reinterpret_input_as_3d is not supported if is_interleaved_transposed is set
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100146 ARM_COMPUTE_ERROR_ON(reshape_info.reinterpret_input_as_3d());
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100147
Gian Marco19835e52018-01-30 13:35:54 +0000148 // Configure kernel window
149 num_elems_processed_per_iteration_x = 4;
150 num_elems_processed_per_iteration_y = 4;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000151
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100152 // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor
153 // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic
154 const int m = reshape_info.m();
155 const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y;
156
157 win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
158 win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000159
Gian Marco19835e52018-01-30 13:35:54 +0000160 AccessWindowRectangle input0_access(input0, 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100161 AccessWindowStatic input1_access(input1, 0, 0,
162 ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x),
163 ceil_to_multiple(input1->dimension(1), num_elems_processed_per_iteration_y));
164 AccessWindowStatic output_access(output, 0, 0,
165 ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x),
166 output->dimension(1) + bottom_pad);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000167
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100168 window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop
169 update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor
Georgios Pinitas358ca202017-12-07 16:47:52 +0000170
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100171 output_access.set_valid_region(win_out, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000172 }
173 else
174 {
175 // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor. num_elems_processed_per_iteration_x
Gian Marco7b4d5472018-01-10 15:56:30 +0000176 num_elems_processed_per_iteration_x = 4;
177 num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 5);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000178
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100179 // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor
180 // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic
181 const int m = reinterpret_input_as_3d ? input0->tensor_shape()[1] * input0->tensor_shape()[2] : input0->tensor_shape()[1];
182 const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y;
183
Georgios Pinitas358ca202017-12-07 16:47:52 +0000184 // Configure window
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100185 win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
186 win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000187
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100188 AccessWindowStatic input0_access(input0, 0, 0, input0->dimension(0), input0->dimension(1) + bottom_pad);
189 AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1));
190 AccessWindowStatic output_access(output, 0, 0,
191 ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x),
192 output->dimension(1) + bottom_pad);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000193
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100194 window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop
195 update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor
Georgios Pinitas358ca202017-12-07 16:47:52 +0000196
197 Coordinates coord;
198 coord.set_num_dimensions(output->num_dimensions());
Isabella Gottardic4f582e2018-10-11 19:14:55 +0100199 output_access.set_valid_region(win_out, ValidRegion(coord, output->tensor_shape()));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000200 }
201
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100202 // Collapse along the Z direction
203 // This collapse needs to be here in order to tune the Z dimension of LWS
204 Window collapsed = win;
205 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u);
206 collapsed = win.collapse(win, dimension_to_collapse);
207
Georgios Pinitas358ca202017-12-07 16:47:52 +0000208 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100209 return std::make_pair(err, collapsed);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000210}
211} // namespace
212
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213CLGEMMLowpMatrixMultiplyKernel::CLGEMMLowpMatrixMultiplyKernel()
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100214 : _input0(nullptr), _input1(nullptr), _output(nullptr), _slide_matrix_b(true), _reinterpret_input_as_3d(false), _reinterpret_output_as_3d(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215{
216}
217
Gian Marco19835e52018-01-30 13:35:54 +0000218void 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 +0100219{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000220 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
Gian Marco19835e52018-01-30 13:35:54 +0000221
Gian Marco19835e52018-01-30 13:35:54 +0000222 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 +0000223
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100224 _input0 = input0;
225 _input1 = input1;
226 _output = output;
227 _reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
228 _reinterpret_output_as_3d = (reshape_info.depth_output_gemm3d() != 1);
229
230 // In case both input and output have to be reinterpreted as 3D tensors,
231 // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false.
232 if(_reinterpret_input_as_3d == _reinterpret_output_as_3d)
233 {
234 _reinterpret_input_as_3d = false;
235 _reinterpret_output_as_3d = false;
236 }
237
238 // Check if we need to slide the matrix B
239 const unsigned int num_dimensions_input0 = _reinterpret_input_as_3d ? _input0->info()->num_dimensions() - 1 : _input0->info()->num_dimensions();
240 _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241
Georgios Pinitas358ca202017-12-07 16:47:52 +0000242 ElementsProcessed num_elements_processed{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243
Gian Marco7b4d5472018-01-10 15:56:30 +0000244 // Get target architecture
245 GPUTarget arch_target = get_arch_from_target(get_target());
246
Georgios Pinitas358ca202017-12-07 16:47:52 +0000247 // Configure kernel window
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100248 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info, num_elements_processed);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000249 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100250 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000251
Giorgio Arena6200fa42018-07-06 17:06:36 +0100252 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
253
Georgios Pinitas358ca202017-12-07 16:47:52 +0000254 // Create build options
Georgios Pinitas358ca202017-12-07 16:47:52 +0000255 std::string kernel_name(" ");
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100256 CLBuildOptions build_opts;
257 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
258 build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
259 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1)));
260 build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2)));
261 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2)));
262
Gian Marco05288a22017-11-21 10:57:50 +0000263 if(is_interleaved_transposed)
264 {
Gian Marco19835e52018-01-30 13:35:54 +0000265 const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width();
266 const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height();
267
268 // Note: The computation tile has the x dimension equal to 4 which is less than the transpose_width (16)
269 // In order to access correctly the elements from the transposed matrix B, we need to pass
270 // the correct step which is calculated as (16 * mult_transpose1xW_width) / 4)
271
Gian Marco05288a22017-11-21 10:57:50 +0000272 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(input1->info()->dimension(0)));
Gian Marco19835e52018-01-30 13:35:54 +0000273 build_opts.add_option("-DTRANSPOSE1XW_WIDTH_STEP=" + support::cpp11::to_string(4 * mult_transpose1xW_width));
274 build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height));
275
Giorgio Arena6200fa42018-07-06 17:06:36 +0100276 kernel_name = "gemmlowp_mm_interleaved_transposed_" + string_from_target(arch_target) + (is_dot8_supported ? "_dot8" : "");
Gian Marco05288a22017-11-21 10:57:50 +0000277 }
278 else
279 {
Gian Marco05288a22017-11-21 10:57:50 +0000280 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(input0->info()->dimension(0)));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000281 build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_X=" + support::cpp11::to_string(num_elements_processed.x()));
282 build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_Y=" + support::cpp11::to_string(num_elements_processed.y()));
Giorgio Arena6200fa42018-07-06 17:06:36 +0100283
284 kernel_name = "gemmlowp_mm_" + string_from_target(arch_target) + (is_dot8_supported ? "_dot8" : "");
Gian Marco05288a22017-11-21 10:57:50 +0000285 }
Giorgio Arena6200fa42018-07-06 17:06:36 +0100286
Georgios Pinitas358ca202017-12-07 16:47:52 +0000287 // Create kernel
288 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Gian Marco05288a22017-11-21 10:57:50 +0000289
290 // Set config_id for enabling LWS tuning
291 _config_id = "gemmlowp_";
292 _config_id += (is_interleaved_transposed ? "reshaped_" : "");
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100293 _config_id += (_reinterpret_input_as_3d ? "3di_" : "");
294 _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
Gian Marco05288a22017-11-21 10:57:50 +0000295 _config_id += lower_string(string_from_data_type(input0->info()->data_type()));
296 _config_id += "_";
297 _config_id += support::cpp11::to_string(output->info()->dimension(1));
298 _config_id += "_";
299 _config_id += support::cpp11::to_string(output->info()->dimension(0));
300 _config_id += "_";
301 _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 +0100302}
303
Gian Marco19835e52018-01-30 13:35:54 +0000304Status 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 +0000305{
306 ElementsProcessed num_elements_processed{};
Gian Marco19835e52018-01-30 13:35:54 +0000307 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, is_interleaved_transposed, reshape_info));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000308 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
309 input1->clone().get(),
310 output->clone().get(),
311 is_interleaved_transposed,
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100312 reshape_info,
Georgios Pinitas358ca202017-12-07 16:47:52 +0000313 num_elements_processed)
314 .first);
315
316 return Status{};
317}
318
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319void CLGEMMLowpMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue)
320{
321 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
322 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
323
Isabella Gottardib92805b2018-09-28 18:24:27 +0100324 if(_input1->info()->num_dimensions() < 3)
325 {
326 // The stride_z for matrix B must be zero if we do not slice
327 ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0);
328 }
329
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100330 Window slice = window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331 Window slice_matrix_b = slice;
Isabella Gottardib92805b2018-09-28 18:24:27 +0100332
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100333 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
334 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
335
336 if(_reinterpret_input_as_3d)
337 {
338 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
339 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3;
340 const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom;
341 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
342 }
343
344 if(_reinterpret_output_as_3d)
345 {
346 // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor
347 const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0);
348 const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom;
349 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
350 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351
352 do
353 {
354 Window slice_b = slice;
355 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
Isabella Gottardib92805b2018-09-28 18:24:27 +0100356 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
357 if(!_slide_matrix_b)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100358 {
359 slice_b = slice_matrix_b;
360 }
361
362 unsigned int idx = 0;
363 add_2D_tensor_argument(idx, _input0, slice);
364 add_2D_tensor_argument(idx, _input1, slice_b);
365 add_2D_tensor_argument(idx, _output, slice);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100366 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2]));
367 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2]));
368 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100369 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100370 }
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100371 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372}