arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / kernels / CLDeconvolutionLayerUpsampleKernel.cpp
1 /*
2  * Copyright (c) 2017-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/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/Error.h"
30 #include "arm_compute/core/Helpers.h"
31 #include "arm_compute/core/Validate.h"
32 #include "arm_compute/core/Window.h"
33
34 using namespace arm_compute;
35
36 CLDeconvolutionLayerUpsampleKernel::CLDeconvolutionLayerUpsampleKernel()
37     : _input(nullptr), _output(nullptr), _inner_border(), _info()
38 {
39 }
40
41 Status CLDeconvolutionLayerUpsampleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const BorderSize &inner_border,
42                                                     const PadStrideInfo &info)
43 {
44     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45
46     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
47     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
48     ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(0) == 0);
49     ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) == 0);
50     ARM_COMPUTE_RETURN_ERROR_ON(!info.padding_is_symmetric());
51
52     for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
53     {
54         ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
55     }
56
57     ARM_COMPUTE_RETURN_ERROR_ON_MSG(inner_border.right > info.stride().first - 1, "inner_border_right must be smaller that stride_x");
58     ARM_COMPUTE_RETURN_ERROR_ON_MSG(inner_border.top > info.stride().second - 1, "inner_border_top must be smaller that stride_y");
59
60     return Status{};
61 }
62
63 void CLDeconvolutionLayerUpsampleKernel::configure(const ICLTensor *input, ICLTensor *output, const BorderSize &inner_border,
64                                                    const PadStrideInfo &info)
65 {
66     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
67
68     _input        = input;
69     _output       = output;
70     _inner_border = inner_border;
71     _info         = info;
72
73     // Perform validation step
74     ARM_COMPUTE_ERROR_THROW_ON(CLDeconvolutionLayerUpsampleKernel::validate(input->info(), output->info(), inner_border, info));
75
76     // Create kernel
77     _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("deconvolution_upsample"));
78
79     constexpr unsigned int num_elems_processed_per_iteration = 1;
80
81     // Configure kernel window
82     Window                 win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
83     AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
84     output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
85
86     ICLKernel::configure(win);
87 }
88
89 void CLDeconvolutionLayerUpsampleKernel::run(const Window &window, cl::CommandQueue &queue)
90 {
91     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
92     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
93
94     const int out_start_x = _info.pad().first;
95     const int out_end_x   = _output->info()->dimension(0) - _inner_border.right - _info.pad().first + _info.stride().first - 1;
96     const int out_step_x  = _info.stride().first;
97
98     const int out_start_y = _inner_border.top + _info.pad().second;
99     const int out_end_y   = _output->info()->dimension(1) - _info.pad().second + _info.stride().second - 1;
100     const int out_step_y  = _info.stride().second;
101
102     Window slice_out = window.first_slice_window_2D();
103     slice_out.set(Window::DimX, Window::Dimension(out_start_x, out_end_x, out_step_x));
104     slice_out.set(Window::DimY, Window::Dimension(out_start_y, out_end_y, out_step_y));
105
106     Window slice_in = window.first_slice_window_2D();
107
108     do
109     {
110         unsigned int idx = 0;
111         add_2D_tensor_argument(idx, _input, slice_in);
112         add_2D_tensor_argument(idx, _output, slice_out);
113         enqueue(queue, *this, slice_out);
114     }
115     while(window.slide_window_slice_2D(slice_in) && window.slide_window_slice_2D(slice_out));
116 }