blob: 92cf880172298cb8234d19ac2663cc1db0a9f9a9 [file] [log] [blame]
giuros0192fd9432018-12-03 17:30:00 +00001/*
Sang-Hoon Park63001ac2021-01-18 14:20:27 +00002 * Copyright (c) 2021 Arm Limited.
giuros0192fd9432018-12-03 17:30:00 +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
George Wortd88590f2018-12-12 17:39:58 +000017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
giuros0192fd9432018-12-03 17:30:00 +000018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
George Wortd88590f2018-12-12 17:39:58 +000019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
giuros0192fd9432018-12-03 17:30:00 +000020 * 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 */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000024#ifndef ARM_COMPUTE_CPU_ELEMENTWISE_KERNEL_H
25#define ARM_COMPUTE_CPU_ELEMENTWISE_KERNEL_H
giuros0192fd9432018-12-03 17:30:00 +000026
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000027#include "src/core/common/Macros.h"
28#include "src/core/cpu/ICpuKernel.h"
giuros0192fd9432018-12-03 17:30:00 +000029
30namespace arm_compute
31{
32class ITensor;
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000033namespace cpu
34{
35namespace kernels
36{
giuros0192fd9432018-12-03 17:30:00 +000037/** Interface for an element-wise operation kernel
38 *
39 * Element-wise operation is computed by:
40 * @f[ output(x,y) = OP(input1(x,y), input2(x,y))@f]
41 *
42 */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000043class CpuElementwiseKernel : public ICpuKernel
giuros0192fd9432018-12-03 17:30:00 +000044{
45public:
46 const char *name() const override
47 {
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000048 return "CpuElementwiseKernel";
giuros0192fd9432018-12-03 17:30:00 +000049 }
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000050
51 CpuElementwiseKernel() = default;
52 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuElementwiseKernel);
giuros0192fd9432018-12-03 17:30:00 +000053
George Wortd88590f2018-12-12 17:39:58 +000054 /** Common signature for all the specialised arithmetic functions
55 *
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010056 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/S16/F16/S32/F32.
57 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
58 * @param[out] output Output tensor info. Data types supported: Dependent on subclass.
59 * @param[in] window Region on which to execute the kernel.
George Wortd88590f2018-12-12 17:39:58 +000060 */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000061 using ElementwiseFunction = void(const ITensor *, const ITensor *, ITensor *, const Window &);
George Wortd88590f2018-12-12 17:39:58 +000062
Michalis Spyrouce0c6752020-06-18 10:14:57 +010063 // Inherited methods overridden:
Georgios Pinitas0499dff2020-07-31 22:21:38 +010064 void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
Michalis Spyrouce0c6752020-06-18 10:14:57 +010065
giuros0192fd9432018-12-03 17:30:00 +000066protected:
67 /** Validate the argument passed to the kernel
68 *
George Wortd88590f2018-12-12 17:39:58 +000069 * @param[in] input1 First tensor input. Data types supported: QASYMM8/S16/F16/S32/F32.
giuros0192fd9432018-12-03 17:30:00 +000070 * @param[in] input2 Second tensor input. Data types supported: Same as @p input1.
George Wortd88590f2018-12-12 17:39:58 +000071 * @param[in] output Output tensor. Data types supported: Dependent on subclass.
giuros0192fd9432018-12-03 17:30:00 +000072 */
George Wortd88590f2018-12-12 17:39:58 +000073 static Status validate_arguments_common(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output);
giuros0192fd9432018-12-03 17:30:00 +000074
75 /** Commmon configure function for element-wise operators with no additional options (e.g. Min, Max, SquaredDiff)
76 *
77 */
Michalis Spyrouce0c6752020-06-18 10:14:57 +010078 void configure_common(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output);
giuros0192fd9432018-12-03 17:30:00 +000079
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000080 /** Function to get the micro kernel implementation
81 *
82 * @param[in] input1 First input tensor information
83 * @param[in] input2 Second input tensor information
84 * @param[in] output Output tensor information
85 *
86 * @return the function instance for the micro kernel
87 */
88 virtual std::function<ElementwiseFunction> get_implementation(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) = 0;
giuros0192fd9432018-12-03 17:30:00 +000089};
90
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000091class CpuArithmeticKernel : public CpuElementwiseKernel
giuros0192fd9432018-12-03 17:30:00 +000092{
93public:
George Worta1e7e282019-01-15 11:00:29 +000094 /** Default constructor */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000095 CpuArithmeticKernel() = default;
giuros0192fd9432018-12-03 17:30:00 +000096
Georgios Pinitas18134222020-09-03 21:00:23 +010097 /** Configure kernel
giuros0192fd9432018-12-03 17:30:00 +000098 *
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010099 * @param[in] op Arithmetic operation to be executed.
100 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/S16/F16/S32/F32.
101 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
102 * @param[out] output Output tensor info. Data types supported: Same as @p input1.
giuros0192fd9432018-12-03 17:30:00 +0000103 */
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100104 void configure(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output);
giuros0192fd9432018-12-03 17:30:00 +0000105
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000106 /** Static function to check if given info will lead to a valid configuration of @ref cpu::kernels::CpuArithmeticKernel
giuros0192fd9432018-12-03 17:30:00 +0000107 *
108 * @param[in] op Arithmetic operation to be executed.
109 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/S16/F16/S32/F32.
giuros0192fd9432018-12-03 17:30:00 +0000110 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
111 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
112 *
113 * @return a Status
114 */
115 static Status validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output);
116
117protected:
118 // Inherited methods overridden:
George Wortd88590f2018-12-12 17:39:58 +0000119 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output);
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000120
121 ArithmeticOperation _op{};
122
123private:
124 /** Function to get the micro kernel implementation
125 *
126 * @param[in] input1 First input tensor information
127 * @param[in] input2 Second input tensor information
128 * @param[in] output Output tensor information
129 *
130 * @return the function instance for the micro kernel
131 */
132 std::function<ElementwiseFunction> get_implementation(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) override;
George Wortd88590f2018-12-12 17:39:58 +0000133};
134
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000135class CpuDivisionKernel : public CpuArithmeticKernel
George Worta1e7e282019-01-15 11:00:29 +0000136{
137public:
138 /** Default constructor */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000139 CpuDivisionKernel() = default;
George Worta1e7e282019-01-15 11:00:29 +0000140
Georgios Pinitas18134222020-09-03 21:00:23 +0100141 /** Configure kernel
George Worta1e7e282019-01-15 11:00:29 +0000142 *
Georgios Pinitas18134222020-09-03 21:00:23 +0100143 * @param[in] input1 First tensor input info. Data types supported: S32/F16/F32.
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100144 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
145 * @param[out] output Output tensor info. Data types supported: Same as @p input1.
George Worta1e7e282019-01-15 11:00:29 +0000146 */
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100147 void configure(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output);
George Worta1e7e282019-01-15 11:00:29 +0000148
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000149 /** Static function to check if given info will lead to a valid configuration of @ref CpuDivisionKernel
George Worta1e7e282019-01-15 11:00:29 +0000150 *
Georgios Pinitas18134222020-09-03 21:00:23 +0100151 * @param[in] input1 First tensor input info. Data types supported: S32/F16/F32.
George Worta1e7e282019-01-15 11:00:29 +0000152 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
153 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
154 *
155 * @return a Status
156 */
157 static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output);
158
159protected:
160 // Inherited methods overridden:
161 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output);
162};
163
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000164class CpuPowerKernel : public CpuArithmeticKernel
Usama Arif81e671e2019-05-13 13:33:14 +0100165{
166public:
167 /** Default constructor */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000168 CpuPowerKernel() = default;
Usama Arif81e671e2019-05-13 13:33:14 +0100169
Georgios Pinitas18134222020-09-03 21:00:23 +0100170 /** Configure kernel
Usama Arif81e671e2019-05-13 13:33:14 +0100171 *
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100172 * @param[in] input1 First tensor input info. Data types supported: F16/F32.
173 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
174 * @param[out] output Output tensor info. Data types supported: Same as @p input1.
Usama Arif81e671e2019-05-13 13:33:14 +0100175 */
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100176 void configure(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output);
Usama Arif81e671e2019-05-13 13:33:14 +0100177
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000178 /** Static function to check if given info will lead to a valid configuration of @ref CpuPowerKernel
Usama Arif81e671e2019-05-13 13:33:14 +0100179 *
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100180 * @param[in] input1 First tensor input info. Data types supported: F16/F32.
181 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
182 * @param[in] output Output tensor info. Data types supported: Same as @p input1.
Usama Arif81e671e2019-05-13 13:33:14 +0100183 *
184 * @return a Status
185 */
186 static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output);
187
188protected:
189 // Inherited methods overridden:
190 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output);
191};
192
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000193class CpuComparisonKernel : public CpuElementwiseKernel
George Wortd88590f2018-12-12 17:39:58 +0000194{
195public:
George Worta1e7e282019-01-15 11:00:29 +0000196 /** Default constructor */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000197 CpuComparisonKernel() = default;
George Wortd88590f2018-12-12 17:39:58 +0000198
Georgios Pinitas18134222020-09-03 21:00:23 +0100199 /** Configure kernel
George Wortd88590f2018-12-12 17:39:58 +0000200 *
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100201 * @param[in] op Comparison operation to be executed.
202 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32.
203 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
204 * @param[out] output Output tensor info. Data types supported: U8.
George Wortd88590f2018-12-12 17:39:58 +0000205 */
Michalis Spyrouce0c6752020-06-18 10:14:57 +0100206 void configure(ComparisonOperation op, const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output);
George Wortd88590f2018-12-12 17:39:58 +0000207
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000208 /** Static function to check if given info will lead to a valid configuration of @ref cpu::kernels::CpuComparisonKernel
George Wortd88590f2018-12-12 17:39:58 +0000209 *
210 * @param[in] op Comparison operation to be executed.
morgolock74a16962020-01-15 11:40:49 +0000211 * @param[in] input1 First tensor input info. Data types supported: QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32.
George Wortd88590f2018-12-12 17:39:58 +0000212 * @param[in] input2 Second tensor input info. Data types supported: Same as @p input1.
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100213 * @param[in] output Output tensor info. Data types supported: U8.
George Wortd88590f2018-12-12 17:39:58 +0000214 *
215 * @return a Status
216 */
217 static Status validate(ComparisonOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output);
218
219protected:
220 // Inherited methods overridden:
221 static Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output);
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000222
223private:
224 /** Function to get the micro kernel implementation
225 *
226 * @param[in] input1 First input tensor information
227 * @param[in] input2 Second input tensor information
228 * @param[in] output Output tensor information
229 *
230 * @return the function instance for the micro kernel
231 */
232 std::function<ElementwiseFunction> get_implementation(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) override;
233
234 ComparisonOperation _op{};
giuros0192fd9432018-12-03 17:30:00 +0000235};
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000236} // namespace kernels
237} // namespace cpu
giuros0192fd9432018-12-03 17:30:00 +0000238} // namespace arm_compute
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000239#endif /* ARM_COMPUTE_CPU_ELEMENTWISE_KERNEL_H */