blob: a061236d37bb118d94900a80beb31351f90ae246 [file] [log] [blame]
Michalis Spyrouceb889e2018-09-17 18:24:41 +01001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2018-2020 ARM Limited.
Michalis Spyrouceb889e2018-09-17 18:24:41 +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 */
24#include "arm_compute/core/CL/kernels/CLUpsampleLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000029#include "arm_compute/core/CL/CLValidate.h"
Michalis Spyrouceb889e2018-09-17 18:24:41 +010030#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/Validate.h"
34#include "arm_compute/core/Window.h"
35#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Michalis Spyrouceb889e2018-09-17 18:24:41 +010037
38namespace arm_compute
39{
40CLUpsampleLayerKernel::CLUpsampleLayerKernel()
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +000041 : _input(nullptr), _output(nullptr), _info(), _data_layout(DataLayout::UNKNOWN), _num_elems_processed_per_iteration_input_x()
Michalis Spyrouceb889e2018-09-17 18:24:41 +010042{
43}
44
45Status CLUpsampleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy upsampling_policy)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
48 ARM_COMPUTE_UNUSED(upsampling_policy);
49
50 DataLayout data_layout = input->data_layout();
51 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
52 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
53
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000054 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000055 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Michalis Spyrouceb889e2018-09-17 18:24:41 +010056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
58 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width));
59 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_height) != info.y() * input->dimension(idx_height));
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported");
61 ARM_COMPUTE_RETURN_ERROR_ON_MSG(upsampling_policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported");
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrouceb889e2018-09-17 18:24:41 +010063
64 return Status{};
65}
66
67void CLUpsampleLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &info, const InterpolationPolicy upsampling_policy)
68{
69 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
70 ARM_COMPUTE_UNUSED(upsampling_policy);
71
72 _input = input;
73 _output = output;
74 _info = info;
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +000075 _data_layout = input->info()->data_layout();
Michalis Spyrouceb889e2018-09-17 18:24:41 +010076 _num_elems_processed_per_iteration_input_x = 1;
77
Michalis Spyrouceb889e2018-09-17 18:24:41 +010078 TensorShape output_shape = misc::shape_calculator::compute_upsample_shape(*input->info(), info);
79 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +000080 output->info()->set_data_layout(_data_layout);
Michalis Spyrouceb889e2018-09-17 18:24:41 +010081
82 unsigned int num_elems_processed_per_iteration_x = 16;
83 const int output_width_x = output->info()->dimension(0);
84 const bool multi_access_x = ((output_width_x / num_elems_processed_per_iteration_x) > 0);
85
86 // Perform validation step
87 ARM_COMPUTE_ERROR_THROW_ON(CLUpsampleLayerKernel::validate(input->info(), output->info(), info, upsampling_policy));
88
89 Window win{};
90
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +000091 switch(_data_layout)
Michalis Spyrouceb889e2018-09-17 18:24:41 +010092 {
93 case DataLayout::NCHW:
94 {
95 win = calculate_max_window(*output->info());
96 win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.y()));
97 if(multi_access_x)
98 {
99 _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x / info.x();
100 win.set(Window::DimX, Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), num_elems_processed_per_iteration_x), num_elems_processed_per_iteration_x));
101 }
102 break;
103 }
104 case DataLayout::NHWC:
105 {
106 win = calculate_max_window(*output->info());
107 win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.x()));
108 win.set(Window::DimZ, Window::Dimension(win.z().start(), win.z().end(), info.y()));
109 if(multi_access_x)
110 {
111 _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x;
112 win.set(Window::DimX, Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(),
113 num_elems_processed_per_iteration_x),
114 num_elems_processed_per_iteration_x));
115 }
116 break;
117 }
118 default:
119 ARM_COMPUTE_ERROR("Not implemented");
120 }
121
122 // Create kernel
123 CLBuildOptions build_opts;
124 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
125 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_IN=" + support::cpp11::to_string(_num_elems_processed_per_iteration_input_x));
126 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration_x));
127 build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X_IN=" + support::cpp11::to_string(std::max<int>(_input->info()->dimension(0) - _num_elems_processed_per_iteration_input_x, 0)));
128 build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X_OUT=" + support::cpp11::to_string(std::max<int>(output_width_x - num_elems_processed_per_iteration_x, 0)));
129 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("upsample_layer_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options()));
130
131 ICLKernel::configure_internal(win);
132}
133
134void CLUpsampleLayerKernel::run(const Window &window, cl::CommandQueue &queue)
135{
136 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
137 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
138
139 Window collapsed_window = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
140 Window slice_out = collapsed_window.first_slice_window_3D();
141 Window slice_in = collapsed_window.first_slice_window_3D();
142
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000143 switch(_data_layout)
Michalis Spyrouceb889e2018-09-17 18:24:41 +0100144 {
145 case DataLayout::NCHW:
146 slice_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_input_x));
147 slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1));
148 break;
149 case DataLayout::NHWC:
150 slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1));
151 slice_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), 1));
152 break;
153 default:
154 ARM_COMPUTE_ERROR("Not implemented");
155 }
156
157 do
158 {
159 unsigned int idx = 0;
160 add_3D_tensor_argument(idx, _input, slice_in);
161 add_3D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100162 enqueue(queue, *this, slice_out, lws_hint());
Michalis Spyrouceb889e2018-09-17 18:24:41 +0100163 }
164 while(collapsed_window.slide_window_slice_3D(slice_out) && collapsed_window.slide_window_slice_3D(slice_in));
165}
166} // namespace arm_compute