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