Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/PixelValue.h" |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 33 | #include "support/ToolchainSupport.h" |
| 34 | |
| 35 | using namespace arm_compute; |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 36 | using namespace arm_compute::misc; |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 37 | using namespace arm_compute::misc::shape_calculator; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 38 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 39 | CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3() |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 40 | : _kernel(nullptr), _border_handler() |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 44 | void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier, |
| 45 | ActivationLayerInfo act_info) |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 46 | { |
Michele Di Giorgio | 933fe86 | 2018-02-19 15:42:12 +0000 | [diff] [blame] | 47 | 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] | 48 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 49 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 50 | if(input->info()->data_layout() == DataLayout::NCHW) |
| 51 | { |
| 52 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>(); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>(); |
| 57 | } |
| 58 | |
| 59 | _kernel->set_target(CLScheduler::get().target()); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 60 | _kernel->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info); |
Diego Lopez Recas | fa0add1 | 2017-11-28 16:44:52 +0000 | [diff] [blame] | 61 | |
| 62 | // Configure border handler |
| 63 | PixelValue &&zero_value(0.f); |
| 64 | if(is_data_type_quantized_asymmetric(input->info()->data_type())) |
| 65 | { |
| 66 | zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().offset)); |
| 67 | } |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 68 | _border_handler.configure(input, _kernel->border_size(), BorderMode::CONSTANT, zero_value); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 71 | Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 72 | unsigned int depth_multiplier, |
| 73 | ActivationLayerInfo act_info, GPUTarget gpu_target) |
| 74 | { |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
Gian Marco Iodice | dff601d | 2018-08-09 13:28:41 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 77 | |
| 78 | if(input->data_layout() == DataLayout::NCHW) |
| 79 | { |
| 80 | return CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, gpu_target); |
| 81 | } |
| 82 | |
| 83 | return CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info); |
| 84 | } |
| 85 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 86 | void CLDepthwiseConvolutionLayer3x3::run() |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 87 | { |
| 88 | CLScheduler::get().enqueue(_border_handler); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 89 | CLScheduler::get().enqueue(*_kernel); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 92 | namespace |
| 93 | { |
| 94 | inline bool can_run_optimised_3x3_kernel(const ITensorInfo *weights, unsigned int depth_multiplier) |
| 95 | { |
| 96 | const DataLayout data_layout = weights->data_layout(); |
| 97 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 98 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 99 | const Size2D weights_size(weights->dimension(idx_w), weights->dimension(idx_h)); |
| 100 | return weights_size == Size2D(3, 3) && (data_layout == DataLayout::NHWC && depth_multiplier <= 1); |
| 101 | } |
| 102 | |
| 103 | } // namespace |
| 104 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 105 | CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer() |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 106 | : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _activationlayer_function(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 107 | _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_prepared(false), _is_quantized(false), _is_activationlayer_enabled(false), _original_weights(nullptr), |
| 108 | _optimised_function(nullptr) |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 109 | { |
| 110 | } |
| 111 | |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 112 | void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
| 113 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 114 | { |
Michele Di Giorgio | d24af8a | 2018-05-08 17:23:52 +0100 | [diff] [blame] | 115 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 116 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
Giorgio Arena | d051e97 | 2018-06-20 11:46:42 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 118 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 119 | if(can_run_optimised_3x3_kernel(weights->info(), depth_multiplier)) |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 120 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 121 | auto f = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3>(); |
| 122 | f->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info); |
| 123 | _optimised_function = std::move(f); |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 124 | } |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 125 | else |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 126 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 127 | const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 128 | const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
| 129 | const size_t idx_c = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 130 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 131 | const size_t weights_w = weights->info()->dimension(idx_w); |
| 132 | const size_t weights_h = weights->info()->dimension(idx_h); |
| 133 | const size_t weights_z = weights->info()->dimension(idx_c); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 134 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 135 | _is_prepared = false; |
| 136 | _original_weights = weights; |
| 137 | _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type()); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 138 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 139 | bool append_bias = (biases != nullptr) && !_is_quantized; |
| 140 | const GPUTarget gpu_target = CLScheduler::get().target(); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 141 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 142 | // Calculate output shape |
| 143 | TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier); |
| 144 | |
| 145 | // Output auto inizialitation if not yet initialized |
| 146 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 147 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
| 148 | |
| 149 | // Output width and height |
| 150 | const unsigned int conv_w = output_shape[idx_w]; |
| 151 | const unsigned int conv_h = output_shape[idx_h]; |
| 152 | |
| 153 | // Set up intermediate tensors |
| 154 | const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0); |
| 155 | const size_t conv_size = conv_w * conv_h; |
| 156 | |
| 157 | // Im2Col configuration |
| 158 | TensorShape shape_im2col = input->info()->tensor_shape(); |
| 159 | shape_im2col.set(0, patch_size); |
| 160 | shape_im2col.set(1, conv_size); |
| 161 | shape_im2col.set(2, weights_z); |
| 162 | _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
| 163 | _im2col_kernel.set_target(gpu_target); |
| 164 | _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier); |
| 165 | CLScheduler::get().tune_kernel_static(_im2col_kernel); |
| 166 | |
| 167 | // Weights reshape configuration |
| 168 | const TensorShape shape_weights_reshape(patch_size, weights_z); |
| 169 | _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape)); |
| 170 | _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr); |
| 171 | |
| 172 | // GEMV configuration |
| 173 | DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type(); |
| 174 | TensorShape shape_v2mm_out = input->info()->tensor_shape(); |
| 175 | shape_v2mm_out.set(0, conv_size * weights_z); |
| 176 | shape_v2mm_out.set(1, 1); |
| 177 | shape_v2mm_out.set(2, 1); |
| 178 | _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out)); |
| 179 | _v2mm_kernel.set_target(gpu_target); |
| 180 | _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output); |
| 181 | CLScheduler::get().tune_kernel_static(_v2mm_kernel); |
| 182 | _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
| 183 | _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h); |
| 184 | |
| 185 | // Output staged configuration |
| 186 | if(_is_quantized) |
| 187 | { |
| 188 | const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info(); |
| 189 | |
| 190 | float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale; |
| 191 | int output_multiplier, output_shift; |
| 192 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); |
| 193 | _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset); |
| 194 | _output_reshaped.allocator()->allocate(); |
| 195 | } |
| 196 | |
| 197 | // Fill borders on inputs |
| 198 | PixelValue zero_in(static_cast<int32_t>(0)); |
| 199 | PixelValue zero_w(static_cast<int32_t>(0)); |
| 200 | if(_is_quantized) |
| 201 | { |
| 202 | zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset)); |
| 203 | zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset)); |
| 204 | } |
| 205 | BorderSize border_size = _v2mm_kernel.border_size(); |
| 206 | _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in); |
| 207 | |
| 208 | border_size.bottom = 0; |
| 209 | _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w); |
| 210 | |
| 211 | // Allocate intermediate tensors |
| 212 | _input_reshaped.allocator()->allocate(); |
| 213 | _v2mm_output.allocator()->allocate(); |
| 214 | |
| 215 | //Configure Activation Layer |
| 216 | _is_activationlayer_enabled = act_info.enabled(); |
| 217 | |
| 218 | if(_is_activationlayer_enabled) |
| 219 | { |
| 220 | _activationlayer_function.configure(output, nullptr, act_info); |
| 221 | } |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 222 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 225 | Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 226 | unsigned int depth_multiplier, const ActivationLayerInfo &act_info) |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 227 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 228 | if(can_run_optimised_3x3_kernel(weights, depth_multiplier)) |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 229 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 230 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info)); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 231 | } |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 232 | else |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 233 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 234 | const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 235 | const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 236 | const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 237 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 238 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output); |
| 239 | ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(idx_c) * depth_multiplier) != weights->dimension(idx_c)); |
| 240 | |
| 241 | const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type()); |
| 242 | const bool append_bias = (biases != nullptr) && !is_quantized; |
| 243 | const TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier); |
| 244 | const size_t weights_w = weights->dimension(idx_w); |
| 245 | const size_t weights_h = weights->dimension(idx_h); |
| 246 | const size_t weights_z = weights->dimension(idx_c); |
| 247 | const unsigned int conv_w = output_shape[idx_w]; |
| 248 | const unsigned int conv_h = output_shape[idx_h]; |
| 249 | const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0); |
| 250 | const size_t conv_size = conv_w * conv_h; |
| 251 | |
| 252 | TensorShape shape_im2col = input->tensor_shape(); |
| 253 | shape_im2col.set(0, patch_size); |
| 254 | shape_im2col.set(1, conv_size); |
| 255 | shape_im2col.set(2, weights_z); |
| 256 | TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
| 257 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseIm2ColKernel::validate(input, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier)); |
| 258 | |
| 259 | const TensorShape shape_weights_reshape(patch_size, weights_z); |
| 260 | TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape)); |
| 261 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseWeightsReshapeKernel::validate(weights, &weights_reshaped, append_bias ? biases : nullptr)); |
| 262 | |
| 263 | DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type(); |
| 264 | TensorShape shape_v2mm_out = input->tensor_shape(); |
| 265 | shape_v2mm_out.set(0, conv_size * weights_z); |
| 266 | shape_v2mm_out.set(1, 1); |
| 267 | shape_v2mm_out.set(2, 1); |
| 268 | TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out)); |
| 269 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output)); |
| 270 | |
| 271 | TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape)); |
| 272 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output, conv_w, conv_h)); |
| 273 | |
| 274 | if(is_quantized) |
| 275 | { |
| 276 | ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output)); |
| 277 | } |
| 278 | |
| 279 | // Validate Activation Layer |
| 280 | if(act_info.enabled()) |
| 281 | { |
| 282 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
| 283 | } |
| 284 | } |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 285 | return Status{}; |
| 286 | } |
| 287 | |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 288 | void CLDepthwiseConvolutionLayer::run() |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 289 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 290 | prepare(); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 291 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 292 | if(_optimised_function != nullptr) |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 293 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 294 | _optimised_function->run(); |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 295 | } |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 296 | else |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 297 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 298 | CLScheduler::get().enqueue(_im2col_kernel); |
| 299 | CLScheduler::get().enqueue(_v2mm_input_fill_border); |
| 300 | CLScheduler::get().enqueue(_v2mm_kernel); |
| 301 | CLScheduler::get().enqueue(_vector_to_tensor_kernel); |
| 302 | if(_is_quantized) |
| 303 | { |
| 304 | CLScheduler::get().enqueue(_output_stage_kernel); |
| 305 | } |
| 306 | if(_is_activationlayer_enabled) |
| 307 | { |
| 308 | _activationlayer_function.run(); |
| 309 | } |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 310 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 311 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 312 | |
| 313 | void CLDepthwiseConvolutionLayer::prepare() |
| 314 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 315 | if(_optimised_function != nullptr) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 316 | { |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 317 | _optimised_function->prepare(); |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | if(!_is_prepared) |
| 322 | { |
| 323 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 324 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 325 | // Run weights reshaping and mark original weights tensor as unused |
| 326 | _weights_reshaped.allocator()->allocate(); |
| 327 | CLScheduler::get().enqueue(_weights_reshape_kernel); |
| 328 | CLScheduler::get().enqueue(_v2mm_weights_fill_border); |
| 329 | _original_weights->mark_as_unused(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 330 | |
Pablo Tello | 8bf622a | 2018-12-03 15:54:49 +0000 | [diff] [blame] | 331 | CLScheduler::get().queue().finish(); |
| 332 | _is_prepared = true; |
| 333 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 334 | } |
| 335 | } |