1 /*
2 * Copyright (c) 2017-2021 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 "src/core/CL/kernels/CLDeconvolutionLayerUpsampleKernel.h"
25
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/Helpers.h"
30 #include "arm_compute/core/Validate.h"
31 #include "src/core/CL/CLValidate.h"
32 #include "src/core/helpers/WindowHelpers.h"
33
34 namespace arm_compute
35 {
CLDeconvolutionLayerUpsampleKernel()36 CLDeconvolutionLayerUpsampleKernel::CLDeconvolutionLayerUpsampleKernel()
37 : _input(nullptr), _output(nullptr), _info(), _data_layout(DataLayout::UNKNOWN)
38 {
39 _type = CLKernelType::ELEMENTWISE;
40 }
41
validate(const ITensorInfo * input,const ITensorInfo * output,const PadStrideInfo & info)42 Status CLDeconvolutionLayerUpsampleKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
43 const PadStrideInfo &info)
44 {
45 ARM_COMPUTE_UNUSED(info);
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
47 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
48 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
51
52 const DataLayout data_layout = input->data_layout();
53
54 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
55 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
56 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
57
58 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_w) == 0);
59 ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_h) == 0);
60
61 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_c) != output->dimension(idx_c));
62 for(size_t i = 3; i < Coordinates::num_max_dimensions; ++i)
63 {
64 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
65 }
66
67 return Status{};
68 }
69
configure(const ICLTensor * input,ICLTensor * output,const PadStrideInfo & info)70 void CLDeconvolutionLayerUpsampleKernel::configure(const ICLTensor *input, ICLTensor *output,
71 const PadStrideInfo &info)
72 {
73 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
74 }
75
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const PadStrideInfo & info)76 void CLDeconvolutionLayerUpsampleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output,
77 const PadStrideInfo &info)
78 {
79 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
80
81 // Perform validation step
82 ARM_COMPUTE_ERROR_THROW_ON(CLDeconvolutionLayerUpsampleKernel::validate(input->info(), output->info(), info));
83 auto padding_info = get_padding_info({ input, output });
84
85 _input = input;
86 _output = output;
87 _info = info;
88 _data_layout = input->info()->data_layout();
89
90 // Create kernel
91 CLBuildOptions build_opts;
92 build_opts.add_option(("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size())));
93 _kernel = create_kernel(compile_context, "deconvolution_upsample", build_opts.options());
94
95 constexpr unsigned int num_elems_processed_per_iteration = 1;
96
97 // Configure kernel window
98 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
99 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
100
101 ICLKernel::configure_internal(win);
102 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
103 }
104
run(const Window & window,cl::CommandQueue & queue)105 void 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
110 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);
112
113 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;
115 const int out_step_x = _info.stride().first;
116
117 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;
119 const int out_step_y = _info.stride().second;
120
121 switch(_data_layout)
122 {
123 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);
138 enqueue(queue, *this, slice_out, lws_hint());
139 }
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);
157 enqueue(queue, *this, slice_out, lws_hint());
158 }
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");
164 }
165 }
166 } // namespace arm_compute
167