blob: af5b6a031584e2e9f1e1d4ef9952422214d61914 [file] [log] [blame]
Sang-Hoon Park0d008f72020-03-13 14:56:05 +00001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2020-2021 Arm Limited.
Sang-Hoon Park0d008f72020-03-13 14:56:05 +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 */
24#ifndef ARM_COMPUTE_NEQLSTMLAYERNORMALIZATIONKERNEL_H
25#define ARM_COMPUTE_NEQLSTMLAYERNORMALIZATIONKERNEL_H
26
Michalis Spyrouebcebf12020-10-21 00:04:14 +010027#include "src/core/NEON/INEKernel.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000029#include <functional>
30
31namespace arm_compute
32{
33class ITensor;
34
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000035/** Kernel to perform layer normalization for QLSTM. */
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000036class NEQLSTMLayerNormalizationKernel : public INEKernel
37{
38public:
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 static Status
74 validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *weight, const ITensorInfo *bias);
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000075 // Inherited methods overridden:
76 void run(const Window &window, const ThreadInfo &info) override;
77
78private:
79 // constants
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 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 Park0d008f72020-03-13 14:56:05 +000084
85 using ComputeFuncType = std::function<void(NEQLSTMLayerNormalizationKernel &)>;
86
87 ComputeFuncType _fn{}; /**< Function pointer to computation function */
88
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 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 Park0d008f72020-03-13 14:56:05 +000093
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 int32_t mean,
135 int32_t inv_std_mul,
136 int32_t inv_std_shift);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100137 /** Function to compute output quantization information */
138 QuantizationInfo compute_output_qinfo();
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000139};
140} // namespace arm_compute
141#endif /* ARM_COMPUTE_NEQLSTMLAYERNORMALIZATIONKERNEL_H */