blob: ee3fa1141d46f0980f80d9cf3ed7a82974bd67ec [file] [log] [blame]
Michalis Spyrouceb889e2018-09-17 18:24:41 +01001/*
2 * Copyright (c) 2018 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 */
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"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34#include "arm_compute/core/utils/misc/ShapeCalculator.h"
35
36namespace arm_compute
37{
38CLUpsampleLayerKernel::CLUpsampleLayerKernel()
39 : _input(nullptr), _output(nullptr), _info(), _num_elems_processed_per_iteration_input_x()
40{
41}
42
43Status CLUpsampleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy upsampling_policy)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
46 ARM_COMPUTE_UNUSED(upsampling_policy);
47
48 DataLayout data_layout = input->data_layout();
49 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
50 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
51
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
54 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width));
55 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_height) != info.y() * input->dimension(idx_height));
56 ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported");
57 ARM_COMPUTE_RETURN_ERROR_ON_MSG(upsampling_policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported");
58
59 return Status{};
60}
61
62void CLUpsampleLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &info, const InterpolationPolicy upsampling_policy)
63{
64 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
65 ARM_COMPUTE_UNUSED(upsampling_policy);
66
67 _input = input;
68 _output = output;
69 _info = info;
70 _num_elems_processed_per_iteration_input_x = 1;
71
72 const DataLayout data_layout = input->info()->data_layout();
73
74 TensorShape output_shape = misc::shape_calculator::compute_upsample_shape(*input->info(), info);
75 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
76 output->info()->set_data_layout(data_layout);
77
78 unsigned int num_elems_processed_per_iteration_x = 16;
79 const int output_width_x = output->info()->dimension(0);
80 const bool multi_access_x = ((output_width_x / num_elems_processed_per_iteration_x) > 0);
81
82 // Perform validation step
83 ARM_COMPUTE_ERROR_THROW_ON(CLUpsampleLayerKernel::validate(input->info(), output->info(), info, upsampling_policy));
84
85 Window win{};
86
87 switch(data_layout)
88 {
89 case DataLayout::NCHW:
90 {
91 win = calculate_max_window(*output->info());
92 win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.y()));
93 if(multi_access_x)
94 {
95 _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x / info.x();
96 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));
97 }
98 break;
99 }
100 case DataLayout::NHWC:
101 {
102 win = calculate_max_window(*output->info());
103 win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.x()));
104 win.set(Window::DimZ, Window::Dimension(win.z().start(), win.z().end(), info.y()));
105 if(multi_access_x)
106 {
107 _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x;
108 win.set(Window::DimX, Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(),
109 num_elems_processed_per_iteration_x),
110 num_elems_processed_per_iteration_x));
111 }
112 break;
113 }
114 default:
115 ARM_COMPUTE_ERROR("Not implemented");
116 }
117
118 // Create kernel
119 CLBuildOptions build_opts;
120 build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
121 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_IN=" + support::cpp11::to_string(_num_elems_processed_per_iteration_input_x));
122 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration_x));
123 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)));
124 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)));
125 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("upsample_layer_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options()));
126
127 ICLKernel::configure_internal(win);
128}
129
130void CLUpsampleLayerKernel::run(const Window &window, cl::CommandQueue &queue)
131{
132 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
133 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
134
135 Window collapsed_window = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
136 Window slice_out = collapsed_window.first_slice_window_3D();
137 Window slice_in = collapsed_window.first_slice_window_3D();
138
139 DataLayout data_layout = _input->info()->data_layout();
140 switch(data_layout)
141 {
142 case DataLayout::NCHW:
143 slice_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_input_x));
144 slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1));
145 break;
146 case DataLayout::NHWC:
147 slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1));
148 slice_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), 1));
149 break;
150 default:
151 ARM_COMPUTE_ERROR("Not implemented");
152 }
153
154 do
155 {
156 unsigned int idx = 0;
157 add_3D_tensor_argument(idx, _input, slice_in);
158 add_3D_tensor_argument(idx, _output, slice_out);
159 enqueue(queue, *this, slice_out);
160 }
161 while(collapsed_window.slide_window_slice_3D(slice_out) && collapsed_window.slide_window_slice_3D(slice_in));
162}
163} // namespace arm_compute