1 /* 2 * Copyright (c) 2018-2020 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_NEELEMENTWISEOPERATIONKERNEL_H 25 #define ARM_COMPUTE_NEELEMENTWISEOPERATIONKERNEL_H 26 27 #include "arm_compute/core/Types.h" 28 #include "src/core/NEON/INEKernel.h" 29 30 namespace arm_compute 31 { 32 class ITensor; 33 34 /** Interface for an element-wise operation kernel 35 * 36 * Element-wise operation is computed by: 37 * @f[ output(x,y) = OP(input1(x,y), input2(x,y))@f] 38 * 39 */ 40 class NEElementwiseOperationKernel : public INEKernel 41 { 42 public: name()43 const char *name() const override 44 { 45 return "NEElementwiseOperationKernel"; 46 } 47 /** Default constructor */ 48 NEElementwiseOperationKernel(); 49 /** Prevent instances of this class from being copied (As this class contains pointers) */ 50 NEElementwiseOperationKernel(const NEElementwiseOperationKernel &) = delete; 51 /** Prevent instances of this class from being copied (As this class contains pointers) */ 52 NEElementwiseOperationKernel &operator=(const NEElementwiseOperationKernel &) = delete; 53 /** Allow instances of this class to be moved */ 54 NEElementwiseOperationKernel(NEElementwiseOperationKernel &&) = default; 55 /** Allow instances of this class to be moved */ 56 NEElementwiseOperationKernel &operator=(NEElementwiseOperationKernel &&) = default; 57 /** Default destructor */ 58 ~NEElementwiseOperationKernel() = default; 59 60 /** Common signature for all the specialised arithmetic functions 61 * 62 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/S16/F16/S32/F32. 63 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 64 * @param[out] output Output tensor info. Data types supported: Dependent on subclass. 65 * @param[in] window Region on which to execute the kernel. 66 */ 67 using ElementwiseFunction = void(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window); 68 69 // Inherited methods overridden: 70 void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override; 71 72 protected: 73 /** Validate the argument passed to the kernel 74 * 75 * @param[in] input1 First tensor input. Data types supported: QASYMM8/S16/F16/S32/F32. 76 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. 77 * @param[in] output Output tensor. Data types supported: Dependent on subclass. 78 */ 79 static Status validate_arguments_common(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output); 80 81 /** Commmon configure function for element-wise operators with no additional options (e.g. Min, Max, SquaredDiff) 82 * 83 */ 84 void configure_common(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output); 85 86 /** Function to use for the particular tensor types passed to configure() */ 87 std::function<void(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window)> _function; 88 89 const ITensor *_input1; 90 const ITensor *_input2; 91 ITensor *_output; 92 }; 93 94 class NEArithmeticOperationKernel : public NEElementwiseOperationKernel 95 { 96 public: 97 /** Default constructor */ 98 NEArithmeticOperationKernel() = default; 99 100 /** Configure kernel 101 * 102 * @param[in] op Arithmetic operation to be executed. 103 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/S16/F16/S32/F32. 104 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 105 * @param[out] output Output tensor info. Data types supported: Same as @p input1. 106 */ 107 void configure(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output); 108 109 /** Static function to check if given info will lead to a valid configuration of @ref NEArithmeticOperationKernel 110 * 111 * @param[in] op Arithmetic operation to be executed. 112 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/S16/F16/S32/F32. 113 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 114 * @param[in] output Output tensor info. Data types supported: Same as @p input1. 115 * 116 * @return a Status 117 */ 118 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); 119 120 protected: 121 // Inherited methods overridden: 122 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output); 123 }; 124 125 class NEDivisionOperationKernel : public NEArithmeticOperationKernel 126 { 127 public: 128 /** Default constructor */ 129 NEDivisionOperationKernel() = default; 130 131 /** Configure kernel 132 * 133 * @param[in] input1 First tensor input info. Data types supported: S32/F16/F32. 134 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 135 * @param[out] output Output tensor info. Data types supported: Same as @p input1. 136 */ 137 void configure(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output); 138 139 /** Static function to check if given info will lead to a valid configuration of @ref NEDivisionOperationKernel 140 * 141 * @param[in] input1 First tensor input info. Data types supported: S32/F16/F32. 142 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 143 * @param[in] output Output tensor info. Data types supported: Same as @p input1. 144 * 145 * @return a Status 146 */ 147 static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); 148 149 protected: 150 // Inherited methods overridden: 151 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output); 152 }; 153 154 class NEPowerOperationKernel : public NEArithmeticOperationKernel 155 { 156 public: 157 /** Default constructor */ 158 NEPowerOperationKernel() = default; 159 160 /** Configure kernel 161 * 162 * @param[in] input1 First tensor input info. Data types supported: F16/F32. 163 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 164 * @param[out] output Output tensor info. Data types supported: Same as @p input1. 165 */ 166 void configure(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output); 167 168 /** Static function to check if given info will lead to a valid configuration of @ref NEPowerOperationKernel 169 * 170 * @param[in] input1 First tensor input info. Data types supported: F16/F32. 171 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 172 * @param[in] output Output tensor info. Data types supported: Same as @p input1. 173 * 174 * @return a Status 175 */ 176 static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); 177 178 protected: 179 // Inherited methods overridden: 180 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output); 181 }; 182 183 class NEComparisonOperationKernel : public NEElementwiseOperationKernel 184 { 185 public: 186 /** Default constructor */ 187 NEComparisonOperationKernel() = default; 188 189 /** Configure kernel 190 * 191 * @param[in] op Comparison operation to be executed. 192 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32. 193 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 194 * @param[out] output Output tensor info. Data types supported: U8. 195 */ 196 void configure(ComparisonOperation op, const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output); 197 198 /** Static function to check if given info will lead to a valid configuration of @ref NEComparisonOperationKernel 199 * 200 * @param[in] op Comparison operation to be executed. 201 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32. 202 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 203 * @param[in] output Output tensor info. Data types supported: U8. 204 * 205 * @return a Status 206 */ 207 static Status validate(ComparisonOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); 208 209 protected: 210 // Inherited methods overridden: 211 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output); 212 }; 213 } // namespace arm_compute 214 #endif /* ARM_COMPUTE_NEELEMENTWISEOPERATIONKERNEL_H */ 215