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