blob: 73e54542b2fb748934448a654770e4ba874765c4 [file] [log] [blame]
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +00001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +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 */
24#ifndef ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H
25#define ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H
26
SiCong Li91295492023-07-21 18:16:13 +010027#include "arm_compute/function_info/ActivationLayerInfo.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000029#include "src/core/common/Macros.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/core/KernelTypes.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010031#include "src/gpu/cl/ClCompileContext.h"
32#include "src/gpu/cl/IClKernel.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000033
34namespace arm_compute
35{
36namespace opencl
37{
38namespace kernels
39{
40/** Interface for an element-wise operation kernel
41 *
42 * Element-wise operation is computed by:
43 * @f[ dst(x,y) = OP(src1(x,y), src2(x,y))@f]
44 *
Sheri Zhanga387e272021-06-29 17:34:06 +010045 * For binary elementwise ops in-place cannot be enabled by passing nullptr to dst, it can only be enabled by passing either src1 or src2 to dst instead.
46 *
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000047 */
48class ClElementwiseKernel : public IClKernel
49{
50public:
Giorgio Arena4a95bba2021-06-28 11:00:27 +010051 ClElementwiseKernel();
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000052 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClElementwiseKernel);
53
54 // Inherited methods overridden:
55 void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override;
56
57protected:
58 /** The name of the operation */
59 virtual std::string name() = 0;
60
61 /** Configure kernel for a given list of arguments
62 *
63 * @param[in] src1 First source tensor info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
64 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
65 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
66 *
67 * @return a pair of Status and Window
68 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 virtual std::pair<Status, Window>
70 validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) = 0;
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000071
72 /** Generate the build options for the specific kernel
73 *
74 * @reutrn a CLBuildOptions struct
75 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 virtual CLBuildOptions
77 generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) = 0;
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000078
79 /** Generate the identifier for tuning
80 *
81 * @reutrn a string
82 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 virtual std::string
84 generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) = 0;
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000085
86 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
87 *
88 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 void
90 configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000091
92 ActivationLayerInfo _act_info{};
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000093};
94
95class ClLogicalBinaryKernel : public ClElementwiseKernel
96{
97public:
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000098 ClLogicalBinaryKernel() = default;
99 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClLogicalBinaryKernel);
100 /** Function to configure kernel
101 *
102 * @param[in] compile_context The compile context to be used.
103 * @param[in] op Logical binary operation to be executed.
104 * @param[in] src1 First source tensor info. Data types supported: U8.
105 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
106 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
107 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 void configure(const ClCompileContext &compile_context,
109 LogicalOperation op,
110 ITensorInfo *src1,
111 ITensorInfo *src2,
112 ITensorInfo *dst);
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100113 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000114 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100115 * Similar to @ref ClLogicalBinaryKernel::configure()
116 *
117 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000118 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 static Status
120 validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst);
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000121
122private:
123 // Inherited methods overridden:
124 std::string name() override;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 std::pair<Status, Window>
126 validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
127 CLBuildOptions
128 generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
129 std::string
130 generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000131
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 LogicalOperation _op{LogicalOperation::Unknown};
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000133};
134
135/** Addition operation */
136class ClSaturatedArithmeticKernel : public ClElementwiseKernel
137{
138public:
139 ClSaturatedArithmeticKernel() = default;
140 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClSaturatedArithmeticKernel);
141 /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel
142 *
143 * @param[in] compile_context The compile context to be used.
144 * @param[in] op Arithmetic operation to be executed.
145 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
146 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
147 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
148 * @param[in] policy Policy to use to handle overflow.
149 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
150 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 void configure(const ClCompileContext &compile_context,
152 ArithmeticOperation op,
153 ITensorInfo *input1,
154 ITensorInfo *input2,
155 ITensorInfo *output,
156 const ConvertPolicy &policy,
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000157 const ActivationLayerInfo &act_info = ActivationLayerInfo());
158
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100159 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000160 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100161 * Similar to @ref ClSaturatedArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000162 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100163 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000164 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100165 static Status validate(ArithmeticOperation op,
166 const ITensorInfo *input1,
167 const ITensorInfo *input2,
168 const ITensorInfo *output,
169 const ConvertPolicy &policy,
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000170 const ActivationLayerInfo &act_info = ActivationLayerInfo());
171
172protected:
173 // Inherited methods overridden:
174 std::string name() override;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 std::pair<Status, Window>
176 validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
177 CLBuildOptions
178 generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
179 std::string generate_id_for_tuning(const std::string &kernel_name,
180 const ITensorInfo &input1,
181 const ITensorInfo &output) override;
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000182
183private:
184 ConvertPolicy _policy{};
185 ArithmeticOperation _op{};
186};
187
188class ClArithmeticKernel : public ClElementwiseKernel
189{
190public:
191 ClArithmeticKernel() = default;
192 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClArithmeticKernel);
193
194 /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel
195 *
196 * @param[in] compile_context The compile context to be used.
197 * @param[in] op Arithmetic operation to be executed.
198 * @param[in] src1 First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
199 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
200 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
201 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
202 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100203 void configure(const ClCompileContext &compile_context,
204 ArithmeticOperation op,
205 ITensorInfo *src1,
206 ITensorInfo *src2,
207 ITensorInfo *dst,
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000208 const ActivationLayerInfo &act_info = ActivationLayerInfo());
209
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100210 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000211 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100212 * Similar to @ref ClArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000213 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100214 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000215 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 static Status validate(ArithmeticOperation op,
217 const ITensorInfo *src1,
218 const ITensorInfo *src2,
219 const ITensorInfo *dst,
220 const ActivationLayerInfo &act_info = ActivationLayerInfo());
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000221
222protected:
223 // Inherited methods overridden:
224 std::string name() override;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225 std::pair<Status, Window>
226 validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
227 CLBuildOptions
228 generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
229 std::string
230 generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000231
232private:
233 ArithmeticOperation _op{};
234};
235} // namespace kernels
236} // namespace opencl
237} // namespace arm_compute
238#endif /* ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H */