blob: d28cffa05f5823152411ef28192520a303cdd3c6 [file] [log] [blame]
Michalis Spyrou780db4e2017-11-23 09:49:51 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Michalis Spyrou780db4e2017-11-23 09:49:51 +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/CLDeconvolutionLayerUpsampleKernel.h"
Michalis Spyrou780db4e2017-11-23 09:49:51 +000025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
Michalis Spyrou780db4e2017-11-23 09:49:51 +000029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CL/CLValidate.h"
32#include "src/core/helpers/WindowHelpers.h"
Michalis Spyrou780db4e2017-11-23 09:49:51 +000033
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000034namespace arm_compute
35{
Michalis Spyrou780db4e2017-11-23 09:49:51 +000036CLDeconvolutionLayerUpsampleKernel::CLDeconvolutionLayerUpsampleKernel()
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +000037 : _input(nullptr), _output(nullptr), _info(), _data_layout(DataLayout::UNKNOWN)
Michalis Spyrou780db4e2017-11-23 09:49:51 +000038{
39}
40
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010041Status CLDeconvolutionLayerUpsampleKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
Michalis Spyrou780db4e2017-11-23 09:49:51 +000042 const PadStrideInfo &info)
43{
Michalis Spyrou6bff1952019-10-02 17:22:11 +010044 ARM_COMPUTE_UNUSED(info);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000045 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000046 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000047 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000048 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010050
51 const DataLayout data_layout = input->data_layout();
52
53 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
54 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
55 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
56
57 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_w) == 0);
58 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_h) == 0);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000059
Michele Di Giorgioed5a4922018-09-13 16:22:01 +010060 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_c) != output->dimension(idx_c));
61 for(size_t i = 3; i < Coordinates::num_max_dimensions; ++i)
Michalis Spyrou780db4e2017-11-23 09:49:51 +000062 {
63 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
64 }
65
Michalis Spyrou780db4e2017-11-23 09:49:51 +000066 return Status{};
67}
68
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010069void CLDeconvolutionLayerUpsampleKernel::configure(const ICLTensor *input, ICLTensor *output,
Michalis Spyrou780db4e2017-11-23 09:49:51 +000070 const PadStrideInfo &info)
71{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010072 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
73}
74
Manuel Bottini256c0b92020-04-21 13:29:30 +010075void CLDeconvolutionLayerUpsampleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010076 const PadStrideInfo &info)
77{
Michalis Spyrou780db4e2017-11-23 09:49:51 +000078 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
79
Michalis Spyrou780db4e2017-11-23 09:49:51 +000080 // Perform validation step
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010081 ARM_COMPUTE_ERROR_THROW_ON(CLDeconvolutionLayerUpsampleKernel::validate(input->info(), output->info(), info));
Manuel Bottinib6869dd2020-12-16 15:34:25 +000082 auto padding_info = get_padding_info({ input, output });
Michalis Spyrou780db4e2017-11-23 09:49:51 +000083
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +000084 _input = input;
85 _output = output;
86 _info = info;
87 _data_layout = input->info()->data_layout();
88
Michalis Spyrou780db4e2017-11-23 09:49:51 +000089 // Create kernel
Georgios Pinitas793f87d2018-05-18 20:08:58 +010090 CLBuildOptions build_opts;
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000091 build_opts.add_option(("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size())));
Manuel Bottini4c6bd512020-04-08 10:15:51 +010092 _kernel = create_kernel(compile_context, "deconvolution_upsample", build_opts.options());
Michalis Spyrou780db4e2017-11-23 09:49:51 +000093
94 constexpr unsigned int num_elems_processed_per_iteration = 1;
95
96 // Configure kernel window
Georgios Pinitas59716fa2018-02-13 19:57:31 +000097 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
98 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Michalis Spyrou780db4e2017-11-23 09:49:51 +000099 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
100
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100101 ICLKernel::configure_internal(win);
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000102 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000103}
104
105void CLDeconvolutionLayerUpsampleKernel::run(const Window &window, cl::CommandQueue &queue)
106{
107 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
108 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
109
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000110 const size_t idx_w = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
111 const size_t idx_h = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100112
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100113 const int out_start_x = _info.pad_left();
114 const int out_end_x = _output->info()->dimension(idx_w) - _info.pad_right() + _info.stride().first - 1;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000115 const int out_step_x = _info.stride().first;
116
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100117 const int out_start_y = _info.pad_top();
118 const int out_end_y = _output->info()->dimension(idx_h) - _info.pad_bottom() + _info.stride().second - 1;
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000119 const int out_step_y = _info.stride().second;
120
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000121 switch(_data_layout)
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000122 {
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100123 case DataLayout::NCHW:
124 {
125 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
126
127 Window slice_out = collapsed.first_slice_window_3D();
128 slice_out.set(Window::DimX, Window::Dimension(out_start_x, out_end_x, out_step_x));
129 slice_out.set(Window::DimY, Window::Dimension(out_start_y, out_end_y, out_step_y));
130
131 Window slice_in = collapsed.first_slice_window_3D();
132
133 do
134 {
135 unsigned int idx = 0;
136 add_3D_tensor_argument(idx, _input, slice_in);
137 add_3D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100138 enqueue(queue, *this, slice_out, lws_hint());
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100139 }
140 while(collapsed.slide_window_slice_3D(slice_in) && collapsed.slide_window_slice_3D(slice_out));
141 break;
142 }
143 case DataLayout::NHWC:
144 {
145 // NOTE: not collapsing in NHWC
146 Window slice_out = window.first_slice_window_3D();
147 slice_out.set(Window::DimY, Window::Dimension(out_start_x, out_end_x, out_step_x));
148 slice_out.set(Window::DimZ, Window::Dimension(out_start_y, out_end_y, out_step_y));
149
150 Window slice_in = window.first_slice_window_3D();
151
152 do
153 {
154 unsigned int idx = 0;
155 add_3D_tensor_argument(idx, _input, slice_in);
156 add_3D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100157 enqueue(queue, *this, slice_out, lws_hint());
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100158 }
159 while(window.slide_window_slice_3D(slice_in) && window.slide_window_slice_3D(slice_out));
160 break;
161 }
162 default:
163 ARM_COMPUTE_ERROR("Unsupported data layout");
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000164 }
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000165}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000166} // namespace arm_compute