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