Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +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/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" |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 40 | #include "support/ToolchainSupport.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 | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 58 | Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_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); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8); |
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"); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_interleaved_transposed && reshape_info.reinterpret_input_as_3d(), "The input tensor cannot be reinterpreted as 3D if is_interleaved_transposed is true"); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 2 && reshape_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] | 67 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 68 | if(!is_interleaved_transposed) |
| 69 | { |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != input1->dimension(1)); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 71 | } |
| 72 | else |
| 73 | { |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 74 | GEMMRHSMatrixInfo rhs_info; |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 75 | GEMMLHSMatrixInfo lhs_info; |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 76 | const int m = reshape_info.m(); |
| 77 | const int n = reshape_info.n(); |
| 78 | const int k = reshape_info.k(); |
| 79 | const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width(); |
| 80 | const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height(); |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 81 | const bool unroll_block = dot8_supported(CLKernelLibrary::get().get_device()); |
| 82 | |
| 83 | rhs_info.n0 = 16 / input1->element_size(); |
| 84 | rhs_info.k0 = 1; |
| 85 | rhs_info.h0 = mult_transpose1xW_width; |
| 86 | rhs_info.interleave = false; |
| 87 | rhs_info.transpose = false; |
| 88 | lhs_info.m0 = 4; |
| 89 | lhs_info.k0 = 4; |
| 90 | lhs_info.v0 = mult_interleave4x4_height; |
| 91 | lhs_info.interleave = true; |
giuros01 | 3ddb9ae | 2019-01-21 14:57:16 +0000 | [diff] [blame] | 92 | lhs_info.transpose = !unroll_block; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 93 | |
| 94 | TensorShape tensor_shape0{ input0->tensor_shape() }; |
| 95 | tensor_shape0.set(0, k); |
| 96 | tensor_shape0.set(1, m); |
| 97 | |
| 98 | TensorShape tensor_shape1{ input1->tensor_shape() }; |
| 99 | tensor_shape1.set(0, n); |
| 100 | tensor_shape1.set(1, k); |
| 101 | |
| 102 | const TensorInfo tensor_info0 = input0->clone()->set_tensor_shape(tensor_shape0); |
| 103 | const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1); |
| 104 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 105 | const TensorInfo tensor_info_reshaped0 = input0->clone()->set_tensor_shape(compute_lhs_reshaped_shape(tensor_info0, lhs_info)); |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 106 | const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_rhs_reshaped_shape(tensor_info1, rhs_info)); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 107 | |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input0, &tensor_info_reshaped0); |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 110 | } |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 111 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 112 | if(output->total_size() != 0) |
| 113 | { |
| 114 | const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, is_interleaved_transposed, reshape_info)); |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 116 | 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] | 117 | } |
| 118 | |
| 119 | return Status{}; |
| 120 | } |
| 121 | |
| 122 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, bool is_interleaved_transposed, |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 123 | const GEMMReshapeInfo &reshape_info, ElementsProcessed &num_elements_processed) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 124 | { |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 125 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 126 | unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0]; |
| 127 | unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1]; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 128 | bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d(); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 129 | bool reinterpret_output_as_3d = (reshape_info.depth_output_gemm3d() != 0); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 130 | |
| 131 | Window win{}; |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 132 | Window win_out{}; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 133 | bool window_changed = false; |
| 134 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 135 | // In case both input and output have to be reinterpreted as 3D tensors, |
| 136 | // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false. |
| 137 | if(reinterpret_input_as_3d == reinterpret_output_as_3d) |
| 138 | { |
| 139 | reinterpret_input_as_3d = false; |
| 140 | reinterpret_output_as_3d = false; |
| 141 | } |
| 142 | |
| 143 | // Output tensor auto inizialitation if not yet initialized |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 144 | auto_init_if_empty(*output, input0->clone()->set_tensor_shape(compute_mm_shape(*input0, *input1, is_interleaved_transposed, reshape_info)).set_data_type(DataType::S32)); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 145 | |
| 146 | TensorInfo tmp_info(*output); |
| 147 | |
| 148 | if(reinterpret_output_as_3d) |
| 149 | { |
| 150 | // Since the output tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM, |
| 151 | // the window needs to be constructed on the 2D collapsed version of the tensor |
| 152 | TensorShape tmp_shape(output->tensor_shape()); |
| 153 | tmp_shape.collapse(2U, 1U); |
| 154 | tmp_info.set_tensor_shape(tmp_shape); |
| 155 | } |
| 156 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 157 | // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication |
| 158 | if(is_interleaved_transposed) |
| 159 | { |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 160 | // reinterpret_input_as_3d is not supported if is_interleaved_transposed is set |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 161 | ARM_COMPUTE_ERROR_ON(reshape_info.reinterpret_input_as_3d()); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 162 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 163 | // Configure kernel window |
| 164 | num_elems_processed_per_iteration_x = 4; |
| 165 | num_elems_processed_per_iteration_y = 4; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 166 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 167 | // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor |
| 168 | // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic |
| 169 | const int m = reshape_info.m(); |
| 170 | const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y; |
| 171 | |
| 172 | win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 173 | win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 174 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 175 | AccessWindowRectangle input0_access(input0, 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 176 | AccessWindowStatic input1_access(input1, 0, 0, |
| 177 | ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), |
| 178 | ceil_to_multiple(input1->dimension(1), num_elems_processed_per_iteration_y)); |
| 179 | AccessWindowStatic output_access(output, 0, 0, |
| 180 | ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x), |
| 181 | output->dimension(1) + bottom_pad); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 182 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 183 | window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop |
| 184 | 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] | 185 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 186 | output_access.set_valid_region(win_out, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 187 | } |
| 188 | else |
| 189 | { |
| 190 | // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor. num_elems_processed_per_iteration_x |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 191 | // Note: if the dot product instruction is available, the 8x2 tile has to be used |
| 192 | num_elems_processed_per_iteration_x = is_dot8_supported ? 8 : 4; |
| 193 | num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), is_dot8_supported ? 2 : 4); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 194 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 195 | // Note: bottom paddings are calculated manually as the output can be reinterpreted as 3D tensor |
| 196 | // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic |
| 197 | const int m = reinterpret_input_as_3d ? input0->tensor_shape()[1] * input0->tensor_shape()[2] : input0->tensor_shape()[1]; |
| 198 | const int bottom_pad = (num_elems_processed_per_iteration_y - (m % num_elems_processed_per_iteration_y)) % num_elems_processed_per_iteration_y; |
| 199 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 200 | // Configure window |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 201 | win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 202 | win_out = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 203 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 204 | AccessWindowStatic input0_access(input0, 0, 0, input0->dimension(0), input0->dimension(1) + bottom_pad); |
| 205 | AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1)); |
| 206 | AccessWindowStatic output_access(output, 0, 0, |
| 207 | ceil_to_multiple(output->dimension(0), num_elems_processed_per_iteration_x), |
| 208 | output->dimension(1) + bottom_pad); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 209 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 210 | window_changed = update_window_and_padding(win, input0_access, input1_access) || // window used by the execute_window_loop |
| 211 | 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] | 212 | |
| 213 | Coordinates coord; |
| 214 | coord.set_num_dimensions(output->num_dimensions()); |
Isabella Gottardi | c4f582e | 2018-10-11 19:14:55 +0100 | [diff] [blame] | 215 | output_access.set_valid_region(win_out, ValidRegion(coord, output->tensor_shape())); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 218 | // Collapse along the Z direction |
| 219 | // This collapse needs to be here in order to tune the Z dimension of LWS |
| 220 | Window collapsed = win; |
| 221 | const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u); |
| 222 | collapsed = win.collapse(win, dimension_to_collapse); |
| 223 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 224 | 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] | 225 | return std::make_pair(err, collapsed); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 226 | } |
| 227 | } // namespace |
| 228 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 229 | CLGEMMLowpMatrixMultiplyKernel::CLGEMMLowpMatrixMultiplyKernel() |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 230 | : _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] | 231 | { |
| 232 | } |
| 233 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 234 | void CLGEMMLowpMatrixMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 236 | ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 237 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 238 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 239 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 240 | _input0 = input0; |
| 241 | _input1 = input1; |
| 242 | _output = output; |
| 243 | _reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d(); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 244 | _reinterpret_output_as_3d = (reshape_info.depth_output_gemm3d() != 0); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 245 | |
| 246 | // In case both input and output have to be reinterpreted as 3D tensors, |
| 247 | // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false. |
| 248 | if(_reinterpret_input_as_3d == _reinterpret_output_as_3d) |
| 249 | { |
| 250 | _reinterpret_input_as_3d = false; |
| 251 | _reinterpret_output_as_3d = false; |
| 252 | } |
| 253 | |
| 254 | // Check if we need to slide the matrix B |
| 255 | const unsigned int num_dimensions_input0 = _reinterpret_input_as_3d ? _input0->info()->num_dimensions() - 1 : _input0->info()->num_dimensions(); |
| 256 | _slide_matrix_b = (_input1->info()->num_dimensions() >= num_dimensions_input0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 257 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 258 | ElementsProcessed num_elements_processed{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 259 | |
Gian Marco | 7b4d547 | 2018-01-10 15:56:30 +0000 | [diff] [blame] | 260 | // Get target architecture |
| 261 | GPUTarget arch_target = get_arch_from_target(get_target()); |
| 262 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 263 | // Configure kernel window |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 264 | auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info, num_elements_processed); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 265 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 266 | ICLKernel::configure_internal(win_config.second); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 267 | |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 268 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
| 269 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 270 | // Create build options |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 271 | std::string kernel_name(" "); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 272 | CLBuildOptions build_opts; |
| 273 | build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D"); |
| 274 | build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D"); |
| 275 | build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(1))); |
| 276 | build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, "-DDEPTH_GEMM3D=" + support::cpp11::to_string(output->info()->dimension(2))); |
| 277 | build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2))); |
| 278 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 279 | if(is_interleaved_transposed) |
| 280 | { |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 281 | const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width(); |
| 282 | const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height(); |
| 283 | |
| 284 | // Note: The computation tile has the x dimension equal to 4 which is less than the transpose_width (16) |
| 285 | // In order to access correctly the elements from the transposed matrix B, we need to pass |
| 286 | // the correct step which is calculated as (16 * mult_transpose1xW_width) / 4) |
| 287 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 288 | build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(input1->info()->dimension(0))); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 289 | build_opts.add_option("-DMULT_TRANSPOSE1XW_WIDTH=" + support::cpp11::to_string(mult_transpose1xW_width)); |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 290 | build_opts.add_option("-DTRANSPOSE1XW_WIDTH_STEP=" + support::cpp11::to_string(4 * mult_transpose1xW_width)); |
| 291 | build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height)); |
| 292 | |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 293 | kernel_name = "gemmlowp_mm_interleaved_transposed_" + string_from_target(arch_target) + (is_dot8_supported ? "_dot8" : ""); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 294 | } |
| 295 | else |
| 296 | { |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 297 | build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(input0->info()->dimension(0))); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 298 | build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_X=" + support::cpp11::to_string(num_elements_processed.x())); |
| 299 | build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_Y=" + support::cpp11::to_string(num_elements_processed.y())); |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 300 | |
| 301 | kernel_name = "gemmlowp_mm_" + string_from_target(arch_target) + (is_dot8_supported ? "_dot8" : ""); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 302 | } |
Giorgio Arena | 6200fa4 | 2018-07-06 17:06:36 +0100 | [diff] [blame] | 303 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 304 | // Create kernel |
| 305 | _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] | 306 | |
| 307 | // Set config_id for enabling LWS tuning |
| 308 | _config_id = "gemmlowp_"; |
| 309 | _config_id += (is_interleaved_transposed ? "reshaped_" : ""); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 310 | _config_id += (_reinterpret_input_as_3d ? "3di_" : ""); |
| 311 | _config_id += (_reinterpret_output_as_3d ? "3do_" : ""); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 312 | _config_id += lower_string(string_from_data_type(input0->info()->data_type())); |
| 313 | _config_id += "_"; |
| 314 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 315 | _config_id += "_"; |
| 316 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 317 | _config_id += "_"; |
| 318 | _config_id += (is_interleaved_transposed ? support::cpp11::to_string(input1->info()->dimension(0)) : support::cpp11::to_string(input1->info()->dimension(1))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 321 | Status CLGEMMLowpMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 322 | { |
| 323 | ElementsProcessed num_elements_processed{}; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 324 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output, is_interleaved_transposed, reshape_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 325 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), |
| 326 | input1->clone().get(), |
| 327 | output->clone().get(), |
| 328 | is_interleaved_transposed, |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 329 | reshape_info, |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 330 | num_elements_processed) |
| 331 | .first); |
| 332 | |
| 333 | return Status{}; |
| 334 | } |
| 335 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 336 | void CLGEMMLowpMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue) |
| 337 | { |
| 338 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 339 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 340 | |
Isabella Gottardi | b92805b | 2018-09-28 18:24:27 +0100 | [diff] [blame] | 341 | if(_input1->info()->num_dimensions() < 3) |
| 342 | { |
| 343 | // The stride_z for matrix B must be zero if we do not slice |
| 344 | ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0); |
| 345 | } |
| 346 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 347 | Window slice = window.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | Window slice_matrix_b = slice; |
Isabella Gottardi | b92805b | 2018-09-28 18:24:27 +0100 | [diff] [blame] | 349 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 350 | slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 351 | slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 352 | |
| 353 | if(_reinterpret_input_as_3d) |
| 354 | { |
| 355 | // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor |
| 356 | const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3; |
| 357 | const unsigned int total_cross_plane_pad = _input0->info()->padding().top + _input0->info()->padding().bottom; |
| 358 | _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad)); |
| 359 | } |
| 360 | |
| 361 | if(_reinterpret_output_as_3d) |
| 362 | { |
| 363 | // Pass bottom paddings to the kernel if the output has to be reinterpreted as 3D tensor |
| 364 | const unsigned int idx0 = 3 * num_arguments_per_2D_tensor() + 3 + (_reinterpret_input_as_3d ? 1 : 0); |
| 365 | const unsigned int total_cross_plane_pad = _output->info()->padding().top + _output->info()->padding().bottom; |
| 366 | _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad)); |
| 367 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 368 | |
| 369 | do |
| 370 | { |
| 371 | Window slice_b = slice; |
| 372 | // 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] | 373 | // This scenario can happen when the matrix multiplication is used to perform a convolution operation |
| 374 | if(!_slide_matrix_b) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 375 | { |
| 376 | slice_b = slice_matrix_b; |
| 377 | } |
| 378 | |
| 379 | unsigned int idx = 0; |
| 380 | add_2D_tensor_argument(idx, _input0, slice); |
| 381 | add_2D_tensor_argument(idx, _input1, slice_b); |
| 382 | add_2D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 383 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2])); |
| 384 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2])); |
| 385 | _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] | 386 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 387 | } |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 388 | while(window.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 389 | } |