blob: c2782aaa89baad07510735181a156b022867354a [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +00002 * Copyright (c) 2017-2018 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
36using namespace arm_compute;
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000037using namespace arm_compute::misc;
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000038using namespace arm_compute::misc::shape_calculator;
Giorgio Arena93a690e2017-08-01 16:09:33 +010039
Georgios Pinitas05045c12018-12-07 18:31:47 +000040CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager)
41 : _memory_group(std::move(memory_manager)), _kernel(nullptr), _border_handler(), _permute_input_to_nchw(), _permute_weights_to_nchw(), _permute_output_to_nhwc(), _permuted_input(),
42 _permuted_weights(), _permuted_output(), _original_weights(nullptr), _needs_permute(false), _is_prepared(false)
Giorgio Arena93a690e2017-08-01 16:09:33 +010043{
44}
45
Giorgio Arena76572242018-04-04 17:44:26 +010046void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
47 ActivationLayerInfo act_info)
Giorgio Arena9fe41442017-08-23 16:36:24 +010048{
Michele Di Giorgio933fe862018-02-19 15:42:12 +000049 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas236bfe72017-11-23 15:59:55 +000050 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena9fe41442017-08-23 16:36:24 +010051
Georgios Pinitas05045c12018-12-07 18:31:47 +000052 const bool is_nhwc = input->info()->data_layout() == DataLayout::NHWC;
53
54 _needs_permute = is_nhwc && (depth_multiplier > 1);
55 _is_prepared = false;
56 _original_weights = weights;
57
58 ICLTensor *input_to_use = input;
59 const ICLTensor *weights_to_use = weights;
60 ICLTensor *output_to_use = output;
61
62 if(_needs_permute)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000063 {
Georgios Pinitas05045c12018-12-07 18:31:47 +000064 _memory_group.manage(&_permuted_input);
65 _memory_group.manage(&_permuted_output);
66
67 // Configure the function to transform the input tensor from NHWC -> NCHW
68 _permute_input_to_nchw.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
69 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
70
71 // Configure the function to transform the weights tensor from HWI -> IHW
72 _permute_weights_to_nchw.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
73 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
74
75 input_to_use = &_permuted_input;
76 weights_to_use = &_permuted_weights;
77 output_to_use = &_permuted_output;
78
Giorgio Arenadfca60b2018-01-31 10:30:59 +000079 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
80 }
Georgios Pinitas05045c12018-12-07 18:31:47 +000081 else if(is_nhwc)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000082 {
83 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NHWCKernel>();
84 }
Georgios Pinitas05045c12018-12-07 18:31:47 +000085 else
86 {
87 _kernel = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3NCHWKernel>();
88 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +000089
Georgios Pinitas05045c12018-12-07 18:31:47 +000090 // Configure kernel
Giorgio Arenadfca60b2018-01-31 10:30:59 +000091 _kernel->set_target(CLScheduler::get().target());
Georgios Pinitas05045c12018-12-07 18:31:47 +000092 _kernel->configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, act_info);
93
94 // Permute output if needed
95 if(_needs_permute)
96 {
97 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
98 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
99 _permute_output_to_nhwc.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
100
101 // Allocate tensors
102 _permuted_input.allocator()->allocate();
103 _permuted_output.allocator()->allocate();
104 }
Diego Lopez Recasfa0add12017-11-28 16:44:52 +0000105
106 // Configure border handler
107 PixelValue &&zero_value(0.f);
108 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
109 {
110 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().offset));
111 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000112 _border_handler.configure(input, _kernel->border_size(), BorderMode::CONSTANT, zero_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100113}
114
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100115Status CLDepthwiseConvolutionLayer3x3::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
116 unsigned int depth_multiplier,
117 ActivationLayerInfo act_info, GPUTarget gpu_target)
118{
119 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodicedff601d2018-08-09 13:28:41 +0100120 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100121
Georgios Pinitas05045c12018-12-07 18:31:47 +0000122 const bool is_nhwc = input->data_layout() == DataLayout::NHWC;
123 const bool needs_permute = is_nhwc && (depth_multiplier > 1);
124
125 if(needs_permute)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100126 {
Georgios Pinitas05045c12018-12-07 18:31:47 +0000127 TensorShape permuted_input_shape = input->tensor_shape();
128 TensorShape permuted_weights_shape = weights->tensor_shape();
129 TensorShape permuted_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
130
131 permute(permuted_input_shape, PermutationVector(1U, 2U, 0U));
132 permute(permuted_weights_shape, PermutationVector(1U, 2U, 0U));
133 permute(permuted_output_shape, PermutationVector(1U, 2U, 0U));
134
135 const TensorInfo permuted_input = input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NCHW);
136 const TensorInfo permuted_weights = weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NCHW);
137 const TensorInfo permuted_output = output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW);
138
139 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NCHWKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, act_info, gpu_target));
140 }
141 else if(is_nhwc)
142 {
143 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info));
144 }
145 else
146 {
147 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 +0100148 }
149
Georgios Pinitas05045c12018-12-07 18:31:47 +0000150 return Status{};
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100151}
152
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000153void CLDepthwiseConvolutionLayer3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +0100154{
Georgios Pinitas05045c12018-12-07 18:31:47 +0000155 prepare();
156
157 _memory_group.acquire();
158
159 if(_needs_permute)
160 {
161 _permute_input_to_nchw.run();
162 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100163 CLScheduler::get().enqueue(_border_handler);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000164 CLScheduler::get().enqueue(*_kernel);
Georgios Pinitas05045c12018-12-07 18:31:47 +0000165
166 if(_needs_permute)
167 {
168 _permute_output_to_nhwc.run();
169 }
170
171 _memory_group.release();
172}
173
174void CLDepthwiseConvolutionLayer3x3::prepare()
175{
176 if(!_is_prepared)
177 {
178 if(_needs_permute)
179 {
180 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
181
182 _permuted_weights.allocator()->allocate();
183 _permute_weights_to_nchw.run();
184 _original_weights->mark_as_unused();
185 }
186 _is_prepared = true;
187 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100188}
189
Pablo Tello8bf622a2018-12-03 15:54:49 +0000190namespace
191{
192inline bool can_run_optimised_3x3_kernel(const ITensorInfo *weights, unsigned int depth_multiplier)
193{
194 const DataLayout data_layout = weights->data_layout();
195 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
196 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
197 const Size2D weights_size(weights->dimension(idx_w), weights->dimension(idx_h));
198 return weights_size == Size2D(3, 3) && (data_layout == DataLayout::NHWC && depth_multiplier <= 1);
199}
200
201} // namespace
202
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000203CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer()
Georgios Pinitas60e98252018-10-22 16:17:20 +0100204 : _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 +0000205 _input_reshaped(), _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_prepared(false), _is_quantized(false), _is_activationlayer_enabled(false), _original_weights(nullptr),
206 _optimised_function(nullptr)
Giorgio Arena9fe41442017-08-23 16:36:24 +0100207{
208}
209
Georgios Pinitas60e98252018-10-22 16:17:20 +0100210void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
211 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100212{
Michele Di Giorgiod24af8a2018-05-08 17:23:52 +0100213 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100214 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arenad051e972018-06-20 11:46:42 +0100215 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100216
Pablo Tello8bf622a2018-12-03 15:54:49 +0000217 if(can_run_optimised_3x3_kernel(weights->info(), depth_multiplier))
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000218 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000219 auto f = arm_compute::support::cpp14::make_unique<CLDepthwiseConvolutionLayer3x3>();
220 f->configure(input, weights, biases, output, conv_info, depth_multiplier, act_info);
221 _optimised_function = std::move(f);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000222 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000223 else
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000224 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000225 const size_t idx_w = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
226 const size_t idx_h = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
227 const size_t idx_c = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100228
Pablo Tello8bf622a2018-12-03 15:54:49 +0000229 const size_t weights_w = weights->info()->dimension(idx_w);
230 const size_t weights_h = weights->info()->dimension(idx_h);
231 const size_t weights_z = weights->info()->dimension(idx_c);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100232
Pablo Tello8bf622a2018-12-03 15:54:49 +0000233 _is_prepared = false;
234 _original_weights = weights;
235 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitas60e98252018-10-22 16:17:20 +0100236
Pablo Tello8bf622a2018-12-03 15:54:49 +0000237 bool append_bias = (biases != nullptr) && !_is_quantized;
238 const GPUTarget gpu_target = CLScheduler::get().target();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100239
Pablo Tello8bf622a2018-12-03 15:54:49 +0000240 // Calculate output shape
241 TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
242
243 // Output auto inizialitation if not yet initialized
244 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
245 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
246
247 // Output width and height
248 const unsigned int conv_w = output_shape[idx_w];
249 const unsigned int conv_h = output_shape[idx_h];
250
251 // Set up intermediate tensors
252 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
253 const size_t conv_size = conv_w * conv_h;
254
255 // Im2Col configuration
256 TensorShape shape_im2col = input->info()->tensor_shape();
257 shape_im2col.set(0, patch_size);
258 shape_im2col.set(1, conv_size);
259 shape_im2col.set(2, weights_z);
260 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
261 _im2col_kernel.set_target(gpu_target);
262 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier);
263 CLScheduler::get().tune_kernel_static(_im2col_kernel);
264
265 // Weights reshape configuration
266 const TensorShape shape_weights_reshape(patch_size, weights_z);
267 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
268 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
269
270 // GEMV configuration
271 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
272 TensorShape shape_v2mm_out = input->info()->tensor_shape();
273 shape_v2mm_out.set(0, conv_size * weights_z);
274 shape_v2mm_out.set(1, 1);
275 shape_v2mm_out.set(2, 1);
276 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
277 _v2mm_kernel.set_target(gpu_target);
278 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
279 CLScheduler::get().tune_kernel_static(_v2mm_kernel);
280 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
281 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
282
283 // Output staged configuration
284 if(_is_quantized)
285 {
286 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
287
288 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
289 int output_multiplier, output_shift;
290 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
291 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
292 _output_reshaped.allocator()->allocate();
293 }
294
295 // Fill borders on inputs
296 PixelValue zero_in(static_cast<int32_t>(0));
297 PixelValue zero_w(static_cast<int32_t>(0));
298 if(_is_quantized)
299 {
300 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
301 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
302 }
303 BorderSize border_size = _v2mm_kernel.border_size();
304 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
305
306 border_size.bottom = 0;
307 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
308
309 // Allocate intermediate tensors
310 _input_reshaped.allocator()->allocate();
311 _v2mm_output.allocator()->allocate();
312
313 //Configure Activation Layer
314 _is_activationlayer_enabled = act_info.enabled();
315
316 if(_is_activationlayer_enabled)
317 {
318 _activationlayer_function.configure(output, nullptr, act_info);
319 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100320 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100321}
322
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100323Status 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 +0100324 unsigned int depth_multiplier, const ActivationLayerInfo &act_info)
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100325{
Pablo Tello8bf622a2018-12-03 15:54:49 +0000326 if(can_run_optimised_3x3_kernel(weights, depth_multiplier))
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100327 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000328 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseConvolutionLayer3x3::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info));
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100329 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000330 else
Georgios Pinitas60e98252018-10-22 16:17:20 +0100331 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000332 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
333 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
334 const size_t idx_c = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
Georgios Pinitas60e98252018-10-22 16:17:20 +0100335
Pablo Tello8bf622a2018-12-03 15:54:49 +0000336 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
337 ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(idx_c) * depth_multiplier) != weights->dimension(idx_c));
338
339 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
340 const bool append_bias = (biases != nullptr) && !is_quantized;
341 const TensorShape output_shape = shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier);
342 const size_t weights_w = weights->dimension(idx_w);
343 const size_t weights_h = weights->dimension(idx_h);
344 const size_t weights_z = weights->dimension(idx_c);
345 const unsigned int conv_w = output_shape[idx_w];
346 const unsigned int conv_h = output_shape[idx_h];
347 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
348 const size_t conv_size = conv_w * conv_h;
349
350 TensorShape shape_im2col = input->tensor_shape();
351 shape_im2col.set(0, patch_size);
352 shape_im2col.set(1, conv_size);
353 shape_im2col.set(2, weights_z);
354 TensorInfo input_reshaped(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
355 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseIm2ColKernel::validate(input, &input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias, depth_multiplier));
356
357 const TensorShape shape_weights_reshape(patch_size, weights_z);
358 TensorInfo weights_reshaped(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
359 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseWeightsReshapeKernel::validate(weights, &weights_reshaped, append_bias ? biases : nullptr));
360
361 DataType v2mm_dt = (input->data_type() == DataType::QASYMM8) ? DataType::S32 : input->data_type();
362 TensorShape shape_v2mm_out = input->tensor_shape();
363 shape_v2mm_out.set(0, conv_size * weights_z);
364 shape_v2mm_out.set(1, 1);
365 shape_v2mm_out.set(2, 1);
366 TensorInfo v2mm_output(input->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
367 ARM_COMPUTE_RETURN_ON_ERROR(CLGEMMMatrixVectorMultiplyKernel::validate(&input_reshaped, &weights_reshaped, &v2mm_output));
368
369 TensorInfo output_reshaped(v2mm_output.clone()->set_is_resizable(true).reset_padding().set_tensor_shape(output_shape));
370 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthwiseVectorToTensorKernel::validate(&v2mm_output, (is_quantized) ? &output_reshaped : output, conv_w, conv_h));
371
372 if(is_quantized)
373 {
374 ARM_COMPUTE_RETURN_ON_ERROR(CLDirectConvolutionLayerOutputStageKernel::validate(&output_reshaped, biases, output));
375 }
376
377 // Validate Activation Layer
378 if(act_info.enabled())
379 {
380 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
381 }
382 }
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100383 return Status{};
384}
385
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000386void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100387{
Georgios Pinitas72219332018-06-05 14:56:06 +0100388 prepare();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000389
Pablo Tello8bf622a2018-12-03 15:54:49 +0000390 if(_optimised_function != nullptr)
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000391 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000392 _optimised_function->run();
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000393 }
Pablo Tello8bf622a2018-12-03 15:54:49 +0000394 else
Georgios Pinitas60e98252018-10-22 16:17:20 +0100395 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000396 CLScheduler::get().enqueue(_im2col_kernel);
397 CLScheduler::get().enqueue(_v2mm_input_fill_border);
398 CLScheduler::get().enqueue(_v2mm_kernel);
399 CLScheduler::get().enqueue(_vector_to_tensor_kernel);
400 if(_is_quantized)
401 {
402 CLScheduler::get().enqueue(_output_stage_kernel);
403 }
404 if(_is_activationlayer_enabled)
405 {
406 _activationlayer_function.run();
407 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100408 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100409}
Georgios Pinitas72219332018-06-05 14:56:06 +0100410
411void CLDepthwiseConvolutionLayer::prepare()
412{
Pablo Tello8bf622a2018-12-03 15:54:49 +0000413 if(_optimised_function != nullptr)
Georgios Pinitas72219332018-06-05 14:56:06 +0100414 {
Pablo Tello8bf622a2018-12-03 15:54:49 +0000415 _optimised_function->prepare();
416 }
417 else
418 {
419 if(!_is_prepared)
420 {
421 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitas72219332018-06-05 14:56:06 +0100422
Pablo Tello8bf622a2018-12-03 15:54:49 +0000423 // Run weights reshaping and mark original weights tensor as unused
424 _weights_reshaped.allocator()->allocate();
425 CLScheduler::get().enqueue(_weights_reshape_kernel);
426 CLScheduler::get().enqueue(_v2mm_weights_fill_border);
427 _original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100428
Pablo Tello8bf622a2018-12-03 15:54:49 +0000429 CLScheduler::get().queue().finish();
430 _is_prepared = true;
431 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100432 }
433}