1 /* 2 * Copyright (c) 2016-2020 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_NECONVOLUTION_H 25 #define ARM_COMPUTE_NECONVOLUTION_H 26 27 #include "arm_compute/core/Types.h" 28 #include "arm_compute/runtime/IFunction.h" 29 #include "arm_compute/runtime/IMemoryManager.h" 30 #include "arm_compute/runtime/MemoryGroup.h" 31 #include "arm_compute/runtime/NEON/INESimpleFunction.h" 32 #include "arm_compute/runtime/Tensor.h" 33 34 #include <cstdint> 35 #include <memory> 36 37 namespace arm_compute 38 { 39 class ITensor; 40 class NEFillBorderKernel; 41 template <unsigned int matrix_size> 42 class NEConvolutionKernel; 43 template <unsigned int matrix_size> 44 class NESeparableConvolutionHorKernel; 45 template <unsigned int matrix_size> 46 class NESeparableConvolutionVertKernel; 47 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 * 53 * @deprecated This function is deprecated and is intended to be removed in 21.05 release 54 * 55 */ 56 class NEConvolution3x3 : public INESimpleFunction 57 { 58 public: 59 /** 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(); 71 /** 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 * 89 * @deprecated This function is deprecated and is intended to be removed in 21.05 release 90 * 91 */ 92 template <unsigned int matrix_size> 93 class NEConvolutionSquare : public IFunction 94 { 95 public: 96 /** Default constructor */ 97 NEConvolutionSquare(std::shared_ptr<IMemoryManager> memory_manager = nullptr); 98 /** 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(); 108 /** 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 122 private: 123 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 */ 130 }; 131 132 /** Basic function to run 5x5 convolution. */ 133 using NEConvolution5x5 = NEConvolutionSquare<5>; 134 /** Basic function to run 7x7 convolution. */ 135 using NEConvolution7x7 = NEConvolutionSquare<7>; 136 /** Basic function to run 9x9 convolution. */ 137 using 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 145 * 146 * @deprecated This function is deprecated and is intended to be removed in 21.05 release 147 * 148 */ 149 class NEConvolutionRectangle : public INESimpleFunction 150 { 151 public: 152 /** 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(); 164 /** 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 } 178 #endif /*ARM_COMPUTE_NECONVOLUTION_H */ 179