blob: 0607011bc5ebfd7c8f1a91a3ec401b8e80f1f2e1 [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
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
Georgios Pinitasf78625b2018-02-22 12:00:22 +000052 ARM_COMPUTE_UNUSED(window);
53
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 Coordinates &anchor = input_valid_region.anchor;
55 TensorShape &shape = input_valid_region.shape;
56
57 // Start of the valid region is equal to the start of the static access but
58 // never outside of the tensor.
59 anchor.set(0, std::max<int>(0, _start_x));
60 if(_info->num_dimensions() > 1)
61 {
62 anchor.set(1, std::max<int>(0, _start_y));
63 }
64
65 // End of the valid region is equal to the end of the static access but
66 // never outside of the tensor.
67 shape.set(0, std::min<int>(_end_x, _info->tensor_shape()[0]));
68 if(_info->num_dimensions() > 1)
69 {
70 shape.set(1, std::min<int>(_end_y, _info->tensor_shape()[1]));
71 }
72
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 return input_valid_region;
74}
75
76void AccessWindowStatic::set_valid_region(const Window &window, const ValidRegion &input_valid_region)
77{
78 if(_info != nullptr)
79 {
80 _info->set_valid_region(compute_valid_region(window, input_valid_region));
81 }
82}
83
84bool AccessWindowStatic::update_window_if_needed(Window &window) const
85{
Giorgio Arenab81fa602017-11-28 18:31:34 +000086 // If the padding is not enough and the tensor is not resizable, shrink the window to size 0
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 if(_info == nullptr || _info->is_resizable())
88 {
89 return false;
90 }
91
92 const TensorShape &shape = _info->tensor_shape();
93 const Strides &strides = _info->strides_in_bytes();
94 const size_t offset_first_element = _info->offset_first_element_in_bytes();
95
96 bool window_modified = false;
97
Giorgio Arenab81fa602017-11-28 18:31:34 +000098 // Calculate if padding is enough
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 if(_start_y < 0)
100 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 const int front_pad_y_available = -static_cast<int>(offset_first_element / strides[1]);
102
103 if(_start_y < front_pad_y_available)
104 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 window_modified = true;
106 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 }
108
Giorgio Arenab81fa602017-11-28 18:31:34 +0000109 if(!window_modified)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 {
Giorgio Arenab81fa602017-11-28 18:31:34 +0000111 if(_end_y > static_cast<int>(shape[1]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 {
Giorgio Arenab81fa602017-11-28 18:31:34 +0000113 const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size();
114 const int tail_pad_y_available = (stride_z / strides[1]) - shape[1];
115
116 if(static_cast<int>(shape[1]) + tail_pad_y_available < _end_y)
117 {
118 window_modified = true;
119 }
120 }
121
122 if(!window_modified)
123 {
124 const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size();
125
126 if(_start_x < 0)
127 {
128 const int front_pad_x_available = -std::min<int>(static_cast<int>(offset_first_element), stride_y - shape[0] * strides[0]) / static_cast<int>(strides[0]);
129
130 if(_start_x < front_pad_x_available)
131 {
132 window_modified = true;
133 }
134 }
135
136 if(!window_modified && _end_x > static_cast<int>(shape[0]))
137 {
138 const int tail_pad_x_available = (stride_y / strides[0]) - shape[0];
139
140 if(static_cast<int>(shape[0]) + tail_pad_x_available < _end_x)
141 {
142 window_modified = true;
143 }
144 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 }
146 }
147
Giorgio Arenab81fa602017-11-28 18:31:34 +0000148 // If padding is not enough
149 if(window_modified)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 {
Giorgio Arenab81fa602017-11-28 18:31:34 +0000151 for(size_t i = 0; i < Coordinates::num_max_dimensions; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 {
Giorgio Arenab81fa602017-11-28 18:31:34 +0000153 window.set(i, Window::Dimension(0, 0, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 }
155 }
156
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 return window_modified;
158}
159
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000160bool AccessWindowStatic::update_padding_if_needed(const Window &window)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161{
162 ARM_COMPUTE_UNUSED(window);
163
164 // Only update the padding if the tensor allows it
165 if(_info == nullptr || !_info->is_resizable())
166 {
167 return false;
168 }
169
170 const TensorShape &shape = _info->tensor_shape();
171
172 PaddingSize padding;
173 padding.left = std::max(0, -_start_x);
174 padding.right = std::max<int>(0, _end_x - shape[0]);
Georgios Pinitasce54b562017-09-14 17:21:51 +0100175 padding.top = std::max(0, -_start_y);
176 padding.bottom = std::max<int>(0, _end_y - shape[1]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177
178 // Update strides in tensor info
179 return _info->extend_padding(padding);
180}