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