• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2019 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 
25 #ifndef ARM_COMPUTE_GCIM2COLKERNEL_H
26 #define ARM_COMPUTE_GCIM2COLKERNEL_H
27 
28 #include "arm_compute/core/GLES_COMPUTE/IGCKernel.h"
29 
30 namespace arm_compute
31 {
32 class IGCTensor;
33 class Size2D;
34 
35 /** Interface for the im2col reshape kernel.
36  *
37  * Rearranges image blocks into columns. It is used to strip out each convolution block to a single column.
38  * It is used to transform a convolution to a plain matrix multiplication.
39  *
40  * For example taking into account the image below and assuming 3x3 image blocks with stride of 1 we have:
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  * =
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 GCIm2ColKernel : public IGCKernel
58 {
59 public:
60     /** Default constructor */
61     GCIm2ColKernel();
62     /** Prevent instances of this class from being copied (As this class contains pointers) */
63     GCIm2ColKernel(const GCIm2ColKernel &) = delete;
64     /** Prevent instances of this class from being copied (As this class contains pointers) */
65     GCIm2ColKernel &operator=(const GCIm2ColKernel &) = delete;
66     /** Allow instances of this class to be moved */
67     GCIm2ColKernel(GCIm2ColKernel &&) = default;
68     /** Allow instances of this class to be moved */
69     GCIm2ColKernel &operator=(GCIm2ColKernel &&) = default;
70     /** Set the input and output of the kernel.
71      *
72      * @param[in]  input       The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM],
73      *                         while every optional dimension from 4 and above represent a batch of inputs. Data types supported: F16/F32
74      * @param[out] output      The output tensor. First 2 lower dimensions represent a transform of each 3D input,
75      *                         while every dimension above represents a batch. Data types supported: Same as @p input
76      * @param[in]  kernel_dims The kernel dimensions (width and height).
77      * @param[in]  conv_info   Contains padding and stride information described in @ref PadStrideInfo.
78      * @param[in]  has_bias    In case biases are provided expands the matrix with 1.
79      * @param[in]  dilation    (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
80      */
81     void configure(const IGCTensor *input, IGCTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation = Size2D(1U, 1U));
82 
83     // Inherited methods overridden:
84     void run(const Window &window) override;
85 
86     /** Static function to check if given info will lead to a valid configuration of @ref CLIm2ColKernel
87      *
88      * @param[in] input       The input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM],
89      *                        while every optional dimension from 4 and above represent a batch of inputs. Data types supported: F16/F32
90      * @param[in] output      The output tensor. First 2 lower dimensions represent a transform of each 3D input,
91      *                        while every dimension above represents a batch. Data types supported: Same as @p input
92      * @param[in] kernel_dims The kernel dimensions (width and height).
93      * @param[in] conv_info   Contains padding and stride information described in @ref PadStrideInfo.
94      * @param[in] has_bias    In case biases are provided expands the matrix with 1.
95      * @param[in] dilation    (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
96      *
97      * @return a status
98      */
99     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));
100 
101 private:
102     /** Run the reshape kernel optimised for the special case (stride is 1, padding is 0 and kernel's low 3 dimensions are same as input)
103      *
104      * @param[in]     window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
105      * @param[in,out] queue  Command queue on which to enqueue the kernel.
106      */
107     void run_reduced(const Window &window);
108     /** run the generic convolution layer input reshape kernel
109      *
110      * @param[in]     window Region on which to execute the kernel. (Must be a valid region of the window returned by window()).
111      * @param[in,out] queue  Command queue on which to enqueue the kernel.
112      */
113     void run_generic(const Window &window);
114 
115     /** Common signature for the kernel to run */
116     using Im2ColFunction = void (GCIm2ColKernel::*)(const Window &);
117 
118 private:
119     const IGCTensor *_input;
120     IGCTensor       *_output;
121     std::pair<unsigned int, unsigned int> _convolved_dims;
122     std::pair<unsigned int, unsigned int> _kernel_dims;
123     unsigned int   _num_elems_processed_per_iteration;
124     Im2ColFunction _run_func;
125 };
126 }
127 
128 #endif /*ARM_COMPUTE_GCIM2COLKERNEL_H */
129