Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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/CLGEMMMatrixMultiplyKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/AccessWindowTranspose.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLHelpers.h" |
| 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" |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/FixedPoint.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Helpers.h" |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Types.h" |
| 37 | #include "arm_compute/core/Utils.h" |
| 38 | #include "arm_compute/core/Validate.h" |
| 39 | #include "arm_compute/core/Window.h" |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 40 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | |
| 42 | #include <set> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | #include <string> |
| 44 | |
| 45 | using namespace arm_compute; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 46 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 48 | namespace |
| 49 | { |
| 50 | using ElementsProcessed = Steps; |
| 51 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 52 | inline 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] | 53 | { |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input0, input1, output); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input0, input1); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input1->num_dimensions() > 3, "The number of dimensions for the matrix B must be <= 3"); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 59 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 60 | if(!is_interleaved_transposed) |
| 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(0) != input1->dimension(1)); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 63 | |
| 64 | if(output->total_size() != 0) |
| 65 | { |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(input1->dimension(0) != output->dimension(0)); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(input0->dimension(1) != output->dimension(1)); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input0, output); |
| 70 | } |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | const int m = reshape_info.m(); |
| 75 | const int n = reshape_info.n(); |
| 76 | const int k = reshape_info.k(); |
| 77 | const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width(); |
| 78 | const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height(); |
| 79 | |
| 80 | TensorShape tensor_shape0{ input0->tensor_shape() }; |
| 81 | tensor_shape0.set(0, k); |
| 82 | tensor_shape0.set(1, m); |
| 83 | |
| 84 | TensorShape tensor_shape1{ input1->tensor_shape() }; |
| 85 | tensor_shape1.set(0, n); |
| 86 | tensor_shape1.set(1, k); |
| 87 | |
| 88 | const TensorInfo tensor_info0 = input0->clone()->set_tensor_shape(tensor_shape0); |
| 89 | const TensorInfo tensor_info1 = input1->clone()->set_tensor_shape(tensor_shape1); |
| 90 | |
| 91 | const TensorInfo tensor_info_reshaped0 = input0->clone()->set_tensor_shape(compute_interleaved_shape(tensor_info0, mult_interleave4x4_height)); |
| 92 | const TensorInfo tensor_info_reshaped1 = input1->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(tensor_info1, mult_transpose1xW_width)); |
| 93 | |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input0, &tensor_info_reshaped0); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, &tensor_info_reshaped1); |
| 96 | |
| 97 | if(output->total_size() != 0) |
| 98 | { |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) != static_cast<size_t>(n)); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != static_cast<size_t>(m)); |
| 101 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, output); |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input0, output); |
| 103 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return Status{}; |
| 107 | } |
| 108 | |
| 109 | inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output, |
| 110 | bool is_interleaved_transposed, GPUTarget gpu_target, |
| 111 | ElementsProcessed &num_elements_processed) |
| 112 | { |
| 113 | bool window_changed = false; |
| 114 | Window win{}; |
| 115 | |
| 116 | const DataType data_type = input0->data_type(); |
| 117 | unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0]; |
| 118 | unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1]; |
| 119 | |
| 120 | if(is_interleaved_transposed) |
| 121 | { |
| 122 | // Configure kernel window |
| 123 | num_elems_processed_per_iteration_x = max_cl_vector_width / data_size_from_type(data_type); |
| 124 | num_elems_processed_per_iteration_y = 4; |
| 125 | |
| 126 | win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 127 | |
| 128 | AccessWindowRectangle input0_access(input0, 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f); |
| 129 | AccessWindowTranspose input1_access(input1, 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f); |
| 130 | AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 131 | |
| 132 | window_changed = update_window_and_padding(win, input0_access, input1_access, output_access); |
| 133 | |
| 134 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
| 135 | } |
| 136 | else // The input tensors have not been reshaped |
| 137 | { |
| 138 | // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor. num_elems_processed_per_iteration_x is set up for the default case. |
| 139 | num_elems_processed_per_iteration_x = max_cl_vector_width / data_size_from_type(data_type); |
| 140 | num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->dimension(1)), 4); |
| 141 | |
| 142 | // Create kernels according to the architecture, data type and input size. |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 143 | GPUTarget arch_target = get_arch_from_target(gpu_target); |
| 144 | if(arch_target == GPUTarget::BIFROST && data_type == DataType::F32) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 145 | { |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 146 | num_elems_processed_per_iteration_x = (input1->dimension(0) <= 1000 && input0->num_dimensions() == 1) ? 2 : 4; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Configure window |
| 150 | win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 151 | |
| 152 | AccessWindowStatic input0_access(input0, 0, 0, input0->dimension(0), ceil_to_multiple(input0->dimension(1), num_elems_processed_per_iteration_y)); |
| 153 | AccessWindowStatic input1_access(input1, 0, 0, ceil_to_multiple(input1->dimension(0), num_elems_processed_per_iteration_x), input1->dimension(1)); |
| 154 | AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 155 | |
| 156 | window_changed = update_window_and_padding(win, input0_access, input1_access, output_access); |
| 157 | |
| 158 | Coordinates coord; |
| 159 | coord.set_num_dimensions(output->num_dimensions()); |
| 160 | output_access.set_valid_region(win, ValidRegion(coord, output->tensor_shape())); |
| 161 | } |
| 162 | |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 163 | // Collapse along the Z direction |
| 164 | // This collapse needs to be here in order to tune the Z dimension of LWS |
Gian Marco Iodice | 81b28c4 | 2018-03-29 10:29:36 +0100 | [diff] [blame] | 165 | Window collapsed = win; |
| 166 | const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(output->num_dimensions()), 2u); |
| 167 | collapsed = win.collapse(win, dimension_to_collapse); |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 168 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 169 | 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] | 170 | return std::make_pair(err, collapsed); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 171 | } |
| 172 | } // namespace |
| 173 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | CLGEMMMatrixMultiplyKernel::CLGEMMMatrixMultiplyKernel() |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 175 | : _input0(nullptr), _input1(nullptr), _output(nullptr), _slide_matrix_b(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | { |
| 177 | } |
| 178 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 179 | void CLGEMMMatrixMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, float alpha, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 181 | ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output); |
| 182 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 183 | // Output tensor auto inizialitation if not yet initialized |
| 184 | TensorShape tensor_shape{ input0->info()->tensor_shape() }; |
| 185 | tensor_shape.set(0, is_interleaved_transposed ? reshape_info.n() : input1->info()->dimension(0)); |
| 186 | tensor_shape.set(1, is_interleaved_transposed ? reshape_info.m() : input0->info()->dimension(1)); |
| 187 | |
| 188 | auto_init_if_empty(*output->info(), input0->info()->clone()->set_tensor_shape(tensor_shape)); |
| 189 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 190 | // Perform validate step |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 191 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info(), is_interleaved_transposed, reshape_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 193 | _input0 = input0; |
| 194 | _input1 = input1; |
| 195 | _output = output; |
| 196 | _slide_matrix_b = _input1->info()->num_dimensions() >= _input0->info()->num_dimensions(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 197 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 198 | const DataType data_type = input0->info()->data_type(); |
| 199 | const int fp_pos = input0->info()->fixed_point_position(); |
| 200 | |
| 201 | // Get target architecture |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 202 | GPUTarget gpu_target = get_target(); |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 203 | |
| 204 | // Configure LWS hint |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 205 | switch(gpu_target) |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 206 | { |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 207 | case GPUTarget::MIDGARD: |
| 208 | case GPUTarget::T600: |
| 209 | case GPUTarget::T700: |
| 210 | case GPUTarget::T800: |
| 211 | if(output->info()->dimension(1) == 196) |
| 212 | { |
| 213 | _lws_hint = cl::NDRange(1, 7); |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | _lws_hint = cl::NDRange(8, 8); |
| 218 | } |
| 219 | break; |
| 220 | case GPUTarget::G71: |
| 221 | case GPUTarget::G72: |
Sam Laynton | 56e8e86 | 2018-04-05 13:26:08 +0100 | [diff] [blame] | 222 | case GPUTarget::G51: |
| 223 | case GPUTarget::G51BIG: |
| 224 | case GPUTarget::G51LIT: |
| 225 | case GPUTarget::TNOX: |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 226 | if(input1->info()->dimension(1) == 24) |
| 227 | { |
| 228 | // LWS optimized for the 11x11 AlexNet convolution on Bifrost. |
| 229 | _lws_hint = cl::NDRange(2, 2); |
| 230 | } |
| 231 | else if(output->info()->dimension(1) == 196) |
| 232 | { |
| 233 | _lws_hint = cl::NDRange(1, 7); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | _lws_hint = cl::NDRange(8, 8); |
| 238 | } |
| 239 | break; |
| 240 | default: |
| 241 | _lws_hint = cl::NullRange; |
Anthony Barbier | fcd52fb | 2017-11-28 10:31:43 +0000 | [diff] [blame] | 242 | } |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 243 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 244 | ElementsProcessed num_elements_processed{}; |
| 245 | |
| 246 | // Configure kernel window |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 247 | auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info(), is_interleaved_transposed, gpu_target, num_elements_processed); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 248 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 249 | ICLKernel::configure(win_config.second); |
| 250 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 251 | // Create build options |
| 252 | CLBuildOptions build_opts; |
| 253 | build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(fp_pos)); |
| 254 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 255 | // Only define ALPHA when alpha is not 1.0f. This avoids performing unnecessary multiplications. |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 256 | if(std::abs(1.0f - alpha) > 0.00001f) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 257 | { |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 258 | build_opts.add_option_if_else(is_data_type_fixed_point(data_type), |
| 259 | "-DALPHA=" + support::cpp11::to_string((data_type == DataType::QS8 ? sqcvt_qs8_f32(alpha, fp_pos) : sqcvt_qs16_f32(alpha, fp_pos))), |
| 260 | "-DALPHA=" + float_to_string_with_full_precision(alpha)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 263 | // Do not slide matrix B if _slide_matrix_b = false |
| 264 | build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(input1->info()->dimension(2))); |
| 265 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 266 | std::string kernel_name; |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 267 | if(is_interleaved_transposed) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 268 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 269 | const int mult_transpose1xW_width = reshape_info.mult_transpose1xW_width(); |
| 270 | const int mult_interleave4x4_height = reshape_info.mult_interleave4x4_height(); |
| 271 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 272 | build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(input1->info()->dimension(0))); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 273 | build_opts.add_option("-DMULT_TRANSPOSE1XW_WIDTH=" + support::cpp11::to_string(mult_transpose1xW_width)); |
| 274 | build_opts.add_option("-DMULT_INTERLEAVE4X4_HEIGHT=" + support::cpp11::to_string(mult_interleave4x4_height)); |
| 275 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 276 | if(data_type == DataType::F32) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 277 | { |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 278 | GPUTarget arch_target = get_arch_from_target(gpu_target); |
| 279 | kernel_name = "gemm_mm_interleaved_transposed_f32_" + string_from_target(arch_target); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 283 | kernel_name = "gemm_mm_interleaved_transposed_" + lower_string(string_from_data_type(data_type)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 284 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 285 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 286 | else // The input tensors have not been reshaped |
| 287 | { |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 288 | build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(input0->info()->dimension(0))); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 289 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 290 | // Create kernels according to the architecture, data type and input size. |
Gian Marco Iodice | fd68311 | 2018-04-17 09:52:44 +0100 | [diff] [blame] | 291 | if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72, GPUTarget::G51, GPUTarget::G51BIG, GPUTarget::G51LIT, GPUTarget::TNOX) && is_data_type_float(data_type)) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 292 | { |
Gian Marco Iodice | fd68311 | 2018-04-17 09:52:44 +0100 | [diff] [blame] | 293 | kernel_name = "gemm_mm_floating_point_" + lower_string(string_from_data_type(data_type)) + "_bifrost"; |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 294 | // The first kernel is optimized for the case of 1000 or less output elements (e.g. FC8 of AlexNet and VGG-16, and |
| 295 | // FC1 of Inception v3). The second kernel is optimized for the case of greater than 1000 output elements (e.g. |
| 296 | // FC6 and FC7 of AlexNet and VGG-16). |
Gian Marco Iodice | fd68311 | 2018-04-17 09:52:44 +0100 | [diff] [blame] | 297 | if(input1->info()->dimension(0) <= 1000 && input0->info()->num_dimensions() == 1 && data_type == DataType::F32) |
| 298 | { |
| 299 | kernel_name += "_1000"; |
| 300 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 301 | |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 302 | // The work-group size equal to the Bifrost quad size has been proved to be optimal for these kernels |
| 303 | // via exhaustive autotuning over a range of representative layer configurations. |
| 304 | _lws_hint = cl::NDRange(4); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 305 | } |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 306 | else if(is_data_type_fixed_point(data_type)) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 307 | { |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 308 | kernel_name = "gemm_mm_" + lower_string(string_from_data_type(data_type)); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 309 | } |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 310 | else // (MIDGARD and F32) or (F16) |
| 311 | { |
| 312 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 313 | kernel_name = "gemm_mm_floating_point"; |
| 314 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 315 | build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_Y=" + support::cpp11::to_string(num_elements_processed.y())); |
| 316 | build_opts.add_option("-DNUM_ELEMS_PROCESSED_PER_THREAD_X=" + support::cpp11::to_string(num_elements_processed.x())); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 317 | } |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 318 | |
| 319 | // Create kernel |
| 320 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 321 | |
| 322 | // Set config_id for enabling LWS tuning |
| 323 | _config_id = "gemm_"; |
| 324 | _config_id += (is_interleaved_transposed ? "reshaped_" : ""); |
| 325 | _config_id += lower_string(string_from_data_type(input0->info()->data_type())); |
| 326 | _config_id += "_"; |
| 327 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 328 | _config_id += "_"; |
| 329 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 330 | _config_id += "_"; |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 331 | _config_id += support::cpp11::to_string(output->info()->dimension(2)); |
| 332 | _config_id += "_"; |
| 333 | _config_id += support::cpp11::to_string(output->info()->dimension(3)); |
| 334 | _config_id += "_"; |
Anton Lokhmotov | 3e80c7f | 2017-11-20 11:02:10 +0000 | [diff] [blame] | 335 | _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] | 336 | } |
| 337 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 338 | Status CLGEMMMatrixMultiplyKernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output, float alpha, bool is_interleaved_transposed, |
| 339 | const GEMMReshapeInfo &reshape_info, GPUTarget gpu_target) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 340 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 341 | // Note: num_elements_processed will be set in validate_and_configure_window() |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 342 | ElementsProcessed num_elements_processed{}; |
| 343 | ARM_COMPUTE_UNUSED(alpha); |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 344 | 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] | 345 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), |
| 346 | input1->clone().get(), |
| 347 | output->clone().get(), |
| 348 | is_interleaved_transposed, |
| 349 | gpu_target, |
| 350 | num_elements_processed) |
| 351 | .first); |
| 352 | |
| 353 | return Status{}; |
| 354 | } |
| 355 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 356 | void CLGEMMMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue) |
| 357 | { |
| 358 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 359 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 360 | |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 361 | if(_input1->info()->num_dimensions() < 3) |
| 362 | { |
| 363 | // The stride_z for matrix B must be zero if we do not slice |
| 364 | ARM_COMPUTE_ERROR_ON(_input1->info()->strides_in_bytes()[3] != 0); |
| 365 | } |
| 366 | |
| 367 | Window slice = window.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 368 | Window slice_matrix_b = slice; |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 369 | |
| 370 | slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 371 | slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 372 | |
| 373 | do |
| 374 | { |
| 375 | Window slice_b = slice; |
| 376 | // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2 |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 377 | // This scenario can happen when the matrix multiplication is used to perform a convolution operation |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 378 | if(!_slide_matrix_b) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 379 | { |
| 380 | slice_b = slice_matrix_b; |
| 381 | } |
| 382 | |
| 383 | unsigned int idx = 0; |
| 384 | add_2D_tensor_argument(idx, _input0, slice); |
| 385 | add_2D_tensor_argument(idx, _input1, slice_b); |
| 386 | add_2D_tensor_argument(idx, _output, slice); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 387 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input0->info()->strides_in_bytes()[2])); |
| 388 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_input1->info()->strides_in_bytes()[2])); |
| 389 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(_output->info()->strides_in_bytes()[2])); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 390 | enqueue(queue, *this, slice, _lws_hint); |
| 391 | } |
Gian Marco | ae2af74 | 2018-02-15 12:35:44 +0000 | [diff] [blame] | 392 | while(window.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 393 | } |