blob: f3a1aa7c75fdba3e5094e277091d0342ca31bb1c [file] [log] [blame]
Michalis Spyrou25f45a42018-08-08 12:53:05 +01001/*
John Kesapides917959c2019-02-04 12:37:29 +00002 * Copyright (c) 2018-2019 ARM Limited.
Michalis Spyrou25f45a42018-08-08 12:53:05 +01003 *
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_NELSTMLAYER_H__
25#define __ARM_COMPUTE_NELSTMLAYER_H__
26
27#include "arm_compute/core/NEON/kernels/NEActivationLayerKernel.h"
28#include "arm_compute/core/NEON/kernels/NEArithmeticAdditionKernel.h"
29#include "arm_compute/core/NEON/kernels/NEArithmeticSubtractionKernel.h"
30#include "arm_compute/core/NEON/kernels/NECopyKernel.h"
31#include "arm_compute/core/NEON/kernels/NEPixelWiseMultiplicationKernel.h"
32
33#include "arm_compute/core/Types.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010034#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h"
35#include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h"
36#include "arm_compute/runtime/NEON/functions/NEGEMM.h"
37#include "arm_compute/runtime/NEON/functions/NEWidthConcatenateLayer.h"
38#include "arm_compute/runtime/common/LSTMParams.h"
39
40namespace arm_compute
41{
42// Forward declarations
43class ITensor;
44
45/** Basic function to run @ref NELSTMLayer */
46class NELSTMLayer : public IFunction
47{
48public:
49 /** Default constructor */
50 NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
51 /** Initialize function's tensors.
52 *
53 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
54 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
55 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
56 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
57 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
58 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
59 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
60 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
61 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
62 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
63 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
64 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
65 * @param[out] scratch_buffer 2D tensor with dimensions [num_units * 4, batch_size] with CIFG or [num_units * 3, batch_size] without CIGF. Data type supported: Same as @p input.
66 * @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
67 * @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
68 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
69 * Data types supported: Same as @p input.
70 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
71 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
72 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
73 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
74 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
75 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
76 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
77 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
78 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
79 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
80 * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0 then clipping is disabled.
81 * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
82 */
83 void configure(const ITensor *input,
84 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
85 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
86 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
87 const ITensor *output_state_in, const ITensor *cell_state_in,
88 ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output,
89 const LSTMParams<ITensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
90
91 /** Static function to check if given info will lead to a valid configuration of @ref NELSTMLayer
92 *
93 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
94 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
95 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
96 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
97 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
98 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
99 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
100 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
101 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
102 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
103 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
104 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
105 * @param[in] scratch_buffer 2D tensor with dimensions [num_units * 4, batch_size] with CIFG or [num_units * 3, batch_size] without CIGF. Data type supported: Same as @p input.
106 * @param[in] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
107 * @param[in] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
108 * @param[in] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
109 * Data types supported: Same as @p input.
110 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
111 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
112 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
113 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
114 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
115 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
116 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
117 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
118 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
119 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
120 * @param[in] cell_threshold The clipping threshold for the cell state, such that values are bound within [-cell_clip, cell_clip]. If set to 0.0 then clipping is disabled.
121 * @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
122 *
123 * @return a status
124 */
125 static Status validate(const ITensorInfo *input,
126 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
127 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
128 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
129 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
130 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
131 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
132
133 // Inherited methods overridden:
134 void run() override;
John Kesapides917959c2019-02-04 12:37:29 +0000135 void prepare() override;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100136
137private:
138 MemoryGroup _memory_group;
139 NEFullyConnectedLayer _fully_connected_input_gate;
140 NEGEMM _gemm_input_gate;
141 NETransposeKernel _transpose_input_gate;
142 NEArithmeticAdditionKernel _accum_input_gate1;
143 NEArithmeticAddition _accum_input_gate2;
144 NEArithmeticSubtractionKernel _subtract_input_gate;
145 NEPixelWiseMultiplicationKernel _pixelwise_mul_input_gate;
146 NEActivationLayerKernel _activation_input_gate;
147 NEFullyConnectedLayer _fully_connected_forget_gate;
148 NEGEMM _gemm_forget_gate;
149 NETransposeKernel _transpose_forget_gate;
150 NEArithmeticAdditionKernel _accum_forget_gate1;
151 NEArithmeticAddition _accum_forget_gate2;
152 NEPixelWiseMultiplicationKernel _pixelwise_mul_forget_gate;
153 NEActivationLayerKernel _activation_forget_gate;
154 NEFullyConnectedLayer _fully_connected_cell_state;
155 NEGEMM _gemm_cell_state1;
156 NEGEMM _gemm_cell_state2;
157 NETransposeKernel _transpose_cell_state;
158 NEArithmeticAdditionKernel _accum_cell_state1;
159 NEArithmeticAdditionKernel _accum_cell_state2;
160 NEPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1;
161 NEActivationLayerKernel _activation_cell_state;
162 NEActivationLayerKernel _cell_clip;
163 NEPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2;
164 NEFullyConnectedLayer _fully_connected_output;
165 NEGEMM _gemm_output;
166 NEPixelWiseMultiplicationKernel _pixelwise_mul_output_state1;
167 NETransposeKernel _transpose_output;
168 NEArithmeticAdditionKernel _accum_output1;
169 NEArithmeticAddition _accum_output2;
170 NEActivationLayerKernel _activation_output;
171 NEActivationLayerKernel _activation_output_state;
172 NEPixelWiseMultiplicationKernel _pixelwise_mul_output_state2;
173 NEFullyConnectedLayer _fully_connected_output_state;
174 NEGEMM _gemm_output_state;
175 NEArithmeticAdditionKernel _accum_output_state;
176 NEActivationLayerKernel _projection_clip;
177 NECopyKernel _copy_cell_state;
178 NECopyKernel _copy_output;
179 NEWidthConcatenateLayer _concat_scratch_buffer;
John Kesapides917959c2019-02-04 12:37:29 +0000180 NEWidthConcatenateLayer _concat_inputs_forget_gate;
181 NEWidthConcatenateLayer _concat_weights_forget_gate;
182 NEWidthConcatenateLayer _concat_weights_input_gate;
183 NEWidthConcatenateLayer _concat_weights_output;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100184 Tensor _input_gate_out1;
185 Tensor _input_gate_out2;
186 Tensor _input_gate_out3;
187 Tensor _input_gate_out4;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100188 Tensor _forget_gate_out1;
189 Tensor _forget_gate_out2;
190 Tensor _forget_gate_out3;
191 Tensor _forget_gate_out4;
192 Tensor _forget_gate_out5;
John Kesapides917959c2019-02-04 12:37:29 +0000193 Tensor _forget_gate_out6;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100194 Tensor _cell_state_out1;
195 Tensor _cell_state_out2;
196 Tensor _cell_state_out3;
197 Tensor _cell_state_out4;
198 Tensor _cell_state_out5;
199 Tensor _output1;
200 Tensor _output2;
201 Tensor _output3;
202 Tensor _output4;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100203 Tensor _cell_state_activation;
204 Tensor _output_state1;
205 Tensor _ones;
206 bool _run_peephole_opt;
207 bool _run_cifg_opt;
208 bool _perform_cell_clipping;
209 bool _has_projection_weights;
210 bool _perform_projection_clipping;
John Kesapides917959c2019-02-04 12:37:29 +0000211 bool _is_prepared;
Michalis Spyrou25f45a42018-08-08 12:53:05 +0100212};
213} // namespace arm_compute
214#endif /* __ARM_COMPUTE_NELSTMLAYER_H__ */