1 /*
2 * Copyright (c) 2020-2022 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/helpers/WindowHelpers.h"
25
26 namespace arm_compute
27 {
calculate_max_window(const ValidRegion & valid_region,const Steps & steps,bool skip_border,BorderSize border_size)28 Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
29 {
30 if(!skip_border)
31 {
32 border_size = BorderSize(0);
33 }
34
35 const Coordinates &anchor = valid_region.anchor;
36 const TensorShape &shape = valid_region.shape;
37
38 Window window;
39
40 window.set(0, Window::Dimension(
41 // Skip the border left of the image
42 anchor[0] + border_size.left,
43 // Skip the border right of the image
44 // Make sure the window width is a multiple of the step size
45 anchor[0] + border_size.left + ceil_to_multiple(std::max(0, static_cast<int>(shape[0]) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)), steps[0]),
46 steps[0]));
47
48 size_t n = 1;
49
50 if(anchor.num_dimensions() > 1)
51 {
52 window.set(1, Window::Dimension(
53 // Skip the border above the image
54 anchor[1] + border_size.top,
55 // Skip the border below the image
56 anchor[1] + border_size.top + ceil_to_multiple(std::max(0, static_cast<int>(shape[1]) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom)), steps[1]),
57 steps[1]));
58
59 ++n;
60 }
61
62 if(anchor.num_dimensions() > 2)
63 {
64 window.set(2, Window::Dimension(anchor[2], std::max<size_t>(1, shape[2]), steps[2]));
65
66 ++n;
67 }
68
69 for(; n < anchor.num_dimensions(); ++n)
70 {
71 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
72 }
73
74 for(; n < Coordinates::num_max_dimensions; ++n)
75 {
76 window.set(n, Window::Dimension(0, 1));
77 }
78
79 return window;
80 }
81
calculate_max_window(const TensorShape & shape,const Steps & steps,bool skip_border,BorderSize border_size)82 Window calculate_max_window(const TensorShape &shape, const Steps &steps, bool skip_border, BorderSize border_size)
83 {
84 if(!skip_border)
85 {
86 border_size = BorderSize(0);
87 }
88
89 Window window;
90
91 window.set(0, Window::Dimension(
92 // Skip the border left of the image
93 border_size.left,
94 // Skip the border right of the image
95 // Make sure the window width is a multiple of the step size
96 border_size.left + ceil_to_multiple(std::max(0, static_cast<int>(shape[0]) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)), steps[0]),
97 steps[0]));
98
99 size_t n = 1;
100
101 if(shape.num_dimensions() > 1)
102 {
103 window.set(1, Window::Dimension(
104 // Skip the border above the image
105 border_size.top,
106 // Skip the border below the image
107 border_size.top + ceil_to_multiple(std::max(0, static_cast<int>(shape[1]) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom)), steps[1]),
108 steps[1]));
109
110 ++n;
111 }
112
113 if(shape.num_dimensions() > 2)
114 {
115 window.set(2, Window::Dimension(0, std::max<size_t>(1, shape[2]), steps[2]));
116
117 ++n;
118 }
119
120 for(; n < shape.num_dimensions(); ++n)
121 {
122 window.set(n, Window::Dimension(0, std::max<size_t>(1, shape[n])));
123 }
124
125 for(; n < Coordinates::num_max_dimensions; ++n)
126 {
127 window.set(n, Window::Dimension(0, 1));
128 }
129
130 return window;
131 }
132
calculate_max_enlarged_window(const ValidRegion & valid_region,const Steps & steps,BorderSize border_size)133 Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps, BorderSize border_size)
134 {
135 const Coordinates &anchor = valid_region.anchor;
136 const TensorShape &shape = valid_region.shape;
137
138 Window window;
139
140 window.set(0, Window::Dimension(
141 // move the anchor to the start from the border
142 anchor[0] - border_size.left,
143 // move the anchor to include the right end border
144 // Make sure the window width is a multiple of the step size
145 anchor[0] - border_size.left + ceil_to_multiple(shape[0] + border_size.left + border_size.right, steps[0]),
146 steps[0]));
147
148 size_t n = 1;
149
150 if(anchor.num_dimensions() > 1)
151 {
152 window.set(1, Window::Dimension(
153 // Include the border above the image
154 anchor[1] - border_size.top,
155 // Include the border below the image
156 anchor[1] - border_size.top + ceil_to_multiple(shape[1] + border_size.top + border_size.bottom, steps[1]),
157 steps[1]));
158
159 ++n;
160 }
161
162 if(anchor.num_dimensions() > 2)
163 {
164 window.set(2, Window::Dimension(0, std::max<size_t>(1, shape[n]), steps[2]));
165
166 ++n;
167 }
168
169 for(; n < anchor.num_dimensions(); ++n)
170 {
171 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
172 }
173
174 for(; n < Coordinates::num_max_dimensions; ++n)
175 {
176 window.set(n, Window::Dimension(0, 1));
177 }
178
179 return window;
180 }
181
calculate_max_window_horizontal(const ValidRegion & valid_region,const Steps & steps,bool skip_border,BorderSize border_size)182 Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
183 {
184 if(skip_border)
185 {
186 border_size.top = 0;
187 border_size.bottom = 0;
188 }
189 else
190 {
191 border_size.left = 0;
192 border_size.right = 0;
193 }
194
195 const Coordinates &anchor = valid_region.anchor;
196 const TensorShape &shape = valid_region.shape;
197
198 Window window;
199
200 window.set(0, Window::Dimension(
201 // Skip the border left of the image
202 anchor[0] + border_size.left,
203 // Skip the border right of the image
204 // Make sure the window width is a multiple of the step size
205 anchor[0] + border_size.left + ceil_to_multiple(std::max(0, static_cast<int>(shape[0]) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)), steps[0]),
206 steps[0]));
207
208 size_t n = 1;
209
210 if(anchor.num_dimensions() > 1)
211 {
212 window.set(1, Window::Dimension(
213 // Skip the border above the image
214 anchor[1] - border_size.top,
215 // Skip the border below the image
216 anchor[1] + shape[1] + border_size.bottom,
217 1));
218
219 ++n;
220 }
221
222 for(; n < anchor.num_dimensions(); ++n)
223 {
224 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
225 }
226
227 for(; n < Coordinates::num_max_dimensions; ++n)
228 {
229 window.set(n, Window::Dimension(0, 1));
230 }
231
232 return window;
233 }
234
calculate_squashed_or_max_window(const ITensorInfo & src0,const ITensorInfo & src1)235 std::pair<Window, size_t> calculate_squashed_or_max_window(const ITensorInfo &src0, const ITensorInfo &src1)
236 {
237 const auto &shape0 = src0.tensor_shape();
238 const auto &shape1 = src1.tensor_shape();
239 const auto &strides0 = src0.strides_in_bytes();
240 const auto &strides1 = src1.strides_in_bytes();
241 const auto num_dimensions = std::max(src0.num_dimensions(), src1.num_dimensions());
242
243 Window win;
244 size_t split_dimension = Window::DimY;
245 size_t dim = 0;
246
247 size_t squashed_bytes = src0.element_size();
248
249 // Try to squash the low dimensions together.
250 for(; dim < num_dimensions; ++dim)
251 {
252 if(shape0[dim] != shape1[dim] || strides0[dim] != squashed_bytes || strides1[dim] != squashed_bytes)
253 {
254 break;
255 }
256
257 squashed_bytes *= shape0[dim];
258 }
259
260 if(dim == num_dimensions)
261 {
262 auto squashed_elements = squashed_bytes / src0.element_size();
263
264 split_dimension = Window::DimX;
265
266 // The input tensors can be interpreted as 1D array.
267 win.set(0, Window::Dimension(0, squashed_elements, 1));
268
269 for(dim = 1; dim < Coordinates::num_max_dimensions; ++dim)
270 {
271 win.set(dim, Window::Dimension(0, 1, 1));
272 }
273 }
274 else
275 {
276 // Generates the max window.
277 for(dim = 0; dim < Coordinates::num_max_dimensions; ++dim)
278 {
279 win.set(dim, Window::Dimension(0, std::max(shape0[dim], shape1[dim]), 1));
280 }
281 }
282
283 return std::make_pair(win, split_dimension);
284 }
285
calculate_squashed_or_max_window(const ITensorInfo & src)286 std::pair<Window, size_t> calculate_squashed_or_max_window(const ITensorInfo &src)
287 {
288 const auto &shape = src.tensor_shape();
289 const auto &strides = src.strides_in_bytes();
290 const auto num_dimensions = src.num_dimensions();
291
292 Window win;
293 size_t split_dimension = Window::DimY;
294 size_t dim = 0;
295 size_t squashed_bytes = src.element_size();
296
297 // Try to squash the low dimensions together.
298 for(; dim < num_dimensions; ++dim)
299 {
300 if(strides[dim] != squashed_bytes)
301 {
302 break;
303 }
304 squashed_bytes *= shape[dim];
305 }
306 if(dim == num_dimensions)
307 {
308 const auto squashed_elements = squashed_bytes / src.element_size();
309 split_dimension = Window::DimX;
310 // The input tensor can be interpreted as 1D array.
311 win.set(0, Window::Dimension(0, squashed_elements, 1));
312 for(dim = 1; dim < Coordinates::num_max_dimensions; ++dim)
313 {
314 win.set(dim, Window::Dimension(0, 1, 1));
315 }
316 }
317 else
318 {
319 // Generate the max window.
320 for(dim = 0; dim < Coordinates::num_max_dimensions; ++dim)
321 {
322 win.set(dim, Window::Dimension(0, shape[dim], 1));
323 }
324 }
325 return std::make_pair(win, split_dimension);
326 }
327
328 } // namespace arm_compute
329