Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 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 Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 38 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | using namespace arm_compute; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 41 | using namespace arm_compute::misc::shape_calculator; |
| 42 | |
| 43 | namespace |
| 44 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 46 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(mult_interleave4x4_height < 1); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 49 | 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 Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 51 | DataType::F16, DataType::F32); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 52 | |
| 53 | if(output->total_size() != 0) |
| 54 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_interleaved_shape(*input, mult_interleave4x4_height, reinterpret_input_as_3d)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return Status{}; |
| 61 | } |
| 62 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 63 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 64 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 65 | constexpr unsigned int num_elems_processed_per_iteration_x = 4; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 66 | constexpr unsigned int num_elems_processed_per_iteration_y = 4; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 67 | const unsigned int num_elems_written_per_iteration = num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y * mult_interleave4x4_height; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 68 | bool window_changed = false; |
| 69 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 70 | 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 Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 80 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 81 | // 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 Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 83 | |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 84 | // 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 Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 88 | // 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 Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 99 | AccessWindowRectangle output_access(output, 0, 0, num_elems_written_per_iteration, 1, scale_x, scale_y); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 100 | |
| 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 Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 104 | |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 105 | // 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 Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 109 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 110 | return std::make_pair(err, collapsed); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 111 | } |
| 112 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
| 114 | CLGEMMInterleave4x4Kernel::CLGEMMInterleave4x4Kernel() |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 115 | : _input(nullptr), _output(nullptr), _reinterpret_input_as_3d(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | { |
| 117 | } |
| 118 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 119 | void CLGEMMInterleave4x4Kernel::configure(const ICLTensor *input, ICLTensor *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d, bool unroll_block) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 121 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 122 | |
| 123 | // Output auto inizialitation if not yet initialized |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 124 | 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 Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 125 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 126 | // Perform validate step |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mult_interleave4x4_height, reinterpret_input_as_3d)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 129 | _input = input; |
| 130 | _output = output; |
| 131 | _reinterpret_input_as_3d = reinterpret_input_as_3d; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 133 | // Create build options |
| 134 | CLBuildOptions build_opts; |
| 135 | build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height)); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 136 | build_opts.add_option_if(unroll_block, "-DUNROLL_BLOCK"); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 137 | 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 Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 141 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | // Create kernel |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 157 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_interleave4x4", build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 158 | |
| 159 | // Configure kernel window |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 160 | auto win_config = validate_and_configure_window(input->info(), output->info(), mult_interleave4x4_height, reinterpret_input_as_3d); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 161 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 162 | ICLKernel::configure_internal(win_config.second); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 163 | |
| 164 | // Set config_id for enabling LWS tuning |
| 165 | _config_id = "interleave4x4_"; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 166 | _config_id += (_reinterpret_input_as_3d ? "3d_" : ""); |
Gian Marco | de691f0 | 2017-09-08 16:13:11 +0100 | [diff] [blame] | 167 | _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 Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 172 | _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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 178 | Status CLGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output, int mult_interleave4x4_height, bool reinterpret_input_as_3d) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 179 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 180 | 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 Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 182 | |
| 183 | return Status{}; |
| 184 | } |
| 185 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | void 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 Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 200 | Window slice = window.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 202 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | do |
| 211 | { |
| 212 | unsigned int idx = 0; |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 213 | add_3D_tensor_argument(idx, _input, slice); |
| 214 | add_3D_tensor_argument(idx, _output, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 215 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | } |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 217 | while(window.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 218 | } |