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) 2017-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_NEIM2COLKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_NEIM2COLKERNEL_H__ |
| 26 | |
| 27 | #include "arm_compute/core/NEON/INEKernel.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | class ITensor; |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 32 | class Size2D; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
| 34 | /** Interface for the im2col reshape kernel. |
| 35 | * |
| 36 | * Rearranges image blocks into columns. It is used to strip out each convolution block to a single column. |
| 37 | * It is used to transform a convolution to a plain matrix multiplication. |
| 38 | * |
| 39 | * For example taking into account the image below and assuming 3x3 image blocks with stride of 1 we have: |
| 40 | * |
| 41 | * @f[ |
| 42 | * \left( \begin{array}{cccc} |
| 43 | * a00 & a01 & a02 & a03 \\ |
| 44 | * a10 & a11 & a12 & a13 \\ |
| 45 | * a20 & a21 & a22 & a23 \\ |
| 46 | * a30 & a31 & a32 & a33 \\ |
| 47 | * \end{array} \right) |
| 48 | * \rightarrow |
| 49 | * \left( \begin{array}{ccccccccc} |
| 50 | * a00 & a01 & a02 & a10 & a11 & a12 & a20 & a21 & a22 \\ |
| 51 | * a01 & a02 & a03 & a11 & a12 & a13 & a21 & a22 & a23 \\ |
| 52 | * a10 & a11 & a12 & a20 & a21 & a22 & a30 & a31 & a32 \\ |
| 53 | * a11 & a12 & a13 & a21 & a22 & a23 & a31 & a32 & a33 \\ |
| 54 | * \end{array} \right) |
| 55 | * @f] |
| 56 | */ |
| 57 | class NEIm2ColKernel : public INEKernel |
| 58 | { |
| 59 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 60 | const char *name() const override |
| 61 | { |
| 62 | return "NEIm2ColKernel"; |
| 63 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | /** Default constructor */ |
| 65 | NEIm2ColKernel(); |
| 66 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 67 | NEIm2ColKernel(const NEIm2ColKernel &) = delete; |
| 68 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 69 | NEIm2ColKernel &operator=(const NEIm2ColKernel &) = delete; |
| 70 | /** Allow instances of this class to be moved */ |
| 71 | NEIm2ColKernel(NEIm2ColKernel &&) = default; |
| 72 | /** Allow instances of this class to be moved */ |
| 73 | NEIm2ColKernel &operator=(NEIm2ColKernel &&) = default; |
| 74 | /** Default destructor */ |
| 75 | ~NEIm2ColKernel() = default; |
| 76 | |
| 77 | /** Set the input and output of the kernel. |
| 78 | * |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 79 | * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], |
| 80 | * while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QS8/QS16/QASYMM8/F16/F32 |
| 81 | * Note: QASYMM8 works only for has_bias = false |
| 82 | * @param[out] output The output tensor. Data types supported: Same as @p input |
| 83 | * @param[in] kernel_dims The kernel dimensions (width and height). |
| 84 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 85 | * @param[in] has_bias In case biases are provided expands the matrix with 1. |
| 86 | * @param[in] is_fully_connected Determines whether this kernel will be called by @ref NEFullyConnectedLayer in order to validate the arguments |
| 87 | * @param[in] is_flatten (Optional) Determines whether this kernel will be called by @ref NEFlattenLayer in order to validate the arguments |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 88 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | */ |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 90 | void configure(const ITensor *input, ITensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 91 | bool has_bias, bool is_fully_connected = false, bool is_flatten = false, const Size2D &dilation = Size2D(1U, 1U)); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 92 | /** Static function to check if given info will lead to a valid configuration of @ref NEIm2ColKernel |
| 93 | * |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 94 | * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], |
| 95 | * while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QS8/QS16/QASYMM8/F16/F32 |
| 96 | * Note: QASYMM8 works only for has_bias = false |
| 97 | * @param[in] output The output tensor. Data types supported: Same as @p input |
| 98 | * @param[in] kernel_dims The kernel dimensions (width and height). |
| 99 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 100 | * @param[in] has_bias In case biases are provided expands the matrix with 1. |
| 101 | * @param[in] is_fully_connected Determines whether this kernel will be called by @ref NEFullyConnectedLayer in order to validate the arguments |
| 102 | * @param[in] is_flatten (Optional) Determines whether this kernel will be called by @ref NEFlattenLayer in order to validate the arguments |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 103 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 104 | * |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 105 | * @return a status |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 106 | */ |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 107 | static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 108 | bool has_bias, bool is_fully_connected, bool is_flatten = false, const Size2D &dilation = Size2D(1U, 1U)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | |
| 110 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 111 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | |
| 113 | private: |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 114 | /** Template function to run the im2col optimised for the fully connected and flatten layers case |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | * |
| 116 | * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()). |
| 117 | */ |
| 118 | template <typename T> |
| 119 | void run_reduced(const Window &window); |
| 120 | /** Template function to run the im2col used for the convolution layer case |
| 121 | * |
| 122 | * @param[in] window Region on which to execute the kernel. (Must be a valid region of the window returned by window()). |
| 123 | */ |
| 124 | template <typename T, bool has_pads> |
| 125 | void run_generic(const Window &window); |
| 126 | /** Common signature for all the specialised im2col functions |
| 127 | * |
| 128 | * @param[in] window Region on which to execute the kernel. |
| 129 | */ |
| 130 | using Im2ColFunctionPtr = void (NEIm2ColKernel::*)(const Window &window); |
| 131 | |
| 132 | Im2ColFunctionPtr _func; |
| 133 | const ITensor *_input; |
| 134 | ITensor *_output; |
| 135 | std::pair<unsigned int, unsigned int> _convolved_dims; |
| 136 | PadStrideInfo _conv_info; |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 137 | unsigned int _kernel_width; |
| 138 | unsigned int _kernel_height; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | bool _has_bias; |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 140 | Size2D _dilation; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 142 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | #endif /*__ARM_COMPUTE_NEIM2COLKERNEL_H__ */ |