giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 1 | /* |
Manuel Bottini | 6eb90d7 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H__ |
| 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 | * |
| 67 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. |
| 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 | * |
| 77 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. |
| 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); |
| 99 | |
| 100 | private: |
| 101 | const ICLTensor *_input1; /**< Source tensor 1 */ |
| 102 | const ICLTensor *_input2; /**< Source tensor 2 */ |
| 103 | ICLTensor *_output; /**< Destination tensor */ |
| 104 | }; |
| 105 | |
| 106 | /** Addition operation */ |
| 107 | class CLSaturatedArithmeticOperationKernel : public CLElementwiseOperationKernel |
| 108 | { |
| 109 | public: |
| 110 | CLSaturatedArithmeticOperationKernel() |
| 111 | : CLElementwiseOperationKernel(), _policy(), _op() |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel |
| 116 | * |
| 117 | * @param[in] op Arithmetic operation to be executed. |
| 118 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 119 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 120 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 121 | * @param[in] policy Policy to use to handle overflow. |
| 122 | */ |
| 123 | void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy); |
| 124 | |
| 125 | /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel |
| 126 | * |
| 127 | * @param[in] op Arithmetic operation to be executed. |
| 128 | * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 129 | * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1. |
| 130 | * @param[in] output Output tensor info. Data types supported: Same as @p input1. |
| 131 | * @param[in] policy Policy to use to handle overflow. |
| 132 | * |
| 133 | * @return a Status |
| 134 | */ |
| 135 | static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy); |
| 136 | |
| 137 | protected: |
| 138 | // Inherited methods overridden: |
| 139 | std::string name() override; |
| 140 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override; |
| 141 | Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 142 | CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 143 | std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override; |
| 144 | |
| 145 | private: |
| 146 | ConvertPolicy _policy; |
| 147 | ArithmeticOperation _op; |
| 148 | }; |
| 149 | |
| 150 | class CLArithmeticOperationKernel : public CLElementwiseOperationKernel |
| 151 | { |
| 152 | public: |
| 153 | CLArithmeticOperationKernel() |
| 154 | : CLElementwiseOperationKernel(), _op() |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel |
| 159 | * |
| 160 | * @param[in] op Arithmetic operation to be executed. |
| 161 | * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 162 | * @param[in] input2 Second tensor input. Data types supported: Same as @p input1. |
| 163 | * @param[in] output Output tensor. Data types supported: Same as @p input1. |
| 164 | */ |
| 165 | void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output); |
| 166 | |
| 167 | /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel |
| 168 | * |
| 169 | * @param[in] op Arithmetic operation to be executed. |
| 170 | * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32. |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 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(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output); |
| 177 | |
| 178 | protected: |
| 179 | // Inherited methods overridden: |
| 180 | std::string name() override; |
| 181 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override; |
| 182 | Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 183 | CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override; |
| 184 | std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override; |
| 185 | |
| 186 | private: |
| 187 | ArithmeticOperation _op; |
| 188 | }; |
| 189 | } // namespace arm_compute |
| 190 | #endif /* __ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H__ */ |