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