blob: 76bc879638c24204816e6ba1f3d22d75298504a5 [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:
Michalis Spyrouad7515d2020-07-24 00:02:23 +010057 void run_op(const InputTensorMap &inputs, const OutputTensorMap &outputs, const Window &window, cl::CommandQueue &queue) override;
giuros01164a2722018-11-20 18:34:46 +000058
59 BorderSize border_size() const override;
60
61protected:
62 /** The name of the operation */
63 virtual std::string name() = 0;
64
65 /** Initialise the kernel's output.
66 *
Michalis Spyrouad7515d2020-07-24 00:02:23 +010067 * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
68 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
69 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
giuros01164a2722018-11-20 18:34:46 +000070 *
71 * @return a pair of Status and Window
72 */
73 virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) = 0;
74
giuros01164a2722018-11-20 18:34:46 +000075 /** Generate the build options for the specific kernel
76 *
77 * @reutrn a CLBuildOptions struct
78 */
79 virtual CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) = 0;
80
81 /** Generate the identifier for tuning
82 *
83 * @reutrn a string
84 */
85 virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) = 0;
86
87 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
88 *
89 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +010090 void configure_common(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010091 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
92 *
93 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +010094 void configure_common(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output);
giuros01164a2722018-11-20 18:34:46 +000095
Giorgio Arena8b2a7d32020-02-11 17:21:31 +000096 ActivationLayerInfo _act_info;
97
giuros01164a2722018-11-20 18:34:46 +000098private:
Michalis Spyrouad7515d2020-07-24 00:02:23 +010099 const ITensorInfo *_input1; /**< Source tensor info 1 */
100 const ITensorInfo *_input2; /**< Source tensor info 2 */
101 ITensorInfo *_output; /**< Destination tensor info */
giuros01164a2722018-11-20 18:34:46 +0000102};
103
104/** Addition operation */
105class CLSaturatedArithmeticOperationKernel : public CLElementwiseOperationKernel
106{
107public:
108 CLSaturatedArithmeticOperationKernel()
109 : CLElementwiseOperationKernel(), _policy(), _op()
110 {
111 }
112
113 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
114 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000115 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100116 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
117 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
118 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000119 * @param[in] policy Policy to use to handle overflow.
120 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000121 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100122 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 +0100123 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
124 *
125 * @param[in] compile_context The compile context to be used.
126 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100127 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
128 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
129 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100130 * @param[in] policy Policy to use to handle overflow.
131 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
132 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100133 void configure(const CLCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100134 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000135
136 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
137 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000138 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100139 * @param[in] input1 First tensor input info info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
140 * @param[in] input2 Second tensor input info info. Data types supported: Same as @p input1.
141 * @param[in] output Output tensor info info. Data types supported: Same as @p input1.
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000142 * @param[in] policy Policy to use to handle overflow.
143 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000144 *
145 * @return a Status
146 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000147 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
148 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000149
150protected:
151 // Inherited methods overridden:
152 std::string name() override;
153 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
giuros01164a2722018-11-20 18:34:46 +0000154 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
155 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
156
157private:
158 ConvertPolicy _policy;
159 ArithmeticOperation _op;
160};
161
162class CLArithmeticOperationKernel : public CLElementwiseOperationKernel
163{
164public:
165 CLArithmeticOperationKernel()
166 : CLElementwiseOperationKernel(), _op()
167 {
168 }
169
170 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
171 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000172 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100173 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
174 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
175 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000176 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000177 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100178 void configure(ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100179 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
180 *
181 * @param[in] compile_context The compile context to be used.
182 * @param[in] op Arithmetic operation to be executed.
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100183 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
184 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
185 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100186 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
187 */
Michalis Spyrouad7515d2020-07-24 00:02:23 +0100188 void configure(const CLCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100189 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000190
191 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
192 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000193 * @param[in] op Arithmetic operation to be executed.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100194 * @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 +0000195 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
196 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
197 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000198 *
199 * @return a Status
200 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000201 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 +0000202
203protected:
204 // Inherited methods overridden:
205 std::string name() override;
206 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
giuros01164a2722018-11-20 18:34:46 +0000207 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
208 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
209
210private:
211 ArithmeticOperation _op;
212};
213} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000214#endif /* ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H */