Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021 Arm Limited. |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 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_NEQLSTMLAYERNORMALIZATIONKERNEL_H |
| 25 | #define ARM_COMPUTE_NEQLSTMLAYERNORMALIZATIONKERNEL_H |
| 26 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 27 | #include "src/core/NEON/INEKernel.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 29 | #include <functional> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | class ITensor; |
| 34 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 35 | /** Kernel to perform layer normalization for QLSTM. */ |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 36 | class NEQLSTMLayerNormalizationKernel : public INEKernel |
| 37 | { |
| 38 | public: |
| 39 | const char *name() const override |
| 40 | { |
| 41 | return "NEQLSTMLayerNormalizationKernel"; |
| 42 | } |
| 43 | /** Default constructor */ |
| 44 | NEQLSTMLayerNormalizationKernel() = default; |
| 45 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 46 | NEQLSTMLayerNormalizationKernel(const NEQLSTMLayerNormalizationKernel &) = delete; |
| 47 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 48 | NEQLSTMLayerNormalizationKernel &operator=(const NEQLSTMLayerNormalizationKernel &) = delete; |
| 49 | /** Default Move Constructor. */ |
| 50 | NEQLSTMLayerNormalizationKernel(NEQLSTMLayerNormalizationKernel &&) = default; |
| 51 | /** Default move assignment operator */ |
| 52 | NEQLSTMLayerNormalizationKernel &operator=(NEQLSTMLayerNormalizationKernel &&) = default; |
| 53 | /** Default destructor */ |
| 54 | ~NEQLSTMLayerNormalizationKernel() = default; |
| 55 | |
| 56 | /** Set the input and output tensors. |
| 57 | * |
| 58 | * @param[in] input Source tensor. Data types supported: QSYMM16. |
| 59 | * @param[out] output Destination tensor. Data types supported: Same as @p input. |
| 60 | * @param[in] weight Weight tensor. Data types supported: Same as @p input. |
| 61 | * @param[in] bias Bias tensor. Data types supported: S32 |
| 62 | */ |
| 63 | void configure(const ITensor *input, ITensor *output, const ITensor *weight, const ITensor *bias); |
| 64 | /** Static function to check if given info will lead to a valid configuration of @ref NEQLSTMLayerNormalizationKernel |
| 65 | * |
| 66 | * @param[in] input Source tensor info. Data types supported: QSYMM16. |
| 67 | * @param[in] output Destination tensor info. Data types supported: Same as @p input. |
| 68 | * @param[in] weight Weight tensor info. Data types supported: Same as @p input. |
| 69 | * @param[in] bias Bias tensor info. Data types supported: S32 |
| 70 | * |
| 71 | * @return a status |
| 72 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | static Status |
| 74 | validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *weight, const ITensorInfo *bias); |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 75 | // Inherited methods overridden: |
| 76 | void run(const Window &window, const ThreadInfo &info) override; |
| 77 | |
| 78 | private: |
| 79 | // constants |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | static constexpr uint32_t max_input_dimension{2}; /**< The maximum input dimension supported */ |
| 81 | static constexpr uint32_t max_weight_dimension{1}; /**< The maximum weight dimension supported */ |
| 82 | static constexpr uint32_t max_bias_dimension{1}; /**< The maximum bias dimension supported */ |
| 83 | static constexpr uint32_t vector_size_byte{16}; /**< Computation vector size in byte */ |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 84 | |
| 85 | using ComputeFuncType = std::function<void(NEQLSTMLayerNormalizationKernel &)>; |
| 86 | |
| 87 | ComputeFuncType _fn{}; /**< Function pointer to computation function */ |
| 88 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | const ITensor *_input{nullptr}; /**< Input tensor */ |
| 90 | const ITensor *_weight{nullptr}; /**< Weight tensor */ |
| 91 | const ITensor *_bias{nullptr}; /**< Bias tensor */ |
| 92 | ITensor *_output{nullptr}; /**< Output tensor */ |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 93 | |
| 94 | int32_t _output_multiplier{}; /**< Multiplier for output values */ |
| 95 | int32_t _output_shift{}; /**< Shift value for output values */ |
| 96 | |
| 97 | int32_t _window_start_x{}; /**< The beginning of x-axis iteration */ |
| 98 | int32_t _window_end_x{}; /**< The end of x-axis iteration */ |
| 99 | int32_t _window_step_x{}; /**< The size of x-axis iteration's step */ |
| 100 | |
| 101 | Window _inout_window{}; /**< Window for input and output tensor */ |
| 102 | Window _weight_window{}; /**< Window for weight and bias tensor */ |
| 103 | |
| 104 | /** Function to configure initial windows for destination of computation |
| 105 | * |
| 106 | * @param[in] Target destination tensor to use for output window |
| 107 | * |
| 108 | * @return configured window |
| 109 | */ |
| 110 | Window configure_window(ITensor *target); |
| 111 | // Function to compute for data type QSYMM16 |
| 112 | void compute_qsymm16(); |
| 113 | /** Function to compute summation and summation of squared input of the given input pointer |
| 114 | * |
| 115 | * @param[in] Input_ptr pointer to input array |
| 116 | * |
| 117 | */ |
| 118 | std::pair<int64_t, int64_t> sum_qsymm16(const int16_t *input_ptr); |
| 119 | /** Function to normalize values using computed mean and standard deviation |
| 120 | * |
| 121 | * @param[in] input_ptr Pointer to input array |
| 122 | * @param[in] output_ptr Pointer to output array |
| 123 | * @param[in] weight_ptr Pointer to weight array |
| 124 | * @param[in] bias_ptr Pointer to bias array |
| 125 | * @param[in] mean Mean value |
| 126 | * @param[in] inv_std_mul Quantized multiplier for standard deviation |
| 127 | * @param[in] inv_std_shift Shift for standard deviation |
| 128 | * |
| 129 | */ |
| 130 | void normalize_qasymm16(const int16_t *input_ptr, |
| 131 | int16_t *output_ptr, |
| 132 | const int16_t *weight_ptr, |
| 133 | const int32_t *bias_ptr, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 134 | int32_t mean, |
| 135 | int32_t inv_std_mul, |
| 136 | int32_t inv_std_shift); |
Sang-Hoon Park | 9230e27 | 2020-04-18 00:46:34 +0100 | [diff] [blame] | 137 | /** Function to compute output quantization information */ |
| 138 | QuantizationInfo compute_output_qinfo(); |
Sang-Hoon Park | 0d008f7 | 2020-03-13 14:56:05 +0000 | [diff] [blame] | 139 | }; |
| 140 | } // namespace arm_compute |
| 141 | #endif /* ARM_COMPUTE_NEQLSTMLAYERNORMALIZATIONKERNEL_H */ |