Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | ca62c6f | 2021-03-23 11:50:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 25 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Helpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/PixelValue.h" |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 30 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 34 | #include "src/core/CL/kernels/CLDepthwiseConvolutionLayerNativeKernel.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 35 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 38 | using namespace arm_compute::misc; |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 39 | using namespace arm_compute::misc::shape_calculator; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 40 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 41 | namespace |
| 42 | { |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 43 | bool export_weights_to_cl_image_heuristic(const ITensorInfo *weights, unsigned int depth_multiplier, GPUTarget gpu_target) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 44 | { |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 45 | if(!export_weights_to_cl_image(weights)) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 46 | { |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 47 | return false; |
| 48 | } |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 49 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 50 | if(depth_multiplier > 1) |
| 51 | { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | if(gpu_target == GPUTarget::G71 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD) |
| 56 | { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | void initialize_dwc_native_compute_info(DWCComputeKernelInfo &dwc_compute_info, const ITensorInfo *weights, const PadStrideInfo &conv_info, const Size2D &dilation, unsigned int depth_multiplier, |
| 64 | GPUTarget gpu_target) |
| 65 | { |
| 66 | if(!is_data_type_float(weights->data_type())) |
| 67 | { |
| 68 | dwc_compute_info.export_weights_to_cl_image = false; |
| 69 | dwc_compute_info.n0 = (depth_multiplier == 1) ? 4 : 1; |
| 70 | if(conv_info.stride().first == 1 && dilation.x() == 1 && depth_multiplier == 1) |
| 71 | { |
| 72 | dwc_compute_info.m0 = 2; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 73 | } |
| 74 | else |
| 75 | { |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 76 | dwc_compute_info.m0 = 1; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 77 | } |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 78 | |
| 79 | return; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 82 | // Floating point path |
| 83 | |
| 84 | // First check if we can export to cl_image. |
| 85 | dwc_compute_info.export_weights_to_cl_image = export_weights_to_cl_image_heuristic(weights, depth_multiplier, gpu_target); |
| 86 | |
| 87 | // Set n0 |
| 88 | if(depth_multiplier == 1) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 89 | { |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 90 | if(dwc_compute_info.export_weights_to_cl_image == false && weights->data_type() == DataType::F16) |
| 91 | { |
| 92 | dwc_compute_info.n0 = 8; |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | dwc_compute_info.n0 = 4; |
| 97 | } |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 101 | dwc_compute_info.n0 = 1; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 102 | } |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 103 | |
| 104 | dwc_compute_info.n0 = adjust_vec_size(dwc_compute_info.n0, weights->dimension(0)); |
| 105 | |
| 106 | // Set m0 only if stride_x == 1 and dilation_x == 1 |
| 107 | if(conv_info.stride().first == 1 && dilation.x() == 1) |
| 108 | { |
| 109 | const size_t idx_w = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::WIDTH); |
| 110 | const size_t kernel_w = weights->tensor_shape()[idx_w]; |
| 111 | |
| 112 | dwc_compute_info.m0 = (kernel_w >= 9) || (kernel_w == 1) ? 1 : 2; |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | dwc_compute_info.m0 = 1; |
| 117 | } |
| 118 | return; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 119 | } |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 120 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 121 | } // namespace |
| 122 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 123 | CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 124 | : _memory_group(std::move(memory_manager)), |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 125 | _dwc_native_kernel(std::make_unique<CLDepthwiseConvolutionLayerNativeKernel>()), |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 126 | _permute_input_to_nhwc(), |
| 127 | _permute_weights_to_nhwc(), |
| 128 | _permute_output_to_nchw(), |
| 129 | _permuted_input(), |
| 130 | _permuted_weights(), |
| 131 | _permuted_output(), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 132 | _output_multipliers(), |
| 133 | _output_shifts(), |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 134 | _original_weights(), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 135 | _input(), |
| 136 | _output(), |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 137 | _needs_permute(false), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 138 | _is_prepared(false), |
| 139 | _is_quantized(false) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 140 | { |
| 141 | } |
| 142 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 143 | CLDepthwiseConvolutionLayer::~CLDepthwiseConvolutionLayer() = default; |
| 144 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 145 | void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
| 146 | unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 147 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 148 | configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
| 149 | } |
| 150 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 151 | void CLDepthwiseConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, |
| 152 | ICLTensor *output, const PadStrideInfo &conv_info, |
| 153 | unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 154 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
| 156 | ARM_COMPUTE_ERROR_THROW_ON(CLDepthwiseConvolutionLayer::validate(input->info(), |
| 157 | weights->info(), |
| 158 | biases != nullptr ? biases->info() : nullptr, |
| 159 | output->info(), |
| 160 | conv_info, |
| 161 | depth_multiplier, |
| 162 | act_info, |
| 163 | dilation)); |
| 164 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 165 | _is_quantized = is_data_type_quantized(input->info()->data_type()); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 166 | _is_prepared = false; |
| 167 | _original_weights = weights; |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 168 | _input = input; |
| 169 | _output = output; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 170 | _needs_permute = input->info()->data_layout() == DataLayout::NCHW; |
| 171 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 172 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 173 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 174 | ICLTensor *input_to_use = input; |
| 175 | const ICLTensor *weights_to_use = weights; |
| 176 | ICLTensor *output_to_use = output; |
| 177 | if(_needs_permute) |
| 178 | { |
| 179 | _memory_group.manage(&_permuted_input); |
| 180 | _memory_group.manage(&_permuted_output); |
| 181 | |
| 182 | // Configure the function to transform the input tensor from NCHW -> NHWC |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 183 | _permute_input_to_nhwc.configure(compile_context, input, &_permuted_input, PermutationVector(2U, 0U, 1U)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 184 | _permuted_input.info()->set_data_layout(DataLayout::NHWC); |
| 185 | |
| 186 | // Configure the function to transform the weights tensor from IHW -> HWI |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 187 | _permute_weights_to_nhwc.configure(compile_context, weights, &_permuted_weights, PermutationVector(2U, 0U, 1U)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 188 | _permuted_weights.info()->set_data_layout(DataLayout::NHWC); |
| 189 | |
| 190 | // Set output quantization info before dwc kernel configure |
| 191 | _permuted_output.info()->set_quantization_info(output->info()->quantization_info()); |
| 192 | |
| 193 | input_to_use = &_permuted_input; |
| 194 | weights_to_use = &_permuted_weights; |
| 195 | output_to_use = &_permuted_output; |
| 196 | } |
| 197 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 198 | CLTensor *output_multipliers_to_use = nullptr; |
| 199 | CLTensor *output_shifts_to_use = nullptr; |
| 200 | if(_is_quantized) |
| 201 | { |
| 202 | const size_t idx_c = get_data_layout_dimension_index(weights->info()->data_layout(), DataLayoutDimension::CHANNEL); |
| 203 | const size_t num_filters = (is_data_type_quantized_per_channel(weights->info()->data_type())) ? weights->info()->dimension(idx_c) : 1; |
| 204 | |
| 205 | _output_multipliers.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32)); |
| 206 | _output_shifts.allocator()->init(TensorInfo(TensorShape(num_filters), 1, DataType::S32)); |
| 207 | |
| 208 | output_multipliers_to_use = &_output_multipliers; |
| 209 | output_shifts_to_use = &_output_shifts; |
| 210 | } |
| 211 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 212 | DWCComputeKernelInfo dwc_native_compute_info; |
| 213 | initialize_dwc_native_compute_info(dwc_native_compute_info, weights_to_use->info(), conv_info, dilation, depth_multiplier, gpu_target); |
| 214 | |
| 215 | const ConvolutionInfo conv_kernel_info{ conv_info, depth_multiplier, act_info, dilation }; |
| 216 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 217 | _dwc_native_kernel->configure(compile_context, input_to_use, weights_to_use, biases, output_to_use, |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 218 | dwc_native_compute_info, conv_kernel_info, output_multipliers_to_use, output_shifts_to_use); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 219 | |
| 220 | if(_needs_permute) |
| 221 | { |
| 222 | _permuted_input.allocator()->allocate(); |
| 223 | |
| 224 | // Configure the function to transform the convoluted output to NCHW format |
| 225 | _permuted_output.info()->set_data_layout(DataLayout::NCHW); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 226 | _permute_output_to_nchw.configure(compile_context, &_permuted_output, output, PermutationVector(1U, 2U, 0U)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 227 | _permuted_output.allocator()->allocate(); |
| 228 | } |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 229 | |
| 230 | if(_is_quantized) |
| 231 | { |
| 232 | _output_multipliers.allocator()->allocate(); |
| 233 | _output_shifts.allocator()->allocate(); |
| 234 | } |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 237 | Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 238 | const PadStrideInfo &conv_info, |
| 239 | unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 240 | { |
| 241 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 242 | const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 243 | const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 244 | |
| 245 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) + (weights->dimension(idx_w) - 1) * (dilation.x() - 1) > input->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right()); |
| 246 | ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) + (weights->dimension(idx_h) - 1) * (dilation.y() - 1) > input->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom()); |
| 247 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 248 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 249 | |
| 250 | const ConvolutionInfo conv_kernel_info{ conv_info, depth_multiplier, act_info, dilation }; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 251 | |
| 252 | const bool needs_permute = input->data_layout() == DataLayout::NCHW; |
| 253 | |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 254 | const bool is_quantized = is_data_type_quantized(input->data_type()); |
| 255 | |
| 256 | TensorInfo output_multipliers_shifts_info(TensorInfo(TensorShape(1U), 1, DataType::S32)); |
| 257 | if(is_quantized) |
| 258 | { |
| 259 | if(is_data_type_quantized_per_channel(weights->data_type())) |
| 260 | { |
| 261 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QSYMM8_PER_CHANNEL); |
| 262 | |
| 263 | const size_t idx_c = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::CHANNEL); |
| 264 | output_multipliers_shifts_info.set_tensor_shape(TensorShape(weights->dimension(idx_c))); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 269 | } |
| 270 | } |
| 271 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 272 | if(needs_permute) |
| 273 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 274 | TensorShape permuted_input_shape = input->tensor_shape(); |
| 275 | TensorShape permuted_weights_shape = weights->tensor_shape(); |
| 276 | const ConvolutionInfo info{ conv_info, depth_multiplier, ActivationLayerInfo(), dilation }; |
| 277 | TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, info); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 278 | |
| 279 | permute(permuted_input_shape, PermutationVector(2U, 0U, 1U)); |
| 280 | permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U)); |
| 281 | permute(permuted_output_shape, PermutationVector(2U, 0U, 1U)); |
| 282 | |
| 283 | const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC); |
| 284 | const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC); |
| 285 | const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NHWC); |
| 286 | |
| 287 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U))); |
| 288 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U))); |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 289 | |
| 290 | DWCComputeKernelInfo dwc_native_compute_info; |
| 291 | initialize_dwc_native_compute_info(dwc_native_compute_info, &permuted_weights, conv_info, dilation, depth_multiplier, gpu_target); |
| 292 | |
| 293 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, |
| 294 | dwc_native_compute_info, conv_kernel_info, &output_multipliers_shifts_info, &output_multipliers_shifts_info)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 295 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U))); |
| 296 | } |
| 297 | else |
| 298 | { |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 299 | DWCComputeKernelInfo dwc_native_compute_info; |
| 300 | initialize_dwc_native_compute_info(dwc_native_compute_info, weights, conv_info, dilation, depth_multiplier, gpu_target); |
| 301 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_native_compute_info, conv_kernel_info, &output_multipliers_shifts_info, |
| 302 | &output_multipliers_shifts_info)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 303 | } |
| 304 | return Status{}; |
| 305 | } |
| 306 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 307 | void CLDepthwiseConvolutionLayer::run() |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 308 | { |
| 309 | prepare(); |
| 310 | |
| 311 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 312 | |
| 313 | if(_needs_permute) |
| 314 | { |
| 315 | _permute_input_to_nhwc.run(); |
| 316 | } |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 317 | CLScheduler::get().enqueue(*_dwc_native_kernel); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 318 | if(_needs_permute) |
| 319 | { |
| 320 | _permute_output_to_nchw.run(); |
| 321 | } |
| 322 | } |
| 323 | |
Gian Marco Iodice | 561c176 | 2021-04-16 15:08:59 +0100 | [diff] [blame^] | 324 | void CLDepthwiseConvolutionLayer::prepare() |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 325 | { |
| 326 | if(!_is_prepared) |
| 327 | { |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 328 | if(_is_quantized) |
| 329 | { |
| 330 | _output_multipliers.map(); |
| 331 | _output_shifts.map(); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 332 | quantization::compute_quantized_multipliers_and_shifts(_input->info(), |
| 333 | _original_weights->info(), |
| 334 | _output->info(), |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 335 | reinterpret_cast<int32_t *>(_output_multipliers.ptr_to_element(Coordinates(0))), |
| 336 | reinterpret_cast<int32_t *>(_output_shifts.ptr_to_element(Coordinates(0)))); |
| 337 | _output_multipliers.unmap(); |
| 338 | _output_shifts.unmap(); |
| 339 | } |
| 340 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 341 | if(_needs_permute) |
| 342 | { |
| 343 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 344 | |
| 345 | _permuted_weights.allocator()->allocate(); |
| 346 | _permute_weights_to_nhwc.run(); |
| 347 | _original_weights->mark_as_unused(); |
| 348 | } |
| 349 | _is_prepared = true; |
| 350 | } |
| 351 | } |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 352 | } // namespace arm_compute |