blob: afd654a5953e2b9b2ae55001eaea098460b7654e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrouebcebf12020-10-21 00:04:14 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NECONVOLUTION_H
25#define ARM_COMPUTE_NECONVOLUTION_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Types.h"
28#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas658039b2017-09-15 16:30:50 +010029#include "arm_compute/runtime/IMemoryManager.h"
30#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/runtime/NEON/INESimpleFunction.h"
32#include "arm_compute/runtime/Tensor.h"
33
34#include <cstdint>
Georgios Pinitas658039b2017-09-15 16:30:50 +010035#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37namespace arm_compute
38{
39class ITensor;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010040class NEFillBorderKernel;
41template <unsigned int matrix_size>
42class NEConvolutionKernel;
43template <unsigned int matrix_size>
44class NESeparableConvolutionHorKernel;
45template <unsigned int matrix_size>
46class NESeparableConvolutionVertKernel;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48/** Basic function to execute convolution of size 3x3. This function calls the following NEON kernels:
49 *
50 * -# @ref NEFillBorderKernel (executed if border_mode == CONSTANT or border_mode == REPLICATE)
51 * -# @ref NEConvolution3x3Kernel
52 *
morgolock0c862652020-11-06 08:59:45 +000053 * @deprecated This function is deprecated and is intended to be removed in 21.05 release
54 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 */
56class NEConvolution3x3 : public INESimpleFunction
57{
58public:
Michalis Spyrouebcebf12020-10-21 00:04:14 +010059 /** Constructor */
60 NEConvolution3x3() = default;
61 /** Prevent instances of this class from being copied (As this class contains pointers) */
62 NEConvolution3x3(const NEConvolution3x3 &) = delete;
63 /** Prevent instances of this class from being copied (As this class contains pointers) */
64 NEConvolution3x3 &operator=(const NEConvolution3x3 &) = delete;
65 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
66 NEConvolution3x3(NEConvolution3x3 &&) = delete;
67 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
68 NEConvolution3x3 &operator=(NEConvolution3x3 &&) = delete;
69 /** Default destructor */
70 ~NEConvolution3x3();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 /** Initialize the function's source, destination, conv and border_mode.
72 *
73 * @param[in,out] input Source tensor. Data type supported: U8. (Written to only for @p border_mode != UNDEFINED)
74 * @param[out] output Destination tensor, Data types supported: U8/S16.
75 * @param[in] conv Matrix_size x matrix_size S16 coefficients structured as a row-major 2D array in a linear buffer.
76 * @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.
77 * @param[in] border_mode Strategy to use for borders.
78 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
79 */
80 void configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value = 0);
81};
82
83/** Basic function to execute convolution of size 5x5, 7x7, 9x9. This function calls the following NEON kernels:
84 *
85 * -# @ref NEFillBorderKernel (executed if border_mode == CONSTANT or border_mode == REPLICATE)
86 * -# @ref NEConvolutionKernel or<br/>
87 * @ref NESeparableConvolutionHorKernel and @ref NESeparableConvolutionVertKernel (if convolution matrix is separable)
88 *
morgolock0c862652020-11-06 08:59:45 +000089 * @deprecated This function is deprecated and is intended to be removed in 21.05 release
90 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 */
92template <unsigned int matrix_size>
93class NEConvolutionSquare : public IFunction
94{
95public:
96 /** Default constructor */
Georgios Pinitas658039b2017-09-15 16:30:50 +010097 NEConvolutionSquare(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Michalis Spyrouebcebf12020-10-21 00:04:14 +010098 /** Prevent instances of this class from being copied (As this class contains pointers) */
99 NEConvolutionSquare(const NEConvolutionSquare &) = delete;
100 /** Prevent instances of this class from being copied (As this class contains pointers) */
101 NEConvolutionSquare &operator=(const NEConvolutionSquare &) = delete;
102 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
103 NEConvolutionSquare(NEConvolutionSquare &&) = delete;
104 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
105 NEConvolutionSquare &operator=(NEConvolutionSquare &&) = delete;
106 /** Default destructor */
107 ~NEConvolutionSquare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 /** Initialize the function's source, destination, conv and border_mode.
109 *
110 * @param[in,out] input Source tensor. Data type supported: U8. (Written to only for @p border_mode != UNDEFINED)
111 * @param[out] output Destination tensor, Data types supported: U8 or S16.
112 * @param[in] conv matrix_size x matrix_size S16 coefficients structured as a row-major 2D array in a linear buffer.
113 * @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.
114 * @param[in] border_mode Strategy to use for borders.
115 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
116 */
117 void configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value = 0);
118
119 // Inherited methods overridden:
120 void run() override;
121
122private:
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100123 MemoryGroup _memory_group; /**< Function memory group */
124 Tensor _tmp; /**< temporary buffer for output of horizontal pass */
125 bool _is_separable; /**< true if the convolution can be separated */
126 std::unique_ptr<NESeparableConvolutionHorKernel<matrix_size>> _kernel_hor; /**< kernel for horizontal pass of separated convolution */
127 std::unique_ptr<NESeparableConvolutionVertKernel<matrix_size>> _kernel_vert; /**< kernel for vertical pass of separated convolution */
128 std::unique_ptr<NEConvolutionKernel<matrix_size>> _kernel; /**< kernel for non-separated convolution **/
129 std::unique_ptr<NEFillBorderKernel> _border_handler; /**< kernel for border handling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130};
131
132/** Basic function to run 5x5 convolution. */
133using NEConvolution5x5 = NEConvolutionSquare<5>;
134/** Basic function to run 7x7 convolution. */
135using NEConvolution7x7 = NEConvolutionSquare<7>;
136/** Basic function to run 9x9 convolution. */
137using NEConvolution9x9 = NEConvolutionSquare<9>;
138
139/** Basic function to execute non-square convolution. This function calls the following NEON kernels:
140 *
141 * -# @ref NEFillBorderKernel (executed if border_mode == CONSTANT or border_mode == REPLICATE)
142 * -# @ref NEConvolutionRectangleKernel or<br/>
143 *
144 * @note Convolution rectangle should have dimensions of 3, 5, 7, 9
morgolock0c862652020-11-06 08:59:45 +0000145 *
146 * @deprecated This function is deprecated and is intended to be removed in 21.05 release
147 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 */
149class NEConvolutionRectangle : public INESimpleFunction
150{
151public:
Michalis Spyrouebcebf12020-10-21 00:04:14 +0100152 /** Constructor */
153 NEConvolutionRectangle() = default;
154 /** Prevent instances of this class from being copied (As this class contains pointers) */
155 NEConvolutionRectangle(const NEConvolutionRectangle &) = delete;
156 /** Prevent instances of this class from being copied (As this class contains pointers) */
157 NEConvolutionRectangle &operator=(const NEConvolutionRectangle &) = delete;
158 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
159 NEConvolutionRectangle(NEConvolutionRectangle &&) = delete;
160 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
161 NEConvolutionRectangle &operator=(NEConvolutionRectangle &&) = delete;
162 /** Default destructor */
163 ~NEConvolutionRectangle();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 /** Initialize the function's source, destination, conv and border_mode.
165 *
166 * @param[in,out] input Source tensor. Data type supported: U8. (Written to only for @p border_mode != UNDEFINED)
167 * @param[out] output Destination tensor, Data types supported: U8 or S16.
168 * @param[in] conv Matrix_size x matrix_size S16 coefficients structured as a row-major 2D array in a linear buffer.
169 * @param[in] rows Rows of convolution kernel.
170 * @param[in] cols Columns of convolution kernel.
171 * @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.
172 * @param[in] border_mode Strategy to use for borders.
173 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
174 */
175 void configure(ITensor *input, ITensor *output, const int16_t *conv, uint32_t rows, uint32_t cols, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value = 0);
176};
177}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000178#endif /*ARM_COMPUTE_NECONVOLUTION_H */