Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | bacfec5 | 2019-01-11 11:30:55 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [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/CLGEMMReshapeLHSMatrixKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/CLValidate.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" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "arm_compute/core/Utils.h" |
| 37 | #include "arm_compute/core/Window.h" |
| 38 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | using namespace arm_compute::misc::shape_calculator; |
| 42 | |
| 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d) |
| 46 | { |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 == 0); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.k0 == 0); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.v0 == 0); |
Gian Marco Iodice | bacfec5 | 2019-01-11 11:30:55 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((lhs_info.k0 & (lhs_info.k0 - 1)) && lhs_info.k0 != 3), "Only 2,3,4,8,16 are supported for k0"); |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.k0 > 16); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 2 || lhs_info.m0 > 8); |
| 53 | |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8, |
| 56 | DataType::U16, DataType::S16, DataType::U32, DataType::S32, |
| 57 | DataType::F16, DataType::F32); |
| 58 | |
| 59 | if(output->total_size() != 0) |
| 60 | { |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_lhs_reshaped_shape(*input, lhs_info, reinterpret_input_as_3d)); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 63 | } |
| 64 | |
| 65 | return Status{}; |
| 66 | } |
| 67 | |
| 68 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d) |
| 69 | { |
| 70 | const unsigned int num_elems_processed_per_iteration_x = lhs_info.k0; |
| 71 | const unsigned int num_elems_processed_per_iteration_y = lhs_info.m0; |
| 72 | bool window_changed = false; |
| 73 | |
| 74 | TensorInfo tmp_info(*input); |
| 75 | |
| 76 | if(reinterpret_input_as_3d) |
| 77 | { |
| 78 | // Since the input tensor has to be reinterpreted as 3D and the execute window is based on a 2D interleave, |
| 79 | // the window needs to be constructed on the 2D collapsed version of the tensor |
| 80 | TensorShape tmp_shape(input->tensor_shape()); |
| 81 | tmp_shape.collapse(2U, 1U); |
| 82 | tmp_info.set_tensor_shape(tmp_shape); |
| 83 | } |
| 84 | |
| 85 | // Output auto inizialitation if not yet initialized |
| 86 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*input, lhs_info, reinterpret_input_as_3d))); |
| 87 | |
| 88 | // Configure window |
| 89 | // Note: bottom paddings are calculated manually as the input can be reinterpreted as 3D tensor |
| 90 | // The only way to set properly the paddings, it is to set those explicitly through the AccessWindowStatic |
| 91 | const int m = reinterpret_input_as_3d ? input->tensor_shape()[1] * input->tensor_shape()[2] : input->tensor_shape()[1]; |
| 92 | const int bottom_pad = ceil_to_multiple(m, num_elems_processed_per_iteration_y) - m; |
| 93 | |
| 94 | Window win = calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 95 | Window win_in = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 96 | |
| 97 | AccessWindowStatic input_access(input, 0, 0, |
| 98 | ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration_x), |
| 99 | input->dimension(1) + bottom_pad); |
| 100 | AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1)); |
| 101 | |
| 102 | window_changed = update_window_and_padding(win_in, input_access) || // window used by the execute_window_loop |
| 103 | update_window_and_padding(win, output_access); // window used to update the padding requirements of output tensor |
| 104 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
| 105 | |
| 106 | // Collapse along the Z direction |
| 107 | // This collapse needs to be here in order to tune the Z dimension of LWS |
| 108 | Window collapsed = win.collapse(win, Window::DimZ); |
| 109 | |
| 110 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 111 | return std::make_pair(err, collapsed); |
| 112 | } |
| 113 | } // namespace |
| 114 | |
| 115 | CLGEMMReshapeLHSMatrixKernel::CLGEMMReshapeLHSMatrixKernel() |
| 116 | : _input(nullptr), _output(nullptr), _reinterpret_input_as_3d(false) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | void CLGEMMReshapeLHSMatrixKernel::configure(const ICLTensor *input, ICLTensor *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d) |
| 121 | { |
| 122 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 123 | |
| 124 | // Perform validate step |
| 125 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), lhs_info, reinterpret_input_as_3d)); |
| 126 | |
| 127 | _input = input; |
| 128 | _output = output; |
| 129 | _reinterpret_input_as_3d = reinterpret_input_as_3d; |
| 130 | |
| 131 | // Create build options |
| 132 | CLBuildOptions build_opts; |
| 133 | build_opts.add_option("-DM0=" + support::cpp11::to_string(lhs_info.m0)); |
| 134 | build_opts.add_option("-DK0=" + support::cpp11::to_string(lhs_info.k0)); |
| 135 | build_opts.add_option("-DV0=" + support::cpp11::to_string(lhs_info.v0)); |
| 136 | build_opts.add_option_if(lhs_info.interleave, "-DINTERLEAVE"); |
| 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 | |
| 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 | |
| 156 | std::string kernel_name("gemm_reshape_lhs_matrix_"); |
| 157 | kernel_name += lhs_info.transpose ? "t" : "nt"; |
| 158 | |
| 159 | // Create kernel |
| 160 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 161 | |
| 162 | // Configure kernel window |
| 163 | auto win_config = validate_and_configure_window(input->info(), output->info(), lhs_info, reinterpret_input_as_3d); |
| 164 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 165 | ICLKernel::configure_internal(win_config.second); |
| 166 | |
| 167 | // Set config_id for enabling LWS tuning |
| 168 | _config_id = "gemm_reshape_lhs_matrix_"; |
| 169 | _config_id += (_reinterpret_input_as_3d ? "3d_" : ""); |
| 170 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 171 | _config_id += "_"; |
| 172 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 173 | _config_id += "_"; |
| 174 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 175 | _config_id += "_"; |
| 176 | _config_id += support::cpp11::to_string(output->info()->dimension(2)); |
| 177 | _config_id += "_"; |
| 178 | _config_id += support::cpp11::to_string(lhs_info.m0); |
| 179 | _config_id += "_"; |
| 180 | _config_id += support::cpp11::to_string(lhs_info.k0); |
| 181 | _config_id += "_"; |
| 182 | _config_id += support::cpp11::to_string(lhs_info.v0); |
| 183 | _config_id += "_"; |
| 184 | _config_id += support::cpp11::to_string(lhs_info.interleave); |
| 185 | _config_id += "_"; |
| 186 | _config_id += support::cpp11::to_string(lhs_info.transpose); |
| 187 | } |
| 188 | |
| 189 | Status CLGEMMReshapeLHSMatrixKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d) |
| 190 | { |
| 191 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, lhs_info, reinterpret_input_as_3d)); |
| 192 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), lhs_info, reinterpret_input_as_3d).first); |
| 193 | |
| 194 | return Status{}; |
| 195 | } |
| 196 | |
| 197 | void CLGEMMReshapeLHSMatrixKernel::run(const Window &window, cl::CommandQueue &queue) |
| 198 | { |
| 199 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 200 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 201 | |
| 202 | Window slice = window.first_slice_window_3D(); |
| 203 | |
| 204 | if(_reinterpret_input_as_3d) |
| 205 | { |
| 206 | // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor |
| 207 | const unsigned int idx0 = 2 * num_arguments_per_3D_tensor(); |
| 208 | const unsigned int total_cross_plane_pad = _input->info()->padding().top + _input->info()->padding().bottom; |
| 209 | _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad)); |
| 210 | } |
| 211 | |
| 212 | do |
| 213 | { |
| 214 | unsigned int idx = 0; |
| 215 | add_3D_tensor_argument(idx, _input, slice); |
| 216 | add_3D_tensor_argument(idx, _output, slice); |
| 217 | enqueue(queue, *this, slice, lws_hint()); |
| 218 | } |
| 219 | while(window.slide_window_slice_3D(slice)); |
| 220 | } |