Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h" |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/PixelValue.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" |
| 34 | #include "support/ToolchainSupport.h" |
| 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 | { |
| 43 | Status validate_arguments_3x3(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 44 | unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation) |
| 45 | { |
| 46 | // This function should be removed and incorporated inside CLDepthwiseConvolutionLayerInternal3x3 once CLDepthwiseConvolutionLayer3x3 is properly removed |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
| 49 | |
| 50 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 51 | const bool is_nhwc = input->data_layout() == DataLayout::NHWC; |
| 52 | const bool needs_permute = is_nhwc && (depth_multiplier > 1); |
| 53 | const bool needs_weights_reshape = is_nhwc && (depth_multiplier == 1) && is_quantized; |
| 54 | const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1)); |
| 55 | const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1); |
| 56 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
| 57 | DepthwiseConvolutionReshapeInfo info; |
| 58 | info.c0 = 4; |
| 59 | info.transpose = is_stride_1_dilation_1 && is_dot8_supported; |
| 60 | |
| 61 | if(is_quantized) |
| 62 | { |
| 63 | const UniformQuantizationInfo iq_info = input->quantization_info().uniform(); |
| 64 | const UniformQuantizationInfo wq_info = weights->quantization_info().uniform(); |
| 65 | const UniformQuantizationInfo oq_info = (output->total_size() == 0) ? iq_info : output->quantization_info().uniform(); |
| 66 | |
| 67 | const float multiplier = iq_info.scale * wq_info.scale / oq_info.scale; |
| 68 | ARM_COMPUTE_UNUSED(multiplier); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON(multiplier > 1.0f); |
| 70 | } |
| 71 | |
| 72 | if(needs_permute) |
| 73 | { |
| 74 | TensorShape permuted_input_shape = input->tensor_shape(); |
| 75 | TensorShape permuted_weights_shape = weights->tensor_shape(); |
| 76 | TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); |
| 77 | |
| 78 | permute(permuted_input_shape, PermutationVector(1U, 2U, 0U)); |
| 79 | permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U)); |
| 80 | permute(permuted_output_shape, PermutationVector(1U, 2U, 0U)); |
| 81 | |
| 82 | const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW); |
| 83 | const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW); |
| 84 | const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW); |
| 85 | |
| 86 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, act_info, gpu_target, |
| 87 | dilation)); |
| 88 | } |
| 89 | else if(is_nhwc) |
| 90 | { |
| 91 | if(needs_weights_reshape) |
| 92 | { |
| 93 | auto reshaped_weights_shape = arm_compute::misc::shape_calculator::compute_reshaped_depthwise_weights_shape(*weights, info); |
| 94 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, &weights->clone()->set_tensor_shape(reshaped_weights_shape), biases, output, conv_info, depth_multiplier, |
| 95 | act_info, dilation)); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation)); |
| 100 | } |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation)); |
| 105 | } |
| 106 | return Status{}; |
| 107 | } |
| 108 | } // namespace |
| 109 | |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 110 | CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 111 | : _func(std::move(memory_manager)) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 112 | { |
| 113 | } |
| 114 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 115 | void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 116 | ActivationLayerInfo act_info, const Size2D &dilation) |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 117 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 118 | _func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
| 119 | } |
| 120 | |
| 121 | Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 122 | unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation) |
| 123 | { |
| 124 | return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation); |
| 125 | } |
| 126 | |
| 127 | void CLDepthwiseConvolutionLayer3x3::run() |
| 128 | { |
| 129 | _func.run(); |
| 130 | } |
| 131 | |
| 132 | void CLDepthwiseConvolutionLayer3x3::prepare() |
| 133 | { |
| 134 | _func.prepare(); |
| 135 | } |
| 136 | |
| 137 | CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::CLDepthwiseConvolutionLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager) |
| 138 | : _memory_group(std::move(memory_manager)), |
| 139 | _dwc_native_kernel(), |
| 140 | _permute_input_to_nhwc(), |
| 141 | _permute_weights_to_nhwc(), |
| 142 | _permute_output_to_nchw(), |
| 143 | _permuted_input(), |
| 144 | _permuted_weights(), |
| 145 | _permuted_output(), |
| 146 | _original_weights(), |
| 147 | _needs_permute(false), |
| 148 | _is_prepared(false) |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
| 153 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) |
| 154 | { |
| 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 | |
| 165 | _is_prepared = false; |
| 166 | _original_weights = weights; |
| 167 | _needs_permute = input->info()->data_layout() == DataLayout::NCHW; |
| 168 | |
| 169 | ICLTensor *input_to_use = input; |
| 170 | const ICLTensor *weights_to_use = weights; |
| 171 | ICLTensor *output_to_use = output; |
| 172 | if(_needs_permute) |
| 173 | { |
| 174 | _memory_group.manage(&_permuted_input); |
| 175 | _memory_group.manage(&_permuted_output); |
| 176 | |
| 177 | // Configure the function to transform the input tensor from NCHW -> NHWC |
| 178 | _permute_input_to_nhwc.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U)); |
| 179 | _permuted_input.info()->set_data_layout(DataLayout::NHWC); |
| 180 | |
| 181 | // Configure the function to transform the weights tensor from IHW -> HWI |
| 182 | _permute_weights_to_nhwc.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U)); |
| 183 | _permuted_weights.info()->set_data_layout(DataLayout::NHWC); |
| 184 | |
| 185 | // Set output quantization info before dwc kernel configure |
| 186 | _permuted_output.info()->set_quantization_info(output->info()->quantization_info()); |
| 187 | |
| 188 | input_to_use = &_permuted_input; |
| 189 | weights_to_use = &_permuted_weights; |
| 190 | output_to_use = &_permuted_output; |
| 191 | } |
| 192 | |
| 193 | DWCWeightsKernelInfo dwc_weights_info; |
| 194 | dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1; |
| 195 | DWCKernelInfo dwc_info; |
| 196 | dwc_info.activation_info = act_info; |
| 197 | _dwc_native_kernel.configure(input_to_use, weights_to_use, biases, output_to_use, dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation); |
| 198 | |
| 199 | if(_needs_permute) |
| 200 | { |
| 201 | _permuted_input.allocator()->allocate(); |
| 202 | |
| 203 | // Configure the function to transform the convoluted output to NCHW format |
| 204 | _permuted_output.info()->set_data_layout(DataLayout::NCHW); |
| 205 | _permute_output_to_nchw.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U)); |
| 206 | _permuted_output.allocator()->allocate(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 211 | const PadStrideInfo &conv_info, |
| 212 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation) |
| 213 | { |
| 214 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 215 | const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 216 | const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 217 | |
| 218 | 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()); |
| 219 | 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()); |
| 220 | |
| 221 | DWCWeightsKernelInfo dwc_weights_info; |
| 222 | dwc_weights_info.n0 = (depth_multiplier == 1) ? 8 : 1; |
| 223 | DWCKernelInfo dwc_info; |
| 224 | dwc_info.activation_info = act_info; |
| 225 | |
| 226 | const bool needs_permute = input->data_layout() == DataLayout::NCHW; |
| 227 | |
| 228 | if(needs_permute) |
| 229 | { |
| 230 | TensorShape permuted_input_shape = input->tensor_shape(); |
| 231 | TensorShape permuted_weights_shape = weights->tensor_shape(); |
| 232 | TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation); |
| 233 | |
| 234 | permute(permuted_input_shape, PermutationVector(2U, 0U, 1U)); |
| 235 | permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U)); |
| 236 | permute(permuted_output_shape, PermutationVector(2U, 0U, 1U)); |
| 237 | |
| 238 | const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC); |
| 239 | const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC); |
| 240 | const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NHWC); |
| 241 | |
| 242 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U))); |
| 243 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U))); |
| 244 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, dwc_weights_info, |
| 245 | dwc_info, conv_info, depth_multiplier, dilation)); |
| 246 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U))); |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, dwc_weights_info, dwc_info, conv_info, depth_multiplier, dilation)); |
| 251 | } |
| 252 | return Status{}; |
| 253 | } |
| 254 | |
| 255 | void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::run() |
| 256 | { |
| 257 | prepare(); |
| 258 | |
| 259 | MemoryGroupResourceScope scope_mg(_memory_group); |
| 260 | |
| 261 | if(_needs_permute) |
| 262 | { |
| 263 | _permute_input_to_nhwc.run(); |
| 264 | } |
| 265 | CLScheduler::get().enqueue(_dwc_native_kernel); |
| 266 | if(_needs_permute) |
| 267 | { |
| 268 | _permute_output_to_nchw.run(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerGeneric::prepare() |
| 273 | { |
| 274 | if(!_is_prepared) |
| 275 | { |
| 276 | if(_needs_permute) |
| 277 | { |
| 278 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 279 | |
| 280 | _permuted_weights.allocator()->allocate(); |
| 281 | _permute_weights_to_nhwc.run(); |
| 282 | _original_weights->mark_as_unused(); |
| 283 | } |
| 284 | _is_prepared = true; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::CLDepthwiseConvolutionLayerInternal3x3(std::shared_ptr<IMemoryManager> memory_manager) |
| 289 | : _memory_group(std::move(memory_manager)), _kernel(nullptr), _border_handler(), _permute_input_to_nchw(), _permute_weights_to_nchw(), _permute_output_to_nhwc(), _reshape_weights(), _permuted_input(), |
| 290 | _permuted_weights(), _permuted_output(), _original_weights(nullptr), _needs_permute(false), _needs_weights_reshape(false), _is_prepared(false) |
| 291 | { |
| 292 | } |
| 293 | |
| 294 | void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 295 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation) |
| 296 | { |
Michele Di Giorgio | 933fe86 | 2018-02-19 15:42:12 +0000 | [diff] [blame] | 297 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Georgios Pinitas | 236bfe7 | 2017-11-23 15:59:55 +0000 | [diff] [blame] | 298 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Usama Arif | 881f2de | 2019-04-12 10:29:17 +0100 | [diff] [blame] | 299 | // idx_w and idx_h only used for validation |
| 300 | const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 301 | const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
| 302 | ARM_COMPUTE_UNUSED(idx_w); |
| 303 | ARM_COMPUTE_UNUSED(idx_h); |
| 304 | |
| 305 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_w) + (weights->info()->dimension(idx_w) - 1) * (dilation.x() - 1) > input->info()->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right()); |
| 306 | ARM_COMPUTE_ERROR_ON(weights->info()->dimension(idx_h) + (weights->info()->dimension(idx_h) - 1) * (dilation.y() - 1) > input->info()->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom()); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 307 | |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 308 | const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC; |
| 309 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 310 | _needs_permute = is_nhwc && (depth_multiplier > 1); |
| 311 | _needs_weights_reshape = is_nhwc && (depth_multiplier == 1) |
| 312 | && is_data_type_quantized_asymmetric(input->info()->data_type()); |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 313 | _is_prepared = false; |
| 314 | _original_weights = weights; |
| 315 | |
| 316 | ICLTensor *input_to_use = input; |
| 317 | const ICLTensor *weights_to_use = weights; |
| 318 | ICLTensor *output_to_use = output; |
| 319 | |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 320 | const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1)); |
| 321 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
| 322 | const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1); |
| 323 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 324 | DepthwiseConvolutionReshapeInfo info; |
| 325 | info.c0 = 4; |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 326 | info.transpose = is_stride_1_dilation_1 && is_dot8_supported; |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 327 | |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 328 | if(_needs_permute) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 329 | { |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 330 | _memory_group.manage(&_permuted_input); |
| 331 | _memory_group.manage(&_permuted_output); |
| 332 | |
| 333 | // Configure the function to transform the input tensor from NHWC -> NCHW |
| 334 | _permute_input_to_nchw.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U)); |
| 335 | _permuted_input.info()->set_data_layout(DataLayout::NCHW); |
| 336 | |
| 337 | // Configure the function to transform the weights tensor from HWI -> IHW |
| 338 | _permute_weights_to_nchw.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U)); |
| 339 | _permuted_weights.info()->set_data_layout(DataLayout::NCHW); |
Pablo Tello | a28aebc | 2019-06-03 14:59:48 +0100 | [diff] [blame] | 340 | _permuted_output.info()->set_quantization_info(output->info()->quantization_info()); |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 341 | |
| 342 | input_to_use = &_permuted_input; |
| 343 | weights_to_use = &_permuted_weights; |
| 344 | output_to_use = &_permuted_output; |
| 345 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 346 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>(); |
| 347 | } |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 348 | else if(is_nhwc) |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 349 | { |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 350 | if(_needs_weights_reshape) |
| 351 | { |
| 352 | _reshape_weights.configure(weights, &_permuted_weights, info); |
| 353 | weights_to_use = &_permuted_weights; |
| 354 | } |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 355 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>(); |
| 356 | } |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 357 | else |
| 358 | { |
| 359 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>(); |
| 360 | } |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 361 | |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 362 | // Configure kernel |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 363 | _kernel->set_target(CLScheduler::get().target()); |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 364 | _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, act_info, dilation); |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 365 | |
| 366 | // Permute output if needed |
| 367 | if(_needs_permute) |
| 368 | { |
| 369 | // Configure the function to transform the convoluted output to ACL's native ordering format NCHW |
Georgios Pinitas | 3f8aac4 | 2018-12-24 13:09:02 +0000 | [diff] [blame] | 370 | _permuted_output.info()->set_data_layout(DataLayout::NCHW); |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 371 | _permute_output_to_nhwc.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U)); |
| 372 | |
| 373 | // Allocate tensors |
| 374 | _permuted_input.allocator()->allocate(); |
| 375 | _permuted_output.allocator()->allocate(); |
| 376 | } |
Diego Lopez Recas | fa0add1 | 2017-11-28 16:44:52 +0000 | [diff] [blame] | 377 | // Configure border handler |
| 378 | PixelValue &&zero_value(0.f); |
| 379 | if(is_data_type_quantized_asymmetric(input->info()->data_type())) |
| 380 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 381 | zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().uniform().offset)); |
Diego Lopez Recas | fa0add1 | 2017-11-28 16:44:52 +0000 | [diff] [blame] | 382 | } |
Georgios Pinitas | 3f8aac4 | 2018-12-24 13:09:02 +0000 | [diff] [blame] | 383 | _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 386 | Status CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 387 | const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, GPUTarget gpu_target, const Size2D &dilation) |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 388 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 389 | return validate_arguments_3x3(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 392 | void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::run() |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 393 | { |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 394 | prepare(); |
| 395 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 396 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 397 | |
| 398 | if(_needs_permute) |
| 399 | { |
| 400 | _permute_input_to_nchw.run(); |
| 401 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 402 | CLScheduler::get().enqueue(_border_handler); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 403 | CLScheduler::get().enqueue(*_kernel); |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 404 | |
| 405 | if(_needs_permute) |
| 406 | { |
| 407 | _permute_output_to_nhwc.run(); |
| 408 | } |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 411 | void CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayerInternal3x3::prepare() |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 412 | { |
| 413 | if(!_is_prepared) |
| 414 | { |
| 415 | if(_needs_permute) |
| 416 | { |
| 417 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 418 | |
| 419 | _permuted_weights.allocator()->allocate(); |
| 420 | _permute_weights_to_nchw.run(); |
| 421 | _original_weights->mark_as_unused(); |
| 422 | } |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 423 | |
| 424 | if(_needs_weights_reshape) |
| 425 | { |
| 426 | ARM_COMPUTE_ERROR_ON(_needs_permute); |
| 427 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 428 | _permuted_weights.allocator()->allocate(); |
| 429 | CLScheduler::get().enqueue(_reshape_weights); |
| 430 | _original_weights->mark_as_unused(); |
| 431 | } |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 432 | _is_prepared = true; |
| 433 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Michele Di Giorgio | a046e16 | 2019-10-08 09:36:26 +0100 | [diff] [blame] | 436 | CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 437 | : _memory_manager(std::move(memory_manager)), _depth_conv_func(DepthwiseConvolutionFunction::GENERIC), _func_3x3(), _func_generic() |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 438 | { |
| 439 | } |
| 440 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 441 | void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
| 442 | ActivationLayerInfo act_info, const Size2D &dilation) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 443 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 444 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 445 | _depth_conv_func = get_depthwiseconvolution_function(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, act_info, |
| 446 | dilation, gpu_target); |
| 447 | switch(_depth_conv_func) |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 448 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 449 | case DepthwiseConvolutionFunction::OPTIMIZED: |
| 450 | _func_3x3.set_memory_group(_memory_manager); |
| 451 | _func_3x3.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
| 452 | break; |
| 453 | case DepthwiseConvolutionFunction::GENERIC: |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 454 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 455 | _func_generic.set_memory_group(_memory_manager); |
| 456 | _func_generic.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 457 | } |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 458 | break; |
| 459 | default: |
| 460 | ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction"); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 461 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 464 | Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 465 | unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation) |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 466 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 467 | const GPUTarget gpu_target = CLScheduler::get().target(); |
| 468 | DepthwiseConvolutionFunction depth_conv_func = get_depthwiseconvolution_function(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation, gpu_target); |
| 469 | switch(depth_conv_func) |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 470 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 471 | case DepthwiseConvolutionFunction::OPTIMIZED: |
| 472 | return CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation); |
| 473 | case DepthwiseConvolutionFunction::GENERIC: |
| 474 | return CLDepthwiseConvolutionLayerGeneric::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation); |
| 475 | default: |
| 476 | ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction"); |
| 477 | } |
| 478 | } |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 479 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 480 | DepthwiseConvolutionFunction CLDepthwiseConvolutionLayer::get_depthwiseconvolution_function(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 481 | const PadStrideInfo &conv_info, |
| 482 | unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation, GPUTarget gpu_target) |
| 483 | { |
| 484 | if(bool(CLDepthwiseConvolutionLayerInternal3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target, dilation)) && (is_data_type_float(input->data_type()) |
| 485 | || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD)) |
| 486 | { |
| 487 | return DepthwiseConvolutionFunction::OPTIMIZED; |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 488 | } |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 489 | else |
| 490 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 491 | return DepthwiseConvolutionFunction::GENERIC; |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 492 | } |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 495 | void CLDepthwiseConvolutionLayer::run() |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 496 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 497 | switch(_depth_conv_func) |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 498 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 499 | case DepthwiseConvolutionFunction::OPTIMIZED: |
| 500 | _func_3x3.run(); |
| 501 | break; |
| 502 | case DepthwiseConvolutionFunction::GENERIC: |
| 503 | _func_generic.run(); |
| 504 | break; |
| 505 | default: |
| 506 | ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured"); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 507 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 508 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 509 | |
| 510 | void CLDepthwiseConvolutionLayer::prepare() |
| 511 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 512 | switch(_depth_conv_func) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 513 | { |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 514 | case DepthwiseConvolutionFunction::OPTIMIZED: |
| 515 | _func_3x3.prepare(); |
| 516 | break; |
| 517 | case DepthwiseConvolutionFunction::GENERIC: |
| 518 | _func_generic.prepare(); |
| 519 | break; |
| 520 | default: |
| 521 | ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured"); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 522 | } |
| 523 | } |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 524 | } // namespace arm_compute |