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