blob: 65d3f5ffe481d28fca9528de37ac4d418ed8909e [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
giuros016d109962019-01-07 17:47:19 +00002 * Copyright (c) 2017-2019 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +01003 *
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 Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010025
26#include "arm_compute/core/CL/ICLTensor.h"
Giorgio Arenaad0c7382018-04-23 16:16:21 +010027#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
28#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
Georgios Pinitas05045c12018-12-07 18:31:47 +000029#include "arm_compute/core/Helpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010030#include "arm_compute/core/PixelValue.h"
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000032#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010033#include "arm_compute/runtime/CL/CLScheduler.h"
34#include "support/ToolchainSupport.h"
35
giuros016d109962019-01-07 17:47:19 +000036namespace arm_compute
37{
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000038using namespace arm_compute::misc;
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000039using namespace arm_compute::misc::shape_calculator;
Giorgio Arena93a690e2017-08-01 16:09:33 +010040
Georgios Pinitas05045c12018-12-07 18:31:47 +000041CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager)
giuros016d109962019-01-07 17:47:19 +000042 : _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)
Giorgio Arena93a690e2017-08-01 16:09:33 +010044{
45}
46
Giorgio Arena76572242018-04-04 17:44:26 +010047void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
48 ActivationLayerInfo act_info)
Giorgio Arena9fe41442017-08-23 16:36:24 +010049{
Michele Di Giorgio933fe862018-02-19 15:42:12 +000050 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas236bfe72017-11-23 15:59:55 +000051 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena9fe41442017-08-23 16:36:24 +010052
Georgios Pinitas05045c12018-12-07 18:31:47 +000053 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
54
giuros016d109962019-01-07 17:47:19 +000055 _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());
Georgios Pinitas05045c12018-12-07 18:31:47 +000058 _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
giuros016d109962019-01-07 17:47:19 +000065 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
Georgios Pinitas05045c12018-12-07 18:31:47 +000071 if(_needs_permute)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000072 {
Georgios Pinitas05045c12018-12-07 18:31:47 +000073 _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
Giorgio Arenadfca60b2018-01-31 10:30:59 +000088 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
89 }
Georgios Pinitas05045c12018-12-07 18:31:47 +000090 else if(is_nhwc)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000091 {
giuros016d109962019-01-07 17:47:19 +000092 if(_needs_weights_reshape)
93 {
94 _reshape_weights.configure(weights, &_permuted_weights, info);
95 weights_to_use = &_permuted_weights;
96 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +000097 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
98 }
Georgios Pinitas05045c12018-12-07 18:31:47 +000099 else
100 {
101 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
102 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000103
Georgios Pinitas05045c12018-12-07 18:31:47 +0000104 // Configure kernel
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000105 _kernel->set_target(CLScheduler::get().target());
Georgios Pinitas05045c12018-12-07 18:31:47 +0000106 _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, act_info);
107
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
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000112 _permuted_output.info()->set_data_layout(DataLayout::NCHW);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000113 _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 }
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000119 // 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 }
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000125 _border_handler.configure(input_to_use, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100126}
127
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100128Status 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);
Gian Marco Iodicedff601d2018-08-09 13:28:41 +0100133 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100134
giuros016d109962019-01-07 17:47:19 +0000135 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;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000143
144 if(needs_permute)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100145 {
Georgios Pinitas05045c12018-12-07 18:31:47 +0000146 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 {
giuros016d109962019-01-07 17:47:19 +0000162 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 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000168 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));
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100173 }
174
Georgios Pinitas05045c12018-12-07 18:31:47 +0000175 return Status{};
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100176}
177
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000178void CLDepthwiseConvolutionLayer3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100179{
Georgios Pinitas05045c12018-12-07 18:31:47 +0000180 prepare();
181
Georgios Pinitasda953f22019-04-02 17:27:03 +0100182 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000183
184 if(_needs_permute)
185 {
186 _permute_input_to_nchw.run();
187 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100188 CLScheduler::get().enqueue(_border_handler);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000189 CLScheduler::get().enqueue(*_kernel);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000190
191 if(_needs_permute)
192 {
193 _permute_output_to_nhwc.run();
194 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000195}
196
197void CLDepthwiseConvolutionLayer3x3::prepare()
198{
199 if(!_is_prepared)
200 {
201 if(_needs_permute)
202 {
203 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
204
205 _permuted_weights.allocator()->allocate();
206 _permute_weights_to_nchw.run();
207 _original_weights->mark_as_unused();
208 }
giuros016d109962019-01-07 17:47:19 +0000209
210 if(_needs_weights_reshape)
211 {
212 ARM_COMPUTE_ERROR_ON(_needs_permute);
213 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
214 _permuted_weights.allocator()->allocate();
215 CLScheduler::get().enqueue(_reshape_weights);
216 _original_weights->mark_as_unused();
217 }
Georgios Pinitas05045c12018-12-07 18:31:47 +0000218 _is_prepared = true;
219 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100220}
221
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000222CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer()
Georgios Pinitas60e98252018-10-22 16:17:20 +0100223 : _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 Tello8bf622a2018-12-03 15:54:49 +0000224 _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_prepared(false), _is_quantized(false), _is_activationlayer_enabled(false), _original_weights(nullptr),
225 _optimised_function(nullptr)
Giorgio Arena9fe41442017-08-23 16:36:24 +0100226{
227}
228
Georgios Pinitas60e98252018-10-22 16:17:20 +0100229void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
230 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100231{
Michele Di Giorgiod24af8a2018-05-08 17:23:52 +0100232 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100233 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arenad051e972018-06-20 11:46:42 +0100234 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100235
giuros016d109962019-01-07 17:47:19 +0000236 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
237 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
238
239 const bool can_run_optimised_3x3_kernel = (weights->info()->dimension(idx_w) == 3) && (weights->info()->dimension(idx_h) == 3);
240
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000241 if(bool(can_run_optimised_3x3_kernel))
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000242 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000243 auto f = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3>();
244 f->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info);
245 _optimised_function = std::move(f);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000246 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000247 else
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000248 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000249 const size_t idx_c = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100250
Pablo Tello8bf622a2018-12-03 15:54:49 +0000251 const size_t weights_w = weights->info()->dimension(idx_w);
252 const size_t weights_h = weights->info()->dimension(idx_h);
253 const size_t weights_z = weights->info()->dimension(idx_c);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100254
Pablo Tello8bf622a2018-12-03 15:54:49 +0000255 _is_prepared = false;
256 _original_weights = weights;
257 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas60e98252018-10-22 16:17:20 +0100258
Pablo Tello8bf622a2018-12-03 15:54:49 +0000259 bool append_bias = (biases != nullptr) && !_is_quantized;
260 const GPUTarget gpu_target = CLScheduler::get().target();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100261
Pablo Tello8bf622a2018-12-03 15:54:49 +0000262 // Calculate output shape
263 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
264
265 // Output auto inizialitation if not yet initialized
266 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
267 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
268
269 // Output width and height
270 const unsigned int conv_w = output_shape[idx_w];
271 const unsigned int conv_h = output_shape[idx_h];
272
273 // Set up intermediate tensors
274 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
275 const size_t conv_size = conv_w * conv_h;
276
277 // Im2Col configuration
278 TensorShape shape_im2col = input->info()->tensor_shape();
279 shape_im2col.set(0, patch_size);
280 shape_im2col.set(1, conv_size);
281 shape_im2col.set(2, weights_z);
282 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
283 _im2col_kernel.set_target(gpu_target);
284 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier);
285 CLScheduler::get().tune_kernel_static(_im2col_kernel);
286
287 // Weights reshape configuration
288 const TensorShape shape_weights_reshape(patch_size, weights_z);
289 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
290 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
291
292 // GEMV configuration
293 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
294 TensorShape shape_v2mm_out = input->info()->tensor_shape();
295 shape_v2mm_out.set(0, conv_size * weights_z);
296 shape_v2mm_out.set(1, 1);
297 shape_v2mm_out.set(2, 1);
298 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
299 _v2mm_kernel.set_target(gpu_target);
300 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
301 CLScheduler::get().tune_kernel_static(_v2mm_kernel);
302 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
303 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
304
305 // Output staged configuration
306 if(_is_quantized)
307 {
308 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
309
310 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
311 int output_multiplier, output_shift;
312 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
313 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
314 _output_reshaped.allocator()->allocate();
315 }
316
317 // Fill borders on inputs
318 PixelValue zero_in(static_cast<int32_t>(0));
319 PixelValue zero_w(static_cast<int32_t>(0));
320 if(_is_quantized)
321 {
322 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
323 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
324 }
325 BorderSize border_size = _v2mm_kernel.border_size();
326 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
327
328 border_size.bottom = 0;
329 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
330
331 // Allocate intermediate tensors
332 _input_reshaped.allocator()->allocate();
333 _v2mm_output.allocator()->allocate();
334
335 //Configure Activation Layer
336 _is_activationlayer_enabled = act_info.enabled();
337
338 if(_is_activationlayer_enabled)
339 {
340 _activationlayer_function.configure(output, nullptr, act_info);
341 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100342 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100343}
344
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100345Status CLDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Georgios Pinitas60e98252018-10-22 16:17:20 +0100346 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100347{
giuros016d109962019-01-07 17:47:19 +0000348 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
349 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
350
351 const bool can_run_optimised_3x3_kernel = (weights->dimension(idx_w) == 3) && (weights->dimension(idx_h) == 3);
352
353 if(can_run_optimised_3x3_kernel)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100354 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000355 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Georgios Pinitas60e98252018-10-22 16:17:20 +0100356
Pablo Tello8bf622a2018-12-03 15:54:49 +0000357 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
358 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(idx_c) * depth_multiplier) != weights->dimension(idx_c));
359
360 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
361 const bool append_bias = (biases != nullptr) && !is_quantized;
362 const TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
363 const size_t weights_w = weights->dimension(idx_w);
364 const size_t weights_h = weights->dimension(idx_h);
365 const size_t weights_z = weights->dimension(idx_c);
366 const unsigned int conv_w = output_shape[idx_w];
367 const unsigned int conv_h = output_shape[idx_h];
368 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
369 const size_t conv_size = conv_w * conv_h;
370
371 TensorShape shape_im2col = input->tensor_shape();
372 shape_im2col.set(0, patch_size);
373 shape_im2col.set(1, conv_size);
374 shape_im2col.set(2, weights_z);
375 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
376 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseIm2ColKernel::validate(input, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier));
377
378 const TensorShape shape_weights_reshape(patch_size, weights_z);
379 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
giuros016d109962019-01-07 17:47:19 +0000380 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayerReshapeWeightsGenericKernel::validate(weights, &weights_reshaped, append_bias ? biases : nullptr));
Pablo Tello8bf622a2018-12-03 15:54:49 +0000381
382 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
383 TensorShape shape_v2mm_out = input->tensor_shape();
384 shape_v2mm_out.set(0, conv_size * weights_z);
385 shape_v2mm_out.set(1, 1);
386 shape_v2mm_out.set(2, 1);
387 TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
388 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
389
390 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
391 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output, conv_w, conv_h));
392
393 if(is_quantized)
394 {
395 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output));
396 }
397
398 // Validate Activation Layer
399 if(act_info.enabled())
400 {
401 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
402 }
403 }
giuros016d109962019-01-07 17:47:19 +0000404 else
405 {
406 CLDepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info);
407 }
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100408 return Status{};
409}
410
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000411void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100412{
Georgios Pinitas72219332018-06-05 14:56:06 +0100413 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000414
Pablo Tello8bf622a2018-12-03 15:54:49 +0000415 if(_optimised_function != nullptr)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000416 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000417 _optimised_function->run();
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000418 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000419 else
Georgios Pinitas60e98252018-10-22 16:17:20 +0100420 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000421 CLScheduler::get().enqueue(_im2col_kernel);
422 CLScheduler::get().enqueue(_v2mm_input_fill_border);
423 CLScheduler::get().enqueue(_v2mm_kernel);
424 CLScheduler::get().enqueue(_vector_to_tensor_kernel);
425 if(_is_quantized)
426 {
427 CLScheduler::get().enqueue(_output_stage_kernel);
428 }
429 if(_is_activationlayer_enabled)
430 {
431 _activationlayer_function.run();
432 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100433 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100434}
Georgios Pinitas72219332018-06-05 14:56:06 +0100435
436void CLDepthwiseConvolutionLayer::prepare()
437{
Pablo Tello8bf622a2018-12-03 15:54:49 +0000438 if(_optimised_function != nullptr)
Georgios Pinitas72219332018-06-05 14:56:06 +0100439 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000440 _optimised_function->prepare();
441 }
442 else
443 {
444 if(!_is_prepared)
445 {
446 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitas72219332018-06-05 14:56:06 +0100447
Pablo Tello8bf622a2018-12-03 15:54:49 +0000448 // Run weights reshaping and mark original weights tensor as unused
449 _weights_reshaped.allocator()->allocate();
450 CLScheduler::get().enqueue(_weights_reshape_kernel);
451 CLScheduler::get().enqueue(_v2mm_weights_fill_border);
452 _original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100453
Pablo Tello8bf622a2018-12-03 15:54:49 +0000454 CLScheduler::get().queue().finish();
455 _is_prepared = true;
456 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100457 }
458}
giuros016d109962019-01-07 17:47:19 +0000459} // namespace arm_compute