Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/KernelDescriptors.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "src/core/AccessWindowStatic.h" |
| 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 32 | #include "support/StringSupport.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 33 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 36 | namespace |
| 37 | { |
| 38 | Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output) |
| 39 | { |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 40 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 42 | |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 43 | if(output->total_size() > 0) |
| 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(0) != input->dimension(1), "Output vector must have length equal to the number of rows of the input matrix"); |
| 47 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 48 | return Status{}; |
| 49 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 50 | |
| 51 | Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output) |
| 52 | { |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
morgolock | d13931d | 2020-06-23 15:49:35 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 55 | |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 56 | if(output->total_size() > 0) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(0) != input->dimension(0), "Output vector must have length equal to the number of columns of the input matrix"); |
| 60 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 61 | return Status{}; |
| 62 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 63 | } // namespace |
| 64 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 65 | ICLGEMMLowpReductionKernel::ICLGEMMLowpReductionKernel() |
| 66 | : _input(), _output() |
| 67 | { |
| 68 | } |
| 69 | |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 70 | void CLGEMMLowpMatrixAReductionKernel::configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 71 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 72 | configure(CLKernelLibrary::get().get_compile_context(), mtx_a, vector_sum_row, info); |
| 73 | } |
| 74 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 75 | void CLGEMMLowpMatrixAReductionKernel::configure(const CLCompileContext &compile_context, const ICLTensor *mtx_a, ICLTensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 76 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 77 | // Perform validate step |
| 78 | ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row); |
| 79 | 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] | 80 | |
Michele Di Giorgio | 1c1b3aa | 2020-04-02 17:35:42 +0100 | [diff] [blame] | 81 | // Output auto initialization if not yet initialized |
| 82 | auto_init_if_empty(*vector_sum_row->info(), TensorShape(mtx_a->info()->dimension(1)), 1, DataType::S32); |
| 83 | |
Michele Di Giorgio | aae3410 | 2020-10-19 15:31:45 +0100 | [diff] [blame] | 84 | auto padding_info = get_padding_info({ mtx_a, vector_sum_row }); |
| 85 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 86 | _input = mtx_a; |
| 87 | _output = vector_sum_row; |
| 88 | |
| 89 | // Set the arguments to pass at compile time |
| 90 | CLBuildOptions build_opts; |
| 91 | build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(mtx_a->info()->dimension(0))); |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 92 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(mtx_a->info()->data_type())); |
Michele Di Giorgio | e7b333e | 2020-01-15 10:30:51 +0000 | [diff] [blame] | 93 | build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(mtx_a->info()->data_type())); |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 94 | build_opts.add_option_if(info.mul_by_scalar, "-DSCALAR=" + support::cpp11::to_string(info.scalar)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 95 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 96 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
| 97 | |
| 98 | std::string kernel_name = "gemmlowp_matrix_a_reduction" + std::string(is_dot8_supported ? "_dot8" : ""); |
| 99 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 100 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 101 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 102 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 103 | // Configure kernel window |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 104 | // This kernel does not need padding |
| 105 | Window win = calculate_max_window(*vector_sum_row->info(), Steps()); |
| 106 | ICLKernel::configure_internal(win); |
Gian Marco Iodice | 2b52add | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 107 | |
| 108 | _config_id = kernel_name; |
| 109 | _config_id += "_"; |
| 110 | _config_id += support::cpp11::to_string(_input->info()->dimension(0)); |
| 111 | _config_id += "_"; |
| 112 | _config_id += support::cpp11::to_string(_input->info()->dimension(1)); |
| 113 | _config_id += "_"; |
| 114 | _config_id += support::cpp11::to_string(_input->info()->dimension(2)); |
Michele Di Giorgio | aae3410 | 2020-10-19 15:31:45 +0100 | [diff] [blame] | 115 | |
| 116 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 117 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 118 | |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 119 | Status CLGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row, const GEMMLowpReductionKernelInfo &info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 120 | { |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 121 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 122 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 123 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 124 | return Status{}; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void CLGEMMLowpMatrixAReductionKernel::run(const Window &window, cl::CommandQueue &queue) |
| 128 | { |
| 129 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 130 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 131 | |
| 132 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimY); |
| 133 | Window slice_in = collapsed.first_slice_window_2D(); |
| 134 | Window slice_out = collapsed.first_slice_window_2D(); |
| 135 | |
| 136 | // Setup input slice. Its dimensions are increased in the cl kernel. |
| 137 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 138 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 139 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 140 | |
| 141 | do |
| 142 | { |
| 143 | unsigned int idx = 0; |
| 144 | add_3D_tensor_argument(idx, _input, slice_in); |
| 145 | add_2D_tensor_argument(idx, _output, slice_out); |
Gian Marco Iodice | 2b52add | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 146 | enqueue(queue, *this, slice_out, lws_hint()); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 147 | } |
| 148 | while(collapsed.slide_window_slice_2D(slice_out)); |
| 149 | } |
| 150 | |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 151 | void CLGEMMLowpMatrixBReductionKernel::configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 152 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 153 | configure(CLKernelLibrary::get().get_compile_context(), mtx_b, vector_sum_col, info); |
| 154 | } |
| 155 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 156 | void CLGEMMLowpMatrixBReductionKernel::configure(const CLCompileContext &compile_context, const ICLTensor *mtx_b, ICLTensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 157 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 158 | ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col); |
| 159 | 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] | 160 | |
| 161 | _input = mtx_b; |
| 162 | _output = vector_sum_col; |
| 163 | |
Michele Di Giorgio | aae3410 | 2020-10-19 15:31:45 +0100 | [diff] [blame] | 164 | // Output auto initialization if not yet initialized |
| 165 | auto_init_if_empty(*_output->info(), TensorShape(mtx_b->info()->dimension(0)), 1, DataType::S32); |
| 166 | |
| 167 | auto padding_info = get_padding_info({ mtx_b, vector_sum_col }); |
| 168 | |
| 169 | const unsigned int num_elems_processed_per_iteration = adjust_vec_size(16, mtx_b->info()->dimension(0)); |
| 170 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 171 | // Set the arguments to pass at compile time |
| 172 | CLBuildOptions build_opts; |
Michele Di Giorgio | aae3410 | 2020-10-19 15:31:45 +0100 | [diff] [blame] | 173 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 174 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(mtx_b->info()->dimension(0) % num_elems_processed_per_iteration)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 175 | build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(0))); |
| 176 | build_opts.add_option("-DROWS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(1))); |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 177 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(mtx_b->info()->data_type())); |
Michele Di Giorgio | e7b333e | 2020-01-15 10:30:51 +0000 | [diff] [blame] | 178 | build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(mtx_b->info()->data_type())); |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 179 | build_opts.add_option_if(info.mul_by_scalar, "-DSCALAR=" + support::cpp11::to_string(info.scalar)); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 180 | |
| 181 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 182 | _kernel = create_kernel(compile_context, "gemmlowp_matrix_b_reduction", build_opts.options()); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 183 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 184 | // Configure kernel window |
Michele Di Giorgio | aae3410 | 2020-10-19 15:31:45 +0100 | [diff] [blame] | 185 | Window win = calculate_max_window(*_output->info(), Steps(num_elems_processed_per_iteration)); |
| 186 | ICLKernel::configure_internal(win); |
| 187 | |
| 188 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
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 | |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 191 | Status CLGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col, const GEMMLowpReductionKernelInfo &info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 192 | { |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 193 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 194 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col)); |
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); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 217 | enqueue(queue, *this, slice_out, lws_hint()); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 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 |