Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 24 | #include "src/core/AccessWindowStatic.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensorInfo.h" |
| 28 | #include "arm_compute/core/Window.h" |
| 29 | |
| 30 | using namespace arm_compute; |
| 31 | |
| 32 | AccessWindowStatic::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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 37 | ValidRegion AccessWindowStatic::compute_valid_region(const Window &window, |
| 38 | ValidRegion input_valid_region, |
| 39 | bool border_undefined, |
| 40 | BorderSize border_size) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | { |
| 42 | ARM_COMPUTE_UNUSED(border_undefined); |
| 43 | ARM_COMPUTE_UNUSED(border_size); |
| 44 | |
| 45 | return compute_valid_region(window, input_valid_region); |
| 46 | } |
| 47 | |
| 48 | ValidRegion AccessWindowStatic::compute_valid_region(const Window &window, ValidRegion input_valid_region) const |
| 49 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | if (_info == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | { |
| 52 | return input_valid_region; |
| 53 | } |
| 54 | |
Georgios Pinitas | f78625b | 2018-02-22 12:00:22 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_UNUSED(window); |
| 56 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | if (_info->num_dimensions() > 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | if (_info->num_dimensions() > 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | { |
| 73 | shape.set(1, std::min<int>(_end_y, _info->tensor_shape()[1])); |
| 74 | } |
| 75 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | return input_valid_region; |
| 77 | } |
| 78 | |
| 79 | void AccessWindowStatic::set_valid_region(const Window &window, const ValidRegion &input_valid_region) |
| 80 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | if (_info != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | { |
| 83 | _info->set_valid_region(compute_valid_region(window, input_valid_region)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool AccessWindowStatic::update_window_if_needed(Window &window) const |
| 88 | { |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 89 | // If the padding is not enough and the tensor is not resizable, shrink the window to size 0 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | if (_info == nullptr || _info->is_resizable()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | { |
| 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 Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 101 | // Calculate if padding is enough |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | if (_start_y < 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | const int front_pad_y_available = -static_cast<int>(offset_first_element / strides[1]); |
| 105 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 106 | if (_start_y < front_pad_y_available) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | window_modified = true; |
| 109 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | if (!window_modified) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | if (_end_y > static_cast<int>(shape[1])) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | { |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 116 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 119 | if (static_cast<int>(shape[1]) + tail_pad_y_available < _end_y) |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 120 | { |
| 121 | window_modified = true; |
| 122 | } |
| 123 | } |
| 124 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | if (!window_modified) |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 126 | { |
| 127 | const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size(); |
| 128 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 129 | if (_start_x < 0) |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 130 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 131 | 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 Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 134 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 135 | if (_start_x < front_pad_x_available) |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 136 | { |
| 137 | window_modified = true; |
| 138 | } |
| 139 | } |
| 140 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | if (!window_modified && _end_x > static_cast<int>(shape[0])) |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 142 | { |
| 143 | const int tail_pad_x_available = (stride_y / strides[0]) - shape[0]; |
| 144 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | if (static_cast<int>(shape[0]) + tail_pad_x_available < _end_x) |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 146 | { |
| 147 | window_modified = true; |
| 148 | } |
| 149 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 153 | // If padding is not enough |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | if (window_modified) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 156 | for (size_t i = 0; i < Coordinates::num_max_dimensions; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | { |
Giorgio Arena | b81fa60 | 2017-11-28 18:31:34 +0000 | [diff] [blame] | 158 | window.set(i, Window::Dimension(0, 0, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | return window_modified; |
| 163 | } |
| 164 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 165 | bool AccessWindowStatic::update_padding_if_needed(const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | { |
| 167 | ARM_COMPUTE_UNUSED(window); |
| 168 | |
| 169 | // Only update the padding if the tensor allows it |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 170 | if (_info == nullptr || !_info->is_resizable()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | { |
| 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 Pinitas | ce54b56 | 2017-09-14 17:21:51 +0100 | [diff] [blame] | 180 | padding.top = std::max(0, -_start_y); |
| 181 | padding.bottom = std::max<int>(0, _end_y - shape[1]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | |
| 183 | // Update strides in tensor info |
| 184 | return _info->extend_padding(padding); |
| 185 | } |