blob: 923c5f8a8593a6d377e0eb1e414095d9f901e1a5 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2018 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/IAccessWindow.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Window.h"
29
30using namespace arm_compute;
31
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032ValidRegion AccessWindowRectangle::compute_valid_region(const Window &window,
33 const ValidRegion &input_valid_region) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034{
35 return compute_valid_region(window, input_valid_region, false, BorderSize(0));
36}
37
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038ValidRegion AccessWindowRectangle::compute_valid_region(const Window &window,
39 ValidRegion input_valid_region,
40 bool border_undefined,
41 BorderSize border_size) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043 if (_info == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 {
45 return input_valid_region;
46 }
47
48 Coordinates &anchor = input_valid_region.anchor;
49 Coordinates old_anchor(anchor);
50 TensorShape &shape = input_valid_region.shape;
51
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 if (!border_undefined)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 {
54 border_size = BorderSize(0);
55 }
56
57 // Start of the valid region is equal to the start of the window. But it
58 // cannot be less than the start of the input's valid region plus the border
59 // size required by this kernel (if undefined).
60 // Additionally the valid region is shifted by the offset that is used by
61 // the kernel to write back output values.
62 anchor.set(0, std::max<int>(window.x().start() * _scale_x, anchor[0] + border_size.left) + _x);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 if (_info->num_dimensions() > 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 {
65 anchor.set(1, std::max<int>(window.y().start() * _scale_y, anchor[1] + border_size.top) + _y);
66 }
67
68 // End of the valid region is equal to the start of the last write of the
69 // kernel plus the number of written elements. (This assumes that all
70 // written elements are valid). Nevertheless the end cannot be larger than
71 // the end of the input's valid region minus the border size.
72 // Note: not the end points of the region are stored but its size. Thus the
73 // old size is first converted into end points to compared against the
74 // execution window. Afterwards the new end points are converted back into
75 // a size of the region.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 shape.set(0, std::min<int>(old_anchor[0] + shape[0] - border_size.right,
77 (window.x().end() - window.x().step()) * _scale_x + _width) -
78 anchor[0]);
79 if (_info->num_dimensions() > 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 shape.set(1, std::min<int>(old_anchor[1] + shape[1] - border_size.bottom,
82 (window.y().end() - window.y().step()) * _scale_y + _height) -
83 anchor[1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 }
85
86 // For higher dimensions use the intersection of the window size and the
87 // valid region of the input
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 for (size_t d = 2; d < _info->num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 {
90 anchor.set(d, std::max(window[d].start(), input_valid_region.anchor[d]));
91 shape.set(d, std::min<int>(window[d].end(), input_valid_region.shape[d]) - anchor[d]);
92 }
93
94 return input_valid_region;
95}
96
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097void AccessWindowRectangle::set_valid_region(const Window &window,
98 const ValidRegion &input_valid_region,
99 bool border_undefined,
100 const BorderSize &border_size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 if (_info != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 {
104 _info->set_valid_region(compute_valid_region(window, input_valid_region, border_undefined, border_size));
105 }
106}
107
108bool AccessWindowRectangle::update_window_if_needed(Window &window) const
109{
110 // Only update the window size if we can't use padding
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100111 if (_info == nullptr || _info->is_resizable())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 {
113 return false;
114 }
115
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 PaddingSize needed = get_needed_padding(window);
Anthony Barbieree50f512018-11-15 09:58:00 +0000117 PaddingSize available = _info->padding();
118
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 if (needed.top <= available.top && needed.right <= available.right && needed.bottom <= available.bottom &&
120 needed.left <= available.left)
Anthony Barbieree50f512018-11-15 09:58:00 +0000121 {
122 return false;
123 }
124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 const TensorShape &shape = _info->tensor_shape();
126 const Strides &strides = _info->strides_in_bytes();
127 const size_t offset_first_element = _info->offset_first_element_in_bytes();
128
129 bool window_modified = false;
130
131 int front_pad_y = 0;
132
133 const int min_y = window.y().start() * _scale_y + _y;
134 const int max_y = (window.y().end() - window.y().step()) * _scale_y + _y + _height;
135
136 // Adjust window start for Y dimension
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 if (min_y < 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 {
139 // Calculate rows available above the tensor
140 const int front_pad_y_available = -static_cast<int>(offset_first_element / strides[1]);
141
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 if (min_y < front_pad_y_available)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 {
144 // Not enough padding available, need to shrink the window
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000145 int start = adjust_up(min_y, front_pad_y_available, window.y().step() * _scale_y) - _y;
146 start = std::min<int>(start / _scale_y, window.y().end());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000148 window.set(1, Window::Dimension(start, window.y().end(), window.y().step()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 window_modified = true;
150 }
151
152 // Update front padding with reconstructed value
153 front_pad_y = std::max(0, static_cast<int>(std::floor(-window.y().start() * _scale_y)) - _y);
154 }
155
156 // Adjust window end for Y dimension
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 if (max_y > static_cast<int>(shape[1]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 {
159 const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size();
160
161 // Calculate rows available below the tensor
162 const int tail_pad_y_available = (stride_z / strides[1]) - shape[1] - front_pad_y;
163
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 if (static_cast<int>(shape[1]) + tail_pad_y_available < max_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 {
166 // Not enough padding available, need to shrink the window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 int end = adjust_down(max_y, shape[1] + tail_pad_y_available, window.y().step() * _scale_y) +
168 window.y().step() * _scale_y - _y - _height;
169 end = std::max<int>(window.y().start(), end / _scale_y);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000170
171 window.set(1, Window::Dimension(window.y().start(), end, window.y().step()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 window_modified = true;
173 }
174 }
175
176 int front_pad_x = 0;
177
178 const int min_x = window.x().start() * _scale_x + _x;
179 const int max_x = (window.x().end() - window.x().step()) * _scale_x + _x + _width;
180
181 const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size();
182
183 // Adjust window start for X dimension
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 if (min_x < 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186 const int front_pad_x_available =
187 -std::min<int>(static_cast<int>(offset_first_element) - front_pad_y * strides[1],
188 stride_y - shape[0] * strides[0]) /
189 static_cast<int>(strides[0]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 if (min_x < front_pad_x_available)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 {
193 // Not enough padding available, need to shrink the window
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000194 int start = adjust_up(min_x, front_pad_x_available, window.x().step() * _scale_x) - _x;
195 start = std::min<int>(start / _scale_x, window.x().end());
196
197 window.set(0, Window::Dimension(start, window.x().end(), window.x().step()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 window_modified = true;
199 }
200
201 // Update front padding with reconstructed value
202 front_pad_x = std::max(0, static_cast<int>(std::floor(-window.x().start() * _scale_x)) - _x);
203 }
204
205 // Adjust window end for X dimension
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100206 if (max_x > static_cast<int>(shape[0]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207 {
208 const int tail_pad_x_available = (stride_y / strides[0]) - shape[0] - front_pad_x;
209
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100210 if (static_cast<int>(shape[0]) + tail_pad_x_available < max_x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 {
212 // Not enough padding available, need to shrink the window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100213 int end = adjust_down(max_x, shape[0] + tail_pad_x_available, window.x().step() * _scale_x) +
214 window.x().step() * _scale_x - _x - _width;
215 end = std::max<int>(window.x().start(), end / _scale_x);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000216
217 window.set(0, Window::Dimension(window.x().start(), end, window.x().step()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218 window_modified = true;
219 }
220 }
221
222 window.validate();
223
224 return window_modified;
225}
226
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000227bool AccessWindowRectangle::update_padding_if_needed(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228{
229 // Only update the padding if the tensor allows it
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100230 if (_info == nullptr || !_info->is_resizable())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 {
232 return false;
233 }
Anthony Barbieree50f512018-11-15 09:58:00 +0000234 // Update strides in tensor info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100235 return _info->extend_padding(get_needed_padding(window));
Anthony Barbieree50f512018-11-15 09:58:00 +0000236}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100238PaddingSize AccessWindowRectangle::get_needed_padding(const Window &window) const
Anthony Barbieree50f512018-11-15 09:58:00 +0000239{
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000240 ARM_COMPUTE_ERROR_ON(_scale_x == 0);
241 ARM_COMPUTE_ERROR_ON(_scale_y == 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100242
243 const int min_x = window.x().start() * _scale_x + _x;
244 const int max_x = (window.x().end() - window.x().step()) * _scale_x + _x + _width;
245 const int min_y = window.y().start() * _scale_y + _y;
246 const int max_y = (window.y().end() - window.y().step()) * _scale_y + _y + _height;
247
248 const TensorShape &shape = _info->tensor_shape();
249
250 PaddingSize padding;
251 padding.left = std::max(0, -min_x);
252 padding.right = std::max<int>(0, max_x - shape[0]);
Moritz Pflanzer5eab6c72017-09-17 12:33:31 +0100253 padding.top = std::max(0, -min_y);
254 padding.bottom = std::max<int>(0, max_y - shape[1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255
Anthony Barbieree50f512018-11-15 09:58:00 +0000256 return padding;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257}