aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/IAccessWindow.h
blob: 22232ab554d257c5f06e6a8eb16d5419745be917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (c) 2017-2018 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef __ARM_COMPUTE_IACCESS_WINDOW_H__
#define __ARM_COMPUTE_IACCESS_WINDOW_H__

#include "arm_compute/core/Coordinates.h"
#include "arm_compute/core/TensorShape.h"
#include "arm_compute/core/Types.h"

#include <array>

namespace arm_compute
{
class Window;
class ITensorInfo;

/** Decrease @p required in steps of @p step until it's less than @p available.
 *
 * @param[in] required  Number of required bytes.
 * @param[in] available Number of available bytes.
 * @param[in] step      Step size used to decrease required bytes.
 *
 * @return Largest value smaller than @p available that is a multiple of @p step
 *
 **/
inline int adjust_down(int required, int available, int step)
{
    ARM_COMPUTE_ERROR_ON(step <= 0);

    return required - step * ((required - available + step - 1) / step);
}

/** Increase @p required in steps of @p step until it's greater than @p available.
 *
 * @param[in] required  Number of required bytes.
 * @param[in] available Number of available bytes.
 * @param[in] step      Step size used to increase required bytes.
 *
 * @return Largest value smaller than @p available that is a multiple of @p step
 *
 **/
inline int adjust_up(int required, int available, int step)
{
    ARM_COMPUTE_ERROR_ON(step <= 0);

    return required + step * ((available - required + step - 1) / step);
}

/** Interface describing methods to update access window and padding based on kernel parameters. */
class IAccessWindow
{
public:
    /** Default virtual destructor */
    virtual ~IAccessWindow() = default;
    /** Shrink the window if padding is not large enough.
     *
     * @param[in] window Window used by the kernel.
     *
     * @return True if the window has been changed.
     *
     */
    virtual bool update_window_if_needed(Window &window) const = 0;
    /** Increase the padding to be large enough for the window.
     *
     * @param[in] window Window used by the kernel.
     *
     * @return True if the padding has been changed.
     */
    virtual bool update_padding_if_needed(const Window &window) = 0;
    /** Compute the valid region based on access pattern and valid region of the inputs.
     *
     * @note This method assumes that there is no border.
     *
     * @param[in] window             Execution window of the kernel.
     * @param[in] input_valid_region Combined valid region of all inputs.
     * @param[in] border_undefined   Undefined borders are excluded from the valid region.
     * @param[in] border_size        Size of the border around the XY-plane of the tensor.
     *
     * @return a valid region.
     *
     */
    virtual ValidRegion compute_valid_region(const Window &window, ValidRegion input_valid_region, bool border_undefined, BorderSize border_size) const = 0;
};

/** Implementation of a rectangular access pattern. */
class AccessWindowRectangle : public IAccessWindow
{
public:
    /** Constructor for a rectangular access pattern.
     *
     * @note Width and height have to be non-negative.
     *
     * @param[in,out] info   Tensor info of the accessed kernel.
     * @param[in]     x      Offset of the access in X direction.
     * @param[in]     y      Offset of the access in Y direction.
     * @param[in]     width  Number of elements that are accessed in X direction.
     * @param[in]     height Number of elements that are accessed in Y direction.
     */
    AccessWindowRectangle(ITensorInfo *info, int x, int y, int width, int height)
        : AccessWindowRectangle(info, x, y, width, height, 1.f, 1.f)
    {
    }

    /** Constructor for a rectangular access pattern.
     *
     * @note Width, height and scale have to be non-negative.
     *
     * @param[in,out] info    Tensor info of the accessed kernel.
     * @param[in]     x       Offset of the access in X direction.
     * @param[in]     y       Offset of the access in Y direction.
     * @param[in]     width   Number of elements that are accessed in X direction.
     * @param[in]     height  Number of elements that are accessed in Y direction.
     * @param[in]     scale_x Ratio along the X direction between the window used by the execute_window_loop and the rectangular access pattern defined
     * @param[in]     scale_y Ratio along the Y direction between the window used by the execute_window_loop and the rectangular access pattern defined
     */
    AccessWindowRectangle(ITensorInfo *info, int x, int y, int width, int height, float scale_x, float scale_y)
        : _info(info), _x(x), _y(y), _width(width), _height(height), _scale_x(scale_x), _scale_y(scale_y)
    {
        ARM_COMPUTE_ERROR_ON(width < 0);
        ARM_COMPUTE_ERROR_ON(height < 0);
        ARM_COMPUTE_ERROR_ON(scale_x < 0);
        ARM_COMPUTE_ERROR_ON(scale_y < 0);
    }

    /** Prevent instances of this class from being copied (As this class contains pointers) */
    AccessWindowRectangle(const AccessWindowRectangle &) = delete;
    /** Allow instances of this class to be move constructed */
    AccessWindowRectangle(AccessWindowRectangle &&) = default;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    AccessWindowRectangle &operator=(const AccessWindowRectangle &) = delete;
    /** Allow instances of this class to be moved */
    AccessWindowRectangle &operator=(AccessWindowRectangle &&) = default;
    /** Default destructor */
    ~AccessWindowRectangle() = default;

    /** Set the valid region based on access pattern, valid region of the inputs and border mode.
     *
     * @param[in] window             Execution window of the kernel.
     * @param[in] input_valid_region Combined valid region of all inputs.
     * @param[in] border_undefined   (Optional) Undefined borders are excluded from the valid region.
     * @param[in] border_size        (Optional) Size of the border around the XY-plane of the tensor.
     */
    void set_valid_region(const Window &window, const ValidRegion &input_valid_region, bool border_undefined = false, const BorderSize &border_size = BorderSize(0));

    /** Compute the valid region based on access pattern, valid region of the inputs and border mode.
     *
     * @note This method assumes that there is no border.
     *
     * @param[in] window             Execution window of the kernel.
     * @param[in] input_valid_region Combined valid region of all inputs.
     *
     * @return a valid region.
     *
     */
    ValidRegion compute_valid_region(const Window &window, const ValidRegion &input_valid_region) const;

    // Inherited methods overridden:

    /** Compute the valid region based on access pattern and valid region of the inputs.
     *
     * @note This method assumes that all elements written by the kernel are valid.
     *
     * @param[in] window             Execution window of the kernel.
     * @param[in] input_valid_region Combined valid region of all inputs.
     * @param[in] border_undefined   Undefined borders are excluded from the valid region.
     * @param[in] border_size        Size of the border around the XY-plane of the tensor.
     *
     * @return a valid region.
     *
     */
    ValidRegion compute_valid_region(const Window &window, ValidRegion input_valid_region, bool border_undefined, BorderSize border_size) const override;

    bool update_window_if_needed(Window &window) const override;
    bool update_padding_if_needed(const Window &window) override;
protected:
    PaddingSize get_needed_padding(const Window &window)const;

protected:
    ITensorInfo *_info;
    int          _x;
    int          _y;
    int          _width;
    int          _height;
    float        _scale_x;
    float        _scale_y;
};

/** Implementation of a column access pattern. */
class AccessWindowVertical : public AccessWindowRectangle
{
public:
    /** Constructor for a column access pattern.
     *
     * @note Height has to be non-negative.
     *
     * @param[in,out] info    Tensor info of the accessed kernel.
     * @param[in]     y       Offset of the access in Y direction.
     * @param[in]     height  Number of elements that are accessed in Y direction.
     * @param[in]     scale_y Ratio along the Y direction between the window used by the execute_window_loop and the rectangular access pattern defined
     */
    AccessWindowVertical(ITensorInfo *info, int y, int height, float scale_y = 1.f)
        : AccessWindowRectangle(info, 0, y, 1, height, 1.f, scale_y)
    {
        ARM_COMPUTE_ERROR_ON(height < 0);
        ARM_COMPUTE_ERROR_ON(scale_y < 0);
    }
};

/** Implementation of a row access pattern. */
class AccessWindowHorizontal : public AccessWindowRectangle
{
public:
    /** Constructor for a row access pattern.
     *
     * @note Width has to be non-negative.
     *
     * @param[in,out] info    Tensor info of the accessed kernel.
     * @param[in]     x       Offset of the access in X direction.
     * @param[in]     width   Number of elements that are accessed in X direction.
     * @param[in]     scale_x Ratio along the X direction between the window used by the execute_window_loop and the rectangular access pattern defined
     */
    AccessWindowHorizontal(ITensorInfo *info, int x, int width, float scale_x = 1.f)
        : AccessWindowRectangle(info, x, 0, width, 1, scale_x, 1.f)
    {
        ARM_COMPUTE_ERROR_ON(width < 0);
        ARM_COMPUTE_ERROR_ON(scale_x < 0);
    }
};
} // namespace arm_compute
#endif /*__ARM_COMPUTE_IACCESS_WINDOW_H__*/