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