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