xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/kernels/ClElementwiseKernel.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H
25*c217d954SCole Faust #define ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "src/core/KernelTypes.h"
28*c217d954SCole Faust #include "src/core/common/Macros.h"
29*c217d954SCole Faust #include "src/gpu/cl/ClCompileContext.h"
30*c217d954SCole Faust #include "src/gpu/cl/IClKernel.h"
31*c217d954SCole Faust 
32*c217d954SCole Faust namespace arm_compute
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace opencl
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace kernels
37*c217d954SCole Faust {
38*c217d954SCole Faust /** Interface for an element-wise operation kernel
39*c217d954SCole Faust  *
40*c217d954SCole Faust  * Element-wise operation is computed by:
41*c217d954SCole Faust  * @f[ dst(x,y) = OP(src1(x,y), src2(x,y))@f]
42*c217d954SCole Faust  *
43*c217d954SCole Faust  * 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.
44*c217d954SCole Faust  *
45*c217d954SCole Faust  */
46*c217d954SCole Faust class ClElementwiseKernel : public IClKernel
47*c217d954SCole Faust {
48*c217d954SCole Faust public:
49*c217d954SCole Faust     ClElementwiseKernel();
50*c217d954SCole Faust     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClElementwiseKernel);
51*c217d954SCole Faust 
52*c217d954SCole Faust     // Inherited methods overridden:
53*c217d954SCole Faust     void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override;
54*c217d954SCole Faust 
55*c217d954SCole Faust protected:
56*c217d954SCole Faust     /** The name of the operation */
57*c217d954SCole Faust     virtual std::string name() = 0;
58*c217d954SCole Faust 
59*c217d954SCole Faust     /** Configure kernel for a given list of arguments
60*c217d954SCole Faust      *
61*c217d954SCole Faust      * @param[in] src1 First source tensor info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
62*c217d954SCole Faust      * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
63*c217d954SCole Faust      * @param[in] dst  Destination tensor info. Data types supported: same as @p src1.
64*c217d954SCole Faust      *
65*c217d954SCole Faust      * @return a pair of Status and Window
66*c217d954SCole Faust      */
67*c217d954SCole Faust     virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) = 0;
68*c217d954SCole Faust 
69*c217d954SCole Faust     /** Generate the build options for the specific kernel
70*c217d954SCole Faust      *
71*c217d954SCole Faust      * @reutrn a CLBuildOptions struct
72*c217d954SCole Faust      */
73*c217d954SCole Faust     virtual CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) = 0;
74*c217d954SCole Faust 
75*c217d954SCole Faust     /** Generate the identifier for tuning
76*c217d954SCole Faust      *
77*c217d954SCole Faust      * @reutrn a string
78*c217d954SCole Faust      */
79*c217d954SCole Faust     virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) = 0;
80*c217d954SCole Faust 
81*c217d954SCole Faust     /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
82*c217d954SCole Faust      *
83*c217d954SCole Faust      */
84*c217d954SCole Faust     void configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
85*c217d954SCole Faust 
86*c217d954SCole Faust     ActivationLayerInfo _act_info{};
87*c217d954SCole Faust };
88*c217d954SCole Faust 
89*c217d954SCole Faust class ClLogicalBinaryKernel : public ClElementwiseKernel
90*c217d954SCole Faust {
91*c217d954SCole Faust public:
92*c217d954SCole Faust     ClLogicalBinaryKernel() = default;
93*c217d954SCole Faust     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClLogicalBinaryKernel);
94*c217d954SCole Faust     /** Function to configure kernel
95*c217d954SCole Faust      *
96*c217d954SCole Faust      * @param[in] compile_context The compile context to be used.
97*c217d954SCole Faust      * @param[in] op              Logical binary operation to be executed.
98*c217d954SCole Faust      * @param[in] src1            First source tensor info. Data types supported: U8.
99*c217d954SCole Faust      * @param[in] src2            Second source tensor info. Data types supported: same as @p src1.
100*c217d954SCole Faust      * @param[in] dst             Destination tensor info. Data types supported: same as @p src1.
101*c217d954SCole Faust      */
102*c217d954SCole Faust     void configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
103*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
104*c217d954SCole Faust      *
105*c217d954SCole Faust      * Similar to @ref ClLogicalBinaryKernel::configure()
106*c217d954SCole Faust      *
107*c217d954SCole Faust      * @return a status
108*c217d954SCole Faust      */
109*c217d954SCole Faust     static Status validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst);
110*c217d954SCole Faust 
111*c217d954SCole Faust private:
112*c217d954SCole Faust     // Inherited methods overridden:
113*c217d954SCole Faust     std::string name() override;
114*c217d954SCole Faust     std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
115*c217d954SCole Faust     CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
116*c217d954SCole Faust     std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
117*c217d954SCole Faust 
118*c217d954SCole Faust     LogicalOperation _op{ LogicalOperation::Unknown };
119*c217d954SCole Faust };
120*c217d954SCole Faust 
121*c217d954SCole Faust /** Addition operation */
122*c217d954SCole Faust class ClSaturatedArithmeticKernel : public ClElementwiseKernel
123*c217d954SCole Faust {
124*c217d954SCole Faust public:
125*c217d954SCole Faust     ClSaturatedArithmeticKernel() = default;
126*c217d954SCole Faust     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClSaturatedArithmeticKernel);
127*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel
128*c217d954SCole Faust      *
129*c217d954SCole Faust      * @param[in] compile_context The compile context to be used.
130*c217d954SCole Faust      * @param[in] op              Arithmetic operation to be executed.
131*c217d954SCole Faust      * @param[in] input1          First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
132*c217d954SCole Faust      * @param[in] input2          Second tensor input info. Data types supported: Same as @p input1.
133*c217d954SCole Faust      * @param[in] output          Output tensor info. Data types supported: Same as @p input1.
134*c217d954SCole Faust      * @param[in] policy          Policy to use to handle overflow.
135*c217d954SCole Faust      * @param[in] act_info        (Optional) Activation layer information in case of a fused activation.
136*c217d954SCole Faust      */
137*c217d954SCole Faust     void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy,
138*c217d954SCole Faust                    const ActivationLayerInfo &act_info = ActivationLayerInfo());
139*c217d954SCole Faust 
140*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
141*c217d954SCole Faust      *
142*c217d954SCole Faust      * Similar to @ref ClSaturatedArithmeticKernel::configure()
143*c217d954SCole Faust      *
144*c217d954SCole Faust      * @return a status
145*c217d954SCole Faust      */
146*c217d954SCole Faust     static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
147*c217d954SCole Faust                            const ActivationLayerInfo &act_info = ActivationLayerInfo());
148*c217d954SCole Faust 
149*c217d954SCole Faust protected:
150*c217d954SCole Faust     // Inherited methods overridden:
151*c217d954SCole Faust     std::string name() override;
152*c217d954SCole Faust     std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
153*c217d954SCole Faust     CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
154*c217d954SCole Faust     std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
155*c217d954SCole Faust 
156*c217d954SCole Faust private:
157*c217d954SCole Faust     ConvertPolicy       _policy{};
158*c217d954SCole Faust     ArithmeticOperation _op{};
159*c217d954SCole Faust };
160*c217d954SCole Faust 
161*c217d954SCole Faust class ClArithmeticKernel : public ClElementwiseKernel
162*c217d954SCole Faust {
163*c217d954SCole Faust public:
164*c217d954SCole Faust     ClArithmeticKernel() = default;
165*c217d954SCole Faust     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClArithmeticKernel);
166*c217d954SCole Faust 
167*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel
168*c217d954SCole Faust      *
169*c217d954SCole Faust      * @param[in] compile_context The compile context to be used.
170*c217d954SCole Faust      * @param[in] op              Arithmetic operation to be executed.
171*c217d954SCole Faust      * @param[in] src1            First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
172*c217d954SCole Faust      * @param[in] src2            Second source tensor info. Data types supported: same as @p src1.
173*c217d954SCole Faust      * @param[in] dst             Destination tensor info. Data types supported: same as @p src1.
174*c217d954SCole Faust      * @param[in] act_info        (Optional) Activation layer information in case of a fused activation.
175*c217d954SCole Faust      */
176*c217d954SCole Faust     void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
177*c217d954SCole Faust                    const ActivationLayerInfo &act_info = ActivationLayerInfo());
178*c217d954SCole Faust 
179*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
180*c217d954SCole Faust      *
181*c217d954SCole Faust      * Similar to @ref ClArithmeticKernel::configure()
182*c217d954SCole Faust      *
183*c217d954SCole Faust      * @return a status
184*c217d954SCole Faust      */
185*c217d954SCole Faust     static Status validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info = ActivationLayerInfo());
186*c217d954SCole Faust 
187*c217d954SCole Faust protected:
188*c217d954SCole Faust     // Inherited methods overridden:
189*c217d954SCole Faust     std::string name() override;
190*c217d954SCole Faust     std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
191*c217d954SCole Faust     CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
192*c217d954SCole Faust     std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
193*c217d954SCole Faust 
194*c217d954SCole Faust private:
195*c217d954SCole Faust     ArithmeticOperation _op{};
196*c217d954SCole Faust };
197*c217d954SCole Faust } // namespace kernels
198*c217d954SCole Faust } // namespace opencl
199*c217d954SCole Faust } // namespace arm_compute
200*c217d954SCole Faust #endif /* ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H */
201