arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #include "NeonLstmFloatWorkload.hpp" |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 7 | #include "NeonWorkloadUtils.hpp" |
| 8 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | #include "backendsCommon/CpuTensorHandle.hpp" |
| 10 | #include "aclCommon/ArmComputeTensorUtils.hpp" |
| 11 | #include "neon/NeonTensorHandle.hpp" |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 12 | |
| 13 | namespace armnn |
| 14 | { |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 15 | using namespace armcomputetensorutils; |
| 16 | |
| 17 | NeonLstmFloatWorkload::NeonLstmFloatWorkload(const LstmQueueDescriptor &descriptor, const WorkloadInfo &info) |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 18 | : FloatWorkload<LstmQueueDescriptor>(descriptor, info) |
| 19 | { |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 20 | arm_compute::LSTMParams<arm_compute::ITensor> lstm_param; |
| 21 | |
| 22 | // Basic parameters |
| 23 | m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 24 | BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo()); |
| 25 | |
| 26 | m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 27 | BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo()); |
| 28 | |
| 29 | m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 30 | BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo()); |
| 31 | |
| 32 | m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 33 | BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo()); |
| 34 | |
| 35 | m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 36 | BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo()); |
| 37 | |
| 38 | m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 39 | BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo()); |
| 40 | |
| 41 | m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 42 | BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo()); |
| 43 | |
| 44 | m_CellBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 45 | BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo()); |
| 46 | |
| 47 | m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 48 | BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo()); |
| 49 | |
| 50 | // for future reference: check the AndroidNN API for the logic here |
| 51 | if (!m_Data.m_Parameters.m_CifgEnabled) |
| 52 | { |
| 53 | m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 54 | BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo()); |
| 55 | |
| 56 | m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 57 | BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo()); |
| 58 | |
| 59 | m_CellToInputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 60 | if (m_Data.m_CellToInputWeights != nullptr) |
| 61 | { |
| 62 | BuildArmComputeTensor(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights->GetTensorInfo()); |
| 63 | } |
| 64 | |
| 65 | m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 66 | BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo()); |
| 67 | |
| 68 | lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(), |
| 69 | m_RecurrentToInputWeightsTensor.get(), |
| 70 | m_Data.m_CellToInputWeights != nullptr ? m_CellToInputWeightsTensor.get() : nullptr, |
| 71 | m_InputGateBiasTensor.get()); |
| 72 | } |
| 73 | |
| 74 | if (m_Data.m_Parameters.m_ProjectionEnabled) |
| 75 | { |
| 76 | m_ProjectionWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 77 | BuildArmComputeTensor(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights->GetTensorInfo()); |
| 78 | |
| 79 | m_ProjectionBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 80 | if (m_Data.m_ProjectionBias != nullptr) |
| 81 | { |
| 82 | BuildArmComputeTensor(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias->GetTensorInfo()); |
| 83 | } |
| 84 | |
| 85 | lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(), |
| 86 | m_Data.m_ProjectionBias != nullptr ? m_ProjectionBiasTensor.get() : nullptr); |
| 87 | } |
| 88 | |
| 89 | if (m_Data.m_Parameters.m_PeepholeEnabled) |
| 90 | { |
| 91 | m_CellToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 92 | BuildArmComputeTensor(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights->GetTensorInfo()); |
| 93 | |
| 94 | m_CellToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 95 | BuildArmComputeTensor(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights->GetTensorInfo()); |
| 96 | |
| 97 | lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get()); |
| 98 | } |
| 99 | |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 100 | if (m_Data.m_Parameters.m_LayerNormEnabled) |
| 101 | { |
| 102 | m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 103 | if (!m_Data.m_Parameters.m_CifgEnabled) |
| 104 | { |
| 105 | BuildArmComputeTensor(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights->GetTensorInfo()); |
| 106 | } |
| 107 | |
| 108 | m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 109 | BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights->GetTensorInfo()); |
| 110 | |
| 111 | m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 112 | BuildArmComputeTensor(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights->GetTensorInfo()); |
| 113 | |
| 114 | m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 115 | BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights->GetTensorInfo()); |
| 116 | |
| 117 | lstm_param.set_layer_normalization_params(m_Data.m_Parameters.m_CifgEnabled ? |
| 118 | nullptr : m_InputLayerNormWeightsTensor.get(), |
| 119 | m_ForgetLayerNormWeightsTensor.get(), |
| 120 | m_CellLayerNormWeightsTensor.get(), |
| 121 | m_OutputLayerNormWeightsTensor.get()); |
| 122 | } |
| 123 | |
Derek Lamberti | c81855f | 2019-06-13 17:34:19 +0100 | [diff] [blame] | 124 | const arm_compute::ITensor& input = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 125 | const arm_compute::ITensor& output_state_in = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
| 126 | const arm_compute::ITensor& cell_state_in = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[2])->GetTensor(); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 127 | |
Derek Lamberti | c81855f | 2019-06-13 17:34:19 +0100 | [diff] [blame] | 128 | arm_compute::ITensor& output_state_out = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[1])->GetTensor(); |
| 129 | arm_compute::ITensor& cell_state_out = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[2])->GetTensor(); |
| 130 | arm_compute::ITensor& output = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[3])->GetTensor(); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 131 | |
| 132 | // Get the batch_size and the num_units from the cellStateIn dimensions |
| 133 | const TensorInfo& inputTensorInfo = info.m_InputTensorInfos[2]; |
| 134 | const unsigned int batch_size = boost::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[0]); |
| 135 | const unsigned int num_units = boost::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[1]); |
| 136 | |
| 137 | m_ScratchBuffer = std::make_unique<arm_compute::Tensor>(); |
| 138 | if (m_Data.m_Parameters.m_CifgEnabled) |
| 139 | { |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 140 | // 2D tensor with dimensions [num_units * 3, batch_size] with CIFG |
Matteo Martincigh | a65b7ae | 2018-11-14 12:39:55 +0000 | [diff] [blame] | 141 | armnn::TensorInfo scratchBuffer1({ batch_size, num_units * 3 }, DataType::Float32); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 142 | BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1); |
| 143 | } |
| 144 | else |
| 145 | { |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 146 | // scratch_buffer [num_units * 4, batch_size] without CIFG |
Matteo Martincigh | a65b7ae | 2018-11-14 12:39:55 +0000 | [diff] [blame] | 147 | armnn::TensorInfo scratchBuffer2({ batch_size, num_units * 4 }, DataType::Float32); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 148 | BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2); |
| 149 | } |
| 150 | |
| 151 | float cell_threshold = m_Data.m_Parameters.m_ClippingThresCell; |
| 152 | float projection_threshold = m_Data.m_Parameters.m_ClippingThresProj; |
| 153 | |
| 154 | // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations |
| 155 | arm_compute::ActivationLayerInfo activationLayerInfo; |
| 156 | if (m_Data.m_Parameters.m_ActivationFunc == 0) |
| 157 | { |
| 158 | // no activation, do nothing |
| 159 | } |
| 160 | else if (m_Data.m_Parameters.m_ActivationFunc == 1) |
| 161 | { |
| 162 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 163 | arm_compute::ActivationLayerInfo::ActivationFunction::RELU); |
| 164 | } |
| 165 | else if (m_Data.m_Parameters.m_ActivationFunc == 3) |
| 166 | { |
| 167 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 168 | arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0); |
| 169 | } |
| 170 | else if (m_Data.m_Parameters.m_ActivationFunc == 4) |
| 171 | { |
| 172 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 173 | arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0); |
| 174 | } |
| 175 | else if (m_Data.m_Parameters.m_ActivationFunc == 6) |
| 176 | { |
| 177 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 178 | arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | throw armnn::Exception("Wrong Type of Activation Function!"); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(), |
| 187 | m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(), |
| 188 | m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(), |
| 189 | m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(), |
| 190 | &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out, |
| 191 | &cell_state_out, &output, lstm_param, activationLayerInfo, |
| 192 | cell_threshold, projection_threshold); |
| 193 | |
| 194 | armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer); |
| 195 | |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 196 | InitializeArmComputeTensorData(*m_InputToForgetWeightsTensor, |
| 197 | m_Data.m_InputToForgetWeights); |
| 198 | InitializeArmComputeTensorData(*m_InputToCellWeightsTensor, |
| 199 | m_Data.m_InputToCellWeights); |
| 200 | InitializeArmComputeTensorData(*m_InputToOutputWeightsTensor, |
| 201 | m_Data.m_InputToOutputWeights); |
| 202 | InitializeArmComputeTensorData(*m_RecurrentToForgetWeightsTensor, |
| 203 | m_Data.m_RecurrentToForgetWeights); |
| 204 | InitializeArmComputeTensorData(*m_RecurrentToCellWeightsTensor, |
| 205 | m_Data.m_RecurrentToCellWeights); |
| 206 | InitializeArmComputeTensorData(*m_RecurrentToOutputWeightsTensor, |
| 207 | m_Data.m_RecurrentToOutputWeights); |
| 208 | InitializeArmComputeTensorData(*m_ForgetGateBiasTensor, |
| 209 | m_Data.m_ForgetGateBias); |
| 210 | InitializeArmComputeTensorData(*m_CellBiasTensor, |
| 211 | m_Data.m_CellBias); |
| 212 | InitializeArmComputeTensorData(*m_OutputGateBiasTensor, |
| 213 | m_Data.m_OutputGateBias); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 214 | |
| 215 | if (!m_Data.m_Parameters.m_CifgEnabled) |
| 216 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 217 | InitializeArmComputeTensorData(*m_InputToInputWeightsTensor, |
| 218 | m_Data.m_InputToInputWeights); |
| 219 | InitializeArmComputeTensorData(*m_RecurrentToInputWeightsTensor, |
| 220 | m_Data.m_RecurrentToInputWeights); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 221 | if (m_Data.m_CellToInputWeights != nullptr) |
| 222 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 223 | InitializeArmComputeTensorData(*m_CellToInputWeightsTensor, |
| 224 | m_Data.m_CellToInputWeights); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 225 | } |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 226 | InitializeArmComputeTensorData(*m_InputGateBiasTensor, |
| 227 | m_Data.m_InputGateBias); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (m_Data.m_Parameters.m_ProjectionEnabled) |
| 231 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 232 | InitializeArmComputeTensorData(*m_ProjectionWeightsTensor, |
| 233 | m_Data.m_ProjectionWeights); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 234 | if (m_Data.m_ProjectionBias != nullptr) |
| 235 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 236 | InitializeArmComputeTensorData(*m_ProjectionBiasTensor, |
| 237 | m_Data.m_ProjectionBias); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | if (m_Data.m_Parameters.m_PeepholeEnabled) |
| 242 | { |
Nattapat Chaimanowong | 177d8d2 | 2018-10-16 13:21:27 +0100 | [diff] [blame] | 243 | InitializeArmComputeTensorData(*m_CellToForgetWeightsTensor, |
| 244 | m_Data.m_CellToForgetWeights); |
| 245 | InitializeArmComputeTensorData(*m_CellToOutputWeightsTensor, |
| 246 | m_Data.m_CellToOutputWeights); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 249 | if (m_Data.m_Parameters.m_LayerNormEnabled) |
| 250 | { |
| 251 | if (!m_Data.m_Parameters.m_CifgEnabled) |
| 252 | { |
| 253 | InitializeArmComputeTensorData(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights); |
| 254 | } |
| 255 | InitializeArmComputeTensorData(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights); |
| 256 | InitializeArmComputeTensorData(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights); |
| 257 | InitializeArmComputeTensorData(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights); |
| 258 | } |
| 259 | |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 260 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 261 | // delete all the input tensors that will no longer be needed |
| 262 | m_LstmLayer.prepare(); |
| 263 | FreeUnusedTensors(); |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void NeonLstmFloatWorkload::Execute() const |
| 267 | { |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 268 | m_LstmLayer.run(); |
arovir01 | 9e53a35 | 2018-08-31 15:26:35 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 271 | arm_compute::Status NeonLstmFloatWorkloadValidate(const TensorInfo& input, |
| 272 | const TensorInfo& outputStateIn, |
| 273 | const TensorInfo& cellStateIn, |
| 274 | const TensorInfo& scratchBuffer, |
| 275 | const TensorInfo& outputStateOut, |
| 276 | const TensorInfo& cellStateOut, |
| 277 | const TensorInfo& output, |
| 278 | const LstmDescriptor& descriptor, |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 279 | const LstmInputParamsInfo& paramsInfo) |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 280 | { |
| 281 | arm_compute::LSTMParams<arm_compute::ITensorInfo> lstm_params_info; |
| 282 | |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 283 | // The inputs and outputs |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 284 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input); |
| 285 | const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn); |
| 286 | const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn); |
| 287 | const arm_compute::TensorInfo aclScratchBufferInfo = BuildArmComputeTensorInfo(scratchBuffer); |
| 288 | const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut); |
| 289 | const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut); |
| 290 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output); |
| 291 | |
| 292 | // Basic parameters |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 293 | const arm_compute::TensorInfo aclInputToForgetWeightsInfo |
| 294 | = BuildArmComputeTensorInfo(paramsInfo.get_InputToForgetWeights()); |
| 295 | const arm_compute::TensorInfo aclInputToCellWeightsInfo |
| 296 | = BuildArmComputeTensorInfo(paramsInfo.get_InputToCellWeights()); |
| 297 | const arm_compute::TensorInfo aclInputToOutputWeightsInfo |
| 298 | = BuildArmComputeTensorInfo(paramsInfo.get_InputToOutputWeights()); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 299 | const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 300 | = BuildArmComputeTensorInfo(paramsInfo.get_RecurrentToForgetWeights()); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 301 | const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 302 | = BuildArmComputeTensorInfo(paramsInfo.get_RecurrentToCellWeights()); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 303 | const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 304 | = BuildArmComputeTensorInfo(paramsInfo.get_RecurrentToOutputWeights()); |
| 305 | const arm_compute::TensorInfo aclForgetGateBiasInfo |
| 306 | = BuildArmComputeTensorInfo(paramsInfo.get_ForgetGateBias()); |
| 307 | const arm_compute::TensorInfo aclCellBiasInfo |
| 308 | = BuildArmComputeTensorInfo(paramsInfo.get_CellBias()); |
| 309 | const arm_compute::TensorInfo aclOutputGateBiasInfo |
| 310 | = BuildArmComputeTensorInfo(paramsInfo.get_OutputGateBias()); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 311 | |
| 312 | arm_compute::TensorInfo aclInputToInputWeightsInfo; |
| 313 | arm_compute::TensorInfo aclRecurrentToInputWeightsInfo; |
| 314 | arm_compute::TensorInfo aclCellToInputWeightsInfo; |
| 315 | arm_compute::TensorInfo aclInputGateBiasInfo; |
| 316 | arm_compute::TensorInfo aclProjectionWeightsInfo; |
| 317 | arm_compute::TensorInfo aclProjectionBiasInfo; |
| 318 | arm_compute::TensorInfo aclCellToForgetWeightsInfo; |
| 319 | arm_compute::TensorInfo aclCellToOutputWeightsInfo; |
| 320 | |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 321 | arm_compute::TensorInfo aclInputLayerNormWeightsInfo; |
| 322 | arm_compute::TensorInfo aclForgetLayerNormWeightsInfo; |
| 323 | arm_compute::TensorInfo aclCellLayerNormWeightsInfo; |
| 324 | arm_compute::TensorInfo aclOutputLayerNormWeightsInfo; |
| 325 | |
| 326 | |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 327 | if (!descriptor.m_CifgEnabled) |
| 328 | { |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 329 | if (descriptor.m_PeepholeEnabled) |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 330 | { |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 331 | aclCellToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_CellToInputWeights()); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 332 | } |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 333 | aclInputToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_InputToInputWeights()); |
| 334 | aclRecurrentToInputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_RecurrentToInputWeights()); |
| 335 | aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.get_InputGateBias()); |
| 336 | |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 337 | lstm_params_info.set_cifg_params(&aclInputToInputWeightsInfo, &aclRecurrentToInputWeightsInfo, |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 338 | descriptor.m_PeepholeEnabled ? &aclCellToInputWeightsInfo : nullptr, |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 339 | &aclInputGateBiasInfo); |
| 340 | } |
| 341 | |
| 342 | if (descriptor.m_ProjectionEnabled) |
| 343 | { |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 344 | if (paramsInfo.m_ProjectionBias != nullptr) |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 345 | { |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 346 | aclProjectionBiasInfo = BuildArmComputeTensorInfo(paramsInfo.get_ProjectionBias()); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 347 | } |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 348 | aclProjectionWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_ProjectionWeights()); |
| 349 | |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 350 | lstm_params_info.set_projection_params(&aclProjectionWeightsInfo, |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 351 | paramsInfo.m_ProjectionBias != nullptr ? |
| 352 | &aclProjectionBiasInfo : nullptr); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (descriptor.m_PeepholeEnabled) |
| 356 | { |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 357 | aclCellToForgetWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_CellToForgetWeights()); |
| 358 | aclCellToOutputWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_CellToOutputWeights()); |
| 359 | |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 360 | lstm_params_info.set_peephole_params(&aclCellToForgetWeightsInfo, &aclCellToOutputWeightsInfo); |
| 361 | } |
| 362 | |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 363 | if (descriptor.m_LayerNormEnabled) |
| 364 | { |
| 365 | if (!descriptor.m_CifgEnabled) |
| 366 | { |
| 367 | aclInputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_InputLayerNormWeights()); |
| 368 | } |
| 369 | aclForgetLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_ForgetLayerNormWeights()); |
| 370 | aclCellLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_CellLayerNormWeights()); |
| 371 | aclOutputLayerNormWeightsInfo = BuildArmComputeTensorInfo(paramsInfo.get_OutputLayerNormWeights()); |
| 372 | |
| 373 | lstm_params_info.set_layer_normalization_params(descriptor.m_CifgEnabled ? |
| 374 | nullptr : &aclInputLayerNormWeightsInfo, |
| 375 | &aclForgetLayerNormWeightsInfo, |
| 376 | &aclCellLayerNormWeightsInfo, |
| 377 | &aclOutputLayerNormWeightsInfo); |
| 378 | } |
| 379 | |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 380 | float cell_threshold = descriptor.m_ClippingThresCell; |
| 381 | float projection_threshold = descriptor.m_ClippingThresProj; |
| 382 | |
| 383 | // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations |
| 384 | arm_compute::ActivationLayerInfo activationLayerInfo; |
| 385 | switch (descriptor.m_ActivationFunc) |
| 386 | { |
| 387 | case 0: |
| 388 | // no activation, do nothing |
| 389 | break; |
| 390 | case 1: |
| 391 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 392 | arm_compute::ActivationLayerInfo::ActivationFunction::RELU); |
| 393 | break; |
| 394 | case 3: |
| 395 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 396 | arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0); |
| 397 | break; |
| 398 | case 4: |
| 399 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 400 | arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0); |
| 401 | break; |
| 402 | case 6: |
| 403 | activationLayerInfo = arm_compute::ActivationLayerInfo( |
| 404 | arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC); |
| 405 | break; |
| 406 | default: |
| 407 | throw armnn::Exception("Wrong Type of Activation Function!"); |
| 408 | } |
| 409 | |
| 410 | return arm_compute::NELSTMLayer::validate(&aclInputInfo, |
| 411 | &aclInputToForgetWeightsInfo, |
| 412 | &aclInputToCellWeightsInfo, |
| 413 | &aclInputToOutputWeightsInfo, |
| 414 | &aclRecurrentToForgetWeightsInfo, |
| 415 | &aclRecurrentToCellWeightsInfo, |
| 416 | &aclRecurrentToOutputWeightsInfo, |
| 417 | &aclForgetGateBiasInfo, |
| 418 | &aclCellBiasInfo, |
| 419 | &aclOutputGateBiasInfo, |
| 420 | &aclOutputStateInInfo, |
| 421 | &aclCellStateInInfo, |
| 422 | &aclScratchBufferInfo, |
| 423 | &aclOutputStateOutInfo, |
| 424 | &aclCellStateOutInfo, |
| 425 | &aclOutputInfo, |
| 426 | lstm_params_info, |
| 427 | activationLayerInfo, |
| 428 | cell_threshold, |
| 429 | projection_threshold); |
| 430 | } |
| 431 | |
| 432 | void NeonLstmFloatWorkload::FreeUnusedTensors() |
| 433 | { |
| 434 | FreeTensorIfUnused(m_InputToInputWeightsTensor); |
| 435 | FreeTensorIfUnused(m_InputToForgetWeightsTensor); |
| 436 | FreeTensorIfUnused(m_InputToCellWeightsTensor); |
| 437 | FreeTensorIfUnused(m_InputToOutputWeightsTensor); |
| 438 | FreeTensorIfUnused(m_RecurrentToInputWeightsTensor); |
| 439 | FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor); |
| 440 | FreeTensorIfUnused(m_RecurrentToCellWeightsTensor); |
| 441 | FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor); |
| 442 | FreeTensorIfUnused(m_CellToInputWeightsTensor); |
| 443 | FreeTensorIfUnused(m_CellToForgetWeightsTensor); |
| 444 | FreeTensorIfUnused(m_CellToOutputWeightsTensor); |
| 445 | FreeTensorIfUnused(m_InputGateBiasTensor); |
| 446 | FreeTensorIfUnused(m_ForgetGateBiasTensor); |
| 447 | FreeTensorIfUnused(m_CellBiasTensor); |
| 448 | FreeTensorIfUnused(m_OutputGateBiasTensor); |
| 449 | FreeTensorIfUnused(m_ProjectionWeightsTensor); |
| 450 | FreeTensorIfUnused(m_ProjectionBiasTensor); |
| 451 | FreeTensorIfUnused(m_ScratchBuffer); |
Jan Eilers | ad5293a | 2019-07-08 09:57:55 +0100 | [diff] [blame] | 452 | FreeTensorIfUnused(m_InputLayerNormWeightsTensor); |
| 453 | FreeTensorIfUnused(m_ForgetLayerNormWeightsTensor); |
| 454 | FreeTensorIfUnused(m_CellLayerNormWeightsTensor); |
| 455 | FreeTensorIfUnused(m_OutputLayerNormWeightsTensor); |
Les Bell | de9011b | 2018-10-03 10:37:52 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | } //namespace armnn |