aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/NEON/kernels/NEConvolutionKernel.h
blob: 534e0b03c28e37dcb7ebfa6274e756ec5fdb23ea (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (c) 2016-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_NECONVOLUTIONKERNEL_H__
#define __ARM_COMPUTE_NECONVOLUTIONKERNEL_H__

#include "arm_compute/core/NEON/INEKernel.h"
#include "arm_compute/core/NEON/INESimpleKernel.h"

#include <array>
#include <cstdint>
#include <vector>

namespace arm_compute
{
class ITensor;

/****************************************************************************************\
 *                                    Square Convolution                                *
\****************************************************************************************/

/** Interface for the kernel to run an arbitrary size convolution on a tensor. (Currently supports 3x3, 5x5, 7x7 and 9x9).
 * The client can supply a convolution matrix \f$ C_{m,n} \f$.
 * @f{eqnarray}{
 *  k_0 &=& \frac{m}{2}  \\
 *  l_0 &=& \frac{n}{2}  \\
 *  sum &=& \sum_{k=0,l=0}^{k=m-1,l=n-1} input(x+k-k_0, y+l-l_0) C_{k,l}
 *  @f}
 *
 * @note The above equation for this function is similar to the default OpenCV Filter2D function,
 *       which actually computes a correlation and not a convolution.
 *       In case of a real convolution the convolution matrix should be flipped both horizontally and vertically.
 */
template <unsigned int matrix_size>
class NEConvolutionKernel : public INESimpleKernel
{
public:
    const char *name() const override
    {
        return "NEConvolutionKernel";
    }
    /** Default constructor */
    NEConvolutionKernel();
    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data type supported: U8.
     * @param[out] output           Destination tensor. Data types supported: U8, S16.
     * @param[in]  conv             Convolution matrix to apply to the input tensor.
     * @param[in]  scale            Scale of the convolution matrix. If 0 is passed, it will be set to the sum of the coefficients of the convolution or 1 if they add up to 0.
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     */
    void configure(const ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, bool border_undefined);

    // Inherited methods overridden:
    void run(const Window &window, const ThreadInfo &info) override;
    BorderSize border_size() const override;

private:
    template <typename OutputType>
    void convolution(const Window &win);

protected:
    uint32_t _scale;                                             /**< scale of the convolution */
    std::array<int16_t, matrix_size *matrix_size> _convolution;  /**< convolution matrix */
};

/** Interface for the kernel which applied a 3x3 convolution to a tensor.*/
using NEConvolution3x3Kernel = NEConvolutionKernel<3>;
/** Interface for the kernel which applied a 5x5 convolution to a tensor.*/
using NEConvolution5x5Kernel = NEConvolutionKernel<5>;
/** Interface for the kernel which applied a 7x7 convolution to a tensor.*/
using NEConvolution7x7Kernel = NEConvolutionKernel<7>;
///** Interface for the kernel which applied a 9x9 convolution to a tensor.*/
using NEConvolution9x9Kernel = NEConvolutionKernel<9>;

/****************************************************************************************\
 *                              Separable Square Convolution                            *
\****************************************************************************************/

/** Kernel for the Horizontal pass of a Separable Convolution */
template <unsigned int matrix_size>
class NESeparableConvolutionHorKernel : public INESimpleKernel
{
public:
    const char *name() const override
    {
        return "NESeparableConvolutionHorKernel";
    }
    /** Default constructor */
    NESeparableConvolutionHorKernel();

    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data type supported: U8.
     * @param[out] output           Destination tensor. Data types supported: U16, S16, S32.
     * @param[in]  conv_row         Convolution matrix to apply to the input tensor.
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     */
    void configure(const ITensor *input, ITensor *output, const int16_t *conv_row, bool border_undefined);

    // Inherited methods overridden:
    void run(const Window &window, const ThreadInfo &info) override;
    BorderSize border_size() const override;

private:
    /** Apply the object's convolution to the given window of the input tensor..
     *
     * @param[in] window Window to apply the convolution on.
     */
    template <typename OutputType>
    void convolve(const Window &window);

    std::array<int16_t, matrix_size> _conv_row; /**< Convolution coefficients */
    BorderSize _border_size;                    /**< Border size */
};

/** Interface for the kernel which applied a 5x1 horizontal convolution to a tensor.*/
using NESeparableConvolution5x5HorKernel = NESeparableConvolutionHorKernel<5>;
/** Interface for the kernel which applied a 7x1 horizontal convolution to a tensor.*/
using NESeparableConvolution7x7HorKernel = NESeparableConvolutionHorKernel<7>;
/** Interface for the kernel which applied a 9x1 horizontal convolution to a tensor.*/
using NESeparableConvolution9x9HorKernel = NESeparableConvolutionHorKernel<9>;

/** Kernel for the Vertical pass of a Separable Convolution */
template <unsigned int matrix_size>
class NESeparableConvolutionVertKernel : public INESimpleKernel
{
public:
    const char *name() const override
    {
        return "NESeparableConvolutionVertKernel";
    }
    /** Default constructor */
    NESeparableConvolutionVertKernel();

    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data type supported: U16, S16, S32.
     * @param[out] output           Destination tensor, Data types supported: U8, S16.
     * @param[in]  conv_col         Convolution matrix to apply to the input tensor.
     * @param[in]  scale            Scale of the convolution matrix
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     */
    void configure(const ITensor *input, ITensor *output, const int16_t *conv_col, uint32_t scale, bool border_undefined);

    // Inherited methods overridden:
    void run(const Window &window, const ThreadInfo &info) override;
    BorderSize border_size() const override;

private:
    /** Apply the object's convolution to the given window of the input tensor.
     *  This function is used if the intermediate values have been stored as U16.
     *
     * @param[in] win Window to apply the convolution on.
     */
    template <typename OutputType>
    void convolution_u16(const Window &win);
    /** Apply the object's convolution to the given window of the input tensor.
     *  This function is used if the intermediate values have been stored as S16.
     *
     * @param[in] win Window to apply the convolution on.
     */
    template <typename OutputType>
    void convolution_s16(const Window &win);
    /** Apply the object's convolution to the given window of the input tensor.
     *  This function is used if the intermediate values have been stored as S32.
     *
     * @param[in] win Window to apply the convolution on.
     */
    template <typename OutputType>
    void convolution_s32(const Window &win);

    std::array<int16_t, matrix_size> _conv_col; /**< Convolution coefficients */
    uint32_t _scale;                            /**< Convolution's scale */
};

/** Interface for the kernel which applied a 1x5 vertical convolution to a tensor.*/
using NESeparableConvolution5x5VertKernel = NESeparableConvolutionVertKernel<5>;
/** Interface for the kernel which applied a 1x7 vertical convolution to a tensor.*/
using NESeparableConvolution7x7VertKernel = NESeparableConvolutionVertKernel<7>;
/** Interface for the kernel which applied a 1x9 vertical convolution to a tensor.*/
using NESeparableConvolution9x9VertKernel = NESeparableConvolutionVertKernel<9>;

/****************************************************************************************\
 *                                 Rectangle Convolution                                *
\****************************************************************************************/

/** Kernel for the running convolution on a rectangle matrix.
 *
 * @note Supports combinations of 3,5,7 and 9.
 */
class NEConvolutionRectangleKernel : public INEKernel
{
public:
    const char *name() const override
    {
        return "NEConvolutionRectangleKernel";
    }
    /** Default constructor */
    NEConvolutionRectangleKernel();
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    NEConvolutionRectangleKernel(NEConvolutionRectangleKernel &) = delete;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    NEConvolutionRectangleKernel &operator=(NEConvolutionRectangleKernel &) = delete;
    /** Allow instances of this class to be moved */
    NEConvolutionRectangleKernel(NEConvolutionRectangleKernel &&) = default;
    /** Allow instances of this class to be moved */
    NEConvolutionRectangleKernel &operator=(NEConvolutionRectangleKernel &&) = default;
    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data type supported: U8.
     * @param[out] output           Destination tensor, Data types supported: U8, S16.
     * @param[in]  conv             Convolution matrix to apply to the input tensor.
     * @param[in]  width            Width of convolution matrix (Number of columns)
     * @param[in]  height           Height of convolution matrix (Number of rows)
     * @param[in]  scale            Scale of the convolution matrix. If 0 is passed, it will be set to the sum of the coefficients of the convolution or 1 if they add up to 0.
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     */
    void configure(const ITensor *input, ITensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined);

    // Inherited methods overridden:
    void run(const Window &window, const ThreadInfo &info) override;
    BorderSize border_size() const override;

private:
    unsigned int get_index(uint32_t val);
    /** Apply the object's convolution to the given window of the input tensor.
     *
     * @param[in] win Window to apply the convolution on.
     */
    template <typename OutputType, unsigned int rows, unsigned int cols>
    void convolution(const Window &win);

protected:
    const ITensor            *_input;       /**< Input tensor */
    ITensor                  *_output;      /**< Output tensor */
    uint32_t                  _scale;       /**< Scale of the convolution */
    std::vector<int16_t>      _convolution; /**< Convolution matrix */
    BorderSize                _border_size; /**< Calculated border width */
    uint32_t                  _func_idx;    /**< Index used to specify convolution function to be used */
    const static unsigned int _nr_supported_sizes
    {
        4
    }; /**< Number of supported permutations */
};
} // namespace arm_compute
#endif /*__ARM_COMPUTE_NECONVOLUTIONKERNEL_H__ */