blob: 63586b0f0fd1e75e0a879eda6676903e69fe1d4c [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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 Arena9fe41442017-08-23 16:36:24 +010024#include "arm_compute/core/CL/kernels/CLDepthwiseConvolution3x3Kernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010025
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLKernel.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
Dmitry Savenkod7295b72017-11-20 22:00:08 +070036#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010037
38using namespace arm_compute;
39
Giorgio Arena9fe41442017-08-23 16:36:24 +010040CLDepthwiseConvolution3x3Kernel::CLDepthwiseConvolution3x3Kernel()
Jaroslaw Rzepecki16cdf892017-10-27 13:15:03 +010041 : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_left(0), _conv_pad_top(0)
Giorgio Arena93a690e2017-08-01 16:09:33 +010042{
43}
44
Giorgio Arena9fe41442017-08-23 16:36:24 +010045BorderSize CLDepthwiseConvolution3x3Kernel::border_size() const
Giorgio Arena93a690e2017-08-01 16:09:33 +010046{
47 return _border_size;
48}
49
Giorgio Arena82afedf2017-11-15 13:36:15 +000050void CLDepthwiseConvolution3x3Kernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010051{
Dmitry Savenkod7295b72017-11-20 22:00:08 +070052 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32);
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::F32);
54 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::F32);
55 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights);
Giorgio Arena93a690e2017-08-01 16:09:33 +010056 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3);
57
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010058 if(biases != nullptr)
59 {
Dmitry Savenkod7295b72017-11-20 22:00:08 +070060 if(is_data_type_quantized_asymmetric(weights->info()->data_type()))
61 {
62 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
63 }
64 else
65 {
66 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
67 }
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010068 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2));
69 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
70 }
71
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010072 std::pair<unsigned int, unsigned int> expected_output = scaled_dimensions(input->info()->tensor_shape().x(), input->info()->tensor_shape().y(),
73 weights->info()->tensor_shape().x(), weights->info()->tensor_shape().y(),
74 conv_info);
75
76 ARM_COMPUTE_UNUSED(expected_output);
77 ARM_COMPUTE_ERROR_ON(expected_output.first != output->info()->tensor_shape().x());
78 ARM_COMPUTE_ERROR_ON(expected_output.second != output->info()->tensor_shape().y());
79
Giorgio Arena93a690e2017-08-01 16:09:33 +010080 _input = input;
81 _output = output;
82 _weights = weights;
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010083 _biases = biases;
Giorgio Arena93a690e2017-08-01 16:09:33 +010084 _conv_stride_x = conv_info.stride().first;
85 _conv_stride_y = conv_info.stride().second;
Jaroslaw Rzepecki16cdf892017-10-27 13:15:03 +010086 _conv_pad_left = conv_info.pad_left();
87 _conv_pad_top = conv_info.pad_top();
88 _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left);
Giorgio Arena93a690e2017-08-01 16:09:33 +010089
90 // Set build options
Georgios Pinitasc26ecf82017-09-22 13:44:05 +010091 ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3);
Dmitry Savenkod7295b72017-11-20 22:00:08 +070092 CLBuildOptions build_opts;
93 build_opts.add_option("-DCONV_STRIDE_X=" + support::cpp11::to_string(_conv_stride_x));
94 build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS");
Giorgio Arena93a690e2017-08-01 16:09:33 +010095
Dmitry Savenkod7295b72017-11-20 22:00:08 +070096 std::string kernel_name = is_data_type_quantized_asymmetric(_input->info()->data_type()) ? "depthwise_convolution_3x3_quantized" : "depthwise_convolution_3x3";
97 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Giorgio Arena93a690e2017-08-01 16:09:33 +010098
99 // Configure kernel window
100 const unsigned int num_elems_processed_per_iteration = 2;
101 const unsigned int num_elems_written_per_iteration = 2;
Georgios Pinitasc26ecf82017-09-22 13:44:05 +0100102 const unsigned int num_elems_read_per_iteration = 3 + _conv_stride_x;
Giorgio Arena93a690e2017-08-01 16:09:33 +0100103 const unsigned int num_rows_read_per_iteration = 3;
104
105 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
106
Georgios Pinitasc26ecf82017-09-22 13:44:05 +0100107 AccessWindowRectangle input_access(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration, _conv_stride_x, _conv_stride_y);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100108 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
109 AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1));
110
111 update_window_and_padding(win, input_access, weights_access, output_access);
112
113 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
114
115 ICLKernel::configure(win);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700116
117 // Set static arguments
118 if(is_data_type_quantized_asymmetric(_input->info()->data_type()))
119 {
120 float multiplier = _input->info()->quantization_info().scale * _weights->info()->quantization_info().scale / _output->info()->quantization_info().scale;
121 int output_multiplier = 0;
122 int output_shift = 0;
123 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
124
125 unsigned int idx = 3 * num_arguments_per_3D_tensor() + ((_biases != nullptr) ? num_arguments_per_1D_tensor() : 0);
126
127 _kernel.setArg(idx++, -_input->info()->quantization_info().offset);
128 _kernel.setArg(idx++, -_weights->info()->quantization_info().offset);
129 _kernel.setArg(idx++, _output->info()->quantization_info().offset);
130 _kernel.setArg(idx++, output_multiplier);
131 _kernel.setArg(idx++, output_shift);
132 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100133}
134
Giorgio Arena9fe41442017-08-23 16:36:24 +0100135void CLDepthwiseConvolution3x3Kernel::run(const Window &window, cl::CommandQueue &queue)
Giorgio Arena93a690e2017-08-01 16:09:33 +0100136{
137 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
138 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
139
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000140 // Create input window and adjust
141 Window win_in = window;
142 win_in.adjust(Window::DimX, -_conv_pad_left, true);
143 win_in.adjust(Window::DimY, -_conv_pad_top, true);
144 win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
145 win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
146
147 Window slice_in = win_in.first_slice_window_3D();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100148 Window slice_out = window.first_slice_window_3D();
149 Window slice_weights = window.first_slice_window_3D();
Giorgio Arena93a690e2017-08-01 16:09:33 +0100150 slice_weights.set_dimension_step(Window::DimX, 0);
151 slice_weights.set_dimension_step(Window::DimY, 0);
152
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100153 // Set biases
154 if(_biases != nullptr)
155 {
156 unsigned int idx = 3 * num_arguments_per_3D_tensor();
157 Window slice_biases;
158 slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
159 add_1D_tensor_argument(idx, _biases, slice_biases);
160 }
161
Giorgio Arena93a690e2017-08-01 16:09:33 +0100162 do
163 {
164 unsigned int idx = 0;
165 add_3D_tensor_argument(idx, _input, slice_in);
166 add_3D_tensor_argument(idx, _output, slice_out);
167 add_3D_tensor_argument(idx, _weights, slice_weights);
168
169 enqueue(queue, *this, slice_out);
170 }
Georgios Pinitasb6f182d32017-11-29 10:17:56 +0000171 while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100172}