blob: 0857702eee5d40b702dd7f31fc5ed5eb3a117122 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00002 * Copyright (c) 2017-2019 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);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000057 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000058 }
59
60 return Status{};
61}
62
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010063std::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 +000064{
Gian Marco36a0a462018-01-12 10:21:40 +000065 constexpr unsigned int num_elems_processed_per_iteration_x = 4;
Georgios Pinitas358ca202017-12-07 16:47:52 +000066 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
Gian Marco36a0a462018-01-12 10:21:40 +000067 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 +000068 bool window_changed = false;
69
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010070 TensorInfo tmp_info(*input);
71
72 if(reinterpret_input_as_3d)
73 {
74 // Since the input tensor has to be reinterpreted as 3D and the execute window is based on a 2D interleave,
75 // the window needs to be constructed on the 2D collapsed version of the tensor
76 TensorShape tmp_shape(input->tensor_shape());
77 tmp_shape.collapse(2U, 1U);
78 tmp_info.set_tensor_shape(tmp_shape);
79 }
Georgios Pinitas358ca202017-12-07 16:47:52 +000080
Gian Marco Iodice750641d2018-05-08 12:01:57 +010081 // Output auto inizialitation if not yet initialized
82 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_interleaved_shape(*input, mult_interleave4x4_height)));
Gian Marco36a0a462018-01-12 10:21:40 +000083
Gian Marco Iodice750641d2018-05-08 12:01:57 +010084 // Configure window
85 const float scale_x = 4.0f * static_cast<float>(mult_interleave4x4_height);
86 const float scale_y = 1.0f / (scale_x);
87
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010088 // Note: bottom paddings are calculated manually as the input can be reinterpreted as 3D tensor
89 // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic
90 const int m = reinterpret_input_as_3d ? input->tensor_shape()[1] * input->tensor_shape()[2] : input->tensor_shape()[1];
91 const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y;
92
93 Window win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
94 Window win_in = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
95
96 AccessWindowStatic input_access(input, 0, 0,
97 ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration_x),
98 input->dimension(1) + bottom_pad);
Gian Marco Iodice750641d2018-05-08 12:01:57 +010099 AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration, 1, scale_x, scale_y);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100100
101 window_changed = update_window_and_padding(win_in, input_access) || // window used by the execute_window_loop
102 update_window_and_padding(win, output_access); // window used to update the padding requirements of output tensor
103 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000104
Gian Marcoae2af742018-02-15 12:35:44 +0000105 // Collapse along the Z direction
106 // This collapse needs to be here in order to tune the Z dimension of LWS
107 Window collapsed = win.collapse(win, Window::DimZ);
108
Georgios Pinitas358ca202017-12-07 16:47:52 +0000109 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marcoae2af742018-02-15 12:35:44 +0000110 return std::make_pair(err, collapsed);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000111}
112} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114CLGEMMInterleave4x4Kernel::CLGEMMInterleave4x4Kernel()
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100115 : _input(nullptr), _output(nullptr), _reinterpret_input_as_3d(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116{
117}
118
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100119void 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 +0100120{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000121 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +0100122
123 // Output auto inizialitation if not yet initialized
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100124 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 +0100125
Georgios Pinitas358ca202017-12-07 16:47:52 +0000126 // Perform validate step
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100127 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 +0100128
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100129 _input = input;
130 _output = output;
131 _reinterpret_input_as_3d = reinterpret_input_as_3d;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132
Gian Marco36a0a462018-01-12 10:21:40 +0000133 // Create build options
134 CLBuildOptions build_opts;
135 build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height));
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100136 build_opts.add_option_if(unroll_block, "-DUNROLL_BLOCK");
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100137 build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D");
138 build_opts.add_option_if(_reinterpret_input_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(1)));
139 build_opts.add_option_if(_reinterpret_input_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(input->info()->dimension(2)));
140
Gian Marco36a0a462018-01-12 10:21:40 +0000141 switch(input->info()->element_size())
142 {
143 case 1:
144 build_opts.add_option("-DDATA_TYPE=uchar");
145 break;
146 case 2:
147 build_opts.add_option("-DDATA_TYPE=ushort");
148 break;
149 case 4:
150 build_opts.add_option("-DDATA_TYPE=uint");
151 break;
152 default:
153 ARM_COMPUTE_ERROR("Data type not supported");
154 }
155
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 // Create kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000157 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_interleave4x4", build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
159 // Configure kernel window
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100160 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 +0000161 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100162 ICLKernel::configure_internal(win_config.second);
Gian Marcode691f02017-09-08 16:13:11 +0100163
164 // Set config_id for enabling LWS tuning
165 _config_id = "interleave4x4_";
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100166 _config_id += (_reinterpret_input_as_3d ? "3d_" : "");
Gian Marcode691f02017-09-08 16:13:11 +0100167 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
168 _config_id += "_";
169 _config_id += support::cpp11::to_string(output->info()->dimension(0));
170 _config_id += "_";
171 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Gian Marcoae2af742018-02-15 12:35:44 +0000172 _config_id += "_";
173 _config_id += support::cpp11::to_string(output->info()->dimension(2));
174 _config_id += "_";
175 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176}
177
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100178Status CLGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000179{
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100180 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mult_interleave4x4_height, reinterpret_input_as_3d));
181 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 +0000182
183 return Status{};
184}
185
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186void CLGEMMInterleave4x4Kernel::run(const Window &window, cl::CommandQueue &queue)
187{
188 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
189 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
190
191 /*
192 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
193 * |a00 a01 a02 a03|
194 * |a10 a11 a12 a13|
195 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
196 * |a30 a31 a32 a33|
197 *
198 * After this operation, the output matrix will have the following shape: [ height * 4, width / 4 ]
199 */
Gian Marcoae2af742018-02-15 12:35:44 +0000200 Window slice = window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100202 if(_reinterpret_input_as_3d)
203 {
204 // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor
205 const unsigned int idx0 = 2 * num_arguments_per_3D_tensor();
206 const unsigned int total_cross_plane_pad = _input->info()->padding().top + _input->info()->padding().bottom;
207 _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad));
208 }
209
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 do
211 {
212 unsigned int idx = 0;
Gian Marcoae2af742018-02-15 12:35:44 +0000213 add_3D_tensor_argument(idx, _input, slice);
214 add_3D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100215 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 }
Gian Marcoae2af742018-02-15 12:35:44 +0000217 while(window.slide_window_slice_3D(slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218}