blob: f2ad677976e9182b91f5c8551c40915b44cdc022 [file] [log] [blame]
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +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/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
25
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000032#include "arm_compute/core/Utils.h"
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
35#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000039
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010040namespace arm_compute
41{
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000042using namespace arm_compute::misc::shape_calculator;
43
44namespace
45{
46Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d)
47{
Manuel Bottini8481d832019-12-10 15:28:40 +000048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000049 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 == 0);
50 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.k0 == 0);
51 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.v0 == 0);
Gian Marco Iodicebacfec52019-01-11 11:30:55 +000052 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");
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000053 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.k0 > 16);
54 ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 2 || lhs_info.m0 > 8);
55
56 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000057 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000058
59 if(output->total_size() != 0)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_lhs_reshaped_shape(*input, lhs_info, reinterpret_input_as_3d));
62 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000063 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000064 }
65
66 return Status{};
67}
68
69std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d)
70{
71 const unsigned int num_elems_processed_per_iteration_x = lhs_info.k0;
72 const unsigned int num_elems_processed_per_iteration_y = lhs_info.m0;
73 bool window_changed = false;
74
75 TensorInfo tmp_info(*input);
76
77 if(reinterpret_input_as_3d)
78 {
79 // Since the input tensor has to be reinterpreted as 3D and the execute window is based on a 2D interleave,
80 // the window needs to be constructed on the 2D collapsed version of the tensor
81 TensorShape tmp_shape(input->tensor_shape());
82 tmp_shape.collapse(2U, 1U);
83 tmp_info.set_tensor_shape(tmp_shape);
84 }
85
86 // Output auto inizialitation if not yet initialized
87 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*input, lhs_info, reinterpret_input_as_3d)));
88
89 // Configure window
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000090 Window win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
91 Window win_in = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
92
93 AccessWindowStatic input_access(input, 0, 0,
Gian Marco Iodice73cdaac2020-08-10 21:44:14 +010094 input->dimension(0),
95 input->dimension(1));
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +000096 AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1));
97
98 window_changed = update_window_and_padding(win_in, input_access) || // window used by the execute_window_loop
99 update_window_and_padding(win, output_access); // window used to update the padding requirements of output tensor
100 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
101
102 // Collapse along the Z direction
103 // This collapse needs to be here in order to tune the Z dimension of LWS
104 Window collapsed = win.collapse(win, Window::DimZ);
105
106 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
107 return std::make_pair(err, collapsed);
108}
109} // namespace
110
111CLGEMMReshapeLHSMatrixKernel::CLGEMMReshapeLHSMatrixKernel()
112 : _input(nullptr), _output(nullptr), _reinterpret_input_as_3d(false)
113{
114}
115
116void CLGEMMReshapeLHSMatrixKernel::configure(const ICLTensor *input, ICLTensor *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d)
117{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100118 configure(CLKernelLibrary::get().get_compile_context(), input, output, lhs_info, reinterpret_input_as_3d);
119}
120
Manuel Bottini679fc962020-04-21 16:08:53 +0100121void CLGEMMReshapeLHSMatrixKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100122{
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000123 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
124
125 // Perform validate step
126 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), lhs_info, reinterpret_input_as_3d));
127
128 _input = input;
129 _output = output;
130 _reinterpret_input_as_3d = reinterpret_input_as_3d;
131
Gian Marco Iodice73cdaac2020-08-10 21:44:14 +0100132 const unsigned int src_w = input->info()->dimension(0);
133 const unsigned int src_h = _reinterpret_input_as_3d ? input->info()->dimension(1) * input->info()->dimension(2) : input->info()->dimension(1);
134 const unsigned int partial_load_m0 = src_h % lhs_info.m0;
135 const unsigned int partial_load_k0 = src_w % lhs_info.k0;
136
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000137 // Create build options
138 CLBuildOptions build_opts;
139 build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0));
140 build_opts.add_option("-DK0=" + support::cpp11::to_string(lhs_info.k0));
141 build_opts.add_option("-DV0=" + support::cpp11::to_string(lhs_info.v0));
Gian Marco Iodice73cdaac2020-08-10 21:44:14 +0100142 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_w));
143 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_h));
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000144 build_opts.add_option_if(lhs_info.interleave, "-DINTERLEAVE");
145 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
146 build_opts.add_option_if(_reinterpret_input_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(1)));
147 build_opts.add_option_if(_reinterpret_input_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(2)));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100148 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
Gian Marco Iodice73cdaac2020-08-10 21:44:14 +0100149 build_opts.add_option("-DPARTIAL_LOAD_M0=" + support::cpp11::to_string(partial_load_m0));
150 build_opts.add_option("-DPARTIAL_LOAD_K0=" + support::cpp11::to_string(partial_load_k0));
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000151
152 std::string kernel_name("gemm_reshape_lhs_matrix_");
153 kernel_name += lhs_info.transpose ? "t" : "nt";
154
155 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100156 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000157
158 // Configure kernel window
159 auto win_config = validate_and_configure_window(input->info(), output->info(), lhs_info, reinterpret_input_as_3d);
160 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
161 ICLKernel::configure_internal(win_config.second);
162
163 // Set config_id for enabling LWS tuning
164 _config_id = "gemm_reshape_lhs_matrix_";
165 _config_id += (_reinterpret_input_as_3d ? "3d_" : "");
166 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
167 _config_id += "_";
168 _config_id += support::cpp11::to_string(output->info()->dimension(0));
169 _config_id += "_";
170 _config_id += support::cpp11::to_string(output->info()->dimension(1));
171 _config_id += "_";
172 _config_id += support::cpp11::to_string(output->info()->dimension(2));
173 _config_id += "_";
174 _config_id += support::cpp11::to_string(lhs_info.m0);
175 _config_id += "_";
176 _config_id += support::cpp11::to_string(lhs_info.k0);
177 _config_id += "_";
178 _config_id += support::cpp11::to_string(lhs_info.v0);
179 _config_id += "_";
180 _config_id += support::cpp11::to_string(lhs_info.interleave);
181 _config_id += "_";
182 _config_id += support::cpp11::to_string(lhs_info.transpose);
183}
184
185Status CLGEMMReshapeLHSMatrixKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d)
186{
187 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, lhs_info, reinterpret_input_as_3d));
188 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), lhs_info, reinterpret_input_as_3d).first);
189
190 return Status{};
191}
192
193void CLGEMMReshapeLHSMatrixKernel::run(const Window &window, cl::CommandQueue &queue)
194{
195 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
196 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
197
198 Window slice = window.first_slice_window_3D();
199
200 if(_reinterpret_input_as_3d)
201 {
202 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
203 const unsigned int idx0 = 2 * num_arguments_per_3D_tensor();
204 const unsigned int total_cross_plane_pad = _input->info()->padding().top + _input->info()->padding().bottom;
205 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
206 }
207
208 do
209 {
210 unsigned int idx = 0;
211 add_3D_tensor_argument(idx, _input, slice);
212 add_3D_tensor_argument(idx, _output, slice);
213 enqueue(queue, *this, slice, lws_hint());
214 }
215 while(window.slide_window_slice_3D(slice));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100216}
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000217} // namespace arm_compute