Compute Library
 21.08
WindowHelpers.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2020-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  */
25 
26 namespace arm_compute
27 {
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 
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 
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 
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 } // namespace arm_compute
unsigned int top
top of the border
Definition: Types.h:372
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape shape
Shape of the valid region.
Definition: Types.h:252
Container for 2D border size.
Definition: Types.h:264
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
unsigned int bottom
bottom of the border
Definition: Types.h:374
const ValidRegion valid_region
Definition: Scale.cpp:223
Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Copyright (c) 2017-2021 Arm Limited.
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:71
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
unsigned int left
left of the border
Definition: Types.h:375
unsigned int right
right of the border
Definition: Types.h:373
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps, BorderSize border_size)
Container for valid region of a window.
Definition: Types.h:179
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:251