blob: beb024c529e74aea44cfe9939730fa2d0eacbdf5 [file] [log] [blame]
Michalis Spyrou7362f0d2017-10-18 17:58:22 +01001/*
Michele Di Giorgio45361932019-12-19 13:53:44 +00002 * Copyright (c) 2017-2020 ARM Limited.
Michalis Spyrou7362f0d2017-10-18 17:58:22 +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/NEON/functions/NEDepthwiseConvolutionLayer.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010025
Giorgio Arenad93e2632019-10-15 11:09:33 +010026#include "arm_compute/core/utils/misc/InfoHelpers.h"
Georgios Pinitasd05dce42018-01-22 16:29:17 +000027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasf72f9362018-01-12 16:29:45 +000028#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010029#include "arm_compute/runtime/NEON/NEScheduler.h"
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000030
Georgios Pinitasd05dce42018-01-22 16:29:17 +000031using namespace arm_compute::misc;
Georgios Pinitas4074c992018-01-30 18:13:46 +000032using namespace arm_compute::misc::shape_calculator;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010033
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000034namespace arm_compute
35{
Manuel Bottini05069f02019-09-26 17:18:26 +010036namespace
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010037{
Manuel Bottini05069f02019-09-26 17:18:26 +010038Status validate_arguments_optimized(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
39 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Georgios Pinitas30271c72019-06-24 14:56:34 +010040{
41 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Michele Di Giorgio13ec5f02020-01-02 12:11:13 +000042 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Giuseppe Rossinif01201a2019-11-06 14:57:49 +000043 if(!is_data_type_quantized_per_channel(weights->data_type()))
44 {
45 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
46 }
Georgios Pinitas30271c72019-06-24 14:56:34 +010047 ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
48 ARM_COMPUTE_RETURN_ERROR_ON(dilation.x() < 1 || dilation.y() < 1);
49 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
50 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
51 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_w) + (weights->dimension(idx_w) - 1) * (dilation.x() - 1) > input->dimension(idx_w) + conv_info.pad_left() + conv_info.pad_right());
52 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_h) + (weights->dimension(idx_h) - 1) * (dilation.y() - 1) > input->dimension(idx_h) + conv_info.pad_top() + conv_info.pad_bottom());
53
54 if(biases != nullptr)
55 {
56 const unsigned int channel_idx = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL);
57 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
58 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(channel_idx));
59 }
60
Giuseppe Rossinif01201a2019-11-06 14:57:49 +000061 const bool is_quantized = (!is_data_type_quantized_per_channel(weights->data_type())) && is_data_type_quantized_asymmetric(input->data_type());
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +010062
Georgios Pinitas30271c72019-06-24 14:56:34 +010063 if(!NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input, weights, conv_info, depth_multiplier, dilation))
64 {
Michele Di Giorgio601ba3f2019-08-22 16:20:04 +010065 TensorInfo accumulator = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
Georgios Pinitas30271c72019-06-24 14:56:34 +010066 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayer3x3Kernel::validate(input, weights, is_quantized ? &accumulator : output, conv_info, depth_multiplier, dilation));
67
68 if(is_quantized)
69 {
Michele Di Giorgio45361932019-12-19 13:53:44 +000070 DirectConvolutionLayerOutputStageKernelInfo direct_conv_info;
71 direct_conv_info.output_data_type = input->data_type();
72 ARM_COMPUTE_RETURN_ON_ERROR(NEDirectConvolutionLayerOutputStageKernel::validate(&accumulator, biases, output, direct_conv_info));
Georgios Pinitas30271c72019-06-24 14:56:34 +010073 }
74 }
75 else
76 {
77 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionAssemblyDispatch::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation));
78 }
79
80 //Validate Activation Layer
81 if(act_info.enabled())
82 {
83 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
84 }
Georgios Pinitas30271c72019-06-24 14:56:34 +010085 return Status{};
86}
Manuel Bottini05069f02019-09-26 17:18:26 +010087} // namespace
Georgios Pinitas30271c72019-06-24 14:56:34 +010088
Manuel Bottini05069f02019-09-26 17:18:26 +010089NEDepthwiseConvolutionLayerOptimized::NEDepthwiseConvolutionLayerOptimized(std::shared_ptr<IMemoryManager> memory_manager)
90 : _func(std::move(memory_manager))
91{
92}
93
94void NEDepthwiseConvolutionLayerOptimized::configure(ITensor *input,
95 const ITensor *weights,
96 const ITensor *biases,
97 ITensor *output, const PadStrideInfo &conv_info,
98 unsigned int depth_multiplier,
99 const ActivationLayerInfo &act_info,
100 const Size2D &dilation)
101{
102 _func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
103}
104
105Status NEDepthwiseConvolutionLayerOptimized::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
106 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
107{
108 return validate_arguments_optimized(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
109}
110
111void NEDepthwiseConvolutionLayerOptimized::run()
112{
113 _func.run();
114}
115
116void NEDepthwiseConvolutionLayerOptimized::prepare()
117{
118 _func.prepare();
119}
120
121NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::NEDepthwiseConvolutionLayerOptimizedInternal(std::shared_ptr<IMemoryManager> memory_manager)
122 : _memory_group(memory_manager), _dwc_kernel(), _dwc_optimized_func(memory_manager), _output_stage_kernel(), _border_handler(), _permute_input(), _permute_weights(), _permute_output(),
123 _activationlayer_function(), _accumulator(), _permuted_input(), _permuted_weights(), _permuted_output(), _original_weights(nullptr), _has_bias(false), _is_quantized(false), _is_optimized(false),
124 _is_nchw(true), _permute(false), _is_activationlayer_enabled(false), _is_prepared(false)
125{
126}
127
128void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::configure_generic(ITensor *input,
129 const ITensor *weights,
130 const ITensor *biases,
131 ITensor *output,
132 const PadStrideInfo &conv_info,
133 unsigned int depth_multiplier,
134 const ActivationLayerInfo &act_info,
135 const Size2D &dilation)
136{
137 ARM_COMPUTE_UNUSED(act_info);
138
139 PixelValue zero_value(0.f);
140
141 // Initialize the intermediate accumulator tensor in case of quantized input
142 if(_is_quantized)
143 {
144 TensorShape accum_shape = output->info()->tensor_shape();
145 DataLayout accum_layout = output->info()->data_layout();
146 if(!_is_nchw)
147 {
148 permute(accum_shape, PermutationVector(1U, 2U, 0U));
149 accum_layout = DataLayout::NCHW;
150 }
151
152 _memory_group.manage(&_accumulator);
153 _accumulator.allocator()->init(TensorInfo(accum_shape, 1, DataType::S32, output->info()->quantization_info()));
154 _accumulator.info()->set_data_layout(accum_layout);
155 zero_value = PixelValue(static_cast<uint32_t>(input->info()->quantization_info().uniform().offset));
156 }
157
158 if(!_is_nchw)
159 {
160 _memory_group.manage(&_permuted_input);
161 _memory_group.manage(&_permuted_output);
162
163 // Configure the function to transform the input tensor from NHWC -> NCHW
164 _permute_input.configure(input, &_permuted_input, PermutationVector(1U, 2U, 0U));
165 _permuted_input.info()->set_data_layout(DataLayout::NCHW);
166
167 // Configure the function to transform the weights tensor from HWI -> IHW
168 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(1U, 2U, 0U));
169 _permuted_weights.info()->set_data_layout(DataLayout::NCHW);
170 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
171
172 // Configure depthwise
173 _dwc_kernel.configure(&_permuted_input, &_permuted_weights, (_is_quantized) ? &_accumulator : &_permuted_output, conv_info, depth_multiplier, dilation);
174
175 // Configure border handler
176 _border_handler.configure(&_permuted_input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
177
178 // Allocate tensors
179 _permuted_input.allocator()->allocate();
180 }
181 else
182 {
183 // Configure depthwise convolution kernel
184 _dwc_kernel.configure(input, weights, (_is_quantized) ? &_accumulator : output, conv_info, depth_multiplier, dilation);
185
186 // Configure border handler
187 _border_handler.configure(input, _dwc_kernel.border_size(), BorderMode::CONSTANT, zero_value);
188 }
189
190 // Configure biases accumulation
191 if(_is_quantized)
192 {
193 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
194 const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform();
195 const UniformQuantizationInfo oq_info = (output->info()->total_size() == 0) ? iq_info : output->info()->quantization_info().uniform();
196
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000197 float multiplier = (iq_info.scale * wq_info.scale) / oq_info.scale;
198 int32_t output_multiplier;
199 int32_t output_shift;
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000200 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Michele Di Giorgio45361932019-12-19 13:53:44 +0000201
202 DirectConvolutionLayerOutputStageKernelInfo direct_conv_info;
203 direct_conv_info.result_fixedpoint_multiplier = output_multiplier;
204 direct_conv_info.result_shift = output_shift;
205 direct_conv_info.result_offset_after_shift = oq_info.offset;
206 direct_conv_info.output_data_type = input->info()->data_type();
207 _output_stage_kernel.configure(&_accumulator, biases, _is_nchw ? output : &_permuted_output, direct_conv_info);
Manuel Bottini05069f02019-09-26 17:18:26 +0100208 _accumulator.allocator()->allocate();
209 }
210 else if(_has_bias)
211 {
212 _output_stage_kernel.configure(_is_nchw ? output : &_permuted_output, biases);
213 }
214
215 // Permute output
216 if(!_is_nchw)
217 {
218 // Configure the function to transform the convoluted output to NHWC
219 _permute_output.configure(&_permuted_output, output, PermutationVector(2U, 0U, 1U));
220 _permuted_output.allocator()->allocate();
221 }
222}
223
224void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::configure_optimized(const ITensor *input,
225 const ITensor *weights,
226 const ITensor *biases,
227 ITensor *output,
228 const PadStrideInfo &conv_info,
229 unsigned int depth_multiplier,
230 const ActivationLayerInfo &act_info,
231 const Size2D &dilation)
232{
233 ActivationLayerInfo act_info_to_use = ActivationLayerInfo();
234 const bool is_relu = arm_compute::utils::info_helpers::is_relu(act_info);
235 const bool is_relu6 = arm_compute::utils::info_helpers::is_relu6(act_info);
236 _is_activationlayer_enabled = act_info.enabled() && !(is_relu || is_relu6);
237 if(!_is_activationlayer_enabled)
238 {
239 act_info_to_use = act_info;
240 }
241
242 if(_is_nchw)
243 {
244 _memory_group.manage(&_permuted_input);
245 _memory_group.manage(&_permuted_output);
246
247 // Configure the function to transform the input tensor from NCHW -> NHWC
248 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
249 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
250
251 // Configure the function to transform the weights tensor from IHW -> HWI
252 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
253 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
254
255 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
256 _permuted_output.info()->set_quantization_info(output->info()->quantization_info());
257
258 // Configure optimized depthwise
259 _dwc_optimized_func.configure(&_permuted_input, &_permuted_weights, biases, &_permuted_output, conv_info, depth_multiplier, act_info_to_use, dilation);
260
261 // Configure the function to transform the convoluted output to ACL's native ordering format NCHW
262 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
263 _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
264
265 // Allocate tensors
266 _permuted_input.allocator()->allocate();
267 _permuted_output.allocator()->allocate();
268 }
269 else
270 {
271 _dwc_optimized_func.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info_to_use, dilation);
272 }
273}
274
275void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::configure(ITensor *input,
276 const ITensor *weights,
277 const ITensor *biases,
278 ITensor *output, const PadStrideInfo &conv_info,
279 unsigned int depth_multiplier,
280 const ActivationLayerInfo &act_info,
281 const Size2D &dilation)
282{
283 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
284 // Perform validation step
285 ARM_COMPUTE_ERROR_THROW_ON(NEDepthwiseConvolutionLayerOptimizedInternal::validate(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(),
286 output->info(), conv_info, depth_multiplier, act_info, dilation));
287
288 _original_weights = weights;
289 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
290 _has_bias = biases != nullptr;
291 _is_optimized = NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(input->info(),
292 weights->info(),
293 conv_info,
294 depth_multiplier,
295 dilation);
296 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
297 _permute = _is_optimized == _is_nchw;
298 _is_prepared = false;
299 _is_activationlayer_enabled = act_info.enabled();
300
301 // Configure appropriate pipeline
302 if(_is_optimized)
303 {
304 configure_optimized(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
305 }
306 else
307 {
308 configure_generic(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
309 }
310
311 // Configure activation
312 if(_is_activationlayer_enabled)
313 {
314 _activationlayer_function.configure(output, nullptr, act_info);
315 }
316}
317
318Status NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::validate(const ITensorInfo *input,
319 const ITensorInfo *weights,
320 const ITensorInfo *biases,
321 const ITensorInfo *output,
322 const PadStrideInfo &conv_info,
323 unsigned int depth_multiplier,
324 const ActivationLayerInfo &act_info,
325 const Size2D &dilation)
326{
327 return validate_arguments_optimized(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
328}
329
330void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::run_generic()
Georgios Pinitas30271c72019-06-24 14:56:34 +0100331{
332 // Fill border
333 NEScheduler::get().schedule(&_border_handler, Window::DimX);
334
335 // Execute depthwise convolution
336 NEScheduler::get().schedule(&_dwc_kernel, Window::DimX);
337
338 // Add biases
339 if(_has_bias || _is_quantized)
340 {
341 NEScheduler::get().schedule(&_output_stage_kernel, Window::DimX);
342 }
343
344 // Permute output
345 if(!_is_nchw)
346 {
347 _permute_output.run();
348 }
349}
350
Manuel Bottini05069f02019-09-26 17:18:26 +0100351void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::run_optimized()
Georgios Pinitas30271c72019-06-24 14:56:34 +0100352{
353 // Run assembly function
354 _dwc_optimized_func.run();
355
356 // Permute output
357 if(_is_nchw)
358 {
359 _permute_output.run();
360 }
361}
362
Manuel Bottini05069f02019-09-26 17:18:26 +0100363void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::run()
Georgios Pinitas30271c72019-06-24 14:56:34 +0100364{
365 prepare();
366
367 MemoryGroupResourceScope scope_mg(_memory_group);
368
369 // Permute input
370 if(_permute)
371 {
372 _permute_input.run();
373 }
374
375 _is_optimized ? run_optimized() : run_generic();
376
377 // Run activation
378 if(_is_activationlayer_enabled)
379 {
380 _activationlayer_function.run();
381 }
382}
383
Manuel Bottini05069f02019-09-26 17:18:26 +0100384void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerOptimizedInternal::prepare()
Georgios Pinitas30271c72019-06-24 14:56:34 +0100385{
386 if(!_is_prepared)
387 {
388 // Permute weights
389 if(_permute)
390 {
391 _permuted_weights.allocator()->allocate();
392 _permute_weights.run();
393 _original_weights->mark_as_unused();
394 }
395
396 // Prepare optimized function
397 if(_is_optimized)
398 {
399 _dwc_optimized_func.prepare();
400 if(!_permuted_weights.is_used())
401 {
402 _permuted_weights.allocator()->free();
403 }
404 }
405
406 _is_prepared = true;
407 }
408}
409
Manuel Bottini05069f02019-09-26 17:18:26 +0100410NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::NEDepthwiseConvolutionLayerGeneric()
Giorgio Arenad93e2632019-10-15 11:09:33 +0100411 : _depthwise_conv_kernel(), _fill_border(), _permute_input(), _permute_weights(), _permute_output(), _activationlayer_function(), _permuted_input(), _permuted_weights(), _permuted_output(),
412 _is_prepared(false), _is_nchw(false), _is_activationlayer_enabled(false), _original_weights(nullptr)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000413{
414}
415
Manuel Bottini05069f02019-09-26 17:18:26 +0100416void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
417 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Michalis Spyroub7b31532017-11-23 12:10:21 +0000418{
Michele Di Giorgioff271922019-07-17 15:59:32 +0100419 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Michele Di Giorgioff271922019-07-17 15:59:32 +0100420 ARM_COMPUTE_ERROR_THROW_ON(NEDepthwiseConvolutionLayer::validate(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(),
421 output->info(), conv_info, depth_multiplier, act_info, dilation));
Michalis Spyroub7b31532017-11-23 12:10:21 +0000422
Giorgio Arenad93e2632019-10-15 11:09:33 +0100423 _is_nchw = input->info()->data_layout() == DataLayout::NCHW;
424 _is_prepared = !_is_nchw;
Giorgio Arena26b22162018-08-13 15:49:49 +0100425
Giorgio Arenad93e2632019-10-15 11:09:33 +0100426 ITensor *input_to_use = input;
427 const ITensor *weights_to_use = weights;
428 ITensor *output_to_use = output;
429 if(_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100430 {
Giorgio Arenad93e2632019-10-15 11:09:33 +0100431 _permute_input.configure(input, &_permuted_input, PermutationVector(2U, 0U, 1U));
432 _permuted_input.info()->set_data_layout(DataLayout::NHWC);
433 input_to_use = &_permuted_input;
Giorgio Arena26b22162018-08-13 15:49:49 +0100434
Giorgio Arenad93e2632019-10-15 11:09:33 +0100435 _permute_weights.configure(weights, &_permuted_weights, PermutationVector(2U, 0U, 1U));
436 _permuted_weights.info()->set_data_layout(DataLayout::NHWC);
437 weights_to_use = &_permuted_weights;
Giorgio Arena44f55722019-07-12 14:49:49 +0100438
Giorgio Arenad93e2632019-10-15 11:09:33 +0100439 _permuted_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(TensorShape()));
440 output_to_use = &_permuted_output;
Giorgio Arena26b22162018-08-13 15:49:49 +0100441 }
Giorgio Arenad93e2632019-10-15 11:09:33 +0100442 _original_weights = weights_to_use;
Giorgio Arena44f55722019-07-12 14:49:49 +0100443
Giorgio Arenad93e2632019-10-15 11:09:33 +0100444 _depthwise_conv_kernel.configure(input_to_use, weights_to_use, biases, output_to_use, conv_info, depth_multiplier, dilation);
445 _fill_border.configure(input_to_use, _depthwise_conv_kernel.border_size(), BorderMode::CONSTANT, PixelValue(static_cast<uint64_t>(0), input->info()->data_type(), input->info()->quantization_info()));
446
447 if(_is_nchw)
448 {
449 _permute_output.configure(&_permuted_output, output, PermutationVector(1U, 2U, 0U));
450 _permuted_output.info()->set_data_layout(DataLayout::NHWC);
451
452 _permuted_input.allocator()->allocate();
453 _permuted_weights.allocator()->allocate();
454 _permuted_output.allocator()->allocate();
Giorgio Arena26b22162018-08-13 15:49:49 +0100455 }
456
Georgios Pinitas60e98252018-10-22 16:17:20 +0100457 //Configure Activation Layer
458 _is_activationlayer_enabled = act_info.enabled();
Georgios Pinitas60e98252018-10-22 16:17:20 +0100459 if(_is_activationlayer_enabled)
460 {
461 _activationlayer_function.configure(output, nullptr, act_info);
462 }
Michalis Spyroub7b31532017-11-23 12:10:21 +0000463}
464
Manuel Bottini05069f02019-09-26 17:18:26 +0100465Status NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
466 const PadStrideInfo &conv_info,
467 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Abe Mbise7784c832018-05-31 16:48:41 +0100468{
469 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
Giorgio Arenad93e2632019-10-15 11:09:33 +0100470 if(input->data_layout() == DataLayout::NCHW)
Giorgio Arena26b22162018-08-13 15:49:49 +0100471 {
Giorgio Arena44f55722019-07-12 14:49:49 +0100472 TensorShape permuted_input_shape = input->tensor_shape();
473 TensorShape permuted_weights_shape = weights->tensor_shape();
Giorgio Arenad93e2632019-10-15 11:09:33 +0100474 TensorShape permuted_output_shape = misc::shape_calculator::compute_depthwise_convolution_shape(*input, *weights, conv_info, depth_multiplier, dilation);
475 permute(permuted_input_shape, PermutationVector(2U, 0U, 1U));
476 permute(permuted_weights_shape, PermutationVector(2U, 0U, 1U));
477 permute(permuted_output_shape, PermutationVector(2U, 0U, 1U));
Giorgio Arena44f55722019-07-12 14:49:49 +0100478
Giorgio Arenad93e2632019-10-15 11:09:33 +0100479 const TensorInfo permuted_input = TensorInfo(input->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_input_shape).set_data_layout(DataLayout::NHWC));
480 const TensorInfo permuted_weights = TensorInfo(weights->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_weights_shape).set_data_layout(DataLayout::NHWC));
481 const TensorInfo permuted_output = TensorInfo(output->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(permuted_output_shape).set_data_layout(DataLayout::NCHW));
Giorgio Arena44f55722019-07-12 14:49:49 +0100482
Giorgio Arenad93e2632019-10-15 11:09:33 +0100483 ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(input, &permuted_input, PermutationVector(2U, 0U, 1U)));
484 ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(weights, &permuted_weights, PermutationVector(2U, 0U, 1U)));
485 ARM_COMPUTE_RETURN_ON_ERROR(NEPermute::validate(&permuted_output, output, PermutationVector(1U, 2U, 0U)));
Giorgio Arena44f55722019-07-12 14:49:49 +0100486
Giorgio Arenad93e2632019-10-15 11:09:33 +0100487 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayerNativeKernel::validate(&permuted_input, &permuted_weights, biases, &permuted_output, conv_info, depth_multiplier, dilation));
Giorgio Arena26b22162018-08-13 15:49:49 +0100488 }
Giorgio Arena44f55722019-07-12 14:49:49 +0100489 else
Giorgio Arena26b22162018-08-13 15:49:49 +0100490 {
Gian Marco Iodicebd9097d2019-07-26 15:31:02 +0100491 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthwiseConvolutionLayerNativeKernel::validate(input, weights, biases, output, conv_info, depth_multiplier, dilation));
Abe Mbise7784c832018-05-31 16:48:41 +0100492 }
493
Georgios Pinitas60e98252018-10-22 16:17:20 +0100494 // Validate Activation Layer
495 if(act_info.enabled())
496 {
497 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
498 }
499
Abe Mbise7784c832018-05-31 16:48:41 +0100500 return Status{};
501}
502
Manuel Bottini05069f02019-09-26 17:18:26 +0100503void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::run()
Michalis Spyroub7b31532017-11-23 12:10:21 +0000504{
Giorgio Arenad93e2632019-10-15 11:09:33 +0100505 if(_is_nchw)
Giorgio Arena26b22162018-08-13 15:49:49 +0100506 {
Giorgio Arena44f55722019-07-12 14:49:49 +0100507 prepare();
Giorgio Arenad93e2632019-10-15 11:09:33 +0100508 _permute_input.run();
Giorgio Arena26b22162018-08-13 15:49:49 +0100509 }
Giorgio Arenad93e2632019-10-15 11:09:33 +0100510
511 NEScheduler::get().schedule(&_fill_border, Window::DimX);
512 NEScheduler::get().schedule(&_depthwise_conv_kernel, Window::DimY);
513
514 if(_is_nchw)
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000515 {
Giorgio Arenad93e2632019-10-15 11:09:33 +0100516 _permute_output.run();
Giorgio Arena26b22162018-08-13 15:49:49 +0100517 }
Georgios Pinitas60e98252018-10-22 16:17:20 +0100518
519 if(_is_activationlayer_enabled)
520 {
521 _activationlayer_function.run();
522 }
Anthony Barbierfb8dda22018-01-30 09:27:05 +0000523}
Georgios Pinitas72219332018-06-05 14:56:06 +0100524
Manuel Bottini05069f02019-09-26 17:18:26 +0100525void NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayerGeneric::prepare()
Georgios Pinitas72219332018-06-05 14:56:06 +0100526{
Giorgio Arenad93e2632019-10-15 11:09:33 +0100527 if(!_is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100528 {
529 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
530
Giorgio Arenad93e2632019-10-15 11:09:33 +0100531 _permute_weights.run();
Georgios Pinitas72219332018-06-05 14:56:06 +0100532 _original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100533 _is_prepared = true;
534 }
535}
Manuel Bottini05069f02019-09-26 17:18:26 +0100536
537NEDepthwiseConvolutionLayer::NEDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
538 : _depth_conv_func(DepthwiseConvolutionFunction::GENERIC), _func_optimized(std::move(memory_manager)), _func_generic()
539{
540}
541
542void NEDepthwiseConvolutionLayer::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier,
543 const ActivationLayerInfo &act_info, const Size2D &dilation)
544{
545 _depth_conv_func = get_depthwiseconvolution_function(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), conv_info, depth_multiplier, act_info, dilation);
546 switch(_depth_conv_func)
547 {
548 case DepthwiseConvolutionFunction::OPTIMIZED:
549 _func_optimized.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
550 break;
551 case DepthwiseConvolutionFunction::GENERIC:
552 _func_generic.configure(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
553 break;
554 default:
555 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
556 }
557}
558
559Status NEDepthwiseConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
560 unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
561{
562 DepthwiseConvolutionFunction depth_conv_func = get_depthwiseconvolution_function(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
563 switch(depth_conv_func)
564 {
565 case DepthwiseConvolutionFunction::OPTIMIZED:
566 return NEDepthwiseConvolutionLayerOptimized::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
567 break;
568 case DepthwiseConvolutionFunction::GENERIC:
569 return NEDepthwiseConvolutionLayerGeneric::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
570 break;
571 default:
572 ARM_COMPUTE_ERROR("Unsupported DepthwiseConvolutionFunction");
573 }
574}
575
576DepthwiseConvolutionFunction NEDepthwiseConvolutionLayer::get_depthwiseconvolution_function(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
577 const PadStrideInfo &conv_info,
578 unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
579{
580 if(bool(NEDepthwiseConvolutionLayerOptimized::validate(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation)))
581 {
582 return DepthwiseConvolutionFunction::OPTIMIZED;
583 }
584 else
585 {
586 return DepthwiseConvolutionFunction::GENERIC;
587 }
588}
589
590void NEDepthwiseConvolutionLayer::run()
591{
592 switch(_depth_conv_func)
593 {
594 case DepthwiseConvolutionFunction::OPTIMIZED:
595 _func_optimized.run();
596 break;
597 case DepthwiseConvolutionFunction::GENERIC:
598 _func_generic.run();
599 break;
600 default:
601 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
602 }
603}
604
605void NEDepthwiseConvolutionLayer::prepare()
606{
607 switch(_depth_conv_func)
608 {
609 case DepthwiseConvolutionFunction::OPTIMIZED:
610 _func_optimized.prepare();
611 break;
612 case DepthwiseConvolutionFunction::GENERIC:
613 _func_generic.prepare();
614 break;
615 default:
616 ARM_COMPUTE_ERROR("DepthwiseConvolutionFunction not properly configured");
617 }
618}
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000619} // namespace arm_compute