Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-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 | */ |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CPU_MUL_KERNEL_H |
| 25 | #define ARM_COMPUTE_CPU_MUL_KERNEL_H |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 26 | |
| 27 | #include "src/core/common/Macros.h" |
| 28 | #include "src/core/cpu/ICpuKernel.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace cpu |
| 33 | { |
| 34 | namespace kernels |
| 35 | { |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 36 | /** Interface for the kernel to perform multiplication between two tensors */ |
| 37 | class CpuMulKernel : public ICpuKernel |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | /** Default constructor */ |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 41 | CpuMulKernel() = default; |
| 42 | ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuMulKernel); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 43 | /** 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 Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 72 | /** Static function to check if given info will lead to a valid configuration |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 73 | * |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 74 | * Similar to @ref CpuMulKernel::configure() |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 75 | * |
| 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 | |
| 84 | private: |
| 85 | /** Common signature for all the specialised multiplication functions with integer scaling factor |
| 86 | * |
| 87 | * @param[in] src1 Src1 tensor object. |
| 88 | * @param[in] src2 Src2 tensor object. |
| 89 | * @param[out] dst Dst tensor object. |
| 90 | * @param[in] window Region on which to execute the kernel |
| 91 | * @param[in] scale Integer scale factor. |
| 92 | */ |
| 93 | using MulFunctionInt = void(const ITensor *src1, const ITensor *src2, ITensor *dst, const Window &window, int scale); |
| 94 | /** Common signature for all the specialised multiplication functions with float scaling factor |
| 95 | * |
| 96 | * @param[in] src1 Src1 tensor object. |
| 97 | * @param[in] src2 Src2 tensor object. |
| 98 | * @param[out] dst Dst tensor object. |
| 99 | * @param[in] window Region on which to execute the kernel |
| 100 | * @param[in] scale Float scale factor. |
| 101 | */ |
| 102 | using MulFunctionFloat = void(const ITensor *src1, const ITensor *src2, ITensor *dst, const Window &window, float scale); |
| 103 | /** Common signature for all the specialised QASYMM8 multiplication functions with float 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 Float scale factor. |
| 110 | * |
| 111 | */ |
| 112 | using MulFunctionQuantized = void(const ITensor *src1, const ITensor *src2, ITensor *dst, const Window &window, float scale); |
| 113 | |
| 114 | MulFunctionFloat *_func_float{ nullptr }; |
| 115 | MulFunctionInt *_func_int{ nullptr }; |
| 116 | MulFunctionQuantized *_func_quantized{ nullptr }; |
| 117 | float _scale{ 0 }; |
| 118 | int _scale_exponent{ 0 }; |
| 119 | }; |
| 120 | |
| 121 | /** Interface for the complex pixelwise multiplication kernel. */ |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 122 | class CpuComplexMulKernel : public ICpuKernel |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 123 | { |
| 124 | public: |
| 125 | /** Default constructor */ |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 126 | CpuComplexMulKernel() = default; |
| 127 | ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuComplexMulKernel); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 128 | /** Initialise the kernel's src, dst and border mode. |
| 129 | * |
| 130 | * @param[in] src1 An src tensor. Data types supported: F32. Number of channels supported: 2 (complex tensor). |
| 131 | * @param[in] src2 An src tensor. Data types supported: same as @p src1. Number of channels supported: same as @p src1. |
| 132 | * @param[out] dst The dst tensor, Data types supported: same as @p src1. Number of channels supported: same as @p src1. |
| 133 | */ |
| 134 | void configure(ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst); |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 135 | /** Static function to check if given info will lead to a valid configuration |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 136 | * |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 137 | * Similar to @ref CpuComplexMulKernel::configure() |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 138 | * |
| 139 | * @return a status |
| 140 | */ |
| 141 | static Status validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst); |
| 142 | |
| 143 | // Inherited methods overridden: |
| 144 | void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override; |
| 145 | const char *name() const override; |
| 146 | }; |
| 147 | } // namespace kernels |
| 148 | } // namespace cpu |
| 149 | } // namespace arm_compute |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 150 | #endif /* ARM_COMPUTE_CPU_MUL_KERNEL_H */ |