blob: 9e4a37110b5f7e76c75ce4aa6158c0b3036f5410 [file] [log] [blame]
Sheri Zhang1e3ab422021-03-16 17:35:08 +00001/*
Matthew Bentham043613f2023-05-30 16:43:14 +00002 * Copyright (c) 2016-2023 Arm Limited.
Sheri Zhang1e3ab422021-03-16 17:35:08 +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
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 */
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +010024#ifndef ARM_COMPUTE_CPU_MUL_KERNEL_H
25#define ARM_COMPUTE_CPU_MUL_KERNEL_H
Sheri Zhang1e3ab422021-03-16 17:35:08 +000026
Matthew Bentham043613f2023-05-30 16:43:14 +000027#include "arm_compute/core/Rounding.h"
Sheri Zhang1e3ab422021-03-16 17:35:08 +000028#include "src/core/common/Macros.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010029#include "src/cpu/ICpuKernel.h"
Sheri Zhang1e3ab422021-03-16 17:35:08 +000030
31namespace arm_compute
32{
33namespace cpu
34{
35namespace kernels
36{
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +010037/** Interface for the kernel to perform multiplication between two tensors */
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +020038class CpuMulKernel : public ICpuKernel<CpuMulKernel>
Sheri Zhang1e3ab422021-03-16 17:35:08 +000039{
40public:
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +010041 CpuMulKernel() = default;
42 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuMulKernel);
Sheri Zhang1e3ab422021-03-16 17:35:08 +000043 /** Initialise the kernel's input, dst and border mode.
44 *
45 * Valid configurations (Src1,Src2) -> Dst :
46 *
47 * Support: Broadcast? Scale=1/255?
48 * - (U8,U8) -> U8, S16 N Y
49 * - (U8,S16) -> S16 N Y
50 * - (S16,U8) -> S16 N Y
51 * - (S16,S16) -> S16 N Y
52 * - (S32,S32) -> S32 Y N
53 * - (F16,F16) -> F16 N Y
54 * - (F32,F32) -> F32 Y Y
55 * - (QASYMM8,QASYMM8) -> QASYMM8 Y Y
56 * - (QASYMM8_SIGNED,QASYMM8_SIGNED) -> QASYMM8_SIGNED Y Y
57 * - (QSYMM16,QSYMM16) -> QSYMM16, S32 N Y
58 *
59 * @note For @p scale equal to 1/255 only round to nearest even (implemented as round half up) is supported.
60 * For all other scale values only round to zero (implemented as round towards minus infinity) is supported.
61 *
62 * @param[in] src1 First input tensor. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/S32/QSYMM16/F16/F32
63 * @param[in] src2 Second input tensor. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/S32/QSYMM16/F16/F32
64 * @param[out] dst Dst tensor. Data types supported: U8/QASYMM8/QASYMM8_SIGNED/S16/S32/QSYMM16/F16/F32
65 * @param[in] scale Scale to apply after multiplication.
66 * Scale must be positive and its value must be either 1/255 or 1/2^n where n is between 0 and 15.
67 * If both @p src1, @p src2 and @p dst are of datatype S32, scale cannot be 1/255
68 * @param[in] overflow_policy Overflow policy. ConvertPolicy cannot be WRAP if any of the inputs is of quantized datatype
69 * @param[in] rounding_policy Rounding policy.
70 */
71 void configure(ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy);
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +010072 /** Static function to check if given info will lead to a valid configuration
Sheri Zhang1e3ab422021-03-16 17:35:08 +000073 *
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +010074 * Similar to @ref CpuMulKernel::configure()
Sheri Zhang1e3ab422021-03-16 17:35:08 +000075 *
76 * @return a status
77 */
78 static Status validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy);
79
80 // Inherited methods overridden
81 void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
82 const char *name() const override;
83
Fadi Arafeh73bb6b72022-10-06 16:20:14 +000084 /** Return minimum workload size of the relevant kernel
85 *
86 * @param[in] platform The CPU platform used to create the context.
87 * @param[in] thread_count Number of threads in the execution.
88 *
89 * @return[out] mws Minimum workload size for requested configuration.
90 */
91 size_t get_mws(const CPUInfo &platform, size_t thread_count) const override;
92
Viet-Hoa Do0d05b662022-09-09 15:39:05 +010093 /** Get the preferred dimension in which the scheduler splits the work into multiple jobs.
94 *
95 * @return The split dimension hint.
96 */
97 size_t get_split_dimension_hint() const
98 {
99 return _split_dimension;
100 }
101
Sheri Zhang1e3ab422021-03-16 17:35:08 +0000102private:
103 /** Common signature for all the specialised multiplication functions with integer scaling factor
104 *
105 * @param[in] src1 Src1 tensor object.
106 * @param[in] src2 Src2 tensor object.
107 * @param[out] dst Dst tensor object.
108 * @param[in] window Region on which to execute the kernel
109 * @param[in] scale Integer scale factor.
110 */
111 using MulFunctionInt = void(const ITensor *src1, const ITensor *src2, ITensor *dst, const Window &window, int scale);
112 /** Common signature for all the specialised multiplication functions with float scaling factor
113 *
114 * @param[in] src1 Src1 tensor object.
115 * @param[in] src2 Src2 tensor object.
116 * @param[out] dst Dst tensor object.
117 * @param[in] window Region on which to execute the kernel
118 * @param[in] scale Float scale factor.
119 */
120 using MulFunctionFloat = void(const ITensor *src1, const ITensor *src2, ITensor *dst, const Window &window, float scale);
121 /** Common signature for all the specialised QASYMM8 multiplication functions with float scaling factor
122 *
123 * @param[in] src1 Src1 tensor object.
124 * @param[in] src2 Src2 tensor object.
125 * @param[out] dst Dst tensor object.
126 * @param[in] window Region on which to execute the kernel
127 * @param[in] scale Float scale factor.
128 *
129 */
130 using MulFunctionQuantized = void(const ITensor *src1, const ITensor *src2, ITensor *dst, const Window &window, float scale);
131
132 MulFunctionFloat *_func_float{ nullptr };
133 MulFunctionInt *_func_int{ nullptr };
134 MulFunctionQuantized *_func_quantized{ nullptr };
135 float _scale{ 0 };
136 int _scale_exponent{ 0 };
Viet-Hoa Do0d05b662022-09-09 15:39:05 +0100137 size_t _split_dimension{ Window::DimY };
Sheri Zhang1e3ab422021-03-16 17:35:08 +0000138};
139
140/** Interface for the complex pixelwise multiplication kernel. */
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +0200141class CpuComplexMulKernel : public ICpuKernel<CpuComplexMulKernel>
Sheri Zhang1e3ab422021-03-16 17:35:08 +0000142{
143public:
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100144 CpuComplexMulKernel() = default;
145 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuComplexMulKernel);
Sheri Zhang1e3ab422021-03-16 17:35:08 +0000146 /** Initialise the kernel's src, dst and border mode.
147 *
148 * @param[in] src1 An src tensor. Data types supported: F32. Number of channels supported: 2 (complex tensor).
149 * @param[in] src2 An src tensor. Data types supported: same as @p src1. Number of channels supported: same as @p src1.
150 * @param[out] dst The dst tensor, Data types supported: same as @p src1. Number of channels supported: same as @p src1.
151 */
152 void configure(ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst);
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100153 /** Static function to check if given info will lead to a valid configuration
Sheri Zhang1e3ab422021-03-16 17:35:08 +0000154 *
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100155 * Similar to @ref CpuComplexMulKernel::configure()
Sheri Zhang1e3ab422021-03-16 17:35:08 +0000156 *
157 * @return a status
158 */
159 static Status validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst);
160
161 // Inherited methods overridden:
162 void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
163 const char *name() const override;
164};
165} // namespace kernels
166} // namespace cpu
167} // namespace arm_compute
Georgios Pinitas0dc0d8e2021-04-30 03:18:37 +0100168#endif /* ARM_COMPUTE_CPU_MUL_KERNEL_H */