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