blob: ab5c777ae6d2b8c0976e3ccf7e3931d454bb8dcf [file] [log] [blame]
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +00001/*
2 * Copyright (c) 2018-2021 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_CL_ELEMENTWISE_KERNEL_H
25#define ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H
26
27#include "src/core/KernelTypes.h"
28#include "src/core/common/Macros.h"
29#include "src/core/gpu/cl/ClCompileContext.h"
30#include "src/core/gpu/cl/IClKernel.h"
31
32namespace arm_compute
33{
34namespace opencl
35{
36namespace kernels
37{
38/** Interface for an element-wise operation kernel
39 *
40 * Element-wise operation is computed by:
41 * @f[ dst(x,y) = OP(src1(x,y), src2(x,y))@f]
42 *
43 */
44class ClElementwiseKernel : public IClKernel
45{
46public:
Giorgio Arena4a95bba2021-06-28 11:00:27 +010047 ClElementwiseKernel();
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000048 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClElementwiseKernel);
49
50 // Inherited methods overridden:
51 void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override;
52
53protected:
54 /** The name of the operation */
55 virtual std::string name() = 0;
56
57 /** Configure kernel for a given list of arguments
58 *
59 * @param[in] src1 First source tensor info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
60 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
61 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
62 *
63 * @return a pair of Status and Window
64 */
65 virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) = 0;
66
67 /** Generate the build options for the specific kernel
68 *
69 * @reutrn a CLBuildOptions struct
70 */
71 virtual CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) = 0;
72
73 /** Generate the identifier for tuning
74 *
75 * @reutrn a string
76 */
77 virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) = 0;
78
79 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
80 *
81 */
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000082 void configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
83
84 ActivationLayerInfo _act_info{};
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000085};
86
87class ClLogicalBinaryKernel : public ClElementwiseKernel
88{
89public:
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000090 ClLogicalBinaryKernel() = default;
91 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClLogicalBinaryKernel);
92 /** Function to configure kernel
93 *
94 * @param[in] compile_context The compile context to be used.
95 * @param[in] op Logical binary operation to be executed.
96 * @param[in] src1 First source tensor info. Data types supported: U8.
97 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
98 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
99 */
100 void configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100101 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000102 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100103 * Similar to @ref ClLogicalBinaryKernel::configure()
104 *
105 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000106 */
107 static Status validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst);
108
109private:
110 // Inherited methods overridden:
111 std::string name() override;
112 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
113 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
114 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
115
116 LogicalOperation _op{ LogicalOperation::Unknown };
117};
118
119/** Addition operation */
120class ClSaturatedArithmeticKernel : public ClElementwiseKernel
121{
122public:
123 ClSaturatedArithmeticKernel() = default;
124 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClSaturatedArithmeticKernel);
125 /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel
126 *
127 * @param[in] compile_context The compile context to be used.
128 * @param[in] op Arithmetic operation to be executed.
129 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
130 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
131 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
132 * @param[in] policy Policy to use to handle overflow.
133 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
134 */
135 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy,
136 const ActivationLayerInfo &act_info = ActivationLayerInfo());
137
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100138 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000139 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100140 * Similar to @ref ClSaturatedArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000141 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100142 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000143 */
144 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
145 const ActivationLayerInfo &act_info = ActivationLayerInfo());
146
147protected:
148 // Inherited methods overridden:
149 std::string name() override;
150 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
151 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
152 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
153
154private:
155 ConvertPolicy _policy{};
156 ArithmeticOperation _op{};
157};
158
159class ClArithmeticKernel : public ClElementwiseKernel
160{
161public:
162 ClArithmeticKernel() = default;
163 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClArithmeticKernel);
164
165 /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel
166 *
167 * @param[in] compile_context The compile context to be used.
168 * @param[in] op Arithmetic operation to be executed.
169 * @param[in] src1 First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
170 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
171 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
172 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
173 */
174 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
175 const ActivationLayerInfo &act_info = ActivationLayerInfo());
176
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100177 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000178 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100179 * Similar to @ref ClArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000180 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100181 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000182 */
183 static Status validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info = ActivationLayerInfo());
184
185protected:
186 // Inherited methods overridden:
187 std::string name() override;
188 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
189 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
190 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
191
192private:
193 ArithmeticOperation _op{};
194};
195} // namespace kernels
196} // namespace opencl
197} // namespace arm_compute
198#endif /* ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H */