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