Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18: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/CLGEMMReshapeRHSMatrixKernel.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" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 39 | #include "support/StringSupport.h" |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 40 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 41 | namespace arm_compute |
| 42 | { |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 43 | using namespace arm_compute::misc::shape_calculator; |
| 44 | |
| 45 | namespace |
| 46 | { |
| 47 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info) |
| 48 | { |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.n0 == 0); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 == 0); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.h0 == 0); |
Gian Marco Iodice | bacfec5 | 2019-01-11 11:30:55 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3), "Only 2,3,4,8,16 are supported for n0"); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.k0 & (rhs_info.k0 - 1)) && (rhs_info.k0 != 1) && (rhs_info.k0 != 3)), "Only 1,2,3,4,8,16 are supported for k0"); |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.n0 > 16); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 > 16); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON((rhs_info.k0 == 1) && (rhs_info.transpose)); |
Gian Marco Iodice | a98dee2 | 2020-06-02 12:12:35 +0100 | [diff] [blame] | 57 | |
| 58 | if(rhs_info.export_to_cl_image) |
| 59 | { |
Gian Marco Iodice | e3a849a | 2020-06-10 17:59:30 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((rhs_info.n0 == 2) || (rhs_info.n0 == 3), "Export to cl_image only supported with n0 = 4, 8 or 16"); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((rhs_info.k0 == 2) || (rhs_info.k0 == 3), "Export to cl_image only supported with k0 = 4, 8 or 16"); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() != DataType::F32, "Export to cl_image only supported with F32 data type"); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!image2d_from_buffer_supported(CLKernelLibrary::get().get_device()), "The extension cl_khr_image2d_from_buffer is not supported on the target platform"); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0, "Impossible to retrieve the cl_image pitch alignment"); |
| 65 | |
Gian Marco Iodice | a98dee2 | 2020-06-02 12:12:35 +0100 | [diff] [blame] | 66 | TensorShape output_shape = compute_rhs_reshaped_shape(*input, rhs_info); |
| 67 | |
| 68 | // Check the width and height of the output tensor. |
| 69 | // Since we cannot create a 3d image from a buffer, the third dimension is collapsed with the second dimension |
| 70 | size_t max_image_w = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>(); |
| 71 | size_t max_image_h = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>(); |
| 72 | |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_shape[0] > max_image_w * 4, "Not supported width for cl_image"); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output_shape[1] * output_shape[2] > max_image_h, "Not supported height for cl_image"); |
| 75 | } |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 76 | |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 79 | |
| 80 | if(output->total_size() != 0) |
| 81 | { |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_rhs_reshaped_shape(*input, rhs_info)); |
| 83 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 84 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return Status{}; |
| 88 | } |
| 89 | |
| 90 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info) |
| 91 | { |
| 92 | const unsigned int num_elems_processed_per_iteration_x = rhs_info.n0; |
| 93 | const unsigned int num_elems_processed_per_iteration_y = rhs_info.k0; |
| 94 | bool window_changed = false; |
| 95 | |
| 96 | // Output auto initialization if not yet initialized |
| 97 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*input, rhs_info))); |
| 98 | |
| 99 | // Configure window |
| 100 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 101 | |
| 102 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 103 | AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1)); |
| 104 | |
| 105 | window_changed = update_window_and_padding(win, input_access); |
| 106 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
| 107 | |
Gian Marco Iodice | a98dee2 | 2020-06-02 12:12:35 +0100 | [diff] [blame] | 108 | if(rhs_info.export_to_cl_image) |
| 109 | { |
| 110 | constexpr unsigned int num_floats_per_pixel = 4; |
| 111 | |
| 112 | const unsigned int stride_y_in_elements = output->strides_in_bytes()[1] / output->element_size(); |
| 113 | const unsigned int pixel_aligment = get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()); |
| 114 | const unsigned int row_pitch_alignment = pixel_aligment * num_floats_per_pixel; |
| 115 | const unsigned int round_up_width = ((stride_y_in_elements + row_pitch_alignment - 1) / row_pitch_alignment) * row_pitch_alignment; |
| 116 | const unsigned int padding = round_up_width - stride_y_in_elements; |
| 117 | |
| 118 | output->extend_padding(PaddingSize(0, padding, 0, 0)); |
| 119 | } |
| 120 | |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 121 | // Collapse along the Z direction |
| 122 | // This collapse needs to be here in order to tune the Z dimension of LWS |
| 123 | Window collapsed = win.collapse(win, Window::DimZ); |
| 124 | |
| 125 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 126 | return std::make_pair(err, collapsed); |
| 127 | } |
| 128 | } // namespace |
| 129 | |
| 130 | CLGEMMReshapeRHSMatrixKernel::CLGEMMReshapeRHSMatrixKernel() |
| 131 | : _input(nullptr), _output(nullptr) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | void CLGEMMReshapeRHSMatrixKernel::configure(const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info) |
| 136 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 137 | configure(CLKernelLibrary::get().get_compile_context(), input, output, rhs_info); |
| 138 | } |
| 139 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 140 | void CLGEMMReshapeRHSMatrixKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const GEMMRHSMatrixInfo &rhs_info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 141 | { |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 143 | |
| 144 | // Perform validate step |
| 145 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), rhs_info)); |
| 146 | |
| 147 | _input = input; |
| 148 | _output = output; |
| 149 | |
| 150 | // Create build options |
| 151 | CLBuildOptions build_opts; |
| 152 | build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0)); |
| 153 | build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0)); |
| 154 | build_opts.add_option("-DH0=" + support::cpp11::to_string(rhs_info.h0)); |
| 155 | build_opts.add_option_if(rhs_info.transpose, "-DTRANSPOSE"); |
| 156 | build_opts.add_option_if(rhs_info.interleave, "-DINTERLEAVE"); |
| 157 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 158 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size())); |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 159 | |
| 160 | std::string kernel_name("gemm_reshape_rhs_matrix_"); |
| 161 | kernel_name += rhs_info.transpose ? "t" : "nt"; |
| 162 | |
| 163 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 164 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 165 | |
| 166 | // Configure kernel window |
| 167 | auto win_config = validate_and_configure_window(input->info(), output->info(), rhs_info); |
| 168 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 169 | ICLKernel::configure_internal(win_config.second); |
| 170 | } |
| 171 | |
| 172 | Status CLGEMMReshapeRHSMatrixKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const GEMMRHSMatrixInfo &rhs_info) |
| 173 | { |
| 174 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, rhs_info)); |
| 175 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), rhs_info).first); |
| 176 | |
| 177 | return Status{}; |
| 178 | } |
| 179 | |
| 180 | void CLGEMMReshapeRHSMatrixKernel::run(const Window &window, cl::CommandQueue &queue) |
| 181 | { |
| 182 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 183 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 184 | |
| 185 | Window slice = window.first_slice_window_3D(); |
| 186 | |
| 187 | do |
| 188 | { |
| 189 | unsigned int idx = 0; |
| 190 | add_3D_tensor_argument(idx, _input, slice); |
| 191 | add_3D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 192 | enqueue(queue, *this, slice, lws_hint()); |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 193 | } |
| 194 | while(window.slide_window_slice_3D(slice)); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 195 | } |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 196 | } // namespace arm_compute |