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