blob: 694b58c81f8d359300091e155c384083dc5589a5 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 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#ifndef __ARM_COMPUTE_NECONVOLUTIONKERNEL_H__
25#define __ARM_COMPUTE_NECONVOLUTIONKERNEL_H__
26
27#include "arm_compute/core/NEON/INEKernel.h"
28#include "arm_compute/core/NEON/INESimpleKernel.h"
29
30#include <array>
31#include <cstdint>
32#include <vector>
33
34namespace arm_compute
35{
36class ITensor;
37
38/****************************************************************************************\
39 * Square Convolution *
40\****************************************************************************************/
41
42/** Interface for the kernel to run an arbitrary size convolution on a tensor. (Currently supports 3x3, 5x5, 7x7 and 9x9).
43 * The client can supply a convolution matrix \f$ C_{m,n} \f$.
44 * @f{eqnarray}{
45 * k_0 &=& \frac{m}{2} \\
46 * l_0 &=& \frac{n}{2} \\
47 * sum &=& \sum_{k=0,l=0}^{k=m-1,l=n-1} input(x+k-k_0, y+l-l_0) C_{k,l}
48 * @f}
49 *
50 * @note The above equation for this function is similar to the default OpenCV Filter2D function,
51 * which actually computes a correlation and not a convolution.
52 * In case of a real convolution the convolution matrix should be flipped both horizontally and vertically.
53 */
54template <unsigned int matrix_size>
55class NEConvolutionKernel : public INESimpleKernel
56{
57public:
58 /** Default constructor */
59 NEConvolutionKernel();
60 /** Initialise the kernel's input, output and border mode.
61 *
62 * @param[in] input Source tensor. Data type supported: U8.
63 * @param[out] output Destination tensor. Data types supported: U8, S16.
64 * @param[in] conv Convolution matrix to apply to the input tensor.
65 * @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.
66 * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant.
67 */
68 void configure(const ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, bool border_undefined);
69
70 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010071 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 BorderSize border_size() const override;
73
74private:
75 template <typename OutputType>
76 void convolution(const Window &win);
77
78protected:
79 uint32_t _scale; /**< scale of the convolution */
80 std::array<int16_t, matrix_size *matrix_size> _convolution; /**< convolution matrix */
81};
82
83/** Interface for the kernel which applied a 3x3 convolution to a tensor.*/
84using NEConvolution3x3Kernel = NEConvolutionKernel<3>;
85/** Interface for the kernel which applied a 5x5 convolution to a tensor.*/
86using NEConvolution5x5Kernel = NEConvolutionKernel<5>;
87/** Interface for the kernel which applied a 7x7 convolution to a tensor.*/
88using NEConvolution7x7Kernel = NEConvolutionKernel<7>;
89///** Interface for the kernel which applied a 9x9 convolution to a tensor.*/
90using NEConvolution9x9Kernel = NEConvolutionKernel<9>;
91
92/****************************************************************************************\
93 * Separable Square Convolution *
94\****************************************************************************************/
95
96/** Kernel for the Horizontal pass of a Separable Convolution */
97template <unsigned int matrix_size>
98class NESeparableConvolutionHorKernel : public INESimpleKernel
99{
100public:
101 /** Default constructor */
102 NESeparableConvolutionHorKernel();
103
104 /** Initialise the kernel's input, output and border mode.
105 *
106 * @param[in] input Source tensor. Data type supported: U8.
107 * @param[out] output Destination tensor. Data types supported: U16, S16, S32.
108 * @param[in] conv_row Convolution matrix to apply to the input tensor.
109 * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant.
110 */
111 void configure(const ITensor *input, ITensor *output, const int16_t *conv_row, bool border_undefined);
112
113 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100114 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 BorderSize border_size() const override;
116
117private:
118 /** Apply the object's convolution to the given window of the input tensor..
119 *
120 * @param[in] window Window to apply the convolution on.
121 */
122 template <typename OutputType>
123 void convolve(const Window &window);
124
125 std::array<int16_t, matrix_size> _conv_row; /**< Convolution coefficients */
126 BorderSize _border_size; /**< Border size */
127};
128
129/** Interface for the kernel which applied a 5x1 horizontal convolution to a tensor.*/
130using NESeparableConvolution5x5HorKernel = NESeparableConvolutionHorKernel<5>;
131/** Interface for the kernel which applied a 7x1 horizontal convolution to a tensor.*/
132using NESeparableConvolution7x7HorKernel = NESeparableConvolutionHorKernel<7>;
133/** Interface for the kernel which applied a 9x1 horizontal convolution to a tensor.*/
134using NESeparableConvolution9x9HorKernel = NESeparableConvolutionHorKernel<9>;
135
136/** Kernel for the Vertical pass of a Separable Convolution */
137template <unsigned int matrix_size>
138class NESeparableConvolutionVertKernel : public INESimpleKernel
139{
140public:
141 /** Default constructor */
142 NESeparableConvolutionVertKernel();
143
144 /** Initialise the kernel's input, output and border mode.
145 *
146 * @param[in] input Source tensor. Data type supported: U16, S16, S32.
147 * @param[out] output Destination tensor, Data types supported: U8, S16.
148 * @param[in] conv_col Convolution matrix to apply to the input tensor.
149 * @param[in] scale Scale of the convolution matrix
150 * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant.
151 */
152 void configure(const ITensor *input, ITensor *output, const int16_t *conv_col, uint32_t scale, bool border_undefined);
153
154 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100155 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 BorderSize border_size() const override;
157
158private:
159 /** Apply the object's convolution to the given window of the input tensor.
160 * This function is used if the intermediate values have been stored as U16.
161 *
162 * @param[in] win Window to apply the convolution on.
163 */
164 template <typename OutputType>
165 void convolution_u16(const Window &win);
166 /** Apply the object's convolution to the given window of the input tensor.
167 * This function is used if the intermediate values have been stored as S16.
168 *
169 * @param[in] win Window to apply the convolution on.
170 */
171 template <typename OutputType>
172 void convolution_s16(const Window &win);
173 /** Apply the object's convolution to the given window of the input tensor.
174 * This function is used if the intermediate values have been stored as S32.
175 *
176 * @param[in] win Window to apply the convolution on.
177 */
178 template <typename OutputType>
179 void convolution_s32(const Window &win);
180
181 std::array<int16_t, matrix_size> _conv_col; /**< Convolution coefficients */
182 uint32_t _scale; /**< Convolution's scale */
183};
184
185/** Interface for the kernel which applied a 1x5 vertical convolution to a tensor.*/
186using NESeparableConvolution5x5VertKernel = NESeparableConvolutionVertKernel<5>;
187/** Interface for the kernel which applied a 1x7 vertical convolution to a tensor.*/
188using NESeparableConvolution7x7VertKernel = NESeparableConvolutionVertKernel<7>;
189/** Interface for the kernel which applied a 1x9 vertical convolution to a tensor.*/
190using NESeparableConvolution9x9VertKernel = NESeparableConvolutionVertKernel<9>;
191
192/****************************************************************************************\
193 * Rectangle Convolution *
194\****************************************************************************************/
195
196/** Kernel for the running convolution on a rectangle matrix.
197 *
198 * @note Supports combinations of 3,5,7 and 9.
199 */
200class NEConvolutionRectangleKernel : public INEKernel
201{
202public:
203 /** Default constructor */
204 NEConvolutionRectangleKernel();
205 /** Prevent instances of this class from being copied (As this class contains pointers) */
206 NEConvolutionRectangleKernel(NEConvolutionRectangleKernel &) = delete;
207 /** Prevent instances of this class from being copied (As this class contains pointers) */
208 NEConvolutionRectangleKernel &operator=(NEConvolutionRectangleKernel &) = delete;
209 /** Allow instances of this class to be moved */
210 NEConvolutionRectangleKernel(NEConvolutionRectangleKernel &&) = default;
211 /** Allow instances of this class to be moved */
212 NEConvolutionRectangleKernel &operator=(NEConvolutionRectangleKernel &&) = default;
213 /** Initialise the kernel's input, output and border mode.
214 *
215 * @param[in] input Source tensor. Data type supported: U8.
216 * @param[out] output Destination tensor, Data types supported: U8, S16.
217 * @param[in] conv Convolution matrix to apply to the input tensor.
218 * @param[in] width Width of convolution matrix (Number of columns)
219 * @param[in] height Height of convolution matrix (Number of rows)
220 * @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.
221 * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant.
222 */
223 void configure(const ITensor *input, ITensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined);
224
225 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100226 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227 BorderSize border_size() const override;
228
229private:
230 unsigned int get_index(uint32_t val);
231 /** Apply the object's convolution to the given window of the input tensor.
232 *
233 * @param[in] win Window to apply the convolution on.
234 */
235 template <typename OutputType, unsigned int rows, unsigned int cols>
236 void convolution(const Window &win);
237
238protected:
239 const ITensor *_input; /**< Input tensor */
240 ITensor *_output; /**< Output tensor */
241 uint32_t _scale; /**< Scale of the convolution */
242 std::vector<int16_t> _convolution; /**< Convolution matrix */
243 BorderSize _border_size; /**< Calculated border width */
244 uint32_t _func_idx; /**< Index used to specify convolution function to be used */
245 const static unsigned int _nr_supported_sizes
246 {
247 4
248 }; /**< Number of supported permutations */
249};
250}
251#endif /*__ARM_COMPUTE_NECONVOLUTIONKERNEL_H__ */