blob: 2f1060126a4339e1ef4cd071e804586d4c0cd025 [file] [log] [blame]
giuros01164a2722018-11-20 18:34:46 +00001/*
Giorgio Arena8b2a7d32020-02-11 17:21:31 +00002 * 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:
57 void run(const Window &window, cl::CommandQueue &queue) override;
58
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 *
Kurtis Charnockec0c4122019-12-05 14:13:46 +000067 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
giuros01164a2722018-11-20 18:34:46 +000068 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
69 * @param[in] output Output tensor. Data types supported: Same as @p input1.
70 *
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
75 /** Validate the argument passed to the kernel
76 *
Kurtis Charnockec0c4122019-12-05 14:13:46 +000077 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
giuros01164a2722018-11-20 18:34:46 +000078 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
79 * @param[in] output Output tensor. Data types supported: Same as @p input1.
80 */
81 virtual Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) = 0;
82
83 /** Generate the build options for the specific kernel
84 *
85 * @reutrn a CLBuildOptions struct
86 */
87 virtual CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) = 0;
88
89 /** Generate the identifier for tuning
90 *
91 * @reutrn a string
92 */
93 virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) = 0;
94
95 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
96 *
97 */
98 void configure_common(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output);
Manuel Bottini4c6bd512020-04-08 10:15:51 +010099 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
100 *
101 */
102 void configure_common(CLCompileContext &compile_context, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output);
giuros01164a2722018-11-20 18:34:46 +0000103
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000104 ActivationLayerInfo _act_info;
105
giuros01164a2722018-11-20 18:34:46 +0000106private:
107 const ICLTensor *_input1; /**< Source tensor 1 */
108 const ICLTensor *_input2; /**< Source tensor 2 */
109 ICLTensor *_output; /**< Destination tensor */
110};
111
112/** Addition operation */
113class CLSaturatedArithmeticOperationKernel : public CLElementwiseOperationKernel
114{
115public:
116 CLSaturatedArithmeticOperationKernel()
117 : CLElementwiseOperationKernel(), _policy(), _op()
118 {
119 }
120
121 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
122 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000123 * @param[in] op Arithmetic operation to be executed.
124 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
125 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
126 * @param[in] output Output tensor. Data types supported: Same as @p input1.
127 * @param[in] policy Policy to use to handle overflow.
128 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000129 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000130 void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100131 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
132 *
133 * @param[in] compile_context The compile context to be used.
134 * @param[in] op Arithmetic operation to be executed.
135 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
136 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
137 * @param[in] output Output tensor. Data types supported: Same as @p input1.
138 * @param[in] policy Policy to use to handle overflow.
139 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
140 */
141 void configure(CLCompileContext &compile_context, ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy,
142 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000143
144 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
145 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000146 * @param[in] op Arithmetic operation to be executed.
147 * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
148 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
149 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
150 * @param[in] policy Policy to use to handle overflow.
151 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000152 *
153 * @return a Status
154 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000155 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
156 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000157
158protected:
159 // Inherited methods overridden:
160 std::string name() override;
161 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
162 Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
163 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
164 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
165
166private:
167 ConvertPolicy _policy;
168 ArithmeticOperation _op;
169};
170
171class CLArithmeticOperationKernel : public CLElementwiseOperationKernel
172{
173public:
174 CLArithmeticOperationKernel()
175 : CLElementwiseOperationKernel(), _op()
176 {
177 }
178
179 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
180 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000181 * @param[in] op Arithmetic operation to be executed.
182 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
183 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
184 * @param[in] output Output tensor. Data types supported: Same as @p input1.
185 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000186 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000187 void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100188 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
189 *
190 * @param[in] compile_context The compile context to be used.
191 * @param[in] op Arithmetic operation to be executed.
192 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
193 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
194 * @param[in] output Output tensor. Data types supported: Same as @p input1.
195 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
196 */
197 void configure(CLCompileContext &compile_context, ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output,
198 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000199
200 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
201 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000202 * @param[in] op Arithmetic operation to be executed.
203 * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
204 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
205 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
206 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000207 *
208 * @return a Status
209 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000210 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 +0000211
212protected:
213 // Inherited methods overridden:
214 std::string name() override;
215 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
216 Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
217 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
218 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
219
220private:
221 ArithmeticOperation _op;
222};
223} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000224#endif /* ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H */