blob: 98182b12022ad776629a00eb78b8e1fd9e1588bf [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2017-2020 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 */
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010024#include "src/core/AccessWindowStatic.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037ValidRegion AccessWindowStatic::compute_valid_region(const Window &window,
38 ValidRegion input_valid_region,
39 bool border_undefined,
40 BorderSize border_size) const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
42 ARM_COMPUTE_UNUSED(border_undefined);
43 ARM_COMPUTE_UNUSED(border_size);
44
45 return compute_valid_region(window, input_valid_region);
46}
47
48ValidRegion AccessWindowStatic::compute_valid_region(const Window &window, ValidRegion input_valid_region) const
49{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 if (_info == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 {
52 return input_valid_region;
53 }
54
Georgios Pinitasf78625b2018-02-22 12:00:22 +000055 ARM_COMPUTE_UNUSED(window);
56
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 Coordinates &anchor = input_valid_region.anchor;
58 TensorShape &shape = input_valid_region.shape;
59
60 // Start of the valid region is equal to the start of the static access but
61 // never outside of the tensor.
62 anchor.set(0, std::max<int>(0, _start_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>(0, _start_y));
66 }
67
68 // End of the valid region is equal to the end of the static access but
69 // never outside of the tensor.
70 shape.set(0, std::min<int>(_end_x, _info->tensor_shape()[0]));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 if (_info->num_dimensions() > 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 {
73 shape.set(1, std::min<int>(_end_y, _info->tensor_shape()[1]));
74 }
75
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 return input_valid_region;
77}
78
79void AccessWindowStatic::set_valid_region(const Window &window, const ValidRegion &input_valid_region)
80{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 if (_info != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 {
83 _info->set_valid_region(compute_valid_region(window, input_valid_region));
84 }
85}
86
87bool AccessWindowStatic::update_window_if_needed(Window &window) const
88{
Giorgio Arenab81fa602017-11-28 18:31:34 +000089 // If the padding is not enough and the tensor is not resizable, shrink the window to size 0
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 if (_info == nullptr || _info->is_resizable())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 {
92 return false;
93 }
94
95 const TensorShape &shape = _info->tensor_shape();
96 const Strides &strides = _info->strides_in_bytes();
97 const size_t offset_first_element = _info->offset_first_element_in_bytes();
98
99 bool window_modified = false;
100
Giorgio Arenab81fa602017-11-28 18:31:34 +0000101 // Calculate if padding is enough
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 if (_start_y < 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 const int front_pad_y_available = -static_cast<int>(offset_first_element / strides[1]);
105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 if (_start_y < front_pad_y_available)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 window_modified = true;
109 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 }
111
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 if (!window_modified)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 if (_end_y > static_cast<int>(shape[1]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 {
Giorgio Arenab81fa602017-11-28 18:31:34 +0000116 const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size();
117 const int tail_pad_y_available = (stride_z / strides[1]) - shape[1];
118
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 if (static_cast<int>(shape[1]) + tail_pad_y_available < _end_y)
Giorgio Arenab81fa602017-11-28 18:31:34 +0000120 {
121 window_modified = true;
122 }
123 }
124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 if (!window_modified)
Giorgio Arenab81fa602017-11-28 18:31:34 +0000126 {
127 const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size();
128
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129 if (_start_x < 0)
Giorgio Arenab81fa602017-11-28 18:31:34 +0000130 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131 const int front_pad_x_available =
132 -std::min<int>(static_cast<int>(offset_first_element), stride_y - shape[0] * strides[0]) /
133 static_cast<int>(strides[0]);
Giorgio Arenab81fa602017-11-28 18:31:34 +0000134
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100135 if (_start_x < front_pad_x_available)
Giorgio Arenab81fa602017-11-28 18:31:34 +0000136 {
137 window_modified = true;
138 }
139 }
140
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100141 if (!window_modified && _end_x > static_cast<int>(shape[0]))
Giorgio Arenab81fa602017-11-28 18:31:34 +0000142 {
143 const int tail_pad_x_available = (stride_y / strides[0]) - shape[0];
144
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145 if (static_cast<int>(shape[0]) + tail_pad_x_available < _end_x)
Giorgio Arenab81fa602017-11-28 18:31:34 +0000146 {
147 window_modified = true;
148 }
149 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 }
151 }
152
Giorgio Arenab81fa602017-11-28 18:31:34 +0000153 // If padding is not enough
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 if (window_modified)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 for (size_t i = 0; i < Coordinates::num_max_dimensions; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 {
Giorgio Arenab81fa602017-11-28 18:31:34 +0000158 window.set(i, Window::Dimension(0, 0, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 }
160 }
161
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 return window_modified;
163}
164
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000165bool AccessWindowStatic::update_padding_if_needed(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166{
167 ARM_COMPUTE_UNUSED(window);
168
169 // Only update the padding if the tensor allows it
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 if (_info == nullptr || !_info->is_resizable())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 {
172 return false;
173 }
174
175 const TensorShape &shape = _info->tensor_shape();
176
177 PaddingSize padding;
178 padding.left = std::max(0, -_start_x);
179 padding.right = std::max<int>(0, _end_x - shape[0]);
Georgios Pinitasce54b562017-09-14 17:21:51 +0100180 padding.top = std::max(0, -_start_y);
181 padding.bottom = std::max<int>(0, _end_y - shape[1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182
183 // Update strides in tensor info
184 return _info->extend_padding(padding);
185}