Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | class 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 | */ |
| 54 | template <unsigned int matrix_size> |
| 55 | class NEConvolutionKernel : public INESimpleKernel |
| 56 | { |
| 57 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 58 | const char *name() const override |
| 59 | { |
| 60 | return "NEConvolutionKernel"; |
| 61 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | /** Default constructor */ |
| 63 | NEConvolutionKernel(); |
| 64 | /** Initialise the kernel's input, output and border mode. |
| 65 | * |
| 66 | * @param[in] input Source tensor. Data type supported: U8. |
| 67 | * @param[out] output Destination tensor. Data types supported: U8, S16. |
| 68 | * @param[in] conv Convolution matrix to apply to the input tensor. |
| 69 | * @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. |
| 70 | * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant. |
| 71 | */ |
| 72 | void configure(const ITensor *input, ITensor *output, const int16_t *conv, uint32_t scale, bool border_undefined); |
| 73 | |
| 74 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 75 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | BorderSize border_size() const override; |
| 77 | |
| 78 | private: |
| 79 | template <typename OutputType> |
| 80 | void convolution(const Window &win); |
| 81 | |
| 82 | protected: |
| 83 | uint32_t _scale; /**< scale of the convolution */ |
| 84 | std::array<int16_t, matrix_size *matrix_size> _convolution; /**< convolution matrix */ |
| 85 | }; |
| 86 | |
| 87 | /** Interface for the kernel which applied a 3x3 convolution to a tensor.*/ |
| 88 | using NEConvolution3x3Kernel = NEConvolutionKernel<3>; |
| 89 | /** Interface for the kernel which applied a 5x5 convolution to a tensor.*/ |
| 90 | using NEConvolution5x5Kernel = NEConvolutionKernel<5>; |
| 91 | /** Interface for the kernel which applied a 7x7 convolution to a tensor.*/ |
| 92 | using NEConvolution7x7Kernel = NEConvolutionKernel<7>; |
| 93 | ///** Interface for the kernel which applied a 9x9 convolution to a tensor.*/ |
| 94 | using NEConvolution9x9Kernel = NEConvolutionKernel<9>; |
| 95 | |
| 96 | /****************************************************************************************\ |
| 97 | * Separable Square Convolution * |
| 98 | \****************************************************************************************/ |
| 99 | |
| 100 | /** Kernel for the Horizontal pass of a Separable Convolution */ |
| 101 | template <unsigned int matrix_size> |
| 102 | class NESeparableConvolutionHorKernel : public INESimpleKernel |
| 103 | { |
| 104 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 105 | const char *name() const override |
| 106 | { |
| 107 | return "NESeparableConvolutionHorKernel"; |
| 108 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | /** Default constructor */ |
| 110 | NESeparableConvolutionHorKernel(); |
| 111 | |
| 112 | /** Initialise the kernel's input, output and border mode. |
| 113 | * |
| 114 | * @param[in] input Source tensor. Data type supported: U8. |
| 115 | * @param[out] output Destination tensor. Data types supported: U16, S16, S32. |
| 116 | * @param[in] conv_row Convolution matrix to apply to the input tensor. |
| 117 | * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant. |
| 118 | */ |
| 119 | void configure(const ITensor *input, ITensor *output, const int16_t *conv_row, bool border_undefined); |
| 120 | |
| 121 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 122 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | BorderSize border_size() const override; |
| 124 | |
| 125 | private: |
| 126 | /** Apply the object's convolution to the given window of the input tensor.. |
| 127 | * |
| 128 | * @param[in] window Window to apply the convolution on. |
| 129 | */ |
| 130 | template <typename OutputType> |
| 131 | void convolve(const Window &window); |
| 132 | |
| 133 | std::array<int16_t, matrix_size> _conv_row; /**< Convolution coefficients */ |
| 134 | BorderSize _border_size; /**< Border size */ |
| 135 | }; |
| 136 | |
| 137 | /** Interface for the kernel which applied a 5x1 horizontal convolution to a tensor.*/ |
| 138 | using NESeparableConvolution5x5HorKernel = NESeparableConvolutionHorKernel<5>; |
| 139 | /** Interface for the kernel which applied a 7x1 horizontal convolution to a tensor.*/ |
| 140 | using NESeparableConvolution7x7HorKernel = NESeparableConvolutionHorKernel<7>; |
| 141 | /** Interface for the kernel which applied a 9x1 horizontal convolution to a tensor.*/ |
| 142 | using NESeparableConvolution9x9HorKernel = NESeparableConvolutionHorKernel<9>; |
| 143 | |
| 144 | /** Kernel for the Vertical pass of a Separable Convolution */ |
| 145 | template <unsigned int matrix_size> |
| 146 | class NESeparableConvolutionVertKernel : public INESimpleKernel |
| 147 | { |
| 148 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 149 | const char *name() const override |
| 150 | { |
| 151 | return "NESeparableConvolutionVertKernel"; |
| 152 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | /** Default constructor */ |
| 154 | NESeparableConvolutionVertKernel(); |
| 155 | |
| 156 | /** Initialise the kernel's input, output and border mode. |
| 157 | * |
| 158 | * @param[in] input Source tensor. Data type supported: U16, S16, S32. |
| 159 | * @param[out] output Destination tensor, Data types supported: U8, S16. |
| 160 | * @param[in] conv_col Convolution matrix to apply to the input tensor. |
| 161 | * @param[in] scale Scale of the convolution matrix |
| 162 | * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant. |
| 163 | */ |
| 164 | void configure(const ITensor *input, ITensor *output, const int16_t *conv_col, uint32_t scale, bool border_undefined); |
| 165 | |
| 166 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 167 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | BorderSize border_size() const override; |
| 169 | |
| 170 | private: |
| 171 | /** Apply the object's convolution to the given window of the input tensor. |
| 172 | * This function is used if the intermediate values have been stored as U16. |
| 173 | * |
| 174 | * @param[in] win Window to apply the convolution on. |
| 175 | */ |
| 176 | template <typename OutputType> |
| 177 | void convolution_u16(const Window &win); |
| 178 | /** Apply the object's convolution to the given window of the input tensor. |
| 179 | * This function is used if the intermediate values have been stored as S16. |
| 180 | * |
| 181 | * @param[in] win Window to apply the convolution on. |
| 182 | */ |
| 183 | template <typename OutputType> |
| 184 | void convolution_s16(const Window &win); |
| 185 | /** Apply the object's convolution to the given window of the input tensor. |
| 186 | * This function is used if the intermediate values have been stored as S32. |
| 187 | * |
| 188 | * @param[in] win Window to apply the convolution on. |
| 189 | */ |
| 190 | template <typename OutputType> |
| 191 | void convolution_s32(const Window &win); |
| 192 | |
| 193 | std::array<int16_t, matrix_size> _conv_col; /**< Convolution coefficients */ |
| 194 | uint32_t _scale; /**< Convolution's scale */ |
| 195 | }; |
| 196 | |
| 197 | /** Interface for the kernel which applied a 1x5 vertical convolution to a tensor.*/ |
| 198 | using NESeparableConvolution5x5VertKernel = NESeparableConvolutionVertKernel<5>; |
| 199 | /** Interface for the kernel which applied a 1x7 vertical convolution to a tensor.*/ |
| 200 | using NESeparableConvolution7x7VertKernel = NESeparableConvolutionVertKernel<7>; |
| 201 | /** Interface for the kernel which applied a 1x9 vertical convolution to a tensor.*/ |
| 202 | using NESeparableConvolution9x9VertKernel = NESeparableConvolutionVertKernel<9>; |
| 203 | |
| 204 | /****************************************************************************************\ |
| 205 | * Rectangle Convolution * |
| 206 | \****************************************************************************************/ |
| 207 | |
| 208 | /** Kernel for the running convolution on a rectangle matrix. |
| 209 | * |
| 210 | * @note Supports combinations of 3,5,7 and 9. |
| 211 | */ |
| 212 | class NEConvolutionRectangleKernel : public INEKernel |
| 213 | { |
| 214 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 215 | const char *name() const override |
| 216 | { |
| 217 | return "NEConvolutionRectangleKernel"; |
| 218 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | /** Default constructor */ |
| 220 | NEConvolutionRectangleKernel(); |
| 221 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 222 | NEConvolutionRectangleKernel(NEConvolutionRectangleKernel &) = delete; |
| 223 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 224 | NEConvolutionRectangleKernel &operator=(NEConvolutionRectangleKernel &) = delete; |
| 225 | /** Allow instances of this class to be moved */ |
| 226 | NEConvolutionRectangleKernel(NEConvolutionRectangleKernel &&) = default; |
| 227 | /** Allow instances of this class to be moved */ |
| 228 | NEConvolutionRectangleKernel &operator=(NEConvolutionRectangleKernel &&) = default; |
| 229 | /** Initialise the kernel's input, output and border mode. |
| 230 | * |
| 231 | * @param[in] input Source tensor. Data type supported: U8. |
| 232 | * @param[out] output Destination tensor, Data types supported: U8, S16. |
| 233 | * @param[in] conv Convolution matrix to apply to the input tensor. |
| 234 | * @param[in] width Width of convolution matrix (Number of columns) |
| 235 | * @param[in] height Height of convolution matrix (Number of rows) |
| 236 | * @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. |
| 237 | * @param[in] border_undefined True if the border mode is undefined. False if it's replicate or constant. |
| 238 | */ |
| 239 | void configure(const ITensor *input, ITensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined); |
| 240 | |
| 241 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 242 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | BorderSize border_size() const override; |
| 244 | |
| 245 | private: |
| 246 | unsigned int get_index(uint32_t val); |
| 247 | /** Apply the object's convolution to the given window of the input tensor. |
| 248 | * |
| 249 | * @param[in] win Window to apply the convolution on. |
| 250 | */ |
| 251 | template <typename OutputType, unsigned int rows, unsigned int cols> |
| 252 | void convolution(const Window &win); |
| 253 | |
| 254 | protected: |
| 255 | const ITensor *_input; /**< Input tensor */ |
| 256 | ITensor *_output; /**< Output tensor */ |
| 257 | uint32_t _scale; /**< Scale of the convolution */ |
| 258 | std::vector<int16_t> _convolution; /**< Convolution matrix */ |
| 259 | BorderSize _border_size; /**< Calculated border width */ |
| 260 | uint32_t _func_idx; /**< Index used to specify convolution function to be used */ |
| 261 | const static unsigned int _nr_supported_sizes |
| 262 | { |
| 263 | 4 |
| 264 | }; /**< Number of supported permutations */ |
| 265 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 266 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 267 | #endif /*__ARM_COMPUTE_NECONVOLUTIONKERNEL_H__ */ |