blob: 2a1365e6e216d68a2c96d482b75cbfb91fc9dca1 [file] [log] [blame]
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001/*
Giorgio Arena9f7d55a2021-02-08 13:20:24 +00002 * Copyright (c) 2018-2021 Arm Limited.
Giorgio Arenadfca60b2018-01-31 10:30:59 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
Giorgio Arenadfca60b2018-01-31 10:30:59 +000025
Giorgio Arenadfca60b2018-01-31 10:30:59 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Giorgio Arenadfca60b2018-01-31 10:30:59 +000028#include "arm_compute/core/CL/ICLTensor.h"
Giorgio Arenadfca60b2018-01-31 10:30:59 +000029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
Giorgio Arenadfca60b2018-01-31 10:30:59 +000031#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/AccessWindowStatic.h"
34#include "src/core/CL/CLValidate.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035#include "src/core/CL/ICLKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
Giorgio Arenadfca60b2018-01-31 10:30:59 +000039
giuros016d109962019-01-07 17:47:19 +000040namespace arm_compute
41{
Giorgio Arenadfca60b2018-01-31 10:30:59 +000042namespace
43{
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010044Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
Giorgio Arenadcf4c872021-04-16 12:41:45 +010045 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000046{
Giorgio Arenadcf4c872021-04-16 12:41:45 +010047 ARM_COMPUTE_UNUSED(act_info);
Giorgio Arenae6bb3c62018-08-23 11:19:11 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Giorgio Arenadcf4c872021-04-16 12:41:45 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Giorgio Arena76572242018-04-04 17:44:26 +010050 ARM_COMPUTE_RETURN_ERROR_ON(depth_multiplier > 1); // COMPMID-1071 Add depth multiplier support for NHWC
giuros016d109962019-01-07 17:47:19 +000051
52 ARM_COMPUTE_RETURN_ERROR_ON(conv_info.stride().first < 1);
Pablo Telloe4cad152019-06-04 11:28:05 +010053 ARM_COMPUTE_RETURN_ERROR_ON(std::max(conv_info.pad_top(), conv_info.pad_bottom()) > 4);
Giorgio Arenadfca60b2018-01-31 10:30:59 +000054
Usama Arife73686a2019-04-08 17:30:48 +010055 ARM_COMPUTE_RETURN_ERROR_ON((dilation.x() < 1) || (dilation.y() < 1));
56
giuros016d109962019-01-07 17:47:19 +000057 const size_t weights_width = 3;
58 const size_t weights_height = 3;
59
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010060 const ConvolutionInfo info{ conv_info, depth_multiplier, ActivationLayerInfo(), dilation };
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010061
Giorgio Arenadcf4c872021-04-16 12:41:45 +010062 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_depthwise_convolution_shape(
63 *input, TensorInfo(TensorShape(weights_width, weights_height), 1, weights->data_type()).set_data_layout(DataLayout::NCHW), info);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010064
Giorgio Arenadcf4c872021-04-16 12:41:45 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
66 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(1) != weights_width) || (weights->dimension(2) != weights_height));
Giorgio Arenad051e972018-06-20 11:46:42 +010067
Giorgio Arenadfca60b2018-01-31 10:30:59 +000068 if(biases != nullptr)
69 {
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010070 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != output_shape[0]);
Giorgio Arenadcf4c872021-04-16 12:41:45 +010071 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
giuros016d109962019-01-07 17:47:19 +000072
Giorgio Arenadfca60b2018-01-31 10:30:59 +000073 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
74 }
75
76 if(output->total_size() != 0)
77 {
Giorgio Arenadfca60b2018-01-31 10:30:59 +000078 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
79 }
80
81 return Status{};
82}
83
Georgios Pinitasb2d9ebb2018-05-14 12:21:51 +010084std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *weights, ITensorInfo *bias, ITensorInfo *output,
Giorgio Arenadcf4c872021-04-16 12:41:45 +010085 const PadStrideInfo &conv_info, unsigned int depth_multiplier, const Size2D &dilation)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000086{
Giorgio Arenadcf4c872021-04-16 12:41:45 +010087 ARM_COMPUTE_UNUSED(weights, bias);
Giorgio Arena1e2af2a2020-10-15 17:39:41 +010088 ARM_COMPUTE_UNUSED(depth_multiplier);
giuros016d109962019-01-07 17:47:19 +000089
Giorgio Arena1e2af2a2020-10-15 17:39:41 +010090 const bool is_stride_1_dilation_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1) && dilation.x() == 1 && dilation.y() == 1);
91 unsigned int num_rows_processed_per_iteration = is_stride_1_dilation_1 ? 2 : 1;
Giorgio Arenaeff8d952018-07-02 15:29:57 +010092
Giorgio Arena1e2af2a2020-10-15 17:39:41 +010093 Window win{};
94 Status err{};
Giorgio Arenaeff8d952018-07-02 15:29:57 +010095
Giorgio Arenadcf4c872021-04-16 12:41:45 +010096 unsigned int num_elems_accessed_per_iteration = adjust_vec_size(4 / input->element_size(), input->dimension(0));
97 win = calculate_max_window(*output, Steps(num_elems_accessed_per_iteration, num_rows_processed_per_iteration));
Giorgio Arenadfca60b2018-01-31 10:30:59 +000098
Giorgio Arenadfca60b2018-01-31 10:30:59 +000099 return std::make_pair(err, win);
100}
101} // namespace
102
103CLDepthwiseConvolutionLayer3x3NHWCKernel::CLDepthwiseConvolutionLayer3x3NHWCKernel()
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100104 : _input(), _output(), _weights(), _biases(), _num_planes_processed_per_iteration(1)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000105{
106}
107
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100108void CLDepthwiseConvolutionLayer3x3NHWCKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100109 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000110{
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100111 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100112}
113
Manuel Bottini256c0b92020-04-21 13:29:30 +0100114void CLDepthwiseConvolutionLayer3x3NHWCKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100115 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100116{
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000117 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100118 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(),
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100119 conv_info, depth_multiplier, act_info, dilation));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100120
121 auto padding_info = get_padding_info({ input, weights, biases, output });
122
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100123 auto win_config = validate_and_configure_window(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(),
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100124 conv_info, depth_multiplier, dilation);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000125
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100126 const bool is_stride_1 = ((conv_info.stride().first == conv_info.stride().second) && (conv_info.stride().first == 1));
127 const bool is_stride_1_dilation_1 = (is_stride_1 && dilation.x() == 1 && dilation.y() == 1);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000128
Giorgio Arenad051e972018-06-20 11:46:42 +0100129 _input = input;
130 _output = output;
131 _weights = weights;
132 _biases = biases;
Usama Arife73686a2019-04-08 17:30:48 +0100133 _num_planes_processed_per_iteration = is_stride_1_dilation_1 ? 2 : 1;
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100134
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100135 unsigned int num_elems_accessed_per_iteration = adjust_vec_size(4 / input->info()->element_size(), input->info()->dimension(0));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100136 unsigned int num_rows_processed_per_iteration = is_stride_1_dilation_1 ? 2 : 1;
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000137
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000138 CLBuildOptions build_opts;
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100139 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(_input->info()->data_type()));
Usama Arif6a98a6e2019-05-10 17:07:27 +0100140 build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000141 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_accessed_per_iteration));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100142 build_opts.add_option("-DSRC_DIM_1=" + support::cpp11::to_string(_input->info()->dimension(1)));
Giorgio Arenafa23f112018-06-19 11:27:38 +0100143 build_opts.add_option("-DSRC_DIM_2=" + support::cpp11::to_string(_input->info()->dimension(2)));
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000144 build_opts.add_option("-DCONV_PAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
Giorgio Arenafa23f112018-06-19 11:27:38 +0100145 build_opts.add_option("-DCONV_PAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100146 build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(input->info()->dimension(0) % num_elems_accessed_per_iteration));
147 build_opts.add_option_if(_biases != nullptr, "-DHAS_BIAS");
148 build_opts.add_option_if(_input->info()->tensor_shape().total_size_upper(3) > 1,
149 "-DDST_DEPTH=" + support::cpp11::to_string(static_cast<int>(std::ceil(_output->info()->dimension(2) / static_cast<float>(_num_planes_processed_per_iteration)))));
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100150 build_opts.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
151 build_opts.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100152
Usama Arife73686a2019-04-08 17:30:48 +0100153 if(is_stride_1_dilation_1)
Giorgio Arenad051e972018-06-20 11:46:42 +0100154 {
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100155 build_opts.add_option("-DNUM_ROWS_PROCESSED=" + support::cpp11::to_string(num_rows_processed_per_iteration));
Giorgio Arenad051e972018-06-20 11:46:42 +0100156 build_opts.add_option("-DNUM_PLANES_PROCESSED=" + support::cpp11::to_string(_num_planes_processed_per_iteration));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100157 build_opts.add_option("-DDST_DIM_1=" + support::cpp11::to_string(_output->info()->dimension(1)));
Giorgio Arenad051e972018-06-20 11:46:42 +0100158 build_opts.add_option("-DDST_DIM_2=" + support::cpp11::to_string(_output->info()->dimension(2)));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100159 build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string((input->info()->dimension(1) + conv_info.pad_left() + conv_info.pad_right()) % num_rows_processed_per_iteration));
Giorgio Arenad051e972018-06-20 11:46:42 +0100160 }
161 else
162 {
Georgios Pinitas3f8aac42018-12-24 13:09:02 +0000163 build_opts.add_option("-DCONV_STRIDE_X=" + support::cpp11::to_string(conv_info.stride().first));
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100164 build_opts.add_option("-DCONV_STRIDE_Y=" + support::cpp11::to_string(conv_info.stride().second));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100165 build_opts.add_option("-DDILATION_X=" + support::cpp11::to_string(dilation.x()));
166 build_opts.add_option("-DDILATION_Y=" + support::cpp11::to_string(dilation.y()));
Giorgio Arenad051e972018-06-20 11:46:42 +0100167 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000168
169 // Create kernel
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100170 std::string kernel_name;
171 kernel_name = std::string("depthwise_convolution_3x3_nhwc");
172 kernel_name += (is_stride_1_dilation_1 ? "_stride1" : "");
Pablo Tello47104362019-02-27 13:32:51 +0000173
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100174 ICLKernel::configure_internal(win_config.second);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100175 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000176
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100177 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100178
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000179 // Set config_id for enabling LWS tuning
180 _config_id = kernel_name;
181 _config_id += "_";
182 _config_id += support::cpp11::to_string(input->info()->dimension(0));
183 _config_id += "_";
184 _config_id += support::cpp11::to_string(input->info()->dimension(1));
185 _config_id += "_";
186 _config_id += support::cpp11::to_string(input->info()->dimension(2));
187 _config_id += "_";
188 _config_id += support::cpp11::to_string(output->info()->dimension(0));
189 _config_id += "_";
190 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Giorgio Arenad051e972018-06-20 11:46:42 +0100191 _config_id += "_";
192 _config_id += string_from_data_type(input->info()->data_type());
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000193}
194
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100195Status CLDepthwiseConvolutionLayer3x3NHWCKernel::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100196 const PadStrideInfo &conv_info, unsigned int depth_multiplier, ActivationLayerInfo act_info, const Size2D &dilation)
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000197{
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100198 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, weights, biases, output, conv_info, depth_multiplier, act_info, dilation));
Georgios Pinitasb2d9ebb2018-05-14 12:21:51 +0100199 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), weights->clone().get(),
200 biases != nullptr ? biases->clone().get() : nullptr,
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100201 output->clone().get(), conv_info, depth_multiplier, dilation)
Georgios Pinitasb2d9ebb2018-05-14 12:21:51 +0100202 .first);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000203 return Status{};
204}
205
206void CLDepthwiseConvolutionLayer3x3NHWCKernel::run(const Window &window, cl::CommandQueue &queue)
207{
208 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
209 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
210
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100211 const size_t total_batches = _input->info()->tensor_shape().total_size_upper(3);
Georgios Pinitas37044642018-10-30 14:53:25 +0000212
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100213 Window win = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
Georgios Pinitas37044642018-10-30 14:53:25 +0000214 win.set(Window::DimZ, Window::Dimension(0, std::ceil(_output->info()->dimension(2) / static_cast<float>(_num_planes_processed_per_iteration)) * total_batches, 1));
Giorgio Arenad051e972018-06-20 11:46:42 +0100215
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100216 unsigned int idx = 2 * num_arguments_per_4D_tensor() + num_arguments_per_3D_tensor();
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100217
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000218 if(_biases != nullptr)
219 {
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100220 Window win_biases;
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000221 win_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
222 win_biases.set_dimension_step(Window::DimX, window.x().step());
223 add_1D_tensor_argument(idx, _biases, win_biases);
224 }
225
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100226 Window slice = win.first_slice_window_4D();
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000227 do
228 {
229 unsigned int idx = 0;
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100230 add_4D_tensor_argument(idx, _input, slice);
231 add_4D_tensor_argument(idx, _output, slice);
Giorgio Arenadcf4c872021-04-16 12:41:45 +0100232 add_3D_tensor_argument(idx, _weights, slice);
233
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100234 enqueue(queue, *this, slice, lws_hint());
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000235 }
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100236 while(win.slide_window_slice_4D(slice));
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000237}
giuros016d109962019-01-07 17:47:19 +0000238} // namespace arm_compute