1 /* 2 * Copyright (c) 2018-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_CL_ELEMENTWISE_KERNEL_H 25 #define ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H 26 27 #include "src/core/KernelTypes.h" 28 #include "src/core/common/Macros.h" 29 #include "src/gpu/cl/ClCompileContext.h" 30 #include "src/gpu/cl/IClKernel.h" 31 32 namespace arm_compute 33 { 34 namespace opencl 35 { 36 namespace kernels 37 { 38 /** Interface for an element-wise operation kernel 39 * 40 * Element-wise operation is computed by: 41 * @f[ dst(x,y) = OP(src1(x,y), src2(x,y))@f] 42 * 43 * 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 * 45 */ 46 class ClElementwiseKernel : public IClKernel 47 { 48 public: 49 ClElementwiseKernel(); 50 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClElementwiseKernel); 51 52 // Inherited methods overridden: 53 void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override; 54 55 protected: 56 /** The name of the operation */ 57 virtual std::string name() = 0; 58 59 /** Configure kernel for a given list of arguments 60 * 61 * @param[in] src1 First source tensor info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32. 62 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1. 63 * @param[in] dst Destination tensor info. Data types supported: same as @p src1. 64 * 65 * @return a pair of Status and Window 66 */ 67 virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) = 0; 68 69 /** Generate the build options for the specific kernel 70 * 71 * @reutrn a CLBuildOptions struct 72 */ 73 virtual CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) = 0; 74 75 /** Generate the identifier for tuning 76 * 77 * @reutrn a string 78 */ 79 virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) = 0; 80 81 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff) 82 * 83 */ 84 void configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst); 85 86 ActivationLayerInfo _act_info{}; 87 }; 88 89 class ClLogicalBinaryKernel : public ClElementwiseKernel 90 { 91 public: 92 ClLogicalBinaryKernel() = default; 93 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClLogicalBinaryKernel); 94 /** Function to configure kernel 95 * 96 * @param[in] compile_context The compile context to be used. 97 * @param[in] op Logical binary operation to be executed. 98 * @param[in] src1 First source tensor info. Data types supported: U8. 99 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1. 100 * @param[in] dst Destination tensor info. Data types supported: same as @p src1. 101 */ 102 void configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst); 103 /** Static function to check if given info will lead to a valid configuration 104 * 105 * Similar to @ref ClLogicalBinaryKernel::configure() 106 * 107 * @return a status 108 */ 109 static Status validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst); 110 111 private: 112 // Inherited methods overridden: 113 std::string name() override; 114 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override; 115 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override; 116 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override; 117 118 LogicalOperation _op{ LogicalOperation::Unknown }; 119 }; 120 121 /** Addition operation */ 122 class ClSaturatedArithmeticKernel : public ClElementwiseKernel 123 { 124 public: 125 ClSaturatedArithmeticKernel() = default; 126 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClSaturatedArithmeticKernel); 127 /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel 128 * 129 * @param[in] compile_context The compile context to be used. 130 * @param[in] op Arithmetic operation to be executed. 131 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32. 132 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. 133 * @param[in] output Output tensor info. Data types supported: Same as @p input1. 134 * @param[in] policy Policy to use to handle overflow. 135 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 136 */ 137 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy, 138 const ActivationLayerInfo &act_info = ActivationLayerInfo()); 139 140 /** Static function to check if given info will lead to a valid configuration 141 * 142 * Similar to @ref ClSaturatedArithmeticKernel::configure() 143 * 144 * @return a status 145 */ 146 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy, 147 const ActivationLayerInfo &act_info = ActivationLayerInfo()); 148 149 protected: 150 // Inherited methods overridden: 151 std::string name() override; 152 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override; 153 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; 154 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override; 155 156 private: 157 ConvertPolicy _policy{}; 158 ArithmeticOperation _op{}; 159 }; 160 161 class ClArithmeticKernel : public ClElementwiseKernel 162 { 163 public: 164 ClArithmeticKernel() = default; 165 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClArithmeticKernel); 166 167 /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel 168 * 169 * @param[in] compile_context The compile context to be used. 170 * @param[in] op Arithmetic operation to be executed. 171 * @param[in] src1 First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32. 172 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1. 173 * @param[in] dst Destination tensor info. Data types supported: same as @p src1. 174 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. 175 */ 176 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, 177 const ActivationLayerInfo &act_info = ActivationLayerInfo()); 178 179 /** Static function to check if given info will lead to a valid configuration 180 * 181 * Similar to @ref ClArithmeticKernel::configure() 182 * 183 * @return a status 184 */ 185 static Status validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info = ActivationLayerInfo()); 186 187 protected: 188 // Inherited methods overridden: 189 std::string name() override; 190 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override; 191 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override; 192 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override; 193 194 private: 195 ArithmeticOperation _op{}; 196 }; 197 } // namespace kernels 198 } // namespace opencl 199 } // namespace arm_compute 200 #endif /* ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H */ 201