blob: 3783e1571c9e66e681b3f28272826aa8d1f9ba09 [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"
Georgios Pinitas7891a732021-08-20 21:39:25 +010029#include "src/gpu/cl/ClCompileContext.h"
30#include "src/gpu/cl/IClKernel.h"
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000031
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 *
Sheri Zhanga387e272021-06-29 17:34:06 +010043 * For binary elementwise ops in-place cannot be enabled by passing nullptr to dst, it can only be enabled by passing either src1 or src2 to dst instead.
44 *
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000045 */
46class ClElementwiseKernel : public IClKernel
47{
48public:
Giorgio Arena4a95bba2021-06-28 11:00:27 +010049 ClElementwiseKernel();
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000050 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClElementwiseKernel);
51
52 // Inherited methods overridden:
53 void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override;
54
55protected:
56 /** The name of the operation */
57 virtual std::string name() = 0;
58
59 /** Configure kernel for a given list of arguments
60 *
61 * @param[in] src1 First source tensor info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
62 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
63 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
64 *
65 * @return a pair of Status and Window
66 */
67 virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) = 0;
68
69 /** Generate the build options for the specific kernel
70 *
71 * @reutrn a CLBuildOptions struct
72 */
73 virtual CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) = 0;
74
75 /** Generate the identifier for tuning
76 *
77 * @reutrn a string
78 */
79 virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) = 0;
80
81 /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
82 *
83 */
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000084 void configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
85
86 ActivationLayerInfo _act_info{};
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000087};
88
89class ClLogicalBinaryKernel : public ClElementwiseKernel
90{
91public:
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +000092 ClLogicalBinaryKernel() = default;
93 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClLogicalBinaryKernel);
94 /** Function to configure kernel
95 *
96 * @param[in] compile_context The compile context to be used.
97 * @param[in] op Logical binary operation to be executed.
98 * @param[in] src1 First source tensor info. Data types supported: U8.
99 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
100 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
101 */
102 void configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100103 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000104 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100105 * Similar to @ref ClLogicalBinaryKernel::configure()
106 *
107 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000108 */
109 static Status validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst);
110
111private:
112 // Inherited methods overridden:
113 std::string name() override;
114 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
115 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
116 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
117
118 LogicalOperation _op{ LogicalOperation::Unknown };
119};
120
121/** Addition operation */
122class ClSaturatedArithmeticKernel : public ClElementwiseKernel
123{
124public:
125 ClSaturatedArithmeticKernel() = default;
126 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClSaturatedArithmeticKernel);
127 /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel
128 *
129 * @param[in] compile_context The compile context to be used.
130 * @param[in] op Arithmetic operation to be executed.
131 * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/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.
136 */
137 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy,
138 const ActivationLayerInfo &act_info = ActivationLayerInfo());
139
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100140 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000141 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100142 * Similar to @ref ClSaturatedArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000143 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100144 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000145 */
146 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
147 const ActivationLayerInfo &act_info = ActivationLayerInfo());
148
149protected:
150 // Inherited methods overridden:
151 std::string name() override;
152 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
153 CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
154 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
155
156private:
157 ConvertPolicy _policy{};
158 ArithmeticOperation _op{};
159};
160
161class ClArithmeticKernel : public ClElementwiseKernel
162{
163public:
164 ClArithmeticKernel() = default;
165 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClArithmeticKernel);
166
167 /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel
168 *
169 * @param[in] compile_context The compile context to be used.
170 * @param[in] op Arithmetic operation to be executed.
171 * @param[in] src1 First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
172 * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
173 * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
174 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
175 */
176 void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
177 const ActivationLayerInfo &act_info = ActivationLayerInfo());
178
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100179 /** Static function to check if given info will lead to a valid configuration
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000180 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100181 * Similar to @ref ClArithmeticKernel::configure()
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000182 *
Georgios Pinitas2eb5d162021-07-02 09:01:49 +0100183 * @return a status
Michele Di Giorgio1e0208a2021-01-22 15:42:59 +0000184 */
185 static Status validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info = ActivationLayerInfo());
186
187protected:
188 // Inherited methods overridden:
189 std::string name() override;
190 std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
191 CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
192 std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
193
194private:
195 ArithmeticOperation _op{};
196};
197} // namespace kernels
198} // namespace opencl
199} // namespace arm_compute
200#endif /* ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H */