aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/CL/kernels/CLConvolutionKernel.h
blob: 089a8cd10e2e13555ba2254038075b09d74dc2bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (c) 2016-2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef ARM_COMPUTE_CLCONVOLUTIONKERNEL_H
#define ARM_COMPUTE_CLCONVOLUTIONKERNEL_H

#include "arm_compute/core/CL/ICLSimple2DKernel.h"

#include <cstdint>

namespace arm_compute
{
class ICLTensor;

/****************************************************************************************\
 *                                    Square Convolution                                *
\****************************************************************************************/

/** Interface for the kernel to run an arbitrary size convolution on a tensor. (Currently supports 3x3, 5x5, 7x7 and 9x9).
 * The client can supply a convolution matrix \f$ C_{m,n} \f$.
 * @f{eqnarray}{
 *  k_0 &=& \frac{m}{2}  \\
 *  l_0 &=& \frac{n}{2}  \\
 *  sum &=& \sum_{k=0,l=0}^{k=m-1,l=n-1} input(x+k-k_0, y+l-l_0) C_{k,l}
 *  @f}
 *
 * @note The above equation for this function is similar to the default OpenCV Filter2D function,
 *       which actually computes a correlation and not a convolution.
 *       In case of a real convolution the convolution matrix should be flipped both horizontally and vertically.
 */
template <unsigned int matrix_size>
class CLConvolutionKernel : public ICLSimple2DKernel
{
public:
    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data types supported: U8.
     * @param[out] output           Destination tensor, Data types supported: U8, S16.
     * @param[in]  conv             Convolution matrix to apply to the input tensor.
     * @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.
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     */
    void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined);

    // Inherited methods overridden:
    BorderSize border_size() const override;
};

/** Interface for the kernel which applies a 3x3 convolution to a tensor. */
using CLConvolution3x3Kernel = CLConvolutionKernel<3>;
/** Interface for the kernel which applies a 5x5 convolution to a tensor. */
using CLConvolution5x5Kernel = CLConvolutionKernel<5>;
/** Interface for the kernel which applies a 7x7 convolution to a tensor. */
using CLConvolution7x7Kernel = CLConvolutionKernel<7>;
/** Interface for the kernel which applies a 9x9 convolution to a tensor. */
using CLConvolution9x9Kernel = CLConvolutionKernel<9>;

/****************************************************************************************\
 *                              Separable Square Convolution                            *
\****************************************************************************************/

/** Kernel for the Horizontal pass of a Separable Convolution. Currently support 5x5, 7x7, 9x9 */
template <unsigned int matrix_size>
class CLSeparableConvolutionHorKernel : public ICLSimple2DKernel
{
public:
    /** Default Constructor */
    CLSeparableConvolutionHorKernel();
    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data types supported: U8.
     * @param[out] output           Destination tensor, Data types supported: S16.
     * @param[in]  conv             Convolution matrix to apply to the input tensor.
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     */
    void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, bool border_undefined);

    // Inherited methods overridden:
    BorderSize border_size() const override;

private:
    BorderSize _border_size; /**< Border size */
};

/** Interface for the kernel which applies a horizontal pass of 5x5 convolution to a tensor. */
using CLSeparableConvolution5x5HorKernel = CLSeparableConvolutionHorKernel<5>;
/** Interface for the kernel which applies a horizontal pass of 7x7 convolution to a tensor. */
using CLSeparableConvolution7x7HorKernel = CLSeparableConvolutionHorKernel<7>;
/** Interface for the kernel which applies a horizontal pass of 9x9 convolution to a tensor. */
using CLSeparableConvolution9x9HorKernel = CLSeparableConvolutionHorKernel<9>;

/** Kernel for the Vertical pass of a Separable Convolution. Currently supports 5x5, 7x7, 9x9 */
template <unsigned int matrix_size>
class CLSeparableConvolutionVertKernel : public ICLSimple2DKernel
{
public:
    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data types supported: S16.
     * @param[out] output           Destination tensor, Data types supported: U8, S16.
     * @param[in]  conv             Convolution matrix to apply to the input tensor.
     * @param[in]  scale            Scale of the convolution matrix.
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     * @param[in]  data_type        Data type to use for intermeidate result. @sa data_type_for_convolution
     */
    void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t scale, bool border_undefined, DataType data_type = DataType::S32);

    // Inherited methods overridden:
    BorderSize border_size() const override;
};

/** Interface for the kernel which applies a vertical pass of 5x5 convolution to a tensor. */
using CLSeparableConvolution5x5VertKernel = CLSeparableConvolutionVertKernel<5>;
/** Interface for the kernel which applies a vertical pass of 7x7 convolution to a tensor. */
using CLSeparableConvolution7x7VertKernel = CLSeparableConvolutionVertKernel<7>;
/** Interface for the kernel which applies a vertical pass of 9x9 convolution to a tensor. */
using CLSeparableConvolution9x9VertKernel = CLSeparableConvolutionVertKernel<9>;

/****************************************************************************************\
 *                                 Rectangle Convolution                                *
\****************************************************************************************/

/** Kernel for the running convolution on a rectangle matrix.
 *
 * @note Supports combinations of 3,5,7 and 9.
 */
class CLConvolutionRectangleKernel : public ICLKernel
{
public:
    /** Default constructor */
    CLConvolutionRectangleKernel();
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    CLConvolutionRectangleKernel(const CLConvolutionRectangleKernel &) = delete;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    CLConvolutionRectangleKernel &operator=(const CLConvolutionRectangleKernel &) = delete;
    /** Allow instances of this class to be moved */
    CLConvolutionRectangleKernel(CLConvolutionRectangleKernel &&) = default;
    /** Allow instances of this class to be moved */
    CLConvolutionRectangleKernel &operator=(CLConvolutionRectangleKernel &&) = default;
    /** Initialise the kernel's input, output and border mode.
     *
     * @param[in]  input            Source tensor. Data types supported: U8.
     * @param[out] output           Destination tensor, Data types supported: U8, S16.
     * @param[in]  conv             Convolution matrix to apply to the input tensor.
     * @param[in]  width            Width of convolution matrix (Number of columns)
     * @param[in]  height           Height of convolution matrix (Number of rows)
     * @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.
     * @param[in]  border_undefined True if the border mode is undefined. False if it's replicate or constant.
     */
    void configure(const ICLTensor *input, ICLTensor *output, const int16_t *conv, uint32_t width, uint32_t height, uint32_t scale, bool border_undefined);

    // Inherited methods overridden:
    void run(const Window &window, cl::CommandQueue &queue) override;
    BorderSize border_size() const override;

private:
    BorderSize       _border_size;
    const ICLTensor *_input;
    ICLTensor       *_output;
};
} // namespace arm_compute
#endif /*ARM_COMPUTE_CLCONVOLUTIONKERNEL_H */