blob: 88e9376a9ebefd6d3c90728aa4674b6451873eb6 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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 */
24#include "arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/PixelValue.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000030#include "arm_compute/runtime/CL/CLScheduler.h"
31#include "support/ToolchainSupport.h"
32
33using namespace arm_compute;
Anthony Barbier06ea0482018-02-22 15:45:35 +000034using namespace arm_compute::misc;
35using namespace arm_compute::misc::shape_calculator;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000036
37CLDepthwiseConvolutionLayer3x3::CLDepthwiseConvolutionLayer3x3()
38 : _kernel(), _border_handler()
39{
40}
41
42void CLDepthwiseConvolutionLayer3x3::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
43{
Anthony Barbier06ea0482018-02-22 15:45:35 +000044 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000045 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
46
47 _kernel.set_target(CLScheduler::get().target());
48 _kernel.configure(input, weights, biases, output, conv_info);
49
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);
57}
58
59void CLDepthwiseConvolutionLayer3x3::run()
60{
61 CLScheduler::get().enqueue(_border_handler);
62 CLScheduler::get().enqueue(_kernel);
63}
64
65CLDepthwiseConvolutionLayer::CLDepthwiseConvolutionLayer()
Anthony Barbier06ea0482018-02-22 15:45:35 +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(),
67 _weights_reshaped(), _v2mm_output(), _output_reshaped(), _is_quantized(false)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000068{
69}
70
71void CLDepthwiseConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
72{
Anthony Barbier06ea0482018-02-22 15:45:35 +000073 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000074 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
75 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != weights->info()->dimension(2));
76
77 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
Anthony Barbier06ea0482018-02-22 15:45:35 +000081 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Anthony Barbier8140e1e2017-12-14 23:48:46 +000082
Anthony Barbier06ea0482018-02-22 15:45:35 +000083 bool append_bias = (biases != nullptr) && !_is_quantized;
84 const GPUTarget gpu_target = CLScheduler::get().target();
85
86 // Calculate output shape
87 TensorShape dwc_output_shape = shape_calculator::compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info);
88
89 // Output width and height
90 const unsigned int conv_w = dwc_output_shape.x();
91 const unsigned int conv_h = dwc_output_shape.y();
Anthony Barbier8140e1e2017-12-14 23:48:46 +000092
93 // Set up intermediate tensors
Anthony Barbier06ea0482018-02-22 15:45:35 +000094 const size_t patch_size = weights_w * weights_h + ((append_bias) ? 1 : 0);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000095 const size_t conv_size = conv_w * conv_h;
96
97 // Im2Col configuration
98 TensorShape shape_im2col = input->info()->tensor_shape();
99 shape_im2col.set(0, patch_size);
100 shape_im2col.set(1, conv_size);
101 shape_im2col.set(2, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000102 _input_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000103 _im2col_kernel.set_target(gpu_target);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000104 _im2col_kernel.configure(input, &_input_reshaped, Size2D(weights_w, weights_h), conv_info, append_bias);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000105
106 // Weights reshape configuration
107 const TensorShape shape_weights_reshape(patch_size, weights_z);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000108 _weights_reshaped.allocator()->init(weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_weights_reshape));
109 _weights_reshape_kernel.configure(weights, &_weights_reshaped, append_bias ? biases : nullptr);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000110
111 // GEMV configuration
Anthony Barbier06ea0482018-02-22 15:45:35 +0000112 DataType v2mm_dt = (input->info()->data_type() == DataType::QASYMM8) ? DataType::S32 : input->info()->data_type();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000113 TensorShape shape_v2mm_out = input->info()->tensor_shape();
114 shape_v2mm_out.set(0, conv_size * weights_z);
115 shape_v2mm_out.set(1, 1);
116 shape_v2mm_out.set(2, 1);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000117 _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 Barbier8140e1e2017-12-14 23:48:46 +0000118 _v2mm_kernel.set_target(gpu_target);
119 _v2mm_kernel.configure(&_input_reshaped, &_weights_reshaped, &_v2mm_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000120 _output_reshaped.allocator()->init(_v2mm_output.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(dwc_output_shape));
121 _vector_to_tensor_kernel.configure(&_v2mm_output, (_is_quantized) ? &_output_reshaped : output, conv_w, conv_h);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000122
Anthony Barbier06ea0482018-02-22 15:45:35 +0000123 // Output staged configuration
124 if(_is_quantized)
125 {
126 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
127
128 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
129 int output_multiplier, output_shift;
130 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
131 _output_stage_kernel.configure(&_output_reshaped, biases, output, output_multiplier, output_shift, output_quant_info.offset);
132 _output_reshaped.allocator()->allocate();
133 }
134
135 // Fill borders on inputs
136 PixelValue zero_in(static_cast<int32_t>(0));
137 PixelValue zero_w(static_cast<int32_t>(0));
138 if(_is_quantized)
139 {
140 zero_in = PixelValue(static_cast<int32_t>(input->info()->quantization_info().offset));
141 zero_w = PixelValue(static_cast<int32_t>(weights->info()->quantization_info().offset));
142 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000143 BorderSize border_size = _v2mm_kernel.border_size();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000144 _v2mm_input_fill_border.configure(&_input_reshaped, border_size, BorderMode::CONSTANT, zero_in);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000145
146 border_size.bottom = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000147 _v2mm_weights_fill_border.configure(&_weights_reshaped, border_size, BorderMode::CONSTANT, zero_w);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000148
149 // Allocate intermediate tensors
150 _input_reshaped.allocator()->allocate();
151 _weights_reshaped.allocator()->allocate();
152 _v2mm_output.allocator()->allocate();
153}
154
155void CLDepthwiseConvolutionLayer::run()
156{
157 CLScheduler::get().enqueue(_im2col_kernel);
158
159 CLScheduler::get().enqueue(_weights_reshape_kernel);
160
161 CLScheduler::get().enqueue(_v2mm_input_fill_border);
162 CLScheduler::get().enqueue(_v2mm_weights_fill_border);
163 CLScheduler::get().enqueue(_v2mm_kernel);
164
165 CLScheduler::get().enqueue(_vector_to_tensor_kernel);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000166
167 if(_is_quantized)
168 {
169 CLScheduler::get().enqueue(_output_stage_kernel);
170 }
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000171}