blob: fb15b42fe2b66ca0037f7a19a20aa277279d982c [file] [log] [blame]
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000025
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000032#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
Gian Marco Iodice781cba72020-06-19 16:56:57 +010036#include "src/core/CL/CLUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/CL/CLValidate.h"
38#include "src/core/CL/gemm/CLGEMMHelpers.h"
39#include "src/core/helpers/AutoConfiguration.h"
40#include "src/core/helpers/WindowHelpers.h"
41#include "src/core/utils/helpers/float_ops.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000042#include "support/StringSupport.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000043
44#include <cstddef>
45#include <cstdint>
46#include <tuple>
47
48using namespace arm_compute;
49using namespace arm_compute::misc::shape_calculator;
50
51namespace arm_compute
52{
53class Coordinates;
54} // namespace arm_compute
55
56namespace
57{
58using ElementsProcessed = Steps;
59
Gian Marco Iodicee16c8902019-06-14 16:11:10 +010060Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float alpha, float beta, const GEMMLHSMatrixInfo &lhs_info,
61 const GEMMRHSMatrixInfo &rhs_info,
Gian Marco Iodice7026b302019-06-26 17:18:11 +010062 const GEMMKernelInfo &gemm_info)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000063{
64 ARM_COMPUTE_UNUSED(alpha);
65 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000066 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input0);
Gian Marco Iodice05639f62019-09-24 12:05:06 +010067 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::F16, DataType::F32);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000068 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
69 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input0->num_dimensions() > 4, "The number of dimensions for the LHS matrix must be <= 4");
70 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the RHS matrix must be <= 3");
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000071 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.k0 != rhs_info.k0);
Giorgio Arenaae99b6e2019-08-01 14:22:12 +010072 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.transpose == rhs_info.transpose);
Gian Marco Iodicebacfec52019-01-11 11:30:55 +000073 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((lhs_info.k0 & (lhs_info.k0 - 1)) && lhs_info.k0 != 3), "Only 2,3,4,8,16 are supported for k0");
74 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.k0 > 16);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000075 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 2 || lhs_info.m0 > 8);
Michele Di Giorgio2568c6b2019-09-17 12:08:46 +010076 ARM_COMPUTE_RETURN_ERROR_ON_MSG((lhs_info.transpose) && ((lhs_info.m0 & (lhs_info.m0 - 1)) && lhs_info.m0 != 3), "Only 2,3,4,8,16 are supported for m0");
77 ARM_COMPUTE_RETURN_ERROR_ON_MSG((rhs_info.transpose) && ((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3), "Only 2,3,4,8,16 are supported for n0");
Gian Marco Iodiceb238f5f2019-08-02 09:09:53 +010078 ARM_COMPUTE_RETURN_ERROR_ON_MSG((gemm_info.reinterpret_input_as_3d || gemm_info.depth_output_gemm3d != 0) && (input2 != nullptr)
Gian Marco Iodiced820db62019-08-05 14:23:23 +010079 && (!gemm_info.broadcast_bias),
80 "Bias addition only supported with broadcast mode in case the input or output has to be reinterpreted as 3D");
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +010081 ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision && (input0->data_type() == DataType::F32), "Mixed precision only supported for F16 data type");
Gian Marco Iodiceed5fe692020-07-09 08:41:10 +010082 ARM_COMPUTE_RETURN_ON_ERROR(cl_gemm::validate_image2d_support_on_rhs(*input1, rhs_info));
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +010083
Gian Marco Iodice7026b302019-06-26 17:18:11 +010084 const unsigned int m = gemm_info.m;
85 const unsigned int n = gemm_info.n;
86 const unsigned int k = gemm_info.k;
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000087
88 TensorShape tensor_shape0{ input0->tensor_shape() };
89 tensor_shape0.set(0, k);
90 tensor_shape0.set(1, m);
91
92 TensorShape tensor_shape1{ input1->tensor_shape() };
93 tensor_shape1.set(0, n);
94 tensor_shape1.set(1, k);
95
Gian Marco Iodicee16c8902019-06-14 16:11:10 +010096 if(input2 != nullptr && !(helpers::float_ops::is_zero(beta)))
97 {
Gian Marco Iodice7026b302019-06-26 17:18:11 +010098 const unsigned int input2_dim0 = input2->dimension(0);
99 const unsigned int input2_dim1 = input2->dimension(1);
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100100
101 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input2, input1);
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100102 if(gemm_info.broadcast_bias)
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100103 {
104 ARM_COMPUTE_RETURN_ERROR_ON_MSG((input2_dim1 != 1 || input2_dim0 != n), "Incorrect dimension of bias matrix which is to be broadcasted");
105 }
106 else
107 {
108 ARM_COMPUTE_RETURN_ERROR_ON_MSG((input2_dim0 != n || input2_dim1 != m), "Incorrect dimension of bias matrix");
109 }
110 }
111
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000112 const TensorInfo tensor_info0 = input0->clone()->set_tensor_shape(tensor_shape0);
113 const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1);
114
115 const TensorInfo tensor_info_reshaped0 = input0->clone()->set_tensor_shape(compute_lhs_reshaped_shape(tensor_info0, lhs_info));
116 const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_rhs_reshaped_shape(tensor_info1, rhs_info));
117
118 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input0, &tensor_info_reshaped0);
119 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1);
120
121 if(output->total_size() != 0)
122 {
123 const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, gemm_info));
124 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
125 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output);
126 }
127
128 return Status{};
129}
130
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100131std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info,
132 const GEMMRHSMatrixInfo &rhs_info,
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100133 const GEMMKernelInfo &gemm_info, ElementsProcessed &num_elements_processed)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000134{
135 unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0];
136 unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1];
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100137 bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000138
139 Window win{};
140 Window win_out{};
141 bool window_changed = false;
142
143 // Output tensor auto initialization if not yet initialized
144 auto_init_if_empty(*output, input0->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, gemm_info)));
145
146 TensorInfo tmp_info(*output);
147
148 if(reinterpret_output_as_3d)
149 {
150 // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM,
151 // the window needs to be constructed on the 2D collapsed version of the tensor
152 TensorShape tmp_shape(output->tensor_shape());
153 tmp_shape.collapse(2U, 1U);
154 tmp_info.set_tensor_shape(tmp_shape);
155 }
156
157 // Configure kernel window
158 num_elems_processed_per_iteration_x = rhs_info.n0;
159 num_elems_processed_per_iteration_y = lhs_info.m0;
160
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000161 win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
162 win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
163
164 AccessWindowStatic input0_access(input0, 0, 0,
Gian Marco Iodice088d63a2020-08-11 14:14:06 +0100165 input0->dimension(0),
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000166 input0->dimension(1));
167 AccessWindowStatic input1_access(input1, 0, 0,
Gian Marco Iodice088d63a2020-08-11 14:14:06 +0100168 input1->dimension(0),
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000169 input1->dimension(1));
170 AccessWindowStatic output_access(output, 0, 0,
Gian Marco Iodice088d63a2020-08-11 14:14:06 +0100171 output->dimension(0),
172 output->dimension(1));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000173
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100174 if(input2 != nullptr)
175 {
176 const int bias_processed_per_iteration_x = num_elems_processed_per_iteration_x;
177
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100178 const int bias_processed_per_iteration_y = gemm_info.broadcast_bias ? 1 : num_elems_processed_per_iteration_y;
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100179
180 AccessWindowStatic input2_access(input2, 0, 0,
181 ceil_to_multiple(input2->dimension(0), bias_processed_per_iteration_x),
182 ceil_to_multiple(input2->dimension(1), bias_processed_per_iteration_y));
183
184 window_changed = update_window_and_padding(win, input0_access, input1_access, input2_access) || // window used by the execute_window_loop
185 update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor
186 }
187 else
188 {
189 window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop
190 update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor
191 }
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000192
193 output_access.set_valid_region(win_out, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
194
195 // Collapse along the Z direction
196 // This collapse needs to be here in order to tune the Z dimension of LWS
197 Window collapsed = win;
198 const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u);
199 collapsed = win.collapse(win, dimension_to_collapse);
200
201 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
202 return std::make_pair(err, collapsed);
203}
204} // namespace
205
206CLGEMMMatrixMultiplyReshapedKernel::CLGEMMMatrixMultiplyReshapedKernel()
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100207 : _input0(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _slide_matrix_b(true), _reinterpret_output_as_3d(false), _use_dummy_work_items(false), _add_bias(false),
Gian Marco Iodicee5563d92020-06-25 17:18:36 +0100208 _broadcast_bias(false), _export_to_cl_image(false), _k(1)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000209{
210}
211
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100212void CLGEMMMatrixMultiplyReshapedKernel::configure(const ICLTensor *input0, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, float alpha, float beta,
213 const GEMMLHSMatrixInfo &lhs_info,
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100214 const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000215{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100216 configure(CLKernelLibrary::get().get_compile_context(), input0, input1, input2, output, alpha, beta, lhs_info, rhs_info, gemm_info);
217}
218
Manuel Bottini679fc962020-04-21 16:08:53 +0100219void CLGEMMMatrixMultiplyReshapedKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input0, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, float alpha,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100220 float beta,
221 const GEMMLHSMatrixInfo &lhs_info,
222 const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
223{
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000224 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
225
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100226 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), (input2 != nullptr ? input2->info() : nullptr), output->info(), alpha, beta, lhs_info, rhs_info, gemm_info));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000227
228 _input0 = input0;
229 _input1 = input1;
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100230 _input2 = helpers::float_ops::is_zero(beta) ? nullptr : input2;
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000231 _output = output;
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100232 _reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000233 _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device());
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100234 _add_bias = _input2 != nullptr;
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100235 _broadcast_bias = gemm_info.broadcast_bias;
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100236 _export_to_cl_image = rhs_info.export_to_cl_image;
Gian Marco Iodicee5563d92020-06-25 17:18:36 +0100237 _k = gemm_info.k;
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000238
239 // Check if we need to slide the matrix B
240 const unsigned int num_dimensions_input0 = _input0->info()->num_dimensions();
241 _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0);
242
243 ElementsProcessed num_elements_processed{};
244
245 // Configure kernel window
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100246 auto win_config = validate_and_configure_window(input0->info(), input1->info(), input2 != nullptr ? input2->info() : nullptr, output->info(), lhs_info, rhs_info, gemm_info, num_elements_processed);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000247 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
248 ICLKernel::configure_internal(win_config.second);
249
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100250 const bool enable_mixed_precision = gemm_info.fp_mixed_precision;
251 const DataType data_type = input0->info()->data_type();
252
Gian Marco Iodice088d63a2020-08-11 14:14:06 +0100253 // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks at the end of a row/column if any. This is to avoid padding.
254 const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : output->info()->dimension(1);
255
256 const unsigned int partial_store_m0 = internal_m % lhs_info.m0;
257 const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0;
258
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000259 // Create build options
260 CLBuildOptions build_opts;
Gian Marco Iodice82d9dd12019-06-10 16:45:40 +0100261 build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)), "-DALPHA=" + float_to_string_with_full_precision(alpha));
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100262 build_opts.add_option_if(_input2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta));
263 build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA");
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000264 build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D");
265 build_opts.add_option_if(_reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1)));
266 build_opts.add_option_if(_reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2)));
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100267 build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS");
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000268 build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2)));
269 build_opts.add_option_if(lhs_info.interleave, "-DLHS_INTERLEAVE");
270 build_opts.add_option_if(rhs_info.interleave, "-DRHS_INTERLEAVE");
Giorgio Arenaae99b6e2019-08-01 14:22:12 +0100271 build_opts.add_option_if(lhs_info.transpose, "-DLHS_TRANSPOSE");
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000272 build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS");
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100273 build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(gemm_info.activation_info.activation())));
274 build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a()));
275 build_opts.add_option_if(gemm_info.activation_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b()));
276 build_opts.add_option_if(enable_mixed_precision, "-DMIXED_PRECISION");
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100277 build_opts.add_option_if(rhs_info.export_to_cl_image, "-DOPENCL_IMAGE_SUPPORT");
278 build_opts.add_option("-DRHS_HEIGHT=" + support::cpp11::to_string(input1->info()->dimension(1)));
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100279 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
280 build_opts.add_option("-DDATA_TYPE_ACCUMULATOR=" + (enable_mixed_precision ? get_cl_type_from_data_type(DataType::F32) : get_cl_type_from_data_type(data_type)));
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100281 build_opts.add_option("-DM=" + support::cpp11::to_string(gemm_info.m));
282 build_opts.add_option("-DN=" + support::cpp11::to_string(gemm_info.n));
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100283 build_opts.add_option("-DK=" + support::cpp11::to_string(gemm_info.k));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000284 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
285 build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0));
286 build_opts.add_option("-DK0=" + support::cpp11::to_string(lhs_info.k0));
287 build_opts.add_option("-DV0=" + support::cpp11::to_string(lhs_info.v0));
288 build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0));
Gian Marco Iodice088d63a2020-08-11 14:14:06 +0100289 build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0));
290 build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000291
292 std::string kernel_name("gemm_mm_reshaped_");
293 kernel_name += lhs_info.transpose ? "lhs_t_" : "lhs_nt_";
294 kernel_name += rhs_info.transpose ? "rhs_t" : "rhs_nt";
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100295 kernel_name += rhs_info.export_to_cl_image ? "_texture" : "";
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000296
297 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100298 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000299
300 // Set config_id for enabling LWS tuning
301 _config_id = kernel_name;
302 _config_id += "_";
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100303 _config_id += (_add_bias ? "add_bias_" : "");
304 _config_id += (_broadcast_bias ? "broadcast_bias_" : "");
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000305 _config_id += (_reinterpret_output_as_3d ? "3do_" : "");
Gian Marco Iodiceca1f4602019-07-16 15:46:48 +0100306 _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : "");
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000307 _config_id += lower_string(string_from_data_type(input0->info()->data_type()));
308 _config_id += "_";
Gian Marco Iodice0c17aa22019-09-27 09:23:15 +0100309 _config_id += (enable_mixed_precision ? "mixed_precision_" : "");
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000310 _config_id += support::cpp11::to_string(output->info()->dimension(1));
311 _config_id += "_";
312 _config_id += support::cpp11::to_string(output->info()->dimension(0));
313 _config_id += "_";
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100314 _config_id += support::cpp11::to_string(gemm_info.k);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000315 _config_id += "_";
316 _config_id += support::cpp11::to_string(output->info()->dimension(2));
317 _config_id += "_";
318 _config_id += support::cpp11::to_string(lhs_info.m0);
319 _config_id += "_";
320 _config_id += support::cpp11::to_string(rhs_info.n0);
321 _config_id += "_";
322 _config_id += support::cpp11::to_string(lhs_info.k0);
323 _config_id += "_";
324 _config_id += support::cpp11::to_string(lhs_info.v0);
325 _config_id += "_";
326 _config_id += support::cpp11::to_string(rhs_info.h0);
327 _config_id += "_";
328 _config_id += support::cpp11::to_string(lhs_info.interleave);
329 _config_id += "_";
330 _config_id += support::cpp11::to_string(rhs_info.interleave);
331}
332
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100333Status CLGEMMMatrixMultiplyReshapedKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float alpha, float beta,
334 const GEMMLHSMatrixInfo &lhs_info,
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100335 const GEMMRHSMatrixInfo &rhs_info, const GEMMKernelInfo &gemm_info)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000336{
337 ElementsProcessed num_elements_processed{};
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100338 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, input2, output, alpha, beta, lhs_info, rhs_info, gemm_info));
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000339 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(),
340 input1->clone().get(),
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100341 input2 != nullptr ? input2->clone().get() : nullptr,
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000342 output->clone().get(),
343 lhs_info,
344 rhs_info,
345 gemm_info,
346 num_elements_processed)
347 .first);
348
349 return Status{};
350}
351
352void CLGEMMMatrixMultiplyReshapedKernel::run(const Window &window, cl::CommandQueue &queue)
353{
354 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
355 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
356
357 if(_input1->info()->num_dimensions() < 3)
358 {
359 // The stride_z for matrix B must be zero if we do not slice
360 ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0);
361 }
362
363 Window slice = window.first_slice_window_3D();
364 Window slice_matrix_b = slice;
365
366 slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1));
367 slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1));
368
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100369 const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom;
370
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100371 cl::Image2D input1_image2d;
372
373 if(_export_to_cl_image)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000374 {
Gian Marco Iodice781cba72020-06-19 16:56:57 +0100375 const TensorShape shape2d(_input1->info()->dimension(0) / 4, _input1->info()->dimension(1) * _input1->info()->dimension(2));
376 const size_t image_row_pitch = _input1->info()->strides_in_bytes()[1];
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100377
Gian Marco Iodice6f931342020-09-15 14:17:41 +0100378 input1_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), _input1->cl_buffer(), shape2d, _input1->info()->data_type(), image_row_pitch);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000379 }
380
381 do
382 {
383 Window slice_b = slice;
384 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
385 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
386 if(!_slide_matrix_b)
387 {
388 slice_b = slice_matrix_b;
389 }
390
391 unsigned int idx = 0;
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100392
393 // LHS buffer
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000394 add_2D_tensor_argument(idx, _input0, slice);
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100395
396 // RHS buffer or RHS OpenCL image (_export_to_cl_image == true)
397 if(_export_to_cl_image)
398 {
399 _kernel.setArg(idx++, input1_image2d);
400 }
401 else
402 {
403 add_2D_tensor_argument(idx, _input1, slice_b);
404 }
405
406 // Bias buffer (_add_bias == true)
407 add_2D_tensor_argument_if(_add_bias, idx, _input2, slice);
408
409 // Output buffer
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000410 add_2D_tensor_argument(idx, _output, slice);
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100411
Gian Marco Iodicee5563d92020-06-25 17:18:36 +0100412 // K dimension (not used if _export_to_cl_image == true)
413 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_k));
414
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100415 // LHS stride_z
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000416 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2]));
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100417
418 // RHS stride_z (not used if _export_to_cl_image == true)
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000419 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2]));
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100420
421 // Bias stride_z (if _add_bias == true)
Gian Marco Iodicee16c8902019-06-14 16:11:10 +0100422 if(_add_bias)
423 {
424 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input2->info()->strides_in_bytes()[2]));
425 }
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100426
427 // Output stride_z
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000428 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2]));
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +0100429
430 // Cross-plan padding (if _reinterpret_output_as_3d = true)
431 if(_reinterpret_output_as_3d)
432 {
433 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad));
434 }
435
436 // Dispatch kernel
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000437 enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000438 }
439 while(window.slide_window_slice_3D(slice));
Michele Di Giorgio2568c6b2019-09-17 12:08:46 +0100440}