giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +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 | */ |
| 24 | #include "arm_compute/runtime/CL/functions/CLGEMMDeconvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/Validate.h" |
| 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame^] | 30 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 32 | #include "src/core/CL/kernels/CLDeconvolutionReshapeOutputKernel.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 33 | #include "src/core/CL/kernels/CLFillBorderKernel.h" |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 34 | |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 35 | #include "src/common/utils/Log.h" |
| 36 | |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 37 | #include <tuple> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | std::pair<Coordinates, Coordinates> compute_start_end_slice_coordinates(const ITensorInfo &output_info, const PadStrideInfo &deconv_info, bool is_nchw) |
| 44 | { |
| 45 | Coordinates start; |
| 46 | Coordinates end; |
| 47 | |
| 48 | if(is_nchw) |
| 49 | { |
| 50 | start.set(0, deconv_info.pad_left()); |
| 51 | start.set(1, deconv_info.pad_top()); |
| 52 | end.set(0, output_info.dimension(0) - deconv_info.pad_right()); |
| 53 | end.set(1, output_info.dimension(1) - deconv_info.pad_bottom()); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | start.set(0, 0); |
| 58 | start.set(1, deconv_info.pad_left()); |
| 59 | start.set(2, deconv_info.pad_top()); |
| 60 | |
| 61 | end.set(0, output_info.dimension(0)); |
| 62 | end.set(1, output_info.dimension(1) - deconv_info.pad_right()); |
| 63 | end.set(2, output_info.dimension(2) - deconv_info.pad_bottom()); |
| 64 | } |
| 65 | |
| 66 | return { start, end }; |
| 67 | } |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 68 | Status construct_gemmlowp_output_stage(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, GEMMLowpOutputStageInfo &output_stage_info) |
| 69 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 70 | const auto data_type = input->data_type(); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 71 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 72 | if(is_data_type_quantized_asymmetric(data_type)) |
| 73 | { |
| 74 | const UniformQuantizationInfo iq_info = input->quantization_info().uniform(); |
| 75 | const UniformQuantizationInfo wq_info = weights->quantization_info().uniform(); |
| 76 | const UniformQuantizationInfo oq_info = output->quantization_info().uniform(); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 77 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 78 | float multiplier = iq_info.scale * wq_info.scale / oq_info.scale; |
| 79 | int output_multiplier(0); |
| 80 | int output_shift(0); |
| 81 | ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift)); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 82 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 83 | output_stage_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT; |
| 84 | output_stage_info.gemmlowp_multiplier = output_multiplier; |
| 85 | output_stage_info.gemmlowp_shift = output_shift; |
| 86 | output_stage_info.gemmlowp_offset = oq_info.offset; |
| 87 | const auto min_max_bound = get_min_max(data_type); |
| 88 | output_stage_info.gemmlowp_min_bound = (std::get<0>(min_max_bound)).get<int32_t>(); |
| 89 | output_stage_info.gemmlowp_max_bound = (std::get<1>(min_max_bound)).get<int32_t>(); |
| 90 | output_stage_info.output_data_type = data_type; |
| 91 | } |
| 92 | return Status{}; |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 93 | } |
| 94 | |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| 97 | CLGEMMDeconvolutionLayer::CLGEMMDeconvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT |
| 98 | : _memory_group(std::move(memory_manager)), |
| 99 | _mm_gemm(), |
| 100 | _mm_gemmlowp(), |
| 101 | _gemmlowp_output_stage(), |
| 102 | _permute_input_to_nhwc(), |
| 103 | _permute_weights_to_nhwc(), |
| 104 | _reshape_weights(), |
| 105 | _transpose_weights(), |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 106 | _deconv_reshape(std::make_unique<CLDeconvolutionReshapeOutputKernel>()), |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 107 | _slice_gemm(), |
| 108 | _gemmlowp_final(), |
| 109 | _reshaped_weights(), |
| 110 | _reshaped_weights_t(), |
| 111 | _permuted_input(), |
| 112 | _permuted_weights(), |
| 113 | _gemm_output(), |
| 114 | _slice_gemm_input(), |
| 115 | _original_weights(), |
| 116 | _is_prepared(false), |
| 117 | _padded_input(false), |
| 118 | _is_nchw(false), |
| 119 | _is_quantized(false) |
| 120 | { |
| 121 | } |
| 122 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 123 | CLGEMMDeconvolutionLayer::~CLGEMMDeconvolutionLayer() = default; |
| 124 | |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 125 | Status CLGEMMDeconvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *output, const PadStrideInfo &deconv_info) |
| 126 | { |
| 127 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 128 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 130 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights); |
| 131 | |
| 132 | DataLayout data_layout = input->data_layout(); |
| 133 | const bool padded_input = deconv_info.pad_bottom() > 0 || deconv_info.pad_left() > 0 || deconv_info.pad_right() > 0 || deconv_info.pad_top() > 0; |
| 134 | const bool is_nchw = input->data_layout() == DataLayout::NCHW; |
| 135 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 136 | |
| 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 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) != deconv_info.stride().first); |
| 142 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) != deconv_info.stride().second); |
| 143 | |
| 144 | TensorShape nhwc_weights_shape = weights->tensor_shape(); |
| 145 | TensorShape nhwc_input_shape = input->tensor_shape(); |
| 146 | |
| 147 | if(is_nchw) |
| 148 | { |
| 149 | permute(nhwc_weights_shape, PermutationVector(2, 0, 1)); |
| 150 | permute(nhwc_input_shape, PermutationVector(2, 0, 1)); |
| 151 | |
| 152 | TensorInfo nhwc_input_info = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(nhwc_input_shape).set_data_layout(DataLayout::NCHW); |
| 153 | |
| 154 | TensorInfo nhwc_weights_info = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(nhwc_weights_shape).set_data_layout(DataLayout::NCHW); |
| 155 | |
| 156 | CLPermute::validate(weights, &nhwc_weights_info, PermutationVector(2, 0, 1)); |
| 157 | CLPermute::validate(input, &nhwc_input_info, PermutationVector(2, 0, 1)); |
| 158 | } |
| 159 | |
| 160 | const TensorShape reshaped_shape = TensorShape(nhwc_weights_shape[0], nhwc_weights_shape[1] * nhwc_weights_shape[2] * nhwc_weights_shape[3]); |
| 161 | const TensorInfo reshaped_info = weights->clone()->set_tensor_shape(reshaped_shape).set_data_layout(DataLayout::NCHW).set_is_resizable(true); |
| 162 | ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(weights, &reshaped_info)); |
| 163 | |
| 164 | TensorShape transposed_shape(reshaped_shape[1], reshaped_shape[0]); |
| 165 | const TensorInfo reshaped_t_info = reshaped_info.clone()->set_is_resizable(true).set_tensor_shape(transposed_shape); |
| 166 | ARM_COMPUTE_RETURN_ON_ERROR(CLTranspose::validate(&reshaped_info, &reshaped_t_info)); |
| 167 | |
| 168 | TensorShape gemm_output_shape(weights->dimension(idx_w) * weights->dimension(idx_h) * weights->dimension(idx_b), |
| 169 | input->dimension(idx_w), |
| 170 | input->dimension(idx_h), |
| 171 | input->dimension(idx_b)); |
| 172 | |
| 173 | TensorInfo gemm_output_info = reshaped_t_info.clone()->set_tensor_shape(gemm_output_shape).set_is_resizable(true); |
| 174 | GEMMInfo gemm_info(false, false, true, input->dimension(idx_h), true); |
| 175 | |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 176 | GEMMLowpOutputStageInfo output_stage_info; |
| 177 | |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 178 | if(is_quantized) |
| 179 | { |
| 180 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpMatrixMultiplyCore::validate(&input->clone()->set_tensor_shape(nhwc_input_shape), &reshaped_t_info, nullptr, &gemm_output_info.set_data_type(DataType::S32), |
| 181 | gemm_info)); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 182 | ARM_COMPUTE_RETURN_ON_ERROR(construct_gemmlowp_output_stage(input, weights, output, output_stage_info)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
| 186 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input->clone()->set_tensor_shape(nhwc_input_shape).set_is_resizable(true), &reshaped_t_info, nullptr, &gemm_output_info, 1.0f, 0.0f, gemm_info)); |
| 187 | } |
| 188 | |
Matthew Jackson | b9070a4 | 2019-08-22 16:13:27 +0100 | [diff] [blame] | 189 | const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second); |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 190 | auto out_dims = deconvolution_output_dimensions(input->dimension(idx_w), input->dimension(idx_h), weights->dimension(idx_w), weights->dimension(idx_h), stride_info); |
| 191 | const TensorShape deconv_shape = misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input, *weights); |
| 192 | TensorInfo col2im_output_info = gemm_output_info.clone()->set_tensor_shape(deconv_shape).set_is_resizable(true); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 193 | |
| 194 | if(padded_input && is_quantized) |
| 195 | { |
| 196 | const auto start_end = compute_start_end_slice_coordinates(col2im_output_info, deconv_info, is_nchw); |
| 197 | ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, &col2im_output_info, input, weights, deconv_info)); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 198 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&col2im_output_info, nullptr, &col2im_output_info.clone()->set_is_resizable(true).set_data_type(input->data_type()), output_stage_info)); |
| 199 | ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&col2im_output_info.clone()->set_is_resizable(true).set_data_type(input->data_type()), output, start_end.first, start_end.second)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 200 | } |
| 201 | else if(padded_input) |
| 202 | { |
| 203 | const auto start_end = compute_start_end_slice_coordinates(col2im_output_info, deconv_info, is_nchw); |
| 204 | ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, &col2im_output_info, input, weights, deconv_info)); |
| 205 | ARM_COMPUTE_RETURN_ON_ERROR(CLSlice::validate(&col2im_output_info, output, start_end.first, start_end.second)); |
| 206 | } |
| 207 | else if(is_quantized) |
| 208 | { |
| 209 | ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, &col2im_output_info, input, weights, deconv_info)); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 210 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMLowpOutputStage::validate(&col2im_output_info, nullptr, output, output_stage_info)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 211 | } |
| 212 | else |
| 213 | { |
| 214 | ARM_COMPUTE_RETURN_ON_ERROR(CLDeconvolutionReshapeOutputKernel::validate(&gemm_output_info, bias, output, input, weights, deconv_info)); |
| 215 | } |
| 216 | |
| 217 | return Status{}; |
| 218 | } |
| 219 | |
| 220 | void CLGEMMDeconvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const PadStrideInfo &deconv_info) |
| 221 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 222 | configure(CLKernelLibrary::get().get_compile_context(), input, weights, bias, output, deconv_info); |
| 223 | } |
| 224 | |
| 225 | void CLGEMMDeconvolutionLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, |
| 226 | const PadStrideInfo &deconv_info) |
| 227 | { |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 228 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 229 | ARM_COMPUTE_ERROR_THROW_ON(CLGEMMDeconvolutionLayer::validate(input->info(), |
| 230 | weights->info(), |
| 231 | bias != nullptr ? bias->info() : nullptr, |
| 232 | output->info(), |
| 233 | deconv_info)); |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 234 | ARM_COMPUTE_LOG_PARAMS(input, weights, bias, output, deconv_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 235 | |
| 236 | _original_weights = weights; |
| 237 | _padded_input = deconv_info.pad_bottom() > 0 || deconv_info.pad_left() > 0 || deconv_info.pad_right() > 0 || deconv_info.pad_top() > 0; |
| 238 | _is_nchw = input->info()->data_layout() == DataLayout::NCHW; |
| 239 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 240 | |
| 241 | const ICLTensor *input_to_use = input; |
| 242 | const ICLTensor *weights_to_use = weights; |
| 243 | |
| 244 | // If the data layout is NCHW, transform everything in NHWC. Another alternative could be to |
| 245 | // do an outer product in NCHW and then an accumulation through a reduction. This would have two |
| 246 | // drawbacks: first, the outer product is less efficient than a full GEMM. Second, the reduction |
| 247 | // might be slower than GEMM. |
| 248 | if(_is_nchw) |
| 249 | { |
| 250 | _memory_group.manage(&_permuted_input); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 251 | _permute_input_to_nhwc.configure(compile_context, input, &_permuted_input, PermutationVector(2U, 0U, 1U)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 252 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 253 | _permute_weights_to_nhwc.configure(compile_context, weights, &_permuted_weights, PermutationVector(2U, 0U, 1U)); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 254 | |
| 255 | input_to_use = &_permuted_input; |
| 256 | weights_to_use = &_permuted_weights; |
| 257 | } |
| 258 | |
| 259 | // Reshape the input weights. The weights will be reshaped only once during the call to prepare() |
| 260 | _reshaped_weights.allocator()->init(TensorInfo(TensorShape(weights_to_use->info()->dimension(0), |
| 261 | weights_to_use->info()->dimension(1) * weights_to_use->info()->dimension(2) * weights_to_use->info()->dimension(3)), |
| 262 | 1, |
| 263 | input->info()->data_type(), weights->info()->quantization_info())); |
| 264 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 265 | _reshape_weights.configure(compile_context, weights_to_use, &_reshaped_weights); |
| 266 | _transpose_weights.configure(compile_context, &_reshaped_weights, &_reshaped_weights_t); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 267 | |
| 268 | const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
| 269 | GEMMInfo gemm_info(false, false, true, input->info()->dimension(idx_h), true); |
| 270 | |
| 271 | // Configure output stage for asymmetric quantized types |
| 272 | if(_is_quantized) |
| 273 | { |
Giuseppe Rossini | 0a958cb | 2020-01-16 16:38:56 +0000 | [diff] [blame] | 274 | // gemmlowp adds the offsets (instead of subtracting them). Thus, we need to negate the original |
| 275 | // and restore them back to make it work properly. |
| 276 | QuantizationInfo iq_info = input->info()->quantization_info(); |
| 277 | QuantizationInfo wq_info = weights->info()->quantization_info(); |
| 278 | |
| 279 | input_to_use->info()->set_quantization_info(QuantizationInfo(iq_info.uniform().scale, -iq_info.uniform().offset)); |
| 280 | _reshaped_weights_t.info()->set_quantization_info(QuantizationInfo(wq_info.uniform().scale, -wq_info.uniform().offset)); |
| 281 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 282 | _mm_gemmlowp.configure(compile_context, input_to_use, &_reshaped_weights_t, nullptr, &_gemm_output, gemm_info); |
Giuseppe Rossini | 0a958cb | 2020-01-16 16:38:56 +0000 | [diff] [blame] | 283 | |
| 284 | input_to_use->info()->set_quantization_info(iq_info); |
| 285 | _reshaped_weights_t.info()->set_quantization_info(wq_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 289 | _mm_gemm.configure(compile_context, input_to_use, &_reshaped_weights_t, nullptr, &_gemm_output, 1.f, 0.0f, gemm_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | if(_is_nchw) |
| 293 | { |
| 294 | _permuted_input.allocator()->allocate(); |
| 295 | } |
| 296 | |
| 297 | ICLTensor *deconv_reshape_output = nullptr; |
| 298 | ICLTensor *slice_output = nullptr; |
| 299 | ICLTensor *output_stage_output = nullptr; |
| 300 | |
| 301 | if(_padded_input && _is_quantized) |
| 302 | { |
| 303 | _memory_group.manage(&_slice_gemm_input); |
| 304 | _memory_group.manage(&_gemmlowp_final); |
| 305 | deconv_reshape_output = &_gemmlowp_final; |
| 306 | output_stage_output = &_slice_gemm_input; |
| 307 | slice_output = output; |
| 308 | } |
| 309 | else if(_padded_input) |
| 310 | { |
| 311 | _memory_group.manage(&_slice_gemm_input); |
| 312 | deconv_reshape_output = &_slice_gemm_input; |
| 313 | slice_output = output; |
| 314 | } |
| 315 | else if(_is_quantized) |
| 316 | { |
| 317 | _memory_group.manage(&_gemmlowp_final); |
| 318 | deconv_reshape_output = &_gemmlowp_final; |
| 319 | output_stage_output = output; |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | deconv_reshape_output = output; |
| 324 | } |
| 325 | |
| 326 | // Configure a Col2Im call to reshape the output of GEMM |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 327 | _deconv_reshape->configure(compile_context, &_gemm_output, bias, deconv_reshape_output, input->info(), weights->info(), deconv_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 328 | _gemm_output.allocator()->allocate(); |
| 329 | |
| 330 | if(_is_quantized) |
| 331 | { |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 332 | GEMMLowpOutputStageInfo output_stage_info; |
| 333 | construct_gemmlowp_output_stage(input->info(), weights->info(), output->info(), output_stage_info); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 334 | _gemmlowp_output_stage.configure(compile_context, &_gemmlowp_final, nullptr, output_stage_output, output_stage_info); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 335 | _gemmlowp_final.allocator()->allocate(); |
| 336 | } |
| 337 | |
| 338 | // If the input was padded, the output needs to be sliced. |
| 339 | if(_padded_input) |
| 340 | { |
| 341 | const auto start_end = compute_start_end_slice_coordinates(*deconv_reshape_output->info(), deconv_info, _is_nchw); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 342 | _slice_gemm.configure(compile_context, &_slice_gemm_input, slice_output, start_end.first, start_end.second); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 343 | _slice_gemm_input.allocator()->allocate(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void CLGEMMDeconvolutionLayer::run() |
| 348 | { |
| 349 | prepare(); |
| 350 | |
| 351 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 352 | |
| 353 | if(_is_nchw) |
| 354 | { |
| 355 | _permute_input_to_nhwc.run(); |
| 356 | } |
| 357 | |
| 358 | if(_is_quantized) |
| 359 | { |
| 360 | _mm_gemmlowp.run(); |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | _mm_gemm.run(); |
| 365 | } |
| 366 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 367 | CLScheduler::get().enqueue(*_deconv_reshape, false); |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 368 | |
| 369 | if(_is_quantized) |
| 370 | { |
| 371 | _gemmlowp_output_stage.run(); |
| 372 | } |
| 373 | |
| 374 | if(_padded_input) |
| 375 | { |
| 376 | _slice_gemm.run(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void CLGEMMDeconvolutionLayer::prepare() |
| 381 | { |
| 382 | if(!_is_prepared) |
| 383 | { |
| 384 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 385 | |
| 386 | if(_is_nchw) |
| 387 | { |
| 388 | _permuted_weights.allocator()->allocate(); |
| 389 | _permute_weights_to_nhwc.run(); |
| 390 | } |
| 391 | |
| 392 | _reshaped_weights.allocator()->allocate(); |
| 393 | _reshape_weights.run(); |
| 394 | |
| 395 | if(_is_nchw) |
| 396 | { |
| 397 | _permuted_weights.allocator()->free(); |
| 398 | } |
| 399 | |
| 400 | _reshaped_weights_t.allocator()->allocate(); |
| 401 | _transpose_weights.run(); |
| 402 | |
| 403 | // Prepare gemm |
| 404 | if(!_is_quantized) |
| 405 | { |
| 406 | _mm_gemm.prepare(); |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | _mm_gemmlowp.prepare(); |
| 411 | } |
| 412 | |
| 413 | // Free resources |
| 414 | if(!_reshaped_weights_t.is_used()) |
| 415 | { |
| 416 | _reshaped_weights_t.allocator()->free(); |
| 417 | } |
| 418 | |
| 419 | _original_weights->mark_as_unused(); |
| 420 | _is_prepared = true; |
| 421 | } |
| 422 | } |
| 423 | } // namespace arm_compute |