blob: 85961f28bc7af7425414ca5bfdf97228be25fa34 [file] [log] [blame]
giuros01164a2722018-11-20 18:34:46 +00001/*
Giorgio Arena8b2a7d32020-02-11 17:21:31 +00002 * Copyright (c) 2018-2020 ARM Limited.
giuros01164a2722018-11-20 18:34:46 +00003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H
25#define ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H
giuros01164a2722018-11-20 18:34:46 +000026
27#include "arm_compute/core/CL/ICLKernel.h"
28#include "arm_compute/core/Types.h"
29
30namespace arm_compute
31{
32class 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 */
40class CLElementwiseOperationKernel : public ICLKernel
41{
42public:
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
61protected:
62 /** The name of the operation */
63 virtual std::string name() = 0;
64
65 /** Initialise the kernel's output.
66 *
Kurtis Charnockec0c4122019-12-05 14:13:46 +000067 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
giuros01164a2722018-11-20 18:34:46 +000068 * @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 Charnockec0c4122019-12-05 14:13:46 +000077 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
giuros01164a2722018-11-20 18:34:46 +000078 * @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
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000100 ActivationLayerInfo _act_info;
101
giuros01164a2722018-11-20 18:34:46 +0000102private:
103 const ICLTensor *_input1; /**< Source tensor 1 */
104 const ICLTensor *_input2; /**< Source tensor 2 */
105 ICLTensor *_output; /**< Destination tensor */
106};
107
108/** Addition operation */
109class CLSaturatedArithmeticOperationKernel : public CLElementwiseOperationKernel
110{
111public:
112 CLSaturatedArithmeticOperationKernel()
113 : CLElementwiseOperationKernel(), _policy(), _op()
114 {
115 }
116
117 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
118 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000119 * @param[in] op Arithmetic operation to be executed.
120 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
121 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
122 * @param[in] output Output tensor. Data types supported: Same as @p input1.
123 * @param[in] policy Policy to use to handle overflow.
124 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000125 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000126 void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ConvertPolicy &policy, const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000127
128 /** Static function to check if given info will lead to a valid configuration of @ref CLSaturatedArithmeticOperationKernel
129 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000130 * @param[in] op Arithmetic operation to be executed.
131 * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/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.
giuros01164a2722018-11-20 18:34:46 +0000136 *
137 * @return a Status
138 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000139 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
140 const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000141
142protected:
143 // Inherited methods overridden:
144 std::string name() override;
145 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
146 Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
147 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
148 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
149
150private:
151 ConvertPolicy _policy;
152 ArithmeticOperation _op;
153};
154
155class CLArithmeticOperationKernel : public CLElementwiseOperationKernel
156{
157public:
158 CLArithmeticOperationKernel()
159 : CLElementwiseOperationKernel(), _op()
160 {
161 }
162
163 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
164 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000165 * @param[in] op Arithmetic operation to be executed.
166 * @param[in] input1 First tensor input. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
167 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
168 * @param[in] output Output tensor. Data types supported: Same as @p input1.
169 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000170 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000171 void configure(ArithmeticOperation op, const ICLTensor *input1, const ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000172
173 /** Static function to check if given info will lead to a valid configuration of @ref CLArithmeticOperationKernel
174 *
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000175 * @param[in] op Arithmetic operation to be executed.
176 * @param[in] input1 First tensor input info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/QSYMM16/F16/U32/S32/F32.
177 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
178 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
179 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
giuros01164a2722018-11-20 18:34:46 +0000180 *
181 * @return a Status
182 */
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000183 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info = ActivationLayerInfo());
giuros01164a2722018-11-20 18:34:46 +0000184
185protected:
186 // Inherited methods overridden:
187 std::string name() override;
188 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
189 Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
190 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
191 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
192
193private:
194 ArithmeticOperation _op;
195};
196} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000197#endif /* ARM_COMPUTE_CLELEMENTWISEOPERATIONKERNEL_H */