giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLDeconvolutionReshapeOutputKernel.h" |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Validate.h" |
| 33 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace |
| 41 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | Status validate_arguments(const ITensorInfo *input, |
| 43 | const ITensorInfo *bias, |
| 44 | const ITensorInfo *output, |
| 45 | const ITensorInfo *input_info, |
| 46 | const ITensorInfo *weights_info, |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 47 | const PadStrideInfo &deconv_info) |
| 48 | { |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, input_info, weights_info); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 50 | const DataLayout data_layout = input_info->data_layout(); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 51 | |
| 52 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 53 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 54 | const size_t idx_b = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 55 | |
| 56 | const bool is_qasymm = is_data_type_quantized_asymmetric(input_info->data_type()); |
| 57 | |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_w) != deconv_info.stride().first); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_h) != deconv_info.stride().second); |
| 60 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8, |
| 62 | DataType::QASYMM8_SIGNED, DataType::S32); |
| 63 | if (!is_qasymm) |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 64 | { |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_info, weights_info); |
| 66 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_info->dimension(idx_w) * weights_info->dimension(idx_h) * |
| 68 | weights_info->dimension(idx_b)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != input_info->dimension(idx_w)); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != input_info->dimension(idx_h)); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) != input_info->dimension(idx_b)); |
| 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | if (bias != nullptr) |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 74 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | if (is_qasymm) |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 76 | { |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(bias, input); |
| 82 | } |
| 83 | ARM_COMPUTE_RETURN_ERROR_ON(bias->dimension(0) != weights_info->dimension(idx_b)); |
| 84 | } |
| 85 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | if (output->total_size() != 0) |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 87 | { |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 88 | const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | auto out_dims = deconvolution_output_dimensions(input_info->dimension(idx_w), input_info->dimension(idx_h), |
| 90 | weights_info->dimension(idx_w), weights_info->dimension(idx_h), |
| 91 | stride_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 92 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 93 | const TensorShape output_shape = |
| 94 | misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input_info, *weights_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 95 | |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 97 | } |
| 98 | return Status{}; |
| 99 | } |
| 100 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 101 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, |
| 102 | ITensorInfo *output, |
| 103 | const ITensorInfo *input_info, |
| 104 | const ITensorInfo *weights_info, |
| 105 | const PadStrideInfo &deconv_info) |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 106 | { |
| 107 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 108 | |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 109 | const DataLayout data_layout = input_info->data_layout(); |
| 110 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 111 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 112 | const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 113 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | auto out_dims = |
| 115 | deconvolution_output_dimensions(input_info->dimension(idx_w), input_info->dimension(idx_h), |
| 116 | weights_info->dimension(idx_w), weights_info->dimension(idx_h), stride_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 117 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | const TensorShape output_shape = |
| 119 | misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input_info, *weights_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 120 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | auto_init_if_empty(*output, input->clone() |
| 122 | ->set_tensor_shape(output_shape) |
| 123 | .set_data_layout(data_layout) |
| 124 | .set_quantization_info(input->quantization_info())); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 125 | |
| 126 | Window win = calculate_max_window(*input); |
| 127 | |
| 128 | return std::make_pair(Status{}, win); |
| 129 | } |
| 130 | } // namespace |
| 131 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | CLDeconvolutionReshapeOutputKernel::CLDeconvolutionReshapeOutputKernel() : _add_bias(false), _bias(nullptr) |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 133 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 134 | _type = CLKernelType::ELEMENTWISE; |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 137 | void CLDeconvolutionReshapeOutputKernel::configure(const ICLTensor *input, |
| 138 | const ICLTensor *bias, |
| 139 | ICLTensor *output, |
| 140 | const ITensorInfo *input_info, |
| 141 | const ITensorInfo *weights_info, |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 142 | const PadStrideInfo &deconv_info) |
| 143 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 144 | configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, input_info, weights_info, deconv_info); |
| 145 | } |
| 146 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | void CLDeconvolutionReshapeOutputKernel::configure(const CLCompileContext &compile_context, |
| 148 | const ICLTensor *input, |
| 149 | const ICLTensor *bias, |
| 150 | ICLTensor *output, |
| 151 | const ITensorInfo *input_info, |
| 152 | const ITensorInfo *weights_info, |
| 153 | const PadStrideInfo &deconv_info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 154 | { |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, input_info, weights_info); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr ? bias->info() : nullptr), |
| 157 | output->info(), input_info, weights_info, deconv_info)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 158 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 159 | auto padding_info = get_padding_info({input, bias, output}); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 160 | // Configure kernel window |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 161 | auto win_config = |
| 162 | validate_and_configure_window(input->info(), output->info(), input_info, weights_info, deconv_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 163 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 164 | |
| 165 | const DataLayout data_layout = input_info->data_layout(); |
| 166 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 167 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 168 | const size_t idx_b = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 169 | |
| 170 | _input = input; |
| 171 | _output = output; |
| 172 | _add_bias = (bias != nullptr); |
| 173 | _bias = bias; |
| 174 | |
| 175 | const int filter_w = weights_info->dimension(idx_w); |
| 176 | const int filter_h = weights_info->dimension(idx_h); |
| 177 | const int filter_b = weights_info->dimension(idx_b); |
| 178 | const int img_w = input_info->dimension(idx_w); |
| 179 | const int img_h = input_info->dimension(idx_h); |
| 180 | |
| 181 | CLBuildOptions build_opts; |
| 182 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 183 | build_opts.add_option("-DFILTER_WIDTH=" + support::cpp11::to_string(filter_w)); |
| 184 | build_opts.add_option("-DFILTER_HEIGHT=" + support::cpp11::to_string(filter_h)); |
| 185 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(img_w)); |
| 186 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(img_h)); |
| 187 | build_opts.add_option_if(data_layout == DataLayout::NCHW, "-DNUM_FILTERS=" + support::cpp11::to_string(filter_b)); |
| 188 | build_opts.add_option_if(_add_bias, "-DADD_BIAS"); |
| 189 | |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 190 | _kernel = create_kernel(compile_context, "deconvolution_reshape", build_opts.options()); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 191 | ICLKernel::configure_internal(win_config.second); |
| 192 | |
| 193 | // Set config_id for enabling LWS tuning |
| 194 | _config_id = "deconvolution_reshape_output_"; |
| 195 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 196 | _config_id += "_"; |
| 197 | _config_id += lower_string(string_from_data_layout(input->info()->data_layout())); |
| 198 | _config_id += "_"; |
| 199 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 200 | _config_id += "_"; |
| 201 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 202 | _config_id += "_"; |
| 203 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 204 | _config_id += "_"; |
| 205 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 206 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 209 | Status CLDeconvolutionReshapeOutputKernel::validate(const ITensorInfo *input, |
| 210 | const ITensorInfo *bias, |
| 211 | const ITensorInfo *output, |
| 212 | const ITensorInfo *input_info, |
| 213 | const ITensorInfo *weights_info, |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 214 | const PadStrideInfo &deconv_info) |
| 215 | { |
| 216 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, input_info, weights_info, deconv_info)); |
| 217 | return Status{}; |
| 218 | } |
| 219 | |
| 220 | void CLDeconvolutionReshapeOutputKernel::run(const Window &window, cl::CommandQueue &queue) |
| 221 | { |
| 222 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 223 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 224 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 225 | |
| 226 | unsigned int idx = 0; |
| 227 | add_3D_tensor_argument(idx, _input, collapsed); |
| 228 | add_3D_tensor_argument(idx, _output, collapsed); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 229 | if (_add_bias) |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 230 | { |
| 231 | add_1D_tensor_argument(idx, _bias, collapsed); |
| 232 | } |
| 233 | enqueue(queue, *this, collapsed, lws_hint()); |
| 234 | } |
| 235 | } // namespace arm_compute |