blob: 8d7c92bdf191c2fb7ea76b76a2337c355e68bc13 [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"
27#include "arm_compute/core/PixelValue.h"
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010030#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "support/ToolchainSupport.h"
32
33using namespace arm_compute;
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000034using namespace arm_compute::misc;
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000035using namespace arm_compute::misc::shape_calculator;
Giorgio Arena93a690e2017-08-01 16:09:33 +010036
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000037CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3()
Giorgio Arena93a690e2017-08-01 16:09:33 +010038 : _kernel(), _border_handler()
39{
40}
41
Giorgio Arena99ac60b2018-02-16 15:17:23 +000042void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, ActivationLayerInfo act_info)
Giorgio Arena9fe41442017-08-23 16:36:24 +010043{
Michele Di Giorgio933fe862018-02-19 15:42:12 +000044 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitas236bfe72017-11-23 15:59:55 +000045 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Giorgio Arena9fe41442017-08-23 16:36:24 +010046
Anthony Barbiera2ea7532017-11-28 10:33:22 +000047 _kernel.set_target(CLScheduler::get().target());
Giorgio Arena99ac60b2018-02-16 15:17:23 +000048 _kernel.configure(input, weights, biases, output, conv_info, act_info);
Diego Lopez Recasfa0add12017-11-28 16:44:52 +000049
50 // Configure border handler
51 PixelValue &&zero_value(0.f);
52 if(is_data_type_quantized_asymmetric(input->info()->data_type()))
53 {
54 zero_value = PixelValue(static_cast<uint8_t>(input->info()->quantization_info().offset));
55 }
56 _border_handler.configure(input, _kernel.border_size(), BorderMode::CONSTANT, zero_value);
Giorgio Arena9fe41442017-08-23 16:36:24 +010057}
58
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000059void CLDepthwiseConvolutionLayer3x3::run()
Giorgio Arena9fe41442017-08-23 16:36:24 +010060{
61 CLScheduler::get().enqueue(_border_handler);
62 CLScheduler::get().enqueue(_kernel);
63}
64
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000065CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer()
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000066 : _im2col_kernel(), _weights_reshape_kernel(), _v2mm_kernel(), _vector_to_tensor_kernel(), _output_stage_kernel(), _v2mm_input_fill_border(), _v2mm_weights_fill_border(), _input_reshaped(),
Georgios Pinitas1562be32018-03-08 19:09:19 +000067 _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_first_run(true), _is_quantized(false), _original_weights(nullptr)
Giorgio Arena9fe41442017-08-23 16:36:24 +010068{
69}
70
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000071void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010072{
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000073 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Giorgio Arena9fe41442017-08-23 16:36:24 +010074 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
75 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != weights->info()->dimension(2));
Giorgio Arena93a690e2017-08-01 16:09:33 +010076
Giorgio Arena9fe41442017-08-23 16:36:24 +010077 const size_t weights_w = weights->info()->dimension(0);
78 const size_t weights_h = weights->info()->dimension(1);
79 const size_t weights_z = weights->info()->dimension(2);
80
Georgios Pinitas1562be32018-03-08 19:09:19 +000081 _is_first_run = true;
82 _original_weights = weights;
83 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000084
85 bool append_bias = (biases != nullptr) && !_is_quantized;
86 const GPUTarget gpu_target = CLScheduler::get().target();
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010087
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000088 // Calculate output shape
89 TensorShape dwc_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info);
90
91 // Output width and height
92 const unsigned int conv_w = dwc_output_shape.x();
93 const unsigned int conv_h = dwc_output_shape.y();
Giorgio Arena9fe41442017-08-23 16:36:24 +010094
95 // Set up intermediate tensors
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +000096 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
Giorgio Arena9fe41442017-08-23 16:36:24 +010097 const size_t conv_size = conv_w * conv_h;
98
Georgios Pinitas5cbd20f2017-10-27 12:01:23 +010099 // Im2Col configuration
Giorgio Arena9fe41442017-08-23 16:36:24 +0100100 TensorShape shape_im2col = input->info()->tensor_shape();
101 shape_im2col.set(0, patch_size);
102 shape_im2col.set(1, conv_size);
103 shape_im2col.set(2, weights_z);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000104 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Anthony Barbiera2ea7532017-11-28 10:33:22 +0000105 _im2col_kernel.set_target(gpu_target);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000106 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100107
Georgios Pinitas5cbd20f2017-10-27 12:01:23 +0100108 // Weights reshape configuration
Giorgio Arena9fe41442017-08-23 16:36:24 +0100109 const TensorShape shape_weights_reshape(patch_size, weights_z);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000110 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
111 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
Georgios Pinitas5cbd20f2017-10-27 12:01:23 +0100112
113 // GEMV configuration
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000114 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Georgios Pinitas5cbd20f2017-10-27 12:01:23 +0100115 TensorShape shape_v2mm_out = input->info()->tensor_shape();
Giorgio Arena9fe41442017-08-23 16:36:24 +0100116 shape_v2mm_out.set(0, conv_size * weights_z);
117 shape_v2mm_out.set(1, 1);
118 shape_v2mm_out.set(2, 1);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000119 _v2mm_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(v2mm_dt).set_tensor_shape(shape_v2mm_out));
Anthony Barbiera2ea7532017-11-28 10:33:22 +0000120 _v2mm_kernel.set_target(gpu_target);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100121 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000122 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(dwc_output_shape));
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000123 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100124
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000125 // Output staged configuration
126 if(_is_quantized)
127 {
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000128 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
129
130 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000131 int output_multiplier, output_shift;
132 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +0000133 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000134 _output_reshaped.allocator()->allocate();
135 }
136
137 // Fill borders on inputs
138 PixelValue zero_in(static_cast<int32_t>(0));
139 PixelValue zero_w(static_cast<int32_t>(0));
140 if(_is_quantized)
141 {
142 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
143 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
144 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100145 BorderSize border_size = _v2mm_kernel.border_size();
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000146 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100147
148 border_size.bottom = 0;
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000149 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100150
151 // Allocate intermediate tensors
152 _input_reshaped.allocator()->allocate();
153 _weights_reshaped.allocator()->allocate();
154 _v2mm_output.allocator()->allocate();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100155}
156
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000157void CLDepthwiseConvolutionLayer::run()
Giorgio Arena93a690e2017-08-01 16:09:33 +0100158{
Georgios Pinitas1562be32018-03-08 19:09:19 +0000159 // Run weights reshaping (Runs once for every configure)
160 if(_is_first_run)
161 {
162 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
163
164 CLScheduler::get().enqueue(_weights_reshape_kernel);
165 CLScheduler::get().enqueue(_v2mm_weights_fill_border);
166 _is_first_run = false;
167
168 // Mark original weights tensor as unused
169 _original_weights->mark_as_unused();
170 }
171
Giorgio Arena9fe41442017-08-23 16:36:24 +0100172 CLScheduler::get().enqueue(_im2col_kernel);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100173 CLScheduler::get().enqueue(_v2mm_input_fill_border);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100174 CLScheduler::get().enqueue(_v2mm_kernel);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100175 CLScheduler::get().enqueue(_vector_to_tensor_kernel);
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000176 if(_is_quantized)
177 {
178 CLScheduler::get().enqueue(_output_stage_kernel);
179 }
Giorgio Arena9fe41442017-08-23 16:36:24 +0100180}