blob: 72e41a7aca53124ee9c23ad526b82b6024eddb6b [file] [log] [blame]
Michalis Spyroubcedf512018-03-22 14:55:08 +00001/*
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_CLLSTMLAYER_H__
25#define __ARM_COMPUTE_CLLSTMLAYER_H__
26
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/CL/kernels/CLActivationLayerKernel.h"
30#include "arm_compute/core/CL/kernels/CLArithmeticAdditionKernel.h"
31#include "arm_compute/core/CL/kernels/CLArithmeticSubtractionKernel.h"
32#include "arm_compute/core/CL/kernels/CLCopyKernel.h"
33#include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/runtime/CL/CLMemoryGroup.h"
36#include "arm_compute/runtime/CL/CLTensor.h"
37#include "arm_compute/runtime/CL/functions/CLArithmeticAddition.h"
38#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
39#include "arm_compute/runtime/CL/functions/CLGEMM.h"
40#include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h"
41#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyrou25f45a42018-08-08 12:53:05 +010042#include "arm_compute/runtime/common/LSTMParams.h"
Michalis Spyroubcedf512018-03-22 14:55:08 +000043
44#include <memory>
45
46namespace arm_compute
47{
48class ICLTensor;
49
Michalis Spyroubcedf512018-03-22 14:55:08 +000050/** This function performs a single time step in a Long Short-Term Memory (LSTM) layer.
51 *
52 */
53class CLLSTMLayer : public IFunction
54{
55public:
56 /** Default constructor */
57 CLLSTMLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
58 /** Initialize function's tensors.
59 *
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010060 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
61 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
62 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
63 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
64 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
65 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
66 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
67 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
68 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
69 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
70 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
71 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
72 * @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.
73 * @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
74 * @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
75 * @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
76 * Data types supported: Same as @p input.
77 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
78 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
79 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
80 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
81 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
82 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
83 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
84 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
85 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
86 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
87 * @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.
88 * @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.
Michalis Spyroubcedf512018-03-22 14:55:08 +000089 */
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010090 void configure(const ICLTensor *input,
91 const ICLTensor *input_to_forget_weights, const ICLTensor *input_to_cell_weights, const ICLTensor *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +000092 const ICLTensor *recurrent_to_forget_weights, const ICLTensor *recurrent_to_cell_weights, const ICLTensor *recurrent_to_output_weights,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +010093 const ICLTensor *forget_gate_bias, const ICLTensor *cell_bias, const ICLTensor *output_gate_bias,
94 const ICLTensor *output_state_in, const ICLTensor *cell_state_in,
95 ICLTensor *scratch_buffer, ICLTensor *output_state_out, ICLTensor *cell_state_out, ICLTensor *output,
Michalis Spyroubcedf512018-03-22 14:55:08 +000096 const LSTMParams<ICLTensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
97
98 /** Static function to check if given info will lead to a valid configuration of @ref CLLSTMLayer
99 *
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100100 * @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32.
101 * @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
102 * @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
103 * @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
104 * @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
105 * @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
106 * @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
107 * @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
108 * @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
109 * @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
110 * @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
111 * @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
112 * @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.
113 * @param[in] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input.
114 * @param[in] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input.
115 * @param[in] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size].
Michalis Spyroubcedf512018-03-22 14:55:08 +0000116 * Data types supported: Same as @p input.
117 * @param[in] lstm_params (Optional) Weights tensors used in peephole optimization:
118 * input_to_input_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input.
119 * recurrent_to_input_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
120 * cell_to_input_weights 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input.
121 * cell_to_forget_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
122 * cell_to_output_weights 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input.
123 * input_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input
124 * projection_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input.
125 * projection_bias 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input.
126 * @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo.
127 * @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.
128 * @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.
129 *
130 * @return a status
131 */
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100132 static Status validate(const ITensorInfo *input,
133 const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000134 const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights,
135 const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias,
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100136 const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in,
137 const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output,
Michalis Spyroubcedf512018-03-22 14:55:08 +0000138 const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold = 0.f, float projection_threshold = 0.f);
139
140 // Inherited methods overridden:
141 void run() override;
142
143private:
144 CLMemoryGroup _memory_group;
145 CLFullyConnectedLayer _fully_connected_input_gate;
Georgios Pinitas42a31722018-07-09 14:35:32 +0100146 CLGEMM _gemm_input_gate;
147 CLTransposeKernel _transpose_input_gate;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000148 CLArithmeticAdditionKernel _accum_input_gate1;
149 CLArithmeticAddition _accum_input_gate2;
150 CLArithmeticSubtractionKernel _subtract_input_gate;
Georgios Pinitas42a31722018-07-09 14:35:32 +0100151 CLPixelWiseMultiplicationKernel _pixelwise_mul_input_gate;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000152 CLActivationLayerKernel _activation_input_gate;
153 CLFullyConnectedLayer _fully_connected_forget_gate;
Georgios Pinitas42a31722018-07-09 14:35:32 +0100154 CLGEMM _gemm_forget_gate;
155 CLTransposeKernel _transpose_forget_gate;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000156 CLArithmeticAdditionKernel _accum_forget_gate1;
157 CLArithmeticAddition _accum_forget_gate2;
Georgios Pinitas42a31722018-07-09 14:35:32 +0100158 CLPixelWiseMultiplicationKernel _pixelwise_mul_forget_gate;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000159 CLActivationLayerKernel _activation_forget_gate;
160 CLFullyConnectedLayer _fully_connected_cell_state;
161 CLGEMM _gemm_cell_state1;
162 CLGEMM _gemm_cell_state2;
Georgios Pinitas42a31722018-07-09 14:35:32 +0100163 CLTransposeKernel _transpose_cell_state;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000164 CLArithmeticAdditionKernel _accum_cell_state1;
165 CLArithmeticAdditionKernel _accum_cell_state2;
166 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state1;
167 CLActivationLayerKernel _activation_cell_state;
168 CLActivationLayerKernel _cell_clip;
169 CLPixelWiseMultiplicationKernel _pixelwise_mul_cell_state2;
170 CLFullyConnectedLayer _fully_connected_output;
Georgios Pinitas42a31722018-07-09 14:35:32 +0100171 CLGEMM _gemm_output;
172 CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state1;
173 CLTransposeKernel _transpose_output;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000174 CLArithmeticAdditionKernel _accum_output1;
175 CLArithmeticAddition _accum_output2;
176 CLActivationLayerKernel _activation_output;
177 CLActivationLayerKernel _activation_output_state;
Georgios Pinitas42a31722018-07-09 14:35:32 +0100178 CLPixelWiseMultiplicationKernel _pixelwise_mul_output_state2;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000179 CLFullyConnectedLayer _fully_connected_output_state;
180 CLGEMM _gemm_output_state;
181 CLArithmeticAdditionKernel _accum_output_state;
182 CLActivationLayerKernel _projection_clip;
183 CLCopyKernel _copy_cell_state;
184 CLCopyKernel _copy_output;
185 CLWidthConcatenateLayer _concat_scratch_buffer;
186 CLTensor _input_gate_out1;
187 CLTensor _input_gate_out2;
188 CLTensor _input_gate_out3;
189 CLTensor _input_gate_out4;
190 CLTensor _input_gate_out5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000191 CLTensor _forget_gate_out1;
192 CLTensor _forget_gate_out2;
193 CLTensor _forget_gate_out3;
194 CLTensor _forget_gate_out4;
195 CLTensor _forget_gate_out5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000196 CLTensor _cell_state_out1;
197 CLTensor _cell_state_out2;
198 CLTensor _cell_state_out3;
199 CLTensor _cell_state_out4;
200 CLTensor _cell_state_out5;
201 CLTensor _output1;
202 CLTensor _output2;
203 CLTensor _output3;
204 CLTensor _output4;
205 CLTensor _output5;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000206 CLTensor _cell_state_activation;
Georgios Pinitas8bc745d2018-07-18 19:51:24 +0100207 CLTensor _output_state1;
Michalis Spyroubcedf512018-03-22 14:55:08 +0000208 CLTensor _ones;
209 bool _run_peephole_opt;
210 bool _run_cifg_opt;
211 bool _perform_cell_clipping;
212 bool _has_projection_weights;
213 bool _perform_projection_clipping;
214};
215}
216#endif /* __ARM_COMPUTE_CLLSTMLAYER_H__ */