blob: 9c4ab2b0684989677884b49de28b1de6dd4c3bf7 [file] [log] [blame]
Michalis Spyrou25f45a42018-08-08 12:53:05 +01001/*
2 * Copyright (c) 2018 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_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"
34#include "arm_compute/runtime/NEON/INESimpleFunction.h"
35#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h"
36#include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h"
37#include "arm_compute/runtime/NEON/functions/NEGEMM.h"
38#include "arm_compute/runtime/NEON/functions/NEWidthConcatenateLayer.h"
39#include "arm_compute/runtime/common/LSTMParams.h"
40
41namespace arm_compute
42{
43// Forward declarations
44class ITensor;
45
46/** Basic function to run @ref NELSTMLayer */
47class NELSTMLayer : public IFunction
48{
49public:
50 /** Default constructor */
51 NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
52 /** Initialize function's tensors.
53 *
54 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
55 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
56 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
57 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
58 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
59 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
60 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
61 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
62 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
63 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
64 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
65 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
66 * @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.
67 * @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
68 * @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
69 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
70 * Data types supported: Same as @p input.
71 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
72 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
73 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
74 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
75 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
76 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
77 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
78 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
79 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
80 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
81 * @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.
82 * @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.
83 */
84 void configure(const ITensor *input,
85 const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights,
86 const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights,
87 const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias,
88 const ITensor *output_state_in, const ITensor *cell_state_in,
89 ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output,
90 const LSTMParams<ITensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
91
92 /** Static function to check if given info will lead to a valid configuration of @ref NELSTMLayer
93 *
94 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
95 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
96 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
97 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
98 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
99 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
100 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
101 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
102 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
103 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
104 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
105 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
106 * @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.
107 * @param[in] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
108 * @param[in] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
109 * @param[in] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
110 * Data types supported: Same as @p input.
111 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
112 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
113 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
114 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
115 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
116 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
117 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
118 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
119 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
120 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
121 * @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.
122 * @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.
123 *
124 * @return a status
125 */
126 static Status validate(const ITensorInfo *input,
127 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
128 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
129 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
130 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
131 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
132 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
133
134 // Inherited methods overridden:
135 void run() override;
136
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;
180 Tensor _input_gate_out1;
181 Tensor _input_gate_out2;
182 Tensor _input_gate_out3;
183 Tensor _input_gate_out4;
184 Tensor _input_gate_out5;
185 Tensor _forget_gate_out1;
186 Tensor _forget_gate_out2;
187 Tensor _forget_gate_out3;
188 Tensor _forget_gate_out4;
189 Tensor _forget_gate_out5;
190 Tensor _cell_state_out1;
191 Tensor _cell_state_out2;
192 Tensor _cell_state_out3;
193 Tensor _cell_state_out4;
194 Tensor _cell_state_out5;
195 Tensor _output1;
196 Tensor _output2;
197 Tensor _output3;
198 Tensor _output4;
199 Tensor _output5;
200 Tensor _cell_state_activation;
201 Tensor _output_state1;
202 Tensor _ones;
203 bool _run_peephole_opt;
204 bool _run_cifg_opt;
205 bool _perform_cell_clipping;
206 bool _has_projection_weights;
207 bool _perform_projection_clipping;
208};
209} // namespace arm_compute
210#endif /* __ARM_COMPUTE_NELSTMLAYER_H__ */