Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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/CLGEMMLowpMatrixMultiplyKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/AccessWindowTranspose.h" |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 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" |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [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" |
| 37 | #include "arm_compute/core/Validate.h" |
| 38 | #include "arm_compute/core/Window.h" |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 39 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 40 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | |
| 42 | #include <cstddef> |
| 43 | #include <cstdint> |
| 44 | #include <tuple> |
| 45 | |
| 46 | using namespace arm_compute; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 47 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | |
| 49 | namespace arm_compute |
| 50 | { |
| 51 | class Coordinates; |
| 52 | } // namespace arm_compute |
| 53 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 54 | namespace |
| 55 | { |
| 56 | using ElementsProcessed = Steps; |
| 57 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 58 | Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMReshapeInfo &gemm_info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 59 | { |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output); |
Sang-Hoon Park | fe7bf01 | 2019-12-03 10:31:43 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1); |
Isabella Gottardi | f02e527 | 2018-10-01 12:26:28 +0100 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input0->num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4"); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the matrix B must be <= 3"); |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 2 && gemm_info.reinterpret_input_as_3d(), "The input1 tensor cannot have more than 2 dimensions if input0 has to be reinterpreted as 3D"); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 66 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 67 | const int m = gemm_info.m(); |
| 68 | const int n = gemm_info.n(); |
| 69 | const int k = gemm_info.k(); |
| 70 | |
| 71 | ARM_COMPUTE_UNUSED(m); |
| 72 | ARM_COMPUTE_UNUSED(n); |
| 73 | ARM_COMPUTE_UNUSED(k); |
| 74 | |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != static_cast<unsigned int>(k)); |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(0) != static_cast<unsigned int>(n)); |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(1) != static_cast<unsigned int>(k)); |
| 78 | if(gemm_info.reinterpret_input_as_3d()) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 79 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 80 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) * input0->dimension(2) != static_cast<unsigned int>(m)); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 81 | } |
| 82 | else |
| 83 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 84 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != static_cast<unsigned int>(m)); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 85 | } |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 86 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 87 | if(output->total_size() != 0) |
| 88 | { |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 89 | const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, false, gemm_info)); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 90 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return Status{}; |
| 95 | } |
| 96 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 97 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, const GEMMReshapeInfo &gemm_info, ElementsProcessed &num_elements_processed) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 98 | { |
| 99 | unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0]; |
| 100 | unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1]; |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 101 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 102 | bool reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d() != 0); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 103 | |
| 104 | Window win{}; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 105 | Window win_out{}; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 106 | bool window_changed = false; |
| 107 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 108 | // In case both input and output have to be reinterpreted as 3D tensors, |
| 109 | // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false. |
| 110 | if(reinterpret_input_as_3d == reinterpret_output_as_3d) |
| 111 | { |
| 112 | reinterpret_input_as_3d = false; |
| 113 | reinterpret_output_as_3d = false; |
| 114 | } |
| 115 | |
| 116 | // Output tensor auto inizialitation if not yet initialized |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 117 | auto_init_if_empty(*output, input0->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, false, gemm_info)).set_data_type(DataType::S32)); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 118 | |
| 119 | TensorInfo tmp_info(*output); |
| 120 | |
| 121 | if(reinterpret_output_as_3d) |
| 122 | { |
| 123 | // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM, |
| 124 | // the window needs to be constructed on the 2D collapsed version of the tensor |
| 125 | TensorShape tmp_shape(output->tensor_shape()); |
| 126 | tmp_shape.collapse(2U, 1U); |
| 127 | tmp_info.set_tensor_shape(tmp_shape); |
| 128 | } |
| 129 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 130 | // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor. num_elems_processed_per_iteration_x |
| 131 | // Note: if the dot product instruction is available, the 8x2 tile has to be used |
| 132 | num_elems_processed_per_iteration_x = 4; |
| 133 | num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 4); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 134 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 135 | // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor |
| 136 | // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic |
| 137 | const int m = reinterpret_input_as_3d ? input0->tensor_shape()[1] * input0->tensor_shape()[2] : input0->tensor_shape()[1]; |
| 138 | const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 139 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 140 | // Configure window |
| 141 | win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 142 | win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 143 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 144 | AccessWindowStatic input0_access(input0, 0, 0, input0->dimension(0), input0->dimension(1) + bottom_pad); |
| 145 | AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1)); |
| 146 | AccessWindowStatic output_access(output, 0, 0, |
| 147 | ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x), |
| 148 | output->dimension(1) + bottom_pad); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 149 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 150 | window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop |
| 151 | update_window_and_padding(win_out, output_access); // window used to update the padding requirements of output tensor |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 152 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 153 | Coordinates coord; |
| 154 | coord.set_num_dimensions(output->num_dimensions()); |
| 155 | output_access.set_valid_region(win_out, ValidRegion(coord, output->tensor_shape())); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 156 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 157 | // Collapse along the Z direction |
| 158 | // This collapse needs to be here in order to tune the Z dimension of LWS |
| 159 | Window collapsed = win; |
| 160 | const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u); |
| 161 | collapsed = win.collapse(win, dimension_to_collapse); |
| 162 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 163 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 164 | return std::make_pair(err, collapsed); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 165 | } |
| 166 | } // namespace |
| 167 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | CLGEMMLowpMatrixMultiplyKernel::CLGEMMLowpMatrixMultiplyKernel() |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 169 | : _input0(nullptr), _input1(nullptr), _output(nullptr), _slide_matrix_b(true), _reinterpret_input_as_3d(false), _reinterpret_output_as_3d(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | { |
| 171 | } |
| 172 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 173 | void CLGEMMLowpMatrixMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, const GEMMReshapeInfo &gemm_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 175 | ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 176 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 177 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info(), gemm_info)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 178 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 179 | _input0 = input0; |
| 180 | _input1 = input1; |
| 181 | _output = output; |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 182 | _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d(); |
| 183 | _reinterpret_output_as_3d = (gemm_info.depth_output_gemm3d() != 0); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 184 | |
| 185 | // In case both input and output have to be reinterpreted as 3D tensors, |
| 186 | // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false. |
| 187 | if(_reinterpret_input_as_3d == _reinterpret_output_as_3d) |
| 188 | { |
| 189 | _reinterpret_input_as_3d = false; |
| 190 | _reinterpret_output_as_3d = false; |
| 191 | } |
| 192 | |
| 193 | // Check if we need to slide the matrix B |
| 194 | const unsigned int num_dimensions_input0 = _reinterpret_input_as_3d ? _input0->info()->num_dimensions() - 1 : _input0->info()->num_dimensions(); |
| 195 | _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 197 | ElementsProcessed num_elements_processed{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 199 | // Configure kernel window |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 200 | auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info(), gemm_info, num_elements_processed); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 201 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 202 | ICLKernel::configure_internal(win_config.second); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 203 | |
| 204 | // Create build options |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 205 | std::string kernel_name(" "); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 206 | CLBuildOptions build_opts; |
| 207 | build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D"); |
| 208 | build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D"); |
| 209 | build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1))); |
| 210 | build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2))); |
| 211 | build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2))); |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 212 | build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(input0->info()->dimension(0))); |
| 213 | build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_X=" + support::cpp11::to_string(num_elements_processed.x())); |
| 214 | build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_Y=" + support::cpp11::to_string(num_elements_processed.y())); |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 215 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input0->info()->data_type())); |
| 216 | build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(input0->info()->data_type())); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 217 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 218 | kernel_name = "gemmlowp_mm_midgard"; |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 219 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 220 | // Create kernel |
| 221 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 222 | |
| 223 | // Set config_id for enabling LWS tuning |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 224 | _config_id = kernel_name; |
| 225 | _config_id += "_"; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 226 | _config_id += (_reinterpret_input_as_3d ? "3di_" : ""); |
| 227 | _config_id += (_reinterpret_output_as_3d ? "3do_" : ""); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 228 | _config_id += lower_string(string_from_data_type(input0->info()->data_type())); |
| 229 | _config_id += "_"; |
| 230 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 231 | _config_id += "_"; |
| 232 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 235 | Status CLGEMMLowpMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, const GEMMReshapeInfo &gemm_info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 236 | { |
| 237 | ElementsProcessed num_elements_processed{}; |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 238 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, gemm_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 239 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), |
| 240 | input1->clone().get(), |
| 241 | output->clone().get(), |
Gian Marco Iodice | 06be6f8 | 2019-06-24 17:47:51 +0100 | [diff] [blame] | 242 | gemm_info, |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 243 | num_elements_processed) |
| 244 | .first); |
| 245 | |
| 246 | return Status{}; |
| 247 | } |
| 248 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 249 | void CLGEMMLowpMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue) |
| 250 | { |
| 251 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 252 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 253 | |
Isabella Gottardi | b92805b | 2018-09-28 18:24:27 +0100 | [diff] [blame] | 254 | if(_input1->info()->num_dimensions() < 3) |
| 255 | { |
| 256 | // The stride_z for matrix B must be zero if we do not slice |
| 257 | ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0); |
| 258 | } |
| 259 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 260 | Window slice = window.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | Window slice_matrix_b = slice; |
Isabella Gottardi | b92805b | 2018-09-28 18:24:27 +0100 | [diff] [blame] | 262 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 263 | slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 264 | slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 265 | |
| 266 | if(_reinterpret_input_as_3d) |
| 267 | { |
| 268 | // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor |
| 269 | const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3; |
| 270 | const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom; |
| 271 | _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad)); |
| 272 | } |
| 273 | |
| 274 | if(_reinterpret_output_as_3d) |
| 275 | { |
| 276 | // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor |
| 277 | const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0); |
| 278 | const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom; |
| 279 | _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad)); |
| 280 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | |
| 282 | do |
| 283 | { |
| 284 | Window slice_b = slice; |
| 285 | // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2 |
Isabella Gottardi | b92805b | 2018-09-28 18:24:27 +0100 | [diff] [blame] | 286 | // This scenario can happen when the matrix multiplication is used to perform a convolution operation |
| 287 | if(!_slide_matrix_b) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 288 | { |
| 289 | slice_b = slice_matrix_b; |
| 290 | } |
| 291 | |
| 292 | unsigned int idx = 0; |
| 293 | add_2D_tensor_argument(idx, _input0, slice); |
| 294 | add_2D_tensor_argument(idx, _input1, slice_b); |
| 295 | add_2D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 296 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2])); |
| 297 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2])); |
| 298 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2])); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 299 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 300 | } |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 301 | while(window.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 302 | } |