arm_compute v17.04
[platform/upstream/armcl.git] / src / core / AccessWindowTranspose.cpp
1 /*
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/AccessWindowTranspose.h"
25
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "arm_compute/core/Window.h"
29
30 using namespace arm_compute;
31
32 ValidRegion AccessWindowTranspose::compute_valid_region(const Window &window, ValidRegion input_valid_region, bool border_undefined, BorderSize border_size) const
33 {
34     if(_info == nullptr)
35     {
36         return input_valid_region;
37     }
38
39     Coordinates &anchor = input_valid_region.anchor;
40     TensorShape &shape  = input_valid_region.shape;
41     Coordinates  old_anchor(anchor);
42     TensorShape  old_shape(shape);
43
44     if(!border_undefined)
45     {
46         border_size = BorderSize(0);
47     }
48
49     // Start of the valid region is equal to the start of the window. But it
50     // cannot be less than the start of the input's valid region plus the border
51     // size required by this kernel (if undefined).
52     // Additionally the valid region is shifted by the offset that is used by
53     // the kernel to write back output values.
54     // Note that because the class can handle skewed transpose operations all
55     // size have to be scaled.
56     anchor.set(0, std::max<int>(DIV_CEIL(window.y().start(), window.y().step()) * _width,
57                                 DIV_CEIL((anchor[1] + border_size.top) * _width, window.y().step()) + _x));
58     anchor.set(1, std::max<int>(DIV_CEIL(window.x().start(), window.x().step()) * _height,
59                                 DIV_CEIL((anchor[0] + border_size.left) * _height, window.x().step()) + _y));
60
61     // End of the valid region is equal to the start of the last write of the
62     // kernel plus the number of written elements. (This assumes that all
63     // written elements are valid). Nevertheless the end cannot be larger than
64     // the end of the input's valid region minus the border size.
65     // Note: not the end points of the region are stored but its size. Thus the
66     // old size is first converted into end points to compared against the
67     // execution window. Afterwards the new end points are converted back into
68     // a size of the region.
69     // Note that because the class can handle skewed transpose operations all
70     // size have to be scaled.
71     shape.set(0, std::min<int>(((old_anchor[1] + old_shape[1] - border_size.right) * _width) / window.y().step(),
72                                (window.y().end() / window.y().step()) * _width));
73     shape.set(1, std::min<int>(((old_anchor[0] + old_shape[0] - border_size.bottom) * _height) / window.x().step(),
74                                (window.x().end() / window.x().step()) * _height));
75
76     // For higher dimensions use the intersection of the window size and the
77     // valid region of the input
78     for(size_t d = 2; d < _info->num_dimensions(); ++d)
79     {
80         anchor.set(d, std::max(window[d].start(), input_valid_region.anchor[d]));
81         shape.set(d, std::min<int>(window[d].end(), input_valid_region.shape[d]) - anchor[d]);
82     }
83
84     return input_valid_region;
85 }
86
87 bool AccessWindowTranspose::update_window_if_needed(Window &window) const
88 {
89     // Only update the window size if we can't use padding
90     if(_info == nullptr || _info->is_resizable())
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
101     int front_pad_y = 0;
102
103     // Transpose and scale according to the number ratio between processed elements in input and output
104     const int min_y = (window.x().start() / window.x().step()) * _height + _y;
105     const int max_y = (window.x().end() / window.x().step()) * _height + _y;
106
107     // Adjust window start for output's Y dimension (so X in (input) window)
108     if(min_y < 0)
109     {
110         // Calculate rows available above the tensor
111         const int front_pad_y_available = -offset_first_element / strides[1];
112
113         if(min_y < front_pad_y_available)
114         {
115             // Not enough padding available, need to shrink the window
116             const int start = ((adjust_up(min_y, front_pad_y_available, _height) - _y) / _height) * window.x().step();
117
118             window.set(0, Window::Dimension(start, window.x().end(), window.x().step()));
119             window_modified = true;
120         }
121
122         // Update front padding with reconstructed value
123         front_pad_y = std::max(0, -(window.x().start() / window.x().step()) * _height - _y);
124     }
125
126     // Adjust window end for Y dimension
127     if(max_y > static_cast<int>(shape[1]))
128     {
129         const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size();
130
131         // Calculate rows available below the tensor
132         const int tail_pad_y_available = (stride_z / strides[1]) - shape[1] - front_pad_y;
133
134         if(static_cast<int>(shape[1]) + tail_pad_y_available < max_y)
135         {
136             // Not enough padding available, need to shrink the window
137             const int end = ((adjust_down(max_y, shape[1] + tail_pad_y_available, _height) - _y) / _height) * window.x().step();
138             window.set(0, Window::Dimension(window.x().start(), end, window.x().step()));
139             window_modified = true;
140         }
141     }
142
143     int front_pad_x = 0;
144
145     // Transpose and scale according to the number ratio between processed elements in input and output
146     const int min_x = (window.y().start() / window.y().step()) * _width + _x;
147     const int max_x = (window.y().end() / window.y().step()) * _width + _x;
148
149     const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size();
150
151     // Adjust window start for X dimension
152     if(min_x < 0)
153     {
154         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]);
155
156         if(min_x < front_pad_x_available)
157         {
158             // Not enough padding available, need to shrink the window
159             const int start = ((adjust_up(min_x, front_pad_x_available, _width) - _x) / _width) * window.y().step();
160             window.set(1, Window::Dimension(start, window.y().end(), window.y().step()));
161             window_modified = true;
162         }
163
164         // Update front padding with reconstructed value
165         front_pad_x = std::max(0, -(window.y().start() / window.y().step()) * _width - _x);
166     }
167
168     // Adjust window end for X dimension
169     if(max_x > static_cast<int>(shape[0]))
170     {
171         const int tail_pad_x_available = (stride_y / strides[0]) - shape[0] - front_pad_x;
172
173         if(static_cast<int>(shape[0]) + tail_pad_x_available < max_x)
174         {
175             // Not enough padding available, need to shrink the window
176             const int end = ((adjust_down(max_x, shape[0] + tail_pad_x_available, _width) - _x) / _width) * window.y().step();
177             window.set(1, Window::Dimension(window.y().start(), end, window.y().step()));
178             window_modified = true;
179         }
180     }
181
182     window.validate();
183
184     return window_modified;
185 }
186
187 bool AccessWindowTranspose::update_padding_if_needed(const Window &window) const
188 {
189     // Only update the padding if the tensor allows it
190     if(_info == nullptr || !_info->is_resizable())
191     {
192         return false;
193     }
194
195     ARM_COMPUTE_ERROR_ON(window.y().step() == 0);
196     ARM_COMPUTE_ERROR_ON(window.x().step() == 0);
197
198     const int min_x = (window.y().start() / window.y().step()) * _width + _x;
199     const int max_x = (window.y().end() / window.y().step()) * _width + _x;
200     const int min_y = (window.x().start() / window.y().step()) * _height + _y;
201     const int max_y = (window.x().end() / window.x().step()) * _height + _y;
202
203     const TensorShape &shape = _info->tensor_shape();
204
205     PaddingSize padding;
206     padding.left   = std::max(0, -min_x);
207     padding.right  = std::max<int>(0, max_x - shape[0]);
208     padding.top    = shape.num_dimensions() == 1 ? 0 : std::max(0, -min_y);
209     padding.bottom = shape.num_dimensions() == 1 ? 0 : std::max<int>(0, max_y - shape[1]);
210
211     // Update strides in tensor info
212     return _info->extend_padding(padding);
213 }