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