blob: 8a7f37ac9b4bfd68e486643be00268c47d5bf768 [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/AccessWindowStatic.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensorInfo.h"
28#include "arm_compute/core/Window.h"
29
30using namespace arm_compute;
31
32AccessWindowStatic::AccessWindowStatic(ITensorInfo *info, int start_x, int start_y, int end_x, int end_y)
33 : _info(info), _start_x(start_x), _start_y(start_y), _end_x(end_x), _end_y(end_y)
34{
35}
36
37ValidRegion AccessWindowStatic::compute_valid_region(const Window &window, ValidRegion input_valid_region, bool border_undefined, BorderSize border_size) const
38{
39 ARM_COMPUTE_UNUSED(border_undefined);
40 ARM_COMPUTE_UNUSED(border_size);
41
42 return compute_valid_region(window, input_valid_region);
43}
44
45ValidRegion AccessWindowStatic::compute_valid_region(const Window &window, ValidRegion input_valid_region) const
46{
47 if(_info == nullptr)
48 {
49 return input_valid_region;
50 }
51
52 Coordinates &anchor = input_valid_region.anchor;
53 TensorShape &shape = input_valid_region.shape;
54
55 // Start of the valid region is equal to the start of the static access but
56 // never outside of the tensor.
57 anchor.set(0, std::max<int>(0, _start_x));
58 if(_info->num_dimensions() > 1)
59 {
60 anchor.set(1, std::max<int>(0, _start_y));
61 }
62
63 // End of the valid region is equal to the end of the static access but
64 // never outside of the tensor.
65 shape.set(0, std::min<int>(_end_x, _info->tensor_shape()[0]));
66 if(_info->num_dimensions() > 1)
67 {
68 shape.set(1, std::min<int>(_end_y, _info->tensor_shape()[1]));
69 }
70
71 // For higher dimension use the intersection of the window size and the
72 // valid region of the input
73 for(size_t d = 2; d < _info->num_dimensions(); ++d)
74 {
75 anchor.set(d, std::max(window[d].start(), input_valid_region.anchor[d]));
76 shape.set(d, std::min<int>(window[d].end(), input_valid_region.shape[d]) - anchor[d]);
77 }
78
79 return input_valid_region;
80}
81
82void AccessWindowStatic::set_valid_region(const Window &window, const ValidRegion &input_valid_region)
83{
84 if(_info != nullptr)
85 {
86 _info->set_valid_region(compute_valid_region(window, input_valid_region));
87 }
88}
89
90bool AccessWindowStatic::update_window_if_needed(Window &window) const
91{
92 // Only update the window size if we can't use padding
93 if(_info == nullptr || _info->is_resizable())
94 {
95 return false;
96 }
97
98 const TensorShape &shape = _info->tensor_shape();
99 const Strides &strides = _info->strides_in_bytes();
100 const size_t offset_first_element = _info->offset_first_element_in_bytes();
101
102 bool window_modified = false;
103
104 int front_pad_y = 0;
105
106 // Adjust window start for Y dimension
107 if(_start_y < 0)
108 {
109 // Calculate rows available above the tensor
110 const int front_pad_y_available = -static_cast<int>(offset_first_element / strides[1]);
111
112 if(_start_y < front_pad_y_available)
113 {
114 // Not enough padding available, need to shrink the window
115 const int start = adjust_up(_start_y, front_pad_y_available, window.y().step());
116
117 window.set(1, Window::Dimension(start, window.y().end(), window.y().step()));
118 window_modified = true;
119 }
120
121 // Update front padding with reconstructed value
122 front_pad_y = std::max(0, -window.y().start());
123 }
124
125 // Adjust window end for Y dimension
126 if(_end_y > static_cast<int>(shape[1]))
127 {
128 const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size();
129
130 // Calculate rows available below the tensor
131 const int tail_pad_y_available = (stride_z / strides[1]) - shape[1] - front_pad_y;
132
133 if(static_cast<int>(shape[1]) + tail_pad_y_available < _end_y)
134 {
135 // Not enough padding available, need to shrink the window
136 const int end = adjust_down(_end_y, shape[1] + tail_pad_y_available, window.y().step()) + window.y().step();
137 window.set(1, Window::Dimension(window.y().start(), end, window.y().step()));
138 window_modified = true;
139 }
140 }
141
142 int front_pad_x = 0;
143
144 const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size();
145
146 // Adjust window start for X dimension
147 if(_start_x < 0)
148 {
149 const int front_pad_x_available = -std::min<int>(static_cast<int>(offset_first_element) - front_pad_y * strides[1], stride_y - shape[0] * strides[0]) / static_cast<int>(strides[0]);
150
151 if(_start_x < front_pad_x_available)
152 {
153 // Not enough padding available, need to shrink the window
154 const int start = adjust_up(_start_x, front_pad_x_available, window.x().step());
155 window.set(0, Window::Dimension(start, window.x().end(), window.x().step()));
156 window_modified = true;
157 }
158
159 // Update front padding with reconstructed value
160 front_pad_x = std::max(0, -window.x().start());
161 }
162
163 // Adjust window end for X dimension
164 if(_end_x > static_cast<int>(shape[0]))
165 {
166 const int tail_pad_x_available = (stride_y / strides[0]) - shape[0] - front_pad_x;
167
168 if(static_cast<int>(shape[0]) + tail_pad_x_available < _end_x)
169 {
170 // Not enough padding available, need to shrink the window
171 const int end = adjust_down(_end_x, shape[0] + tail_pad_x_available, window.x().step()) + window.x().step();
172 window.set(0, Window::Dimension(window.x().start(), end, window.x().step()));
173 window_modified = true;
174 }
175 }
176
177 window.validate();
178
179 return window_modified;
180}
181
182bool AccessWindowStatic::update_padding_if_needed(const Window &window) const
183{
184 ARM_COMPUTE_UNUSED(window);
185
186 // Only update the padding if the tensor allows it
187 if(_info == nullptr || !_info->is_resizable())
188 {
189 return false;
190 }
191
192 const TensorShape &shape = _info->tensor_shape();
193
194 PaddingSize padding;
195 padding.left = std::max(0, -_start_x);
196 padding.right = std::max<int>(0, _end_x - shape[0]);
Georgios Pinitasce54b562017-09-14 17:21:51 +0100197 padding.top = std::max(0, -_start_y);
198 padding.bottom = std::max<int>(0, _end_y - shape[1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199
200 // Update strides in tensor info
201 return _info->extend_padding(padding);
202}