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/AccessWindowTranspose.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 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | ValidRegion AccessWindowTranspose::compute_valid_region(const Window &window, |
| 33 | ValidRegion input_valid_region, |
| 34 | bool border_undefined, |
| 35 | BorderSize border_size) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 37 | if (_info == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | { |
| 39 | return input_valid_region; |
| 40 | } |
| 41 | |
| 42 | Coordinates &anchor = input_valid_region.anchor; |
| 43 | TensorShape &shape = input_valid_region.shape; |
| 44 | Coordinates old_anchor(anchor); |
| 45 | TensorShape old_shape(shape); |
| 46 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | if (!border_undefined) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
| 49 | border_size = BorderSize(0); |
| 50 | } |
| 51 | |
| 52 | // Start of the valid region is equal to the start of the window. But it |
| 53 | // cannot be less than the start of the input's valid region plus the border |
| 54 | // size required by this kernel (if undefined). |
| 55 | // Additionally the valid region is shifted by the offset that is used by |
| 56 | // the kernel to write back output values. |
| 57 | // As the relation between input and output is transposed window.y() is |
| 58 | // used for x anchor and window.x() for y anchor. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | if (_info->dimension(0) > 1) |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 60 | { |
| 61 | anchor.set(0, std::max<int>(window.y().start() * _scale_x, anchor[1] + border_size.top) + _x); |
| 62 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | anchor.set(1, std::max<int>(window.x().start() * _scale_y, anchor[0] + border_size.left) + _y); |
| 64 | |
| 65 | // End of the valid region is equal to the start of the last write of the |
| 66 | // kernel plus the number of written elements. (This assumes that all |
| 67 | // written elements are valid). Nevertheless the end cannot be larger than |
| 68 | // the end of the input's valid region minus the border size. |
| 69 | // Note: not the end points of the region are stored but its size. Thus the |
| 70 | // old size is first converted into end points to compared against the |
| 71 | // execution window. Afterwards the new end points are converted back into |
| 72 | // a size of the region. |
| 73 | // As the relation between input and output is transposed window.y() is |
| 74 | // used for x shape and window.x() for y shape. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | if (_info->dimension(0) > 1) |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 76 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | shape.set(0, std::min<int>((old_anchor[1] + old_shape[0]) * _scale_x - border_size.right, |
| 78 | (window.y().end() - window.y().step()) * _scale_x + _width) - |
| 79 | anchor[0]); |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 80 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | shape.set(1, std::min<int>((old_anchor[0] + old_shape[1]) * _scale_y - border_size.bottom, |
| 82 | (window.x().end() - window.x().step()) * _scale_y + _height) - |
| 83 | anchor[1]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | |
| 85 | // For higher dimensions use the intersection of the window size and the |
| 86 | // valid region of the input |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | for (size_t d = 2; d < _info->num_dimensions(); ++d) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | { |
| 89 | anchor.set(d, std::max(window[d].start(), input_valid_region.anchor[d])); |
| 90 | shape.set(d, std::min<int>(window[d].end(), input_valid_region.shape[d]) - anchor[d]); |
| 91 | } |
| 92 | |
| 93 | return input_valid_region; |
| 94 | } |
| 95 | |
| 96 | bool AccessWindowTranspose::update_window_if_needed(Window &window) const |
| 97 | { |
| 98 | // Only update the window size if we can't use padding |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 99 | if (_info == nullptr || _info->is_resizable()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | const TensorShape &shape = _info->tensor_shape(); |
| 105 | const Strides &strides = _info->strides_in_bytes(); |
| 106 | const size_t offset_first_element = _info->offset_first_element_in_bytes(); |
| 107 | |
| 108 | bool window_modified = false; |
| 109 | |
| 110 | int front_pad_y = 0; |
| 111 | |
| 112 | // Transpose and scale |
| 113 | const int min_y = window.x().start() * _scale_y + _y; |
| 114 | const int max_y = window.x().end() * _scale_y + _y; |
| 115 | |
| 116 | // Adjust window start for output's Y dimension (so X in (input) window) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | if (min_y < 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | { |
| 119 | // Calculate rows available above the tensor |
| 120 | const int front_pad_y_available = -offset_first_element / strides[1]; |
| 121 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | if (min_y < front_pad_y_available) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | { |
| 124 | // Not enough padding available, need to shrink the window |
| 125 | const int start = adjust_up(min_y, front_pad_y_available, window.x().step() * _scale_y) - _y; |
| 126 | |
| 127 | window.set(0, Window::Dimension(start / _scale_y, window.x().end(), window.x().step())); |
| 128 | window_modified = true; |
| 129 | } |
| 130 | |
| 131 | // Update front padding with reconstructed value |
| 132 | front_pad_y = std::max(0, static_cast<int>(std::floor(-window.x().start() * _scale_y)) - _y); |
| 133 | } |
| 134 | |
| 135 | // Adjust window end for Y dimension |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | if (max_y > static_cast<int>(shape[1])) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | { |
| 138 | const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size(); |
| 139 | |
| 140 | // Calculate rows available below the tensor |
| 141 | const int tail_pad_y_available = (stride_z / strides[1]) - shape[1] - front_pad_y; |
| 142 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | if (static_cast<int>(shape[1]) + tail_pad_y_available < max_y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | { |
| 145 | // Not enough padding available, need to shrink the window |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 146 | const int end = adjust_down(max_y, shape[1] + tail_pad_y_available, window.x().step() * _scale_y) + |
| 147 | window.x().step() * _scale_y - _y - _height; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | window.set(0, Window::Dimension(window.x().start(), end / _scale_y, window.x().step())); |
| 149 | window_modified = true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | int front_pad_x = 0; |
| 154 | |
| 155 | // Transpose and scale |
| 156 | const int min_x = window.y().start() * _scale_x + _x; |
| 157 | const int max_x = window.y().end() * _scale_x + _x; |
| 158 | |
| 159 | const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size(); |
| 160 | |
| 161 | // Adjust window start for X dimension |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 162 | if (min_x < 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 164 | const int front_pad_x_available = |
| 165 | -std::min<int>(static_cast<int>(offset_first_element) - front_pad_y * strides[1], |
| 166 | stride_y - shape[0] * strides[0]) / |
| 167 | static_cast<int>(strides[0]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 169 | if (min_x < front_pad_x_available) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | { |
| 171 | // Not enough padding available, need to shrink the window |
| 172 | const int start = adjust_up(min_x, front_pad_x_available, window.y().step() * _scale_x) - _x; |
| 173 | window.set(1, Window::Dimension(start / _scale_x, window.y().end(), window.y().step())); |
| 174 | window_modified = true; |
| 175 | } |
| 176 | |
| 177 | // Update front padding with reconstructed value |
| 178 | front_pad_x = std::max(0, static_cast<int>(std::floor(-window.y().start() * _scale_x)) - _x); |
| 179 | } |
| 180 | |
| 181 | // Adjust window end for X dimension |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 182 | if (max_x > static_cast<int>(shape[0])) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | { |
| 184 | const int tail_pad_x_available = (stride_y / strides[0]) - shape[0] - front_pad_x; |
| 185 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 186 | if (static_cast<int>(shape[0]) + tail_pad_x_available < max_x) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | { |
| 188 | // Not enough padding available, need to shrink the window |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 189 | const int end = adjust_down(max_x, shape[0] + tail_pad_x_available, window.y().step() * _scale_x) + |
| 190 | window.y().step() * _scale_x - _x - _width; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | window.set(1, Window::Dimension(window.y().start(), end / _scale_x, window.y().step())); |
| 192 | window_modified = true; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | window.validate(); |
| 197 | |
| 198 | return window_modified; |
| 199 | } |
| 200 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 201 | bool AccessWindowTranspose::update_padding_if_needed(const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 202 | { |
| 203 | // Only update the padding if the tensor allows it |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 204 | if (_info == nullptr || !_info->is_resizable()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | ARM_COMPUTE_ERROR_ON(window.y().step() == 0); |
| 210 | ARM_COMPUTE_ERROR_ON(window.x().step() == 0); |
| 211 | |
| 212 | const int min_x = window.y().start() * _scale_x + _x; |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 213 | const int max_x = (window.y().end() - window.y().step()) * _scale_x + _x + _width; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | const int min_y = window.x().start() * _scale_y + _y; |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 215 | const int max_y = (window.x().end() - window.x().step()) * _scale_y + _y + _height; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | |
| 217 | const TensorShape &shape = _info->tensor_shape(); |
| 218 | |
| 219 | PaddingSize padding; |
| 220 | padding.left = std::max(0, -min_x); |
| 221 | padding.right = std::max<int>(0, max_x - shape[0]); |
Georgios Pinitas | 96880cf | 2017-10-20 18:52:20 +0100 | [diff] [blame] | 222 | padding.top = std::max(0, -min_y); |
| 223 | padding.bottom = std::max<int>(0, max_y - shape[1]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | |
| 225 | // Update strides in tensor info |
| 226 | return _info->extend_padding(padding); |
| 227 | } |