Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 1 | /* |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [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/CLDepthwiseConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h" |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 29 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/PixelValue.h" |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 32 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 34 | #include "support/ToolchainSupport.h" |
| 35 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 36 | namespace arm_compute |
| 37 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 38 | using namespace arm_compute::misc; |
| 39 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 40 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 41 | CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager) |
| 42 | : _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(), |
| 43 | _permuted_weights(), _permuted_output(), _original_weights(nullptr), _needs_permute(false), _needs_weights_reshape(false), _is_prepared(false) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 47 | void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
| 48 | ActivationLayerInfo act_info) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 49 | { |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 52 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 53 | const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC; |
| 54 | |
| 55 | _needs_permute = is_nhwc && (depth_multiplier > 1); |
| 56 | _needs_weights_reshape = is_nhwc && (depth_multiplier == 1) |
| 57 | && is_data_type_quantized_asymmetric(input->info()->data_type()); |
| 58 | _is_prepared = false; |
| 59 | _original_weights = weights; |
| 60 | |
| 61 | ICLTensor *input_to_use = input; |
| 62 | const ICLTensor *weights_to_use = weights; |
| 63 | ICLTensor *output_to_use = output; |
| 64 | |
| 65 | const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1)); |
| 66 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
| 67 | DepthwiseConvolutionReshapeInfo info; |
| 68 | info.c0 = 4; |
| 69 | info.transpose = is_stride_1 && is_dot8_supported; |
| 70 | |
| 71 | if(_needs_permute) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 72 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 73 | _memory_group.manage(&_permuted_input); |
| 74 | _memory_group.manage(&_permuted_output); |
| 75 | |
| 76 | // Configure the function to transform the input tensor from NHWC -> NCHW |
| 77 | _permute_input_to_nchw.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U)); |
| 78 | _permuted_input.info()->set_data_layout(DataLayout::NCHW); |
| 79 | |
| 80 | // Configure the function to transform the weights tensor from HWI -> IHW |
| 81 | _permute_weights_to_nchw.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U)); |
| 82 | _permuted_weights.info()->set_data_layout(DataLayout::NCHW); |
| 83 | |
| 84 | input_to_use = &_permuted_input; |
| 85 | weights_to_use = &_permuted_weights; |
| 86 | output_to_use = &_permuted_output; |
| 87 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 88 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>(); |
| 89 | } |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 90 | else if(is_nhwc) |
| 91 | { |
| 92 | if(_needs_weights_reshape) |
| 93 | { |
| 94 | _reshape_weights.configure(weights, &_permuted_weights, info); |
| 95 | weights_to_use = &_permuted_weights; |
| 96 | } |
| 97 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>(); |
| 98 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 99 | else |
| 100 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 101 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 104 | // Configure kernel |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 105 | _kernel->set_target(CLScheduler::get().target()); |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 106 | _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, act_info); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 107 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 108 | // Permute output if needed |
| 109 | if(_needs_permute) |
| 110 | { |
| 111 | // Configure the function to transform the convoluted output to ACL's native ordering format NCHW |
| 112 | _permuted_output.info()->set_data_layout(DataLayout::NCHW); |
| 113 | _permute_output_to_nhwc.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U)); |
| 114 | |
| 115 | // Allocate tensors |
| 116 | _permuted_input.allocator()->allocate(); |
| 117 | _permuted_output.allocator()->allocate(); |
| 118 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 119 | // Configure border handler |
| 120 | PixelValue &&zero_value(0.f); |
| 121 | if(is_data_type_quantized_asymmetric(input->info()->data_type())) |
| 122 | { |
| 123 | zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().offset)); |
| 124 | } |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 125 | _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 129 | unsigned int depth_multiplier, |
| 130 | ActivationLayerInfo act_info, GPUTarget gpu_target) |
| 131 | { |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 133 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 134 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 135 | const bool is_nhwc = input->data_layout() == DataLayout::NHWC; |
| 136 | const bool needs_permute = is_nhwc && (depth_multiplier > 1); |
| 137 | const bool needs_weights_reshape = is_nhwc && (depth_multiplier == 1); |
| 138 | const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1)); |
| 139 | const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device()); |
| 140 | DepthwiseConvolutionReshapeInfo info; |
| 141 | info.c0 = 4; |
| 142 | info.transpose = is_stride_1 && is_dot8_supported; |
| 143 | |
| 144 | if(needs_permute) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 145 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 146 | TensorShape permuted_input_shape = input->tensor_shape(); |
| 147 | TensorShape permuted_weights_shape = weights->tensor_shape(); |
| 148 | TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier); |
| 149 | |
| 150 | permute(permuted_input_shape, PermutationVector(1U, 2U, 0U)); |
| 151 | permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U)); |
| 152 | permute(permuted_output_shape, PermutationVector(1U, 2U, 0U)); |
| 153 | |
| 154 | const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW); |
| 155 | const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW); |
| 156 | const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW); |
| 157 | |
| 158 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, act_info, gpu_target)); |
| 159 | } |
| 160 | else if(is_nhwc) |
| 161 | { |
| 162 | if(needs_weights_reshape) |
| 163 | { |
| 164 | auto reshaped_weights_shape = arm_compute::misc::shape_calculator::compute_reshaped_depthwise_weights_shape(*weights, info); |
| 165 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, &weights->clone()->set_tensor_shape(reshaped_weights_shape), biases, output, conv_info, depth_multiplier, |
| 166 | act_info)); |
| 167 | } |
| 168 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info)); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target)); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 175 | return Status{}; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void CLDepthwiseConvolutionLayer3x3::run() |
| 179 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 180 | prepare(); |
| 181 | |
| 182 | _memory_group.acquire(); |
| 183 | |
| 184 | if(_needs_permute) |
| 185 | { |
| 186 | _permute_input_to_nchw.run(); |
| 187 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 188 | CLScheduler::get().enqueue(_border_handler); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 189 | CLScheduler::get().enqueue(*_kernel); |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 190 | |
| 191 | if(_needs_permute) |
| 192 | { |
| 193 | _permute_output_to_nhwc.run(); |
| 194 | } |
| 195 | |
| 196 | _memory_group.release(); |
| 197 | } |
| 198 | |
| 199 | void CLDepthwiseConvolutionLayer3x3::prepare() |
| 200 | { |
| 201 | if(!_is_prepared) |
| 202 | { |
| 203 | if(_needs_permute) |
| 204 | { |
| 205 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 206 | |
| 207 | _permuted_weights.allocator()->allocate(); |
| 208 | _permute_weights_to_nchw.run(); |
| 209 | _original_weights->mark_as_unused(); |
| 210 | } |
| 211 | |
| 212 | if(_needs_weights_reshape) |
| 213 | { |
| 214 | ARM_COMPUTE_ERROR_ON(_needs_permute); |
| 215 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 216 | _permuted_weights.allocator()->allocate(); |
| 217 | CLScheduler::get().enqueue(_reshape_weights); |
| 218 | _original_weights->mark_as_unused(); |
| 219 | } |
| 220 | _is_prepared = true; |
| 221 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer() |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 225 | : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _activationlayer_function(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 226 | _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_prepared(false), _is_quantized(false), _is_activationlayer_enabled(false), _original_weights(nullptr), |
| 227 | _optimised_function(nullptr) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 228 | { |
| 229 | } |
| 230 | |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 231 | void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
| 232 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 233 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 234 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 235 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 236 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 237 | |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 238 | const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 239 | const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 240 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 241 | const bool can_run_optimised_3x3_kernel = (weights->info()->dimension(idx_w) == 3) && (weights->info()->dimension(idx_h) == 3); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 242 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 243 | if(bool(can_run_optimised_3x3_kernel)) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 244 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 245 | auto f = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3>(); |
| 246 | f->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info); |
| 247 | _optimised_function = std::move(f); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 248 | } |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 249 | else |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 250 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 251 | const size_t idx_c = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 252 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 253 | const size_t weights_w = weights->info()->dimension(idx_w); |
| 254 | const size_t weights_h = weights->info()->dimension(idx_h); |
| 255 | const size_t weights_z = weights->info()->dimension(idx_c); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 256 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 257 | _is_prepared = false; |
| 258 | _original_weights = weights; |
| 259 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 260 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 261 | bool append_bias = (biases != nullptr) && !_is_quantized; |
| 262 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 263 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 264 | // Calculate output shape |
| 265 | TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier); |
| 266 | |
| 267 | // Output auto inizialitation if not yet initialized |
| 268 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 269 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
| 270 | |
| 271 | // Output width and height |
| 272 | const unsigned int conv_w = output_shape[idx_w]; |
| 273 | const unsigned int conv_h = output_shape[idx_h]; |
| 274 | |
| 275 | // Set up intermediate tensors |
| 276 | const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0); |
| 277 | const size_t conv_size = conv_w * conv_h; |
| 278 | |
| 279 | // Im2Col configuration |
| 280 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 281 | shape_im2col.set(0, patch_size); |
| 282 | shape_im2col.set(1, conv_size); |
| 283 | shape_im2col.set(2, weights_z); |
| 284 | _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
| 285 | _im2col_kernel.set_target(gpu_target); |
| 286 | _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier); |
| 287 | CLScheduler::get().tune_kernel_static(_im2col_kernel); |
| 288 | |
| 289 | // Weights reshape configuration |
| 290 | const TensorShape shape_weights_reshape(patch_size, weights_z); |
| 291 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape)); |
| 292 | _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr); |
| 293 | |
| 294 | // GEMV configuration |
| 295 | DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type(); |
| 296 | TensorShape shape_v2mm_out = input->info()->tensor_shape(); |
| 297 | shape_v2mm_out.set(0, conv_size * weights_z); |
| 298 | shape_v2mm_out.set(1, 1); |
| 299 | shape_v2mm_out.set(2, 1); |
| 300 | _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out)); |
| 301 | _v2mm_kernel.set_target(gpu_target); |
| 302 | _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output); |
| 303 | CLScheduler::get().tune_kernel_static(_v2mm_kernel); |
| 304 | _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
| 305 | _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h); |
| 306 | |
| 307 | // Output staged configuration |
| 308 | if(_is_quantized) |
| 309 | { |
| 310 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info(); |
| 311 | |
| 312 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
| 313 | int output_multiplier, output_shift; |
| 314 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 315 | _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset); |
| 316 | _output_reshaped.allocator()->allocate(); |
| 317 | } |
| 318 | |
| 319 | // Fill borders on inputs |
| 320 | PixelValue zero_in(static_cast<int32_t>(0)); |
| 321 | PixelValue zero_w(static_cast<int32_t>(0)); |
| 322 | if(_is_quantized) |
| 323 | { |
| 324 | zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset)); |
| 325 | zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset)); |
| 326 | } |
| 327 | BorderSize border_size = _v2mm_kernel.border_size(); |
| 328 | _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in); |
| 329 | |
| 330 | border_size.bottom = 0; |
| 331 | _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w); |
| 332 | |
| 333 | // Allocate intermediate tensors |
| 334 | _input_reshaped.allocator()->allocate(); |
| 335 | _v2mm_output.allocator()->allocate(); |
| 336 | |
| 337 | //Configure Activation Layer |
| 338 | _is_activationlayer_enabled = act_info.enabled(); |
| 339 | |
| 340 | if(_is_activationlayer_enabled) |
| 341 | { |
| 342 | _activationlayer_function.configure(output, nullptr, act_info); |
| 343 | } |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 344 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 347 | Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 348 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 349 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 350 | const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 351 | const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 352 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 353 | const bool can_run_optimised_3x3_kernel = (weights->dimension(idx_w) == 3) && (weights->dimension(idx_h) == 3); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 354 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 355 | if(can_run_optimised_3x3_kernel) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 356 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 357 | const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 358 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 359 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 360 | ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(idx_c) * depth_multiplier) != weights->dimension(idx_c)); |
| 361 | |
| 362 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 363 | const bool append_bias = (biases != nullptr) && !is_quantized; |
| 364 | const TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier); |
| 365 | const size_t weights_w = weights->dimension(idx_w); |
| 366 | const size_t weights_h = weights->dimension(idx_h); |
| 367 | const size_t weights_z = weights->dimension(idx_c); |
| 368 | const unsigned int conv_w = output_shape[idx_w]; |
| 369 | const unsigned int conv_h = output_shape[idx_h]; |
| 370 | const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0); |
| 371 | const size_t conv_size = conv_w * conv_h; |
| 372 | |
| 373 | TensorShape shape_im2col = input->tensor_shape(); |
| 374 | shape_im2col.set(0, patch_size); |
| 375 | shape_im2col.set(1, conv_size); |
| 376 | shape_im2col.set(2, weights_z); |
| 377 | TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
| 378 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseIm2ColKernel::validate(input, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier)); |
| 379 | |
| 380 | const TensorShape shape_weights_reshape(patch_size, weights_z); |
| 381 | TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape)); |
| 382 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerReshapeWeightsGenericKernel::validate(weights, &weights_reshaped, append_bias ? biases : nullptr)); |
| 383 | |
| 384 | DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type(); |
| 385 | TensorShape shape_v2mm_out = input->tensor_shape(); |
| 386 | shape_v2mm_out.set(0, conv_size * weights_z); |
| 387 | shape_v2mm_out.set(1, 1); |
| 388 | shape_v2mm_out.set(2, 1); |
| 389 | TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out)); |
| 390 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output)); |
| 391 | |
| 392 | TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
| 393 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output, conv_w, conv_h)); |
| 394 | |
| 395 | if(is_quantized) |
| 396 | { |
| 397 | ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output)); |
| 398 | } |
| 399 | |
| 400 | // Validate Activation Layer |
| 401 | if(act_info.enabled()) |
| 402 | { |
| 403 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
| 404 | } |
| 405 | } |
| 406 | else |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 407 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 408 | CLDepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 409 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 410 | return Status{}; |
| 411 | } |
| 412 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 413 | void CLDepthwiseConvolutionLayer::run() |
| 414 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 415 | prepare(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 416 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 417 | if(_optimised_function != nullptr) |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 418 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 419 | _optimised_function->run(); |
Anthony Barbier | 06ea048 | 2018-02-22 15:45:35 +0000 | [diff] [blame] | 420 | } |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 421 | else |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 422 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 423 | CLScheduler::get().enqueue(_im2col_kernel); |
| 424 | CLScheduler::get().enqueue(_v2mm_input_fill_border); |
| 425 | CLScheduler::get().enqueue(_v2mm_kernel); |
| 426 | CLScheduler::get().enqueue(_vector_to_tensor_kernel); |
| 427 | if(_is_quantized) |
| 428 | { |
| 429 | CLScheduler::get().enqueue(_output_stage_kernel); |
| 430 | } |
| 431 | if(_is_activationlayer_enabled) |
| 432 | { |
| 433 | _activationlayer_function.run(); |
| 434 | } |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 435 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 436 | } |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 437 | |
| 438 | void CLDepthwiseConvolutionLayer::prepare() |
| 439 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 440 | if(_optimised_function != nullptr) |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 441 | { |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 442 | _optimised_function->prepare(); |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | if(!_is_prepared) |
| 447 | { |
| 448 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 449 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 450 | // Run weights reshaping and mark original weights tensor as unused |
| 451 | _weights_reshaped.allocator()->allocate(); |
| 452 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
| 453 | CLScheduler::get().enqueue(_v2mm_weights_fill_border); |
| 454 | _original_weights->mark_as_unused(); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 455 | |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 456 | CLScheduler::get().queue().finish(); |
| 457 | _is_prepared = true; |
| 458 | } |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
Jenkins | 514be65 | 2019-02-28 12:25:18 +0000 | [diff] [blame^] | 461 | } // namespace arm_compute |