blob: 634e38bf9f7b72c7cc3fa632966b1d69fc959e8e [file] [log] [blame]
giuros0192fd9432018-12-03 17:30:00 +00001/*
Giorgio Arena5ae8d802021-11-18 18:02:13 +00002 * Copyright (c) 2021-2022 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"
Georgios Pinitas7891a732021-08-20 21:39:25 +010028#include "src/cpu/ICpuKernel.h"
giuros0192fd9432018-12-03 17:30:00 +000029
30namespace arm_compute
31{
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000032namespace cpu
33{
34namespace kernels
35{
giuros0192fd9432018-12-03 17:30:00 +000036/** Interface for an element-wise operation kernel
37 *
38 * Element-wise operation is computed by:
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000039 * @f[ dst(x,y) = OP(src0(x,y), src1(x,y))@f]
giuros0192fd9432018-12-03 17:30:00 +000040 *
41 */
Dana Zlotnik6a2df882022-01-17 09:54:26 +020042template <class Derived>
43class CpuElementwiseKernel : public ICpuKernel<Derived>
giuros0192fd9432018-12-03 17:30:00 +000044{
Dana Zlotnik6a2df882022-01-17 09:54:26 +020045private:
46 using ElementwiseKernelPtr = std::add_pointer<void(const ITensor *, const ITensor *, ITensor *, const Window &)>::type;
47
giuros0192fd9432018-12-03 17:30:00 +000048public:
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000049 CpuElementwiseKernel() = default;
50 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuElementwiseKernel);
giuros0192fd9432018-12-03 17:30:00 +000051
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000052 using ElementwiseFunction = void(const ITensor *, const ITensor *, ITensor *, const Window &);
Michalis Spyrouce0c6752020-06-18 10:14:57 +010053 // Inherited methods overridden:
Georgios Pinitas0499dff2020-07-31 22:21:38 +010054 void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
Dana Zlotnik6a2df882022-01-17 09:54:26 +020055
Georgios Pinitas5fdde992021-06-25 05:42:57 +010056 const char *name() const override;
Michalis Spyrouce0c6752020-06-18 10:14:57 +010057
Dana Zlotnik6a2df882022-01-17 09:54:26 +020058 struct ElementwiseKernel
59 {
60 const char *name;
61 const ElementwiseDataTypeISASelectorPtr is_selected;
62 ElementwiseKernelPtr ukernel;
63 };
64
giuros0192fd9432018-12-03 17:30:00 +000065protected:
66 /** Validate the argument passed to the kernel
67 *
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000068 * @param[in] src0 First tensor input. Data types supported: QASYMM8/S16/F16/S32/F32.
69 * @param[in] src1 Second tensor input. Data types supported: Same as @p src0.
70 * @param[in] dst Output tensor. Data types supported: Dependent on subclass.
giuros0192fd9432018-12-03 17:30:00 +000071 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000072 static Status validate_arguments_common(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst);
giuros0192fd9432018-12-03 17:30:00 +000073
Georgios Pinitas5fdde992021-06-25 05:42:57 +010074protected:
Fadi Arafeh73bb6b72022-10-06 16:20:14 +000075 ElementwiseKernelPtr _run_method{ nullptr };
76 std::string _name{};
giuros0192fd9432018-12-03 17:30:00 +000077};
78
Dana Zlotnik6a2df882022-01-17 09:54:26 +020079class CpuArithmeticKernel : public CpuElementwiseKernel<CpuArithmeticKernel>
giuros0192fd9432018-12-03 17:30:00 +000080{
81public:
Sang-Hoon Park63001ac2021-01-18 14:20:27 +000082 CpuArithmeticKernel() = default;
giuros0192fd9432018-12-03 17:30:00 +000083
Georgios Pinitas18134222020-09-03 21:00:23 +010084 /** Configure kernel
giuros0192fd9432018-12-03 17:30:00 +000085 *
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000086 * @param[in] op Arithmetic operation to be executed.
87 * @param[in] src0 First tensor input info. Data types supported: QASYMM8/S16/F16/S32/F32.
88 * @param[in] src1 Second tensor input info. Data types supported: Same as @p src0.
89 * @param[out] dst Output tensor info. Data types supported: Same as @p src0.
giuros0192fd9432018-12-03 17:30:00 +000090 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000091 void configure(ArithmeticOperation op, const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
giuros0192fd9432018-12-03 17:30:00 +000092
Georgios Pinitas5fdde992021-06-25 05:42:57 +010093 /** Static function to check if given info will lead to a valid configuration
giuros0192fd9432018-12-03 17:30:00 +000094 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +010095 * Similar to CpuArithmeticKernel::configure()
giuros0192fd9432018-12-03 17:30:00 +000096 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +010097 * @return a status
giuros0192fd9432018-12-03 17:30:00 +000098 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +000099 static Status validate(ArithmeticOperation op, const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
giuros0192fd9432018-12-03 17:30:00 +0000100
Dana Zlotnik6a2df882022-01-17 09:54:26 +0200101 static const std::vector<CpuElementwiseKernel<CpuArithmeticKernel>::ElementwiseKernel> &get_available_kernels();
102
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000103 /** Return minimum workload size of the relevant kernel
104 *
105 * @param[in] platform The CPU platform used to create the context.
106 * @param[in] thread_count Number of threads in the execution.
107 *
108 * @return[out] mws Minimum workload size for requested configuration.
109 */
110 size_t get_mws(const CPUInfo &platform, size_t thread_count) const override;
111
giuros0192fd9432018-12-03 17:30:00 +0000112protected:
Dana Zlotnik6a2df882022-01-17 09:54:26 +0200113 /** Commmon configure function for element-wise operators with no additional options (e.g. Min, Max, SquaredDiff)
114 */
115 void configure_common(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
giuros0192fd9432018-12-03 17:30:00 +0000116 // Inherited methods overridden:
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000117 static Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst);
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000118
119 ArithmeticOperation _op{};
George Wortd88590f2018-12-12 17:39:58 +0000120};
121
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000122class CpuDivisionKernel : public CpuArithmeticKernel
George Worta1e7e282019-01-15 11:00:29 +0000123{
124public:
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000125 CpuDivisionKernel() = default;
George Worta1e7e282019-01-15 11:00:29 +0000126
Georgios Pinitas18134222020-09-03 21:00:23 +0100127 /** Configure kernel
George Worta1e7e282019-01-15 11:00:29 +0000128 *
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000129 * @param[in] src0 First tensor input info. Data types supported: S32/F16/F32.
130 * @param[in] src1 Second tensor input info. Data types supported: Same as @p src0.
131 * @param[out] dst Output tensor info. Data types supported: Same as @p src0.
George Worta1e7e282019-01-15 11:00:29 +0000132 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000133 void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
George Worta1e7e282019-01-15 11:00:29 +0000134
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100135 /** Static function to check if given info will lead to a valid configuration
George Worta1e7e282019-01-15 11:00:29 +0000136 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100137 * Similar to CpuDivisionKernel::configure()
George Worta1e7e282019-01-15 11:00:29 +0000138 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100139 * @return a status
George Worta1e7e282019-01-15 11:00:29 +0000140 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000141 static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
George Worta1e7e282019-01-15 11:00:29 +0000142
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000143 /** Return minimum workload size of the relevant kernel
144 *
145 * @param[in] platform The CPU platform used to create the context.
146 * @param[in] thread_count Number of threads in the execution.
147 *
148 * @return[out] mws Minimum workload size for requested configuration.
149 */
150 size_t get_mws(const CPUInfo &platform, size_t thread_count) const override;
151
George Worta1e7e282019-01-15 11:00:29 +0000152protected:
153 // Inherited methods overridden:
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000154 static Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst);
George Worta1e7e282019-01-15 11:00:29 +0000155};
156
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000157class CpuPowerKernel : public CpuArithmeticKernel
Usama Arif81e671e2019-05-13 13:33:14 +0100158{
159public:
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000160 CpuPowerKernel() = default;
Usama Arif81e671e2019-05-13 13:33:14 +0100161
Georgios Pinitas18134222020-09-03 21:00:23 +0100162 /** Configure kernel
Usama Arif81e671e2019-05-13 13:33:14 +0100163 *
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000164 * @param[in] src0 First tensor input info. Data types supported: F16/F32.
165 * @param[in] src1 Second tensor input info. Data types supported: Same as @p src0.
166 * @param[out] dst Output tensor info. Data types supported: Same as @p src0.
Usama Arif81e671e2019-05-13 13:33:14 +0100167 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000168 void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
Usama Arif81e671e2019-05-13 13:33:14 +0100169
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100170 /** Static function to check if given info will lead to a valid configuration
Usama Arif81e671e2019-05-13 13:33:14 +0100171 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100172 * Similar to CpuPowerKernel::configure()
Usama Arif81e671e2019-05-13 13:33:14 +0100173 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100174 * @return a status
Usama Arif81e671e2019-05-13 13:33:14 +0100175 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000176 static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
Usama Arif81e671e2019-05-13 13:33:14 +0100177
178protected:
179 // Inherited methods overridden:
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000180 static Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst);
Usama Arif81e671e2019-05-13 13:33:14 +0100181};
182
Dana Zlotnik6a2df882022-01-17 09:54:26 +0200183class CpuComparisonKernel : public CpuElementwiseKernel<CpuComparisonKernel>
George Wortd88590f2018-12-12 17:39:58 +0000184{
185public:
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000186 CpuComparisonKernel() = default;
George Wortd88590f2018-12-12 17:39:58 +0000187
Georgios Pinitas18134222020-09-03 21:00:23 +0100188 /** Configure kernel
George Wortd88590f2018-12-12 17:39:58 +0000189 *
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000190 * @param[in] op Comparison operation to be executed.
191 * @param[in] src0 First tensor input info. Data types supported: QASYMM8/QASYMM8_SIGNED/S16/F16/S32/F32.
192 * @param[in] src1 Second tensor input info. Data types supported: Same as @p src0.
193 * @param[out] dst Output tensor info. Data types supported: U8.
George Wortd88590f2018-12-12 17:39:58 +0000194 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000195 void configure(ComparisonOperation op, const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
George Wortd88590f2018-12-12 17:39:58 +0000196
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100197 /** Static function to check if given info will lead to a valid configuration
George Wortd88590f2018-12-12 17:39:58 +0000198 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100199 * Similar to CpuComparisonKernel::configure()
George Wortd88590f2018-12-12 17:39:58 +0000200 *
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100201 * @return a status
George Wortd88590f2018-12-12 17:39:58 +0000202 */
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000203 static Status validate(ComparisonOperation op, const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst);
George Wortd88590f2018-12-12 17:39:58 +0000204
Dana Zlotnik6a2df882022-01-17 09:54:26 +0200205 static const std::vector<CpuElementwiseKernel<CpuComparisonKernel>::ElementwiseKernel> &get_available_kernels();
206
George Wortd88590f2018-12-12 17:39:58 +0000207protected:
Dana Zlotnik6a2df882022-01-17 09:54:26 +0200208 /** Commmon configure function for element-wise operators with no additional options (e.g. Min, Max, SquaredDiff)
209 */
210 void configure_common(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst);
George Wortd88590f2018-12-12 17:39:58 +0000211 // Inherited methods overridden:
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000212 static Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst);
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000213
214private:
215 /** Function to get the micro kernel implementation
216 *
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000217 * @param[in] src0 First input tensor information
218 * @param[in] src1 Second input tensor information
219 * @param[in] dst Output tensor information
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000220 *
221 * @return the function instance for the micro kernel
222 */
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000223
224 ComparisonOperation _op{};
giuros0192fd9432018-12-03 17:30:00 +0000225};
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000226} // namespace kernels
227} // namespace cpu
giuros0192fd9432018-12-03 17:30:00 +0000228} // namespace arm_compute
Sang-Hoon Park63001ac2021-01-18 14:20:27 +0000229#endif /* ARM_COMPUTE_CPU_ELEMENTWISE_KERNEL_H */