blob: 34a228c7179beecff0804013aea79fa17d50b395 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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/CLGaussianPyramidKernel.h"
25
26#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/Validate.h"
31
32using namespace arm_compute;
33
34CLGaussianPyramidHorKernel::CLGaussianPyramidHorKernel()
35 : _border_size(0), _l2_load_offset(0)
36{
37}
38
39BorderSize CLGaussianPyramidHorKernel::border_size() const
40{
41 return _border_size;
42}
43
44void CLGaussianPyramidHorKernel::configure(const ICLTensor *input, ICLTensor *output, bool border_undefined)
45{
46 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U16);
48 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != 2 * output->info()->dimension(0));
49 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != output->info()->dimension(1));
50
51 for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
52 {
53 ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i));
54 }
55
56 _input = input;
57 _output = output;
58 _border_size = BorderSize(border_undefined ? 0 : 2, 2);
59
60 // Create kernel
61 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gaussian1x5_sub_x"));
62
63 // Configure kernel window
64 constexpr unsigned int num_elems_processed_per_iteration = 16;
65 constexpr unsigned int num_elems_read_per_iteration = 20;
66 constexpr unsigned int num_elems_written_per_iteration = 8;
67 constexpr float scale_x = 0.5f;
68
69 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
70 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration, scale_x);
71
72 // Sub sampling selects odd pixels (1, 3, 5, ...) for images with even
73 // width and even pixels (0, 2, 4, ...) for images with odd width. (Whether
74 // a pixel is even or odd is determined based on the tensor shape not the
75 // valid region!)
76 // Thus the offset from which the first pixel (L2) for the convolution is
77 // loaded depends on the anchor and shape of the valid region.
78 // In the case of an even shape (= even image width) we need to load L2
79 // from -2 if the anchor is odd and from -1 if the anchor is even. That
80 // makes sure that L2 is always loaded from an odd pixel.
81 // On the other hand, for an odd shape (= odd image width) we need to load
82 // L2 from -1 if the anchor is odd and from -2 if the anchor is even to
83 // achieve the opposite effect.
84 // The condition can be simplified to checking whether anchor + shape is
85 // odd (-2) or even (-1) as only adding an odd and an even number will have
86 // an odd result.
87 _l2_load_offset = -border_size().left;
88
89 if((_input->info()->valid_region().anchor[0] + _input->info()->valid_region().shape[0]) % 2 == 0)
90 {
91 _l2_load_offset += 1;
92 }
93
94 update_window_and_padding(win,
95 AccessWindowHorizontal(input->info(), _l2_load_offset, num_elems_read_per_iteration),
96 output_access);
97
98 ValidRegion valid_region = input->info()->valid_region();
99 valid_region.anchor.set(0, std::ceil((valid_region.anchor[0] + (border_undefined ? border_size().left : 0)) / 2.f));
100 valid_region.shape.set(0, (valid_region.shape[0] - (border_undefined ? border_size().right : 0)) / 2 - valid_region.anchor[0]);
101
102 output_access.set_valid_region(win, valid_region);
103
104 ICLKernel::configure(win);
105}
106
107void CLGaussianPyramidHorKernel::run(const Window &window, cl::CommandQueue &queue)
108{
109 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
110 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
111
112 Window win_in(window);
113 win_in.shift(Window::DimX, _l2_load_offset);
114
115 //The output is half the width of the input:
116 Window win_out(window);
117 win_out.scale(Window::DimX, 0.5f);
118
119 Window slice_in = win_in.first_slice_window_2D();
120 Window slice_out = win_out.first_slice_window_2D();
121
122 do
123 {
124 unsigned int idx = 0;
125 add_2D_tensor_argument(idx, _input, slice_in);
126 add_2D_tensor_argument(idx, _output, slice_out);
127 enqueue(queue, *this, slice_out);
128 }
129 while(win_in.slide_window_slice_2D(slice_in) && win_out.slide_window_slice_2D(slice_out));
130}
131
132CLGaussianPyramidVertKernel::CLGaussianPyramidVertKernel()
133 : _t2_load_offset(0)
134{
135}
136
137BorderSize CLGaussianPyramidVertKernel::border_size() const
138{
139 return BorderSize(2, 0);
140}
141
142void CLGaussianPyramidVertKernel::configure(const ICLTensor *input, ICLTensor *output, bool border_undefined)
143{
144 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U16);
145 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
146 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != output->info()->dimension(0));
147 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != 2 * output->info()->dimension(1));
148
149 for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
150 {
151 ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i));
152 }
153
154 _input = input;
155 _output = output;
156
157 // Create kernel
158 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gaussian5x1_sub_y"));
159
160 // Configure kernel window
161 constexpr unsigned int num_elems_processed_per_iteration = 8;
162 constexpr unsigned int num_rows_processed_per_iteration = 2;
163 constexpr unsigned int num_elems_written_per_iteration = 8;
164 constexpr unsigned int num_elems_read_per_iteration = 8;
165 constexpr unsigned int num_rows_per_iteration = 5;
166 constexpr float scale_y = 0.5f;
167
168 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration, num_rows_processed_per_iteration),
169 border_undefined, border_size());
170 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, num_rows_per_iteration, 1.f, scale_y);
171
172 // Determine whether we need to load even or odd rows. See above for a
173 // detailed explanation.
174 _t2_load_offset = -border_size().top;
175
176 if((_input->info()->valid_region().anchor[1] + _input->info()->valid_region().shape[1]) % 2 == 0)
177 {
178 _t2_load_offset += 1;
179 }
180
181 update_window_and_padding(win,
182 AccessWindowRectangle(input->info(), 0, _t2_load_offset, num_elems_read_per_iteration, num_rows_per_iteration),
183 output_access);
184
185 ValidRegion valid_region = input->info()->valid_region();
186 valid_region.anchor.set(1, std::ceil((valid_region.anchor[1] + (border_undefined ? border_size().top : 0)) / 2.f));
187 valid_region.shape.set(1, (valid_region.shape[1] - (border_undefined ? border_size().bottom : 0)) / 2 - valid_region.anchor[1]);
188
189 output_access.set_valid_region(win, valid_region);
190
191 ICLKernel::configure(win);
192}
193
194void CLGaussianPyramidVertKernel::run(const Window &window, cl::CommandQueue &queue)
195{
196 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
197 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
198 ARM_COMPUTE_ERROR_ON(window.x().step() != 8);
199 ARM_COMPUTE_ERROR_ON(window.y().step() % 2);
200
201 Window win_in(window);
202 win_in.shift(Window::DimY, _t2_load_offset);
203
204 Window win_out(window);
205 win_out.scale(Window::DimY, 0.5f);
206
207 Window slice_in = win_in.first_slice_window_2D();
208 Window slice_out = win_out.first_slice_window_2D();
209
210 do
211 {
212 unsigned int idx = 0;
213 add_2D_tensor_argument(idx, _input, slice_in);
214 add_2D_tensor_argument(idx, _output, slice_out);
215 enqueue(queue, *this, slice_out);
216 }
217 while(win_in.slide_window_slice_2D(slice_in) && win_out.slide_window_slice_2D(slice_out));
218}