blob: ea3ddb2124c14a3cb08730a307b0b33bc623de20 [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"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000028#include "src/core/KernelTypes.h"
29#include "src/core/common/Macros.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010030#include "src/gpu/cl/ClCompileContext.h"
31#include "src/gpu/cl/IClKernel.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000032
33namespace arm_compute
34{
35namespace opencl
36{
37namespace kernels
38{
39/** Interface for an element-wise operation kernel
40 *
41 * Element-wise operation is computed by:
42 * @f[ dst(x,y) = OP(src1(x,y), src2(x,y))@f]
43 *
Sheri Zhanga387e272021-06-29 17:34:06 +010044 * 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.
45 *
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000046 */
47class ClElementwiseKernel : public IClKernel
48{
49public:
Giorgio Arena4a95bba2021-06-28 11:00:27 +010050 ClElementwiseKernel();
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000051 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClElementwiseKernel);
52
53 // Inherited methods overridden:
54 void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override;
55
56protected:
57 /** The name of the operation */
58 virtual std::string name() = 0;
59
60 /** Configure kernel for a given list of arguments
61 *
62 * @param[in] src1 First source tensor info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
63 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
64 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
65 *
66 * @return a pair of Status and Window
67 */
68 virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) = 0;
69
70 /** Generate the build options for the specific kernel
71 *
72 * @reutrn a CLBuildOptions struct
73 */
74 virtual CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) = 0;
75
76 /** Generate the identifier for tuning
77 *
78 * @reutrn a string
79 */
80 virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) = 0;
81
82 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
83 *
84 */
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000085 void configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
86
87 ActivationLayerInfo _act_info{};
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000088};
89
90class ClLogicalBinaryKernel : public ClElementwiseKernel
91{
92public:
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000093 ClLogicalBinaryKernel() = default;
94 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClLogicalBinaryKernel);
95 /** Function to configure kernel
96 *
97 * @param[in] compile_context The compile context to be used.
98 * @param[in] op Logical binary operation to be executed.
99 * @param[in] src1 First source tensor info. Data types supported: U8.
100 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
101 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
102 */
103 void configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100104 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000105 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100106 * Similar to @ref ClLogicalBinaryKernel::configure()
107 *
108 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000109 */
110 static Status validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst);
111
112private:
113 // Inherited methods overridden:
114 std::string name() override;
115 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
116 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
117 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
118
119 LogicalOperation _op{ LogicalOperation::Unknown };
120};
121
122/** Addition operation */
123class ClSaturatedArithmeticKernel : public ClElementwiseKernel
124{
125public:
126 ClSaturatedArithmeticKernel() = default;
127 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClSaturatedArithmeticKernel);
128 /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel
129 *
130 * @param[in] compile_context The compile context to be used.
131 * @param[in] op Arithmetic operation to be executed.
132 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
133 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
134 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
135 * @param[in] policy Policy to use to handle overflow.
136 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
137 */
138 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy,
139 const ActivationLayerInfo &act_info = ActivationLayerInfo());
140
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100141 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000142 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100143 * Similar to @ref ClSaturatedArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000144 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100145 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000146 */
147 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
148 const ActivationLayerInfo &act_info = ActivationLayerInfo());
149
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;
154 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 ClArithmeticKernel : public ClElementwiseKernel
163{
164public:
165 ClArithmeticKernel() = default;
166 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClArithmeticKernel);
167
168 /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel
169 *
170 * @param[in] compile_context The compile context to be used.
171 * @param[in] op Arithmetic operation to be executed.
172 * @param[in] src1 First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
173 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
174 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
175 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
176 */
177 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
178 const ActivationLayerInfo &act_info = ActivationLayerInfo());
179
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100180 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000181 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100182 * Similar to @ref ClArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000183 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100184 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000185 */
186 static Status validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info = ActivationLayerInfo());
187
188protected:
189 // Inherited methods overridden:
190 std::string name() override;
191 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
192 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
193 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
194
195private:
196 ArithmeticOperation _op{};
197};
198} // namespace kernels
199} // namespace opencl
200} // namespace arm_compute
201#endif /* ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H */