| 1 |
/* |
| 2 |
* Copyright (c) 2018-2021, 2024 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 ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NELSTMLAYER_H |
| 25 |
#define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NELSTMLAYER_H |
| 26 |
|
| 27 |
#include "arm_compute/core/Types.h" |
| 28 |
#include "arm_compute/runtime/common/LSTMParams.h" |
| 29 |
#include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 30 |
#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" |
| 31 |
#include "arm_compute/runtime/NEON/functions/NEArithmeticAddition.h" |
| 32 |
#include "arm_compute/runtime/NEON/functions/NEArithmeticSubtraction.h" |
| 33 |
#include "arm_compute/runtime/NEON/functions/NEConcatenateLayer.h" |
| 34 |
#include "arm_compute/runtime/NEON/functions/NECopy.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/NEMeanStdDevNormalizationLayer.h" |
| 38 |
#include "arm_compute/runtime/NEON/functions/NEPixelWiseMultiplication.h" |
| 39 |
#include "arm_compute/runtime/NEON/functions/NETranspose.h" |
| 40 |
|
| 41 |
namespace arm_compute |
| 42 |
{ |
| 43 |
// Forward declarations |
| 44 |
class ITensor; |
| 45 |
|
| 46 |
/** Basic function to run @ref NELSTMLayer */ |
| 47 |
class NELSTMLayer : public IFunction |
| 48 |
{ |
| 49 |
public: |
| 50 |
/** Default constructor */ |
| 51 |
NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager); |
| 52 |
NELSTMLayer() : NELSTMLayer(MemoryManagerOnDemand::make_default()) |
| 53 |
{ |
| 54 |
} |
| 55 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 56 |
NELSTMLayer(const NELSTMLayer &) = delete; |
| 57 |
/** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 58 |
NELSTMLayer &operator=(const NELSTMLayer &) = delete; |
| 59 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 60 |
NELSTMLayer(NELSTMLayer &&) = delete; |
| 61 |
/** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 62 |
NELSTMLayer &operator=(NELSTMLayer &&) = delete; |
| 63 |
/** Default destructor */ |
| 64 |
~NELSTMLayer(); |
| 65 |
/** Initialize function's tensors. |
| 66 |
* |
| 67 |
* Valid data layouts: |
| 68 |
* - All |
| 69 |
* |
| 70 |
* Valid data type configurations: |
| 71 |
* |src0 - src13 | dst0 - dst3 | |
| 72 |
* |:------------|:------------| |
| 73 |
* |F16 |F16 | |
| 74 |
* |F32 |F32 | |
| 75 |
* |
| 76 |
* @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32. |
| 77 |
* @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 78 |
* @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 79 |
* @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 80 |
* @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 81 |
* @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 82 |
* @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 83 |
* @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 84 |
* @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 85 |
* @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 86 |
* @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 87 |
* @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 88 |
* @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. |
| 89 |
* @param[out] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 90 |
* @param[out] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 91 |
* @param[out] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. |
| 92 |
* Data types supported: Same as @p input. |
| 93 |
* @param[in] lstm_params Weights tensors used in peephole optimization: |
| 94 |
* input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 95 |
* recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 96 |
* cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. |
| 97 |
* cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 98 |
* cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 99 |
* input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input |
| 100 |
* projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 101 |
* projection_bias (Optional) 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. |
| 102 |
* input_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 103 |
* forget_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 104 |
* cell_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 105 |
* output_layer_norm_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 106 |
* @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. |
| 107 |
* @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. |
| 108 |
* @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. |
| 109 |
* If set to 0.0 then clipping is disabled. |
| 110 |
*/ |
| 111 |
void configure(const ITensor *input, |
| 112 |
const ITensor *input_to_forget_weights, |
| 113 |
const ITensor *input_to_cell_weights, |
| 114 |
const ITensor *input_to_output_weights, |
| 115 |
const ITensor *recurrent_to_forget_weights, |
| 116 |
const ITensor *recurrent_to_cell_weights, |
| 117 |
const ITensor *recurrent_to_output_weights, |
| 118 |
const ITensor *forget_gate_bias, |
| 119 |
const ITensor *cell_bias, |
| 120 |
const ITensor *output_gate_bias, |
| 121 |
const ITensor *output_state_in, |
| 122 |
const ITensor *cell_state_in, |
| 123 |
ITensor *scratch_buffer, |
| 124 |
ITensor *output_state_out, |
| 125 |
ITensor *cell_state_out, |
| 126 |
ITensor *output, |
| 127 |
const LSTMParams<ITensor> &lstm_params, |
| 128 |
const ActivationLayerInfo &activation_info, |
| 129 |
float cell_threshold = 0.f, |
| 130 |
float projection_threshold = 0.f); |
| 131 |
|
| 132 |
/** Static function to check if given info will lead to a valid configuration of @ref NELSTMLayer |
| 133 |
* |
| 134 |
* @param[in] input Source tensor. Input is a 2D tensor with dimensions [input_size, batch_size]. Data types supported: F16/F32. |
| 135 |
* @param[in] input_to_forget_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 136 |
* @param[in] input_to_cell_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 137 |
* @param[in] input_to_output_weights 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 138 |
* @param[in] recurrent_to_forget_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 139 |
* @param[in] recurrent_to_cell_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 140 |
* @param[in] recurrent_to_output_weights 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 141 |
* @param[in] forget_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 142 |
* @param[in] cell_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 143 |
* @param[in] output_gate_bias 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 144 |
* @param[in] output_state_in 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 145 |
* @param[in] cell_state_in 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 146 |
* @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. |
| 147 |
* @param[in] output_state_out 2D weights tensor with dimensions [output_size, batch_size]. Data type supported: Same as @p input. |
| 148 |
* @param[in] cell_state_out 2D tensor with dimensions [num_units, batch_size]. Data type supported: Same as @p input. |
| 149 |
* @param[in] output Destination tensor. Output is a 2D tensor with dimensions [output_size, batch_size]. |
| 150 |
* Data types supported: Same as @p input. |
| 151 |
* @param[in] lstm_params Weights tensors used in peephole optimization: |
| 152 |
* input_to_input_weights (Optional) 2D weights tensor with dimensions [input_size, num_units]. Data type supported: Same as @p input. |
| 153 |
* recurrent_to_input_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 154 |
* cell_to_input_weights (Optional) 1D weights tensor with dimensions [num_units]. Can be nullptr. Data type supported: Same as @p input. |
| 155 |
* cell_to_forget_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 156 |
* cell_to_output_weights (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input. |
| 157 |
* input_gate_bias (Optional) 1D weights tensor with dimensions [num_units]. Data type supported: Same as @p input |
| 158 |
* projection_weights (Optional) 2D weights tensor with dimensions [output_size, num_units]. Data type supported: Same as @p input. |
| 159 |
* projection_bias (Optional) 1D weights tensor with dimensions [output_size]. Data type supported: Same as @p input. |
| 160 |
* input_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. |
| 161 |
* forget_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. |
| 162 |
* cell_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. |
| 163 |
* output_layer_norm_weights (Optional) 1D weights tensor info with dimensions [num_units]. Data type supported: Same as @p input. |
| 164 |
* @param[in] activation_info Contains activation information described in @ref ActivationLayerInfo. |
| 165 |
* @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. |
| 166 |
* @param[in] projection_threshold The clipping threshold for the output from the projection layer, such that values are bound within [-proj_clip, proj_clip]. |
| 167 |
* If set to 0.0 then clipping is disabled. |
| 168 |
* |
| 169 |
* @return a status |
| 170 |
*/ |
| 171 |
static Status validate(const ITensorInfo *input, |
| 172 |
const ITensorInfo *input_to_forget_weights, |
| 173 |
const ITensorInfo *input_to_cell_weights, |
| 174 |
const ITensorInfo *input_to_output_weights, |
| 175 |
const ITensorInfo *recurrent_to_forget_weights, |
| 176 |
const ITensorInfo *recurrent_to_cell_weights, |
| 177 |
const ITensorInfo *recurrent_to_output_weights, |
| 178 |
const ITensorInfo *forget_gate_bias, |
| 179 |
const ITensorInfo *cell_bias, |
| 180 |
const ITensorInfo *output_gate_bias, |
| 181 |
const ITensorInfo *output_state_in, |
| 182 |
const ITensorInfo *cell_state_in, |
| 183 |
const ITensorInfo *scratch_buffer, |
| 184 |
const ITensorInfo *output_state_out, |
| 185 |
const ITensorInfo *cell_state_out, |
| 186 |
const ITensorInfo *output, |
| 187 |
const LSTMParams<ITensorInfo> &lstm_params, |
| 188 |
const ActivationLayerInfo &activation_info, |
| 189 |
float cell_threshold = 0.f, |
| 190 |
float projection_threshold = 0.f); |
| 191 |
|
| 192 |
// Inherited methods overridden: |
| 193 |
void run() override; |
| 194 |
void prepare() override; |
| 195 |
|
| 196 |
private: |
| 197 |
MemoryGroup _memory_group; |
| 198 |
NEFullyConnectedLayer _fully_connected_input_gate; |
| 199 |
NEArithmeticAddition _accum_input_gate1; |
| 200 |
NEArithmeticSubtraction _subtract_input_gate; |
| 201 |
NEPixelWiseMultiplication _pixelwise_mul_input_gate; |
| 202 |
NEActivationLayer _activation_input_gate; |
| 203 |
NEFullyConnectedLayer _fully_connected_forget_gate; |
| 204 |
NEArithmeticAddition _accum_forget_gate1; |
| 205 |
NEPixelWiseMultiplication _pixelwise_mul_forget_gate; |
| 206 |
NEActivationLayer _activation_forget_gate; |
| 207 |
NEFullyConnectedLayer _fully_connected_cell_state; |
| 208 |
NEGEMM _gemm_cell_state1; |
| 209 |
NETranspose _transpose_cell_state; |
| 210 |
NEArithmeticAddition _accum_cell_state1; |
| 211 |
NEArithmeticAddition _accum_cell_state2; |
| 212 |
NEPixelWiseMultiplication _pixelwise_mul_cell_state1; |
| 213 |
NEActivationLayer _activation_cell_state; |
| 214 |
NEActivationLayer _cell_clip; |
| 215 |
NEPixelWiseMultiplication _pixelwise_mul_cell_state2; |
| 216 |
NEFullyConnectedLayer _fully_connected_output; |
| 217 |
NEPixelWiseMultiplication _pixelwise_mul_output_state1; |
| 218 |
NEArithmeticAddition _accum_output1; |
| 219 |
NEActivationLayer _activation_output; |
| 220 |
NEActivationLayer _activation_output_state; |
| 221 |
NEPixelWiseMultiplication _pixelwise_mul_output_state2; |
| 222 |
NEFullyConnectedLayer _fully_connected_output_state; |
| 223 |
NEActivationLayer _projection_clip; |
| 224 |
NECopy _copy_cell_state; |
| 225 |
NECopy _copy_output; |
| 226 |
NEConcatenateLayer _concat_scratch_buffer; |
| 227 |
NEConcatenateLayer _concat_inputs_forget_gate; |
| 228 |
NEConcatenateLayer _concat_weights_forget_gate; |
| 229 |
NEConcatenateLayer _concat_weights_input_gate; |
| 230 |
NEConcatenateLayer _concat_weights_output; |
| 231 |
NEMeanStdDevNormalizationLayer _mean_std_norm_input_gate; |
| 232 |
NEPixelWiseMultiplication _pixelwise_mul_input_gate_coeff; |
| 233 |
NEArithmeticAddition _accum_input_gate_bias; |
| 234 |
NEMeanStdDevNormalizationLayer _mean_std_norm_forget_gate; |
| 235 |
NEPixelWiseMultiplication _pixelwise_mul_forget_gate_coeff; |
| 236 |
NEArithmeticAddition _accum_forget_gate_bias; |
| 237 |
NEMeanStdDevNormalizationLayer _mean_std_norm_cell_gate; |
| 238 |
NEPixelWiseMultiplication _pixelwise_mul_cell_gate_coeff; |
| 239 |
NEArithmeticAddition _accum_cell_gate_bias; |
| 240 |
NEMeanStdDevNormalizationLayer _mean_std_norm_output_gate; |
| 241 |
NEPixelWiseMultiplication _pixelwise_mul_output_gate_coeff; |
| 242 |
NEArithmeticAddition _accum_output_gate_bias; |
| 243 |
Tensor _input_gate_out1; |
| 244 |
Tensor _input_gate_out2; |
| 245 |
Tensor _input_gate_out3; |
| 246 |
Tensor _input_gate_out4; |
| 247 |
Tensor _forget_gate_out1; |
| 248 |
Tensor _forget_gate_out2; |
| 249 |
Tensor _forget_gate_out3; |
| 250 |
Tensor _forget_gate_out4; |
| 251 |
Tensor _forget_gate_out5; |
| 252 |
Tensor _cell_state_out1; |
| 253 |
Tensor _cell_state_out2; |
| 254 |
Tensor _cell_state_out3; |
| 255 |
Tensor _cell_state_out4; |
| 256 |
Tensor _cell_state_out5; |
| 257 |
Tensor _output1; |
| 258 |
Tensor _output2; |
| 259 |
Tensor _output3; |
| 260 |
Tensor _output4; |
| 261 |
Tensor _cell_state_activation; |
| 262 |
Tensor _output_state1; |
| 263 |
Tensor _ones; |
| 264 |
Tensor _input_layer_norm_out1; |
| 265 |
Tensor _input_layer_norm_out2; |
| 266 |
Tensor _forget_layer_norm_out1; |
| 267 |
Tensor _forget_layer_norm_out2; |
| 268 |
Tensor _cell_layer_norm_out1; |
| 269 |
Tensor _cell_layer_norm_out2; |
| 270 |
Tensor _output_layer_norm_out1; |
| 271 |
Tensor _output_layer_norm_out2; |
| 272 |
bool _run_peephole_opt; |
| 273 |
bool _run_cifg_opt; |
| 274 |
bool _perform_cell_clipping; |
| 275 |
bool _has_projection_weights; |
| 276 |
bool _perform_projection_clipping; |
| 277 |
bool _is_prepared; |
| 278 |
bool _is_layer_norm_lstm; |
| 279 |
}; |
| 280 |
} // namespace arm_compute |
| 281 |
#endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NELSTMLAYER_H |