Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLIM2COLKERNEL_H |
| 25 | #define ARM_COMPUTE_CLIM2COLKERNEL_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/CL/ICLKernel.h" |
Gian Marco | 76faef8 | 2018-01-29 12:15:32 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | class ICLTensor; |
| 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 | * @f[ |
| 41 | * \left( \begin{array}{cccc} |
| 42 | * a00 & a01 & a02 & a03 \\ |
| 43 | * a10 & a11 & a12 & a13 \\ |
| 44 | * a20 & a21 & a22 & a23 \\ |
| 45 | * a30 & a31 & a32 & a33 \\ |
| 46 | * \end{array} \right) |
| 47 | * = |
| 48 | * \left( \begin{array}{ccccccccc} |
| 49 | * a00 & a01 & a02 & a10 & a11 & a12 & a20 & a21 & a22 \\ |
| 50 | * a01 & a02 & a03 & a11 & a12 & a13 & a21 & a22 & a23 \\ |
| 51 | * a10 & a11 & a12 & a20 & a21 & a22 & a30 & a31 & a32 \\ |
| 52 | * a11 & a12 & a13 & a21 & a22 & a23 & a31 & a32 & a33 \\ |
| 53 | * \end{array} \right) |
| 54 | * @f] |
| 55 | */ |
| 56 | class CLIm2ColKernel : public ICLKernel |
| 57 | { |
| 58 | public: |
| 59 | /** Default constructor */ |
| 60 | CLIm2ColKernel(); |
| 61 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 62 | CLIm2ColKernel(const CLIm2ColKernel &) = delete; |
| 63 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 64 | CLIm2ColKernel &operator=(const CLIm2ColKernel &) = delete; |
| 65 | /** Allow instances of this class to be moved */ |
| 66 | CLIm2ColKernel(CLIm2ColKernel &&) = default; |
| 67 | /** Allow instances of this class to be moved */ |
| 68 | CLIm2ColKernel &operator=(CLIm2ColKernel &&) = default; |
| 69 | /** Set the input and output of the kernel. |
| 70 | * |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 71 | * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 72 | * while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32 |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 73 | * @param[out] output The output tensor. First 2 lower dimensions represent a transform of each 3D input, |
| 74 | * while every dimension above represents a batch. Data types supported: Same as @p input |
| 75 | * @param[in] kernel_dims The kernel dimensions (width and height). |
| 76 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 77 | * @param[in] has_bias In case biases are provided expands the matrix with 1. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 78 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 79 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | */ |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 81 | void configure(const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation = Size2D(1U, 1U), |
| 82 | unsigned int num_groups = 1); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 83 | /** Set the input and output of the kernel. |
| 84 | * |
| 85 | * @param[in] compile_context The compile context to be used. |
| 86 | * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], |
| 87 | * while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32 |
| 88 | * @param[out] output The output tensor. First 2 lower dimensions represent a transform of each 3D input, |
| 89 | * while every dimension above represents a batch. Data types supported: Same as @p input |
| 90 | * @param[in] kernel_dims The kernel dimensions (width and height). |
| 91 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 92 | * @param[in] has_bias In case biases are provided expands the matrix with 1. |
| 93 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 94 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
| 95 | */ |
| 96 | void configure(CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, |
| 97 | const Size2D &dilation = Size2D(1U, 1U), |
| 98 | unsigned int num_groups = 1); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 99 | /** Static function to check if given info will lead to a valid configuration of @ref CLIm2ColKernel |
| 100 | * |
| 101 | * @param[in] input The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 102 | * while every optional dimension from 4 and above represent a batch of inputs. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32 |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 103 | * @param[in] output The output tensor. First 2 lower dimensions represent a transform of each 3D input, |
| 104 | * while every dimension above represents a batch. Data types supported: Same as @p input |
| 105 | * @param[in] kernel_dims The kernel dimensions (width and height). |
| 106 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 107 | * @param[in] has_bias In case biases are provided expands the matrix with 1. |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 108 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 109 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 110 | * |
| 111 | * @return a status |
| 112 | */ |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 113 | static Status validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation = Size2D(1U, 1U), |
| 114 | unsigned int num_groups = 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 116 | // Inherited methods overridden: |
| 117 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 118 | |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 119 | public: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | const ICLTensor *_input; |
| 121 | ICLTensor *_output; |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 122 | DataLayout _data_layout; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | std::pair<unsigned int, unsigned int> _convolved_dims; |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 124 | unsigned int _num_elems_processed_per_iteration; |
| 125 | Size2D _kernel_dims; |
| 126 | PadStrideInfo _conv_info; |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 127 | unsigned int _num_groups; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | }; |
Gian Marco Iodice | f670a0a | 2017-09-18 12:20:45 +0100 | [diff] [blame] | 129 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 130 | #endif /*ARM_COMPUTE_CLIM2COLKERNEL_H */ |