Compute Library
 21.05
WindowHelpers.h
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  */
24 #ifndef SRC_CORE_HELPERS_WINDOWHELPERS_H
25 #define SRC_CORE_HELPERS_WINDOWHELPERS_H
26 
28 #include "arm_compute/core/Steps.h"
30 
31 namespace arm_compute
32 {
33 /** Update window and padding size for each of the access patterns.
34  *
35  * First the window size is reduced based on all access patterns that are not
36  * allowed to modify the padding of the underlying tensor. Then the padding of
37  * the remaining tensors is increased to match the window.
38  *
39  * @param[in] win Window that is used by the kernel.
40  * @param[in] patterns Access patterns used to calculate the final window and padding.
41  *
42  * @return True if the window has been changed. Changes to the padding do not
43  * influence the returned value.
44  */
45 template <typename... Ts>
46 bool update_window_and_padding(Window &win, Ts &&... patterns)
47 {
48  bool window_changed = false;
49 
50  utility::for_each([&](const IAccessWindow & w)
51  {
52  window_changed |= w.update_window_if_needed(win);
53  },
54  patterns...);
55 
56  bool padding_changed = false;
57 
59  {
60  padding_changed |= w.update_padding_if_needed(win);
61  },
62  patterns...);
63 
64  return window_changed;
65 }
66 
67 /** Intersect multiple valid regions.
68  *
69  * @param[in] regions Valid regions.
70  *
71  * @return Intersection of all regions.
72  */
73 template <typename... Ts>
74 ValidRegion intersect_valid_regions(const Ts &... regions)
75 {
76  auto intersect = [](const ValidRegion & r1, const ValidRegion & r2) -> ValidRegion
77  {
78  ValidRegion region;
79 
80  for(size_t d = 0; d < std::min(r1.anchor.num_dimensions(), r2.anchor.num_dimensions()); ++d)
81  {
82  region.anchor.set(d, std::max(r1.anchor[d], r2.anchor[d]));
83  }
84 
85  for(size_t d = 0; d < std::min(r1.shape.num_dimensions(), r2.shape.num_dimensions()); ++d)
86  {
87  region.shape.set(d, std::min(r1.shape[d], r2.shape[d]));
88  }
89 
90  return region;
91  };
92 
93  return utility::foldl(intersect, regions...);
94 }
95 
96 #ifndef DOXYGEN_SKIP_THIS
97 /** Calculate the maximum window for a given tensor shape and border setting
98  *
99  * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
100  * @param[in] steps (Optional) Number of elements processed for each step.
101  * @param[in] skip_border (Optional) If true exclude the border region from the window.
102  * @param[in] border_size (Optional) Border size.
103  *
104  * @return The maximum window the kernel can be executed on.
105  */
106 Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize());
107 
108 /** Calculate the maximum window for a given tensor shape and border setting
109  *
110  * @param[in] shape Shape of the tensor space
111  * @param[in] steps (Optional) Number of elements processed for each step.
112  * @param[in] skip_border (Optional) If true exclude the border region from the window.
113  * @param[in] border_size (Optional) Border size.
114  *
115  * @return The maximum window the kernel can be executed on.
116  */
117 Window calculate_max_window(const TensorShape &shape, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize());
118 
119 /** Calculate the maximum window for a given tensor shape and border setting
120  *
121  * @param[in] info Tensor info object defining the shape of the object for which the window is created.
122  * @param[in] steps (Optional) Number of elements processed for each step.
123  * @param[in] skip_border (Optional) If true exclude the border region from the window.
124  * @param[in] border_size (Optional) Border size.
125  *
126  * @return The maximum window the kernel can be executed on.
127  */
128 inline Window calculate_max_window(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize())
129 {
130  return calculate_max_window(info.tensor_shape(), steps, skip_border, border_size);
131 }
132 
133 /** Calculate the maximum window used by a horizontal kernel for a given tensor shape and border setting
134  *
135  * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
136  * @param[in] steps (Optional) Number of elements processed for each step.
137  * @param[in] skip_border (Optional) If true exclude the border region from the window.
138  * @param[in] border_size (Optional) Border size. The border region will be excluded from the window.
139  *
140  * @return The maximum window the kernel can be executed on.
141  */
142 Window calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize());
143 
144 /** Calculate the maximum window used by a horizontal kernel for a given tensor shape and border setting
145  *
146  * @param[in] info Tensor info object defining the shape of the object for which the window is created.
147  * @param[in] steps (Optional) Number of elements processed for each step.
148  * @param[in] skip_border (Optional) If true exclude the border region from the window.
149  * @param[in] border_size (Optional) Border size.
150  *
151  * @return The maximum window the kernel can be executed on.
152  */
153 inline Window calculate_max_window_horizontal(const ITensorInfo &info, const Steps &steps = Steps(), bool skip_border = false, BorderSize border_size = BorderSize())
154 {
155  return calculate_max_window_horizontal(info.valid_region(), steps, skip_border, border_size);
156 }
157 
158 /** Calculate the maximum window for a given tensor shape and border setting. The window will also includes the border.
159  *
160  * @param[in] valid_region Valid region object defining the shape of the tensor space for which the window is created.
161  * @param[in] steps (Optional) Number of elements processed for each step.
162  * @param[in] border_size (Optional) Border size. The border region will be included in the window.
163  *
164  * @return The maximum window the kernel can be executed on.
165  */
166 Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps = Steps(), BorderSize border_size = BorderSize());
167 
168 /** Calculate the maximum window for a given tensor shape and border setting. The window will also includes the border.
169  *
170  * @param[in] info Tensor info object defining the shape of the object for which the window is created.
171  * @param[in] steps (Optional) Number of elements processed for each step.
172  * @param[in] border_size (Optional) Border size. The border region will be included in the window.
173  *
174  * @return The maximum window the kernel can be executed on.
175  */
176 inline Window calculate_max_enlarged_window(const ITensorInfo &info, const Steps &steps = Steps(), BorderSize border_size = BorderSize())
177 {
178  return calculate_max_enlarged_window(info.valid_region(), steps, border_size);
179 }
180 
181 /** Function to compute the shape of output and window for the given inputs
182  *
183  * @param[in] infos Input tensor informations
184  *
185  * @return A pair of the shape and window
186  */
187 template <typename... Shapes>
188 std::pair<TensorShape, Window> compute_output_shape_and_window(const Shapes &... shapes)
189 {
190  const TensorShape out_shape = TensorShape::broadcast_shape(shapes...);
191  return std::make_pair(out_shape, calculate_max_window(out_shape));
192 }
193 #endif /* DOXYGEN_SKIP_THIS */
194 } // namespace arm_compute
195 
196 #endif /* SRC_CORE_HELPERS_WINDOWHELPERS_H */
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
SimpleTensor< float > w
Definition: DFT.cpp:156
TensorShape shape
Shape of the valid region.
Definition: Types.h:255
static TensorShape broadcast_shape(const Shapes &... shapes)
If shapes are broadcast compatible, return the broadcasted shape.
Definition: TensorShape.h:211
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.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
T && foldl(F &&, T &&value)
Base case of foldl.
Definition: Utility.h:132
Interface describing methods to update access window and padding based on kernel parameters.
Definition: IAccessWindow.h:71
ValidRegion intersect_valid_regions(const Ts &... regions)
Intersect multiple valid regions.
Definition: WindowHelpers.h:74
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:110
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
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:182
Describe a multidimensional execution window.
Definition: Window.h:39
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:254