blob: f333c1bff35b5f7dfe65a0975427d8f152171a22 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +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/CLGEMMInterleave4x4Kernel.h"
25
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010026#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#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"
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010034#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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include "arm_compute/core/Window.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000038#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40using namespace arm_compute;
Georgios Pinitas358ca202017-12-07 16:47:52 +000041using namespace arm_compute::misc::shape_calculator;
42
43namespace
44{
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d)
Georgios Pinitas358ca202017-12-07 16:47:52 +000046{
Gian Marco36a0a462018-01-12 10:21:40 +000047 ARM_COMPUTE_RETURN_ERROR_ON(mult_interleave4x4_height < 1);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8,
50 DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Georgios Pinitas358ca202017-12-07 16:47:52 +000051 DataType::F16, DataType::F32);
Georgios Pinitas358ca202017-12-07 16:47:52 +000052
53 if(output->total_size() != 0)
54 {
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010055 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_interleaved_shape(*input, mult_interleave4x4_height, reinterpret_input_as_3d));
Georgios Pinitas358ca202017-12-07 16:47:52 +000056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000057 }
58
59 return Status{};
60}
61
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010062std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d)
Georgios Pinitas358ca202017-12-07 16:47:52 +000063{
Gian Marco36a0a462018-01-12 10:21:40 +000064 constexpr unsigned int num_elems_processed_per_iteration_x = 4;
Georgios Pinitas358ca202017-12-07 16:47:52 +000065 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
Gian Marco36a0a462018-01-12 10:21:40 +000066 const unsigned int num_elems_written_per_iteration = num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +000067 bool window_changed = false;
68
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010069 TensorInfo tmp_info(*input);
70
71 if(reinterpret_input_as_3d)
72 {
73 // Since the input tensor has to be reinterpreted as 3D and the execute window is based on a 2D interleave,
74 // the window needs to be constructed on the 2D collapsed version of the tensor
75 TensorShape tmp_shape(input->tensor_shape());
76 tmp_shape.collapse(2U, 1U);
77 tmp_info.set_tensor_shape(tmp_shape);
78 }
Georgios Pinitas358ca202017-12-07 16:47:52 +000079
Gian Marco Iodice750641d2018-05-08 12:01:57 +010080 // Output auto inizialitation if not yet initialized
81 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_interleaved_shape(*input, mult_interleave4x4_height)));
Gian Marco36a0a462018-01-12 10:21:40 +000082
Gian Marco Iodice750641d2018-05-08 12:01:57 +010083 // Configure window
84 const float scale_x = 4.0f * static_cast<float>(mult_interleave4x4_height);
85 const float scale_y = 1.0f / (scale_x);
86
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010087 // Note: bottom paddings are calculated manually as the input can be reinterpreted as 3D tensor
88 // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic
89 const int m = reinterpret_input_as_3d ? input->tensor_shape()[1] * input->tensor_shape()[2] : input->tensor_shape()[1];
90 const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y;
91
92 Window win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
93 Window win_in = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
94
95 AccessWindowStatic input_access(input, 0, 0,
96 ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration_x),
97 input->dimension(1) + bottom_pad);
Gian Marco Iodice750641d2018-05-08 12:01:57 +010098 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration, 1, scale_x, scale_y);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010099
100 window_changed = update_window_and_padding(win_in, input_access) || // window used by the execute_window_loop
101 update_window_and_padding(win, output_access); // window used to update the padding requirements of output tensor
102 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000103
Gian Marcoae2af742018-02-15 12:35:44 +0000104 // Collapse along the Z direction
105 // This collapse needs to be here in order to tune the Z dimension of LWS
106 Window collapsed = win.collapse(win, Window::DimZ);
107
Georgios Pinitas358ca202017-12-07 16:47:52 +0000108 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marcoae2af742018-02-15 12:35:44 +0000109 return std::make_pair(err, collapsed);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000110}
111} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
113CLGEMMInterleave4x4Kernel::CLGEMMInterleave4x4Kernel()
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100114 : _input(nullptr), _output(nullptr), _reinterpret_input_as_3d(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115{
116}
117
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100118void CLGEMMInterleave4x4Kernel::configure(const ICLTensor *input, ICLTensor *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d, bool unroll_block)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000120 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100121
122 // Output auto inizialitation if not yet initialized
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100123 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_interleaved_shape(*input->info(), mult_interleave4x4_height, reinterpret_input_as_3d)));
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100124
Georgios Pinitas358ca202017-12-07 16:47:52 +0000125 // Perform validate step
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100126 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mult_interleave4x4_height, reinterpret_input_as_3d));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100128 _input = input;
129 _output = output;
130 _reinterpret_input_as_3d = reinterpret_input_as_3d;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
Gian Marco36a0a462018-01-12 10:21:40 +0000132 // Create build options
133 CLBuildOptions build_opts;
134 build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height));
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100135 build_opts.add_option_if(unroll_block, "-DUNROLL_BLOCK");
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100136 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
137 build_opts.add_option_if(_reinterpret_input_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(1)));
138 build_opts.add_option_if(_reinterpret_input_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(2)));
139
Gian Marco36a0a462018-01-12 10:21:40 +0000140 switch(input->info()->element_size())
141 {
142 case 1:
143 build_opts.add_option("-DDATA_TYPE=uchar");
144 break;
145 case 2:
146 build_opts.add_option("-DDATA_TYPE=ushort");
147 break;
148 case 4:
149 build_opts.add_option("-DDATA_TYPE=uint");
150 break;
151 default:
152 ARM_COMPUTE_ERROR("Data type not supported");
153 }
154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 // Create kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000156 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_interleave4x4", build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157
158 // Configure kernel window
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100159 auto win_config = validate_and_configure_window(input->info(), output->info(), mult_interleave4x4_height, reinterpret_input_as_3d);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000160 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100161 ICLKernel::configure_internal(win_config.second);
Gian Marcode691f02017-09-08 16:13:11 +0100162
163 // Set config_id for enabling LWS tuning
164 _config_id = "interleave4x4_";
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100165 _config_id += (_reinterpret_input_as_3d ? "3d_" : "");
Gian Marcode691f02017-09-08 16:13:11 +0100166 _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));
Gian Marcoae2af742018-02-15 12:35:44 +0000171 _config_id += "_";
172 _config_id += support::cpp11::to_string(output->info()->dimension(2));
173 _config_id += "_";
174 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175}
176
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100177Status CLGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000178{
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100179 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mult_interleave4x4_height, reinterpret_input_as_3d));
180 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), mult_interleave4x4_height, reinterpret_input_as_3d).first);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000181
182 return Status{};
183}
184
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185void CLGEMMInterleave4x4Kernel::run(const Window &window, cl::CommandQueue &queue)
186{
187 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
188 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
189
190 /*
191 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
192 * |a00 a01 a02 a03|
193 * |a10 a11 a12 a13|
194 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
195 * |a30 a31 a32 a33|
196 *
197 * After this operation, the output matrix will have the following shape: [ height * 4, width / 4 ]
198 */
Gian Marcoae2af742018-02-15 12:35:44 +0000199 Window slice = window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100201 if(_reinterpret_input_as_3d)
202 {
203 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
204 const unsigned int idx0 = 2 * num_arguments_per_3D_tensor();
205 const unsigned int total_cross_plane_pad = _input->info()->padding().top + _input->info()->padding().bottom;
206 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
207 }
208
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 do
210 {
211 unsigned int idx = 0;
Gian Marcoae2af742018-02-15 12:35:44 +0000212 add_3D_tensor_argument(idx, _input, slice);
213 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100214 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 }
Gian Marcoae2af742018-02-15 12:35:44 +0000216 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217}