giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 1 | /* |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H |
| 25 | #define ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/CL/ICLKernel.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | class ICLTensor; |
| 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 CLElementwiseOperationKernel : public ICLKernel |
| 41 | { |
| 42 | public: |
| 43 | /** Default constructor */ |
| 44 | CLElementwiseOperationKernel(); |
| 45 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 46 | CLElementwiseOperationKernel(const CLElementwiseOperationKernel &) = delete; |
| 47 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 48 | CLElementwiseOperationKernel &operator=(const CLElementwiseOperationKernel &) = delete; |
| 49 | /** Allow instances of this class to be moved */ |
| 50 | CLElementwiseOperationKernel(CLElementwiseOperationKernel &&) = default; |
| 51 | /** Allow instances of this class to be moved */ |
| 52 | CLElementwiseOperationKernel &operator=(CLElementwiseOperationKernel &&) = default; |
| 53 | /** Default destructor */ |
| 54 | ~CLElementwiseOperationKernel() = default; |
| 55 | |
| 56 | // Inherited methods overridden: |
| 57 | void run(const Window &window, cl::CommandQueue &queue) override; |
| 58 | |
| 59 | BorderSize border_size() const override; |
| 60 | |
| 61 | protected: |
| 62 | /** The name of the operation */ |
| 63 | virtual std::string name() = 0; |
| 64 | |
| 65 | /** Initialise the kernel's output. |
| 66 | * |
Kurtis Charnock | ec0c412 | 2019-12-05 14:13:46 +0000 | [diff] [blame] | 67 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 68 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 69 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 70 | * |
| 71 | * @return a pair of Status and Window |
| 72 | */ |
| 73 | virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) = 0; |
| 74 | |
| 75 | /** Validate the argument passed to the kernel |
| 76 | * |
Kurtis Charnock | ec0c412 | 2019-12-05 14:13:46 +0000 | [diff] [blame] | 77 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 78 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 79 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 80 | */ |
| 81 | virtual Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) = 0; |
| 82 | |
| 83 | /** Generate the build options for the specific kernel |
| 84 | * |
| 85 | * @reutrn a CLBuildOptions struct |
| 86 | */ |
| 87 | virtual CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) = 0; |
| 88 | |
| 89 | /** Generate the identifier for tuning |
| 90 | * |
| 91 | * @reutrn a string |
| 92 | */ |
| 93 | virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) = 0; |
| 94 | |
| 95 | /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff) |
| 96 | * |
| 97 | */ |
| 98 | void configure_common(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 99 | /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff) |
| 100 | * |
| 101 | */ |
| 102 | void configure_common(CLCompileContext &compile_context, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 103 | |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 104 | ActivationLayerInfo _act_info; |
| 105 | |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 106 | private: |
| 107 | const ICLTensor *_input1; /**< Source tensor 1 */ |
| 108 | const ICLTensor *_input2; /**< Source tensor 2 */ |
| 109 | ICLTensor *_output; /**< Destination tensor */ |
| 110 | }; |
| 111 | |
| 112 | /** Addition operation */ |
| 113 | class CLSaturatedArithmeticOperationKernel : public CLElementwiseOperationKernel |
| 114 | { |
| 115 | public: |
| 116 | CLSaturatedArithmeticOperationKernel() |
| 117 | : CLElementwiseOperationKernel(), _policy(), _op() |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel |
| 122 | * |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 123 | * @param[in] op Arithmetic operation to be executed. |
| 124 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32. |
| 125 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 126 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 127 | * @param[in] policy Policy to use to handle overflow. |
| 128 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 129 | */ |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 130 | void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy, const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 131 | /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel |
| 132 | * |
| 133 | * @param[in] compile_context The compile context to be used. |
| 134 | * @param[in] op Arithmetic operation to be executed. |
| 135 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32. |
| 136 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 137 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 138 | * @param[in] policy Policy to use to handle overflow. |
| 139 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 140 | */ |
| 141 | void configure(CLCompileContext &compile_context, ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy, |
| 142 | const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 143 | |
| 144 | /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel |
| 145 | * |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 146 | * @param[in] op Arithmetic operation to be executed. |
| 147 | * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32. |
| 148 | * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. |
| 149 | * @param[in] output Output tensor info. Data types supported: Same as @p input1. |
| 150 | * @param[in] policy Policy to use to handle overflow. |
| 151 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 152 | * |
| 153 | * @return a Status |
| 154 | */ |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 155 | static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy, |
| 156 | const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 157 | |
| 158 | protected: |
| 159 | // Inherited methods overridden: |
| 160 | std::string name() override; |
| 161 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override; |
| 162 | Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 163 | CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 164 | std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override; |
| 165 | |
| 166 | private: |
| 167 | ConvertPolicy _policy; |
| 168 | ArithmeticOperation _op; |
| 169 | }; |
| 170 | |
| 171 | class CLArithmeticOperationKernel : public CLElementwiseOperationKernel |
| 172 | { |
| 173 | public: |
| 174 | CLArithmeticOperationKernel() |
| 175 | : CLElementwiseOperationKernel(), _op() |
| 176 | { |
| 177 | } |
| 178 | |
| 179 | /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel |
| 180 | * |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 181 | * @param[in] op Arithmetic operation to be executed. |
| 182 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32. |
| 183 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 184 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 185 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 186 | */ |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 187 | void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 188 | /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel |
| 189 | * |
| 190 | * @param[in] compile_context The compile context to be used. |
| 191 | * @param[in] op Arithmetic operation to be executed. |
| 192 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32. |
| 193 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 194 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 195 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 196 | */ |
| 197 | void configure(CLCompileContext &compile_context, ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, |
| 198 | const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 199 | |
| 200 | /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel |
| 201 | * |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 202 | * @param[in] op Arithmetic operation to be executed. |
| 203 | * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32. |
| 204 | * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. |
| 205 | * @param[in] output Output tensor info. Data types supported: Same as @p input1. |
| 206 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 207 | * |
| 208 | * @return a Status |
| 209 | */ |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 210 | static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info = ActivationLayerInfo()); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 211 | |
| 212 | protected: |
| 213 | // Inherited methods overridden: |
| 214 | std::string name() override; |
| 215 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override; |
| 216 | Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 217 | CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 218 | std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override; |
| 219 | |
| 220 | private: |
| 221 | ArithmeticOperation _op; |
| 222 | }; |
| 223 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 224 | #endif /* ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H */ |