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