xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/operators/CpuElementwise.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 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_CPU_ELEMENTWISE_H
25*c217d954SCole Faust #define ARM_COMPUTE_CPU_ELEMENTWISE_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "src/cpu/ICpuOperator.h"
28*c217d954SCole Faust 
29*c217d954SCole Faust namespace arm_compute
30*c217d954SCole Faust {
31*c217d954SCole Faust namespace cpu
32*c217d954SCole Faust {
33*c217d954SCole Faust class CpuElementwiseBase : public ICpuOperator
34*c217d954SCole Faust {
35*c217d954SCole Faust public:
36*c217d954SCole Faust     // Inherited methods overridden:
37*c217d954SCole Faust     void run(ITensorPack &tensors) override;
38*c217d954SCole Faust };
39*c217d954SCole Faust /** Class to run @ref cpu::kernels::CpuArithmeticKernel except for division and power
40*c217d954SCole Faust  *
41*c217d954SCole Faust  * @note Max/Min/Squared difference supports input data type of QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32
42*c217d954SCole Faust  * @note PRelu supports inpute data type of QASYMM8/QASYMM8_SIGNED/F16/F32.
43*c217d954SCole Faust  */
44*c217d954SCole Faust template <ArithmeticOperation op>
45*c217d954SCole Faust class CpuElementwiseArithmetic : public CpuElementwiseBase
46*c217d954SCole Faust {
47*c217d954SCole Faust public:
48*c217d954SCole Faust     /** Configure the operator
49*c217d954SCole Faust      *
50*c217d954SCole Faust      * @param[in]  src0 The first source tensor information.
51*c217d954SCole Faust      * @param[in]  src1 The second source tensor information. With PRelu, this is used as alpha tensor.
52*c217d954SCole Faust      * @param[out] dst  The output tensor information.
53*c217d954SCole Faust      */
54*c217d954SCole Faust     void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
55*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
56*c217d954SCole Faust      *
57*c217d954SCole Faust      * Similar to @ref CpuElementwiseArithmetic::configure()
58*c217d954SCole Faust      *
59*c217d954SCole Faust      * @return a status
60*c217d954SCole Faust      */
61*c217d954SCole Faust     static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
62*c217d954SCole Faust };
63*c217d954SCole Faust 
64*c217d954SCole Faust /** Class to run @ref cpu::kernels::CpuArithmeticKernel except for maximum operation */
65*c217d954SCole Faust using CpuElementwiseMax = CpuElementwiseArithmetic<ArithmeticOperation::MAX>;
66*c217d954SCole Faust /** Class to run @ref cpu::kernels::CpuArithmeticKernel except for minimum operation */
67*c217d954SCole Faust using CpuElementwiseMin = CpuElementwiseArithmetic<ArithmeticOperation::MIN>;
68*c217d954SCole Faust /** Class to run @ref cpu::kernels::CpuArithmeticKernel except for squared difference operation */
69*c217d954SCole Faust using CpuElementwiseSquaredDiff = CpuElementwiseArithmetic<ArithmeticOperation::SQUARED_DIFF>;
70*c217d954SCole Faust 
71*c217d954SCole Faust /** Basic function to run @ref cpu::kernels::CpuArithmeticKernel for division
72*c217d954SCole Faust  *
73*c217d954SCole Faust  * @note The tensor data type for the inputs must be S32/F16/F32.
74*c217d954SCole Faust  * @note The function performs a division operation between two tensors (i.e., out[i] = in1[i] / in2[i])
75*c217d954SCole Faust  */
76*c217d954SCole Faust class CpuElementwiseDivision : public CpuElementwiseBase
77*c217d954SCole Faust {
78*c217d954SCole Faust public:
79*c217d954SCole Faust     /** Initialise the kernel's inputs, dst and conversion policy.
80*c217d954SCole Faust      *
81*c217d954SCole Faust      * @param[in, out] src0 First tensor input info. Data types supported: S32/F16/F32.
82*c217d954SCole Faust      * @param[in, out] src1 Second tensor input info. Data types supported: Same as @p src0.
83*c217d954SCole Faust      * @param[out]     dst  Output tensor info. Data types supported: Same as @p src0.
84*c217d954SCole Faust      */
85*c217d954SCole Faust     void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
86*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
87*c217d954SCole Faust      *
88*c217d954SCole Faust      * Similar to @ref CpuElementwiseDivision::configure()
89*c217d954SCole Faust      *
90*c217d954SCole Faust      * @return a status
91*c217d954SCole Faust      */
92*c217d954SCole Faust     static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
93*c217d954SCole Faust };
94*c217d954SCole Faust 
95*c217d954SCole Faust /** Basic function to run @ref cpu::kernels::CpuArithmeticKernel for power
96*c217d954SCole Faust  *
97*c217d954SCole Faust  * @note The tensor data type for the inputs must be F16/F32.
98*c217d954SCole Faust  * @note The function performs a elementwise power of in1 to in2 (i.e., out[i] = in1[i] ^ in2[i])
99*c217d954SCole Faust  * @note For an exponent that is a float, this function will only work with a positive base.
100*c217d954SCole Faust  */
101*c217d954SCole Faust class CpuElementwisePower : public CpuElementwiseBase
102*c217d954SCole Faust {
103*c217d954SCole Faust public:
104*c217d954SCole Faust     /** Initialise the kernel's inputs, dst and conversion policy.
105*c217d954SCole Faust      *
106*c217d954SCole Faust      * @param[in, out] src0 First tensor input info. Data types supported: F16/F32.
107*c217d954SCole Faust      * @param[in, out] src1 Second tensor input info. Data types supported: Same as @p src0.
108*c217d954SCole Faust      * @param[out]     dst  Output tensor info. Data types supported: Same as @p src0.
109*c217d954SCole Faust      */
110*c217d954SCole Faust     void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
111*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
112*c217d954SCole Faust      *
113*c217d954SCole Faust      * Similar to @ref CpuElementwisePower::configure()
114*c217d954SCole Faust      *
115*c217d954SCole Faust      * @return a status
116*c217d954SCole Faust      */
117*c217d954SCole Faust     static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
118*c217d954SCole Faust };
119*c217d954SCole Faust 
120*c217d954SCole Faust /** Basic function to run @ref cpu::kernels::CpuComparisonKernel.
121*c217d954SCole Faust  *
122*c217d954SCole Faust  * @note The tensor data type for the inputs must be QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32.
123*c217d954SCole Faust  * @note The function performs a comparison operation between two tensors.
124*c217d954SCole Faust  */
125*c217d954SCole Faust class CpuElementwiseComparison : public CpuElementwiseBase
126*c217d954SCole Faust {
127*c217d954SCole Faust public:
128*c217d954SCole Faust     /** Initialise the kernel's inputs, dst and conversion policy.
129*c217d954SCole Faust      *
130*c217d954SCole Faust      * @param[in, out] src0 First tensor input info. Data types supported: QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32.
131*c217d954SCole Faust      * @param[in, out] src1 Second tensor input info. Data types supported: Same as @p src0.
132*c217d954SCole Faust      * @param[out]     dst  Output tensor info. Data types supported: U16/U32.
133*c217d954SCole Faust      * @param[in]      op   Comparison Operation to be performed.
134*c217d954SCole Faust      */
135*c217d954SCole Faust     void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ComparisonOperation op);
136*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
137*c217d954SCole Faust      *
138*c217d954SCole Faust      * Similar to @ref CpuElementwiseComparison::configure()
139*c217d954SCole Faust      *
140*c217d954SCole Faust      * @return a status
141*c217d954SCole Faust      */
142*c217d954SCole Faust     static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ComparisonOperation op);
143*c217d954SCole Faust };
144*c217d954SCole Faust 
145*c217d954SCole Faust /** Basic function to run @ref cpu::kernels::CpuComparisonKernel
146*c217d954SCole Faust  *
147*c217d954SCole Faust  * @note The tensor data type for the inputs must be QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32.
148*c217d954SCole Faust  * @note The function performs a comparison operation between two tensors.
149*c217d954SCole Faust  */
150*c217d954SCole Faust template <ComparisonOperation op>
151*c217d954SCole Faust class CpuElementwiseComparisonStatic : public CpuElementwiseBase
152*c217d954SCole Faust {
153*c217d954SCole Faust public:
154*c217d954SCole Faust     /** Initialise the kernel's inputs, dst and conversion policy.
155*c217d954SCole Faust      *
156*c217d954SCole Faust      * @param[in, out] src0 First tensor input info. Data types supported: QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32.
157*c217d954SCole Faust      * @param[in, out] src1 Second tensor input info. Data types supported: Same as @p src0.
158*c217d954SCole Faust      * @param[out]     dst  Output tensor info. Data types supported: U16/U32.
159*c217d954SCole Faust      */
160*c217d954SCole Faust     void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
161*c217d954SCole Faust     /** Static function to check if given info will lead to a valid configuration
162*c217d954SCole Faust      *
163*c217d954SCole Faust      * Similar to @ref CpuElementwiseComparisonStatic::configure()
164*c217d954SCole Faust      *
165*c217d954SCole Faust      * @return a status
166*c217d954SCole Faust      */
167*c217d954SCole Faust     static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
168*c217d954SCole Faust };
169*c217d954SCole Faust 
170*c217d954SCole Faust /** Basic function to run equal comparison. */
171*c217d954SCole Faust using NEEqual = CpuElementwiseComparisonStatic<ComparisonOperation::Equal>;
172*c217d954SCole Faust /** Basic function to run not equal comparison. */
173*c217d954SCole Faust using NENotEqual = CpuElementwiseComparisonStatic<ComparisonOperation::NotEqual>;
174*c217d954SCole Faust /** Basic function to run greater comparison. */
175*c217d954SCole Faust using NEGreater = CpuElementwiseComparisonStatic<ComparisonOperation::Greater>;
176*c217d954SCole Faust /** Basic function to run greater-equal comparison. */
177*c217d954SCole Faust using NEGreaterEqual = CpuElementwiseComparisonStatic<ComparisonOperation::GreaterEqual>;
178*c217d954SCole Faust /** Basic function to run less comparison. */
179*c217d954SCole Faust using NELess = CpuElementwiseComparisonStatic<ComparisonOperation::Less>;
180*c217d954SCole Faust /** Basic function to run less-equal comparison. */
181*c217d954SCole Faust using NELessEqual = CpuElementwiseComparisonStatic<ComparisonOperation::LessEqual>;
182*c217d954SCole Faust } // namespace cpu
183*c217d954SCole Faust } // namespace arm_compute
184*c217d954SCole Faust 
185*c217d954SCole Faust #endif /* ARM_COMPUTE_CPU_ELEMENTWISE_H */