Compute Library
 21.05
ClElementwiseKernel.h
Go to the documentation of this file.
1 /*
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"
31 
32 namespace arm_compute
33 {
34 namespace opencl
35 {
36 namespace 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  */
45 {
46 public:
47  /** Default constructor */
48  ClElementwiseKernel() = default;
50 
51  // Inherited methods overridden:
52  void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override;
53 
54 protected:
55  /** The name of the operation */
56  virtual std::string name() = 0;
57 
58  /** Configure kernel for a given list of arguments
59  *
60  * @param[in] src1 First source tensor info. Data types supported: U8/S8/QASYMM8/QASYMM8_SIGNED/U16/S16/F16/U32/S32/F32.
61  * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
62  * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
63  *
64  * @return a pair of Status and Window
65  */
66  virtual std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) = 0;
67 
68  /** Generate the build options for the specific kernel
69  *
70  * @reutrn a CLBuildOptions struct
71  */
72  virtual CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) = 0;
73 
74  /** Generate the identifier for tuning
75  *
76  * @reutrn a string
77  */
78  virtual std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) = 0;
79 
80  /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
81  *
82  */
83  void configure_common(ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
84  /** Commmon configure function for element-wise operators with no additional options (e.g., Div, Min, Max, SquaredDiff)
85  *
86  */
87  void configure_common(const ClCompileContext &compile_context, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
88 
89  ActivationLayerInfo _act_info{};
90 
91 private:
92  const ITensorInfo *_src1{ nullptr }; /**< Source tensor info 1 */
93  const ITensorInfo *_src2{ nullptr }; /**< Source tensor info 2 */
94  ITensorInfo *_dst{ nullptr }; /**< Destination tensor info */
95 };
96 
98 {
99 public:
100  /** Default constructor */
101  ClLogicalBinaryKernel() = default;
103  /** Function to configure kernel
104  *
105  * @param[in] compile_context The compile context to be used.
106  * @param[in] op Logical binary operation to be executed.
107  * @param[in] src1 First source tensor info. Data types supported: U8.
108  * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
109  * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
110  */
111  void configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
112  /** Static function to check if the given configuration is valid for this kernel
113  *
114  * @param[in] op Logical binary operation to be executed.
115  * @param[in] src1 First source tensor info. Data types supported: U8.
116  * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
117  * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
118  */
119  static Status validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst);
120 
121 private:
122  // Inherited methods overridden:
123  std::string name() override;
124  std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
125  CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
126  std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
127 
129 };
130 
131 /** Addition operation */
133 {
134 public:
135  ClSaturatedArithmeticKernel() = default;
137  /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel
138  *
139  * @param[in] compile_context The compile context to be used.
140  * @param[in] op Arithmetic operation to be executed.
141  * @param[in] input1 First tensor input info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
142  * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
143  * @param[in] output Output tensor info. Data types supported: Same as @p input1.
144  * @param[in] policy Policy to use to handle overflow.
145  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
146  */
147  void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy,
148  const ActivationLayerInfo &act_info = ActivationLayerInfo());
149 
150  /** Static function to check if given info will lead to a valid configuration of @ref ClSaturatedArithmeticKernel
151  *
152  * @param[in] op Arithmetic operation to be executed.
153  * @param[in] input1 First tensor input info info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
154  * @param[in] input2 Second tensor input info info. Data types supported: Same as @p input1.
155  * @param[in] output Output tensor info info. Data types supported: Same as @p input1.
156  * @param[in] policy Policy to use to handle overflow.
157  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
158  *
159  * @return a Status
160  */
161  static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy,
162  const ActivationLayerInfo &act_info = ActivationLayerInfo());
163 
164 protected:
165  // Inherited methods overridden:
166  std::string name() override;
167  std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) override;
168  CLBuildOptions generate_build_options(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) override;
169  std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &input1, const ITensorInfo &output) override;
170 
171 private:
172  ConvertPolicy _policy{};
173  ArithmeticOperation _op{};
174 };
175 
177 {
178 public:
179  ClArithmeticKernel() = default;
181 
182  /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel
183  *
184  * @param[in] compile_context The compile context to be used.
185  * @param[in] op Arithmetic operation to be executed.
186  * @param[in] src1 First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
187  * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
188  * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
189  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
190  */
191  void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst,
192  const ActivationLayerInfo &act_info = ActivationLayerInfo());
193 
194  /** Static function to check if given info will lead to a valid configuration of @ref ClArithmeticKernel
195  *
196  * @param[in] op Arithmetic operation to be executed.
197  * @param[in] src1 First source tensor info. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/QSYMM16/F16/S32/F32.
198  * @param[in] src2 Second source tensor info. Data types supported: same as @p src1.
199  * @param[in] dst Destination tensor info. Data types supported: same as @p src1.
200  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
201  *
202  * @return a Status
203  */
204  static Status validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info = ActivationLayerInfo());
205 
206 protected:
207  // Inherited methods overridden:
208  std::string name() override;
209  std::pair<Status, Window> validate_and_configure_window(ITensorInfo &src1, ITensorInfo &src2, ITensorInfo &dst) override;
210  CLBuildOptions generate_build_options(const ITensorInfo &src1, const ITensorInfo &src2, const ITensorInfo &dst) override;
211  std::string generate_id_for_tuning(const std::string &kernel_name, const ITensorInfo &src1, const ITensorInfo &dst) override;
212 
213 private:
214  ArithmeticOperation _op{};
215 };
216 } // namespace kernels
217 } // namespace opencl
218 } // namespace arm_compute
219 #endif /* ARM_COMPUTE_CL_ELEMENTWISE_KERNEL_H */
static Status validate(LogicalOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst)
Static function to check if the given configuration is valid for this kernel.
static Status validate(ArithmeticOperation op, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of ClArithmeticKernel.
ArithmeticOperation
Available element-wise operations.
Definition: Types.h:470
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClSaturatedArithmeticKernel)
Store the tensor's metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Common interface for all the OpenCL kernels.
Definition: ICLKernel.h:46
Activation Layer Information class.
Definition: Types.h:1478
void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ConvertPolicy &policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of ClSaturatedArithmeticKer...
Copyright (c) 2017-2021 Arm Limited.
void run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue) override
ClLogicalBinaryKernel()=default
Default constructor.
static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ConvertPolicy &policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of ClSaturatedArithmeticKer...
std::string kernel_name
Interface for an element-wise operation kernel.
ClElementwiseKernel()=default
Default constructor.
CLCompileContext class.
void configure(const ClCompileContext &compile_context, LogicalOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst)
Function to configure kernel.
Tensor packing service.
Definition: ITensorPack.h:37
void configure(const ClCompileContext &compile_context, ArithmeticOperation op, ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of ClArithmeticKernel.
LogicalOperation
List of supported logical operations.
Definition: KernelTypes.h:30
Describe a multidimensional execution window.
Definition: Window.h:39
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:385