Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 2b52add | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +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/CLGEMMLowpReductionKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLHelpers.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | #include "support/ToolchainSupport.h" |
| 37 | |
| 38 | #include <cstddef> |
| 39 | #include <cstdint> |
| 40 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 41 | namespace arm_compute |
| 42 | { |
| 43 | class Coordinates; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 44 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 45 | namespace |
| 46 | { |
| 47 | Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output) |
| 48 | { |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 51 | |
| 52 | return Status{}; |
| 53 | } |
| 54 | std::pair<Status, Window> validate_and_configure_window_matrix_a_reduction(ITensorInfo *input, ITensorInfo *output) |
| 55 | { |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame^] | 56 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 57 | |
| 58 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 59 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 60 | AccessWindowStatic input_access(input, 0, 0, input->dimension(0), input->dimension(1)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 61 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 62 | |
| 63 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 64 | |
| 65 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
| 66 | |
| 67 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 68 | return std::make_pair(err, win); |
| 69 | } |
| 70 | |
| 71 | Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output) |
| 72 | { |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 75 | |
| 76 | return Status{}; |
| 77 | } |
| 78 | |
| 79 | std::pair<Status, Window> validate_and_configure_window_matrix_b_reduction(ITensorInfo *input, ITensorInfo *output) |
| 80 | { |
| 81 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 82 | |
| 83 | // Configure kernel window |
| 84 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 85 | |
| 86 | AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1)); |
| 87 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 88 | |
| 89 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 90 | |
| 91 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
| 92 | |
| 93 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 94 | return std::make_pair(err, win); |
| 95 | } |
| 96 | } // namespace |
| 97 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 98 | ICLGEMMLowpReductionKernel::ICLGEMMLowpReductionKernel() |
| 99 | : _input(), _output() |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | void CLGEMMLowpMatrixAReductionKernel::configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row) |
| 104 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 105 | // Perform validate step |
| 106 | ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row); |
| 107 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(mtx_a->info(), vector_sum_row->info())); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 108 | |
| 109 | _input = mtx_a; |
| 110 | _output = vector_sum_row; |
| 111 | |
| 112 | // Set the arguments to pass at compile time |
| 113 | CLBuildOptions build_opts; |
| 114 | build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(mtx_a->info()->dimension(0))); |
| 115 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 116 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
| 117 | |
| 118 | std::string kernel_name = "gemmlowp_matrix_a_reduction" + std::string(is_dot8_supported ? "_dot8" : ""); |
| 119 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 120 | // Create kernel |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 121 | _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] | 122 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 123 | // Configure kernel window |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 124 | auto win_config = validate_and_configure_window_matrix_a_reduction(_input->info(), _output->info()); |
| 125 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 126 | ICLKernel::configure_internal(win_config.second); |
Gian Marco Iodice | 2b52add | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 127 | |
| 128 | _config_id = kernel_name; |
| 129 | _config_id += "_"; |
| 130 | _config_id += support::cpp11::to_string(_input->info()->dimension(0)); |
| 131 | _config_id += "_"; |
| 132 | _config_id += support::cpp11::to_string(_input->info()->dimension(1)); |
| 133 | _config_id += "_"; |
| 134 | _config_id += support::cpp11::to_string(_input->info()->dimension(2)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 135 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 136 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 137 | Status CLGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row) |
| 138 | { |
| 139 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row)); |
| 140 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_a_reduction(mtx_a->clone().get(), vector_sum_row->clone().get()).first); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 141 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 142 | return Status{}; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void CLGEMMLowpMatrixAReductionKernel::run(const Window &window, cl::CommandQueue &queue) |
| 146 | { |
| 147 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 148 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 149 | |
| 150 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimY); |
| 151 | Window slice_in = collapsed.first_slice_window_2D(); |
| 152 | Window slice_out = collapsed.first_slice_window_2D(); |
| 153 | |
| 154 | // Setup input slice. Its dimensions are increased in the cl kernel. |
| 155 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 156 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 157 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 158 | |
| 159 | do |
| 160 | { |
| 161 | unsigned int idx = 0; |
| 162 | add_3D_tensor_argument(idx, _input, slice_in); |
| 163 | add_2D_tensor_argument(idx, _output, slice_out); |
Gian Marco Iodice | 2b52add | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 164 | enqueue(queue, *this, slice_out, lws_hint()); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 165 | } |
| 166 | while(collapsed.slide_window_slice_2D(slice_out)); |
| 167 | } |
| 168 | |
| 169 | void CLGEMMLowpMatrixBReductionKernel::configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col) |
| 170 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 171 | ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col); |
| 172 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(mtx_b->info(), vector_sum_col->info())); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 173 | |
| 174 | _input = mtx_b; |
| 175 | _output = vector_sum_col; |
| 176 | |
| 177 | // Set the arguments to pass at compile time |
| 178 | CLBuildOptions build_opts; |
| 179 | build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(0))); |
| 180 | build_opts.add_option("-DROWS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(1))); |
| 181 | |
| 182 | // Create kernel |
| 183 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_matrix_b_reduction", build_opts.options())); |
| 184 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 185 | // Configure kernel window |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 186 | auto win_config = validate_and_configure_window_matrix_b_reduction(_input->info(), _output->info()); |
| 187 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 188 | ICLKernel::configure_internal(win_config.second); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 189 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 190 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 191 | Status CLGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col) |
| 192 | { |
| 193 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col)); |
| 194 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_b_reduction(mtx_b->clone().get(), vector_sum_col->clone().get()).first); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 195 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 196 | return Status{}; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void CLGEMMLowpMatrixBReductionKernel::run(const Window &window, cl::CommandQueue &queue) |
| 200 | { |
| 201 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 202 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 203 | |
| 204 | Window collapsed = window.collapse_if_possible(IKernel::window(), Window::DimY); |
| 205 | |
| 206 | Window slice_out = collapsed.first_slice_window_2D(); |
| 207 | Window slice_in = slice_out; |
| 208 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 209 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 210 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 211 | |
| 212 | do |
| 213 | { |
| 214 | unsigned int idx = 0; |
| 215 | add_3D_tensor_argument(idx, _input, slice_in); |
| 216 | add_2D_tensor_argument(idx, _output, slice_out); |
| 217 | enqueue(queue, *this, slice_out); |
| 218 | } |
| 219 | while(collapsed.slide_window_slice_2D(slice_out)); |
| 220 | } |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame^] | 221 | } // namespace arm_compute |