blob: b459292161f5d11be2fde501ced7ff355762e5ba [file] [log] [blame]
giuros01164a2722018-11-20 18:34:46 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
giuros01164a2722018-11-20 18:34:46 +00003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H
25#define ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H
giuros01164a2722018-11-20 18:34:46 +000026
27#include "arm_compute/core/CL/ICLKernel.h"
28#include "arm_compute/core/Types.h"
29
30namespace arm_compute
31{
32class ICLTensor;
33
34/** Interface for an element-wise operation kernel
35 *
36 * Element-wise operation is computed by:
37 * @f[ output(x,y) = OP(input1(x,y), input2(x,y))@f]
38 *
39 */
40class CLElementwiseOperationKernel : public ICLKernel
41{
42public:
43 /** Default constructor */
44 CLElementwiseOperationKernel();
45 /** Prevent instances of this class from being copied (As this class contains pointers) */
46 CLElementwiseOperationKernel(const CLElementwiseOperationKernel &) = delete;
47 /** Prevent instances of this class from being copied (As this class contains pointers) */
48 CLElementwiseOperationKernel &operator=(const CLElementwiseOperationKernel &) = delete;
49 /** Allow instances of this class to be moved */
50 CLElementwiseOperationKernel(CLElementwiseOperationKernel &&) = default;
51 /** Allow instances of this class to be moved */
52 CLElementwiseOperationKernel &operator=(CLElementwiseOperationKernel &&) = default;
53 /** Default destructor */
54 ~CLElementwiseOperationKernel() = default;
55
56 // Inherited methods overridden:
Georgios Pinitas0499dff2020-07-31 22:21:38 +010057 void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override;
giuros01164a2722018-11-20 18:34:46 +000058 BorderSize border_size() const override;
59
60protected:
61 /** The name of the operation */
62 virtual std::string name() = 0;
63
64 /** Initialise the kernel's output.
65 *
Michalis Spyrouad7515d2020-07-24 00:02:23 +010066 * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
67 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
68 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
giuros01164a2722018-11-20 18:34:46 +000069 *
70 * @return a pair of Status and Window
71 */
72 virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) = 0;
73
giuros01164a2722018-11-20 18:34:46 +000074 /** Generate the build options for the specific kernel
75 *
76 * @reutrn a CLBuildOptions struct
77 */
78 virtual CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) = 0;
79
80 /** Generate the identifier for tuning
81 *
82 * @reutrn a string
83 */
84 virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) = 0;
85
86 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
87 *
88 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +010089 void configure_common(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010090 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
91 *
92 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +010093 void configure_common(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output);
giuros01164a2722018-11-20 18:34:46 +000094
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000095 ActivationLayerInfo _act_info;
96
giuros01164a2722018-11-20 18:34:46 +000097private:
Michalis Spyrouad7515d2020-07-24 00:02:23 +010098 const ITensorInfo *_input1; /**< Source tensor info 1 */
99 const ITensorInfo *_input2; /**< Source tensor info 2 */
100 ITensorInfo *_output; /**< Destination tensor info */
giuros01164a2722018-11-20 18:34:46 +0000101};
102
103/** Addition operation */
104class CLSaturatedArithmeticOperationKernel : public CLElementwiseOperationKernel
105{
106public:
107 CLSaturatedArithmeticOperationKernel()
108 : CLElementwiseOperationKernel(), _policy(), _op()
109 {
110 }
111
112 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
113 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000114 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100115 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
116 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
117 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000118 * @param[in] policy Policy to use to handle overflow.
119 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000120 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100121 void configure(ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100122 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
123 *
124 * @param[in] compile_context The compile context to be used.
125 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100126 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
127 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
128 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100129 * @param[in] policy Policy to use to handle overflow.
130 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
131 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100132 void configure(const CLCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100133 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000134
135 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
136 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000137 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100138 * @param[in] input1 First tensor input info info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
139 * @param[in] input2 Second tensor input info info. Data types supported: Same as @p input1.
140 * @param[in] output Output tensor info info. Data types supported: Same as @p input1.
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000141 * @param[in] policy Policy to use to handle overflow.
142 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000143 *
144 * @return a Status
145 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000146 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
147 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000148
149protected:
150 // Inherited methods overridden:
151 std::string name() override;
152 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
giuros01164a2722018-11-20 18:34:46 +0000153 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
154 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
155
156private:
157 ConvertPolicy _policy;
158 ArithmeticOperation _op;
159};
160
161class CLArithmeticOperationKernel : public CLElementwiseOperationKernel
162{
163public:
164 CLArithmeticOperationKernel()
165 : CLElementwiseOperationKernel(), _op()
166 {
167 }
168
169 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
170 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000171 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100172 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
173 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
174 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000175 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000176 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100177 void configure(ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100178 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
179 *
180 * @param[in] compile_context The compile context to be used.
181 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100182 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
183 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
184 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100185 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
186 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100187 void configure(const CLCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100188 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000189
190 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
191 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000192 * @param[in] op Arithmetic operation to be executed.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100193 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000194 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
195 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
196 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000197 *
198 * @return a Status
199 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000200 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000201
202protected:
203 // Inherited methods overridden:
204 std::string name() override;
205 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
giuros01164a2722018-11-20 18:34:46 +0000206 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
207 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
208
209private:
210 ArithmeticOperation _op;
211};
212} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000213#endif /* ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H */