Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "NeonQuantizedLstmWorkload.hpp" |
| 7 | #include "NeonWorkloadUtils.hpp" |
| 8 | |
| 9 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 10 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 11 | #include <neon/NeonTensorHandle.hpp> |
| 12 | |
| 13 | namespace armnn |
| 14 | { |
| 15 | using namespace armcomputetensorutils; |
| 16 | |
| 17 | NeonQuantizedLstmWorkload::NeonQuantizedLstmWorkload(const QuantizedLstmQueueDescriptor &descriptor, |
| 18 | const WorkloadInfo &info) |
| 19 | : BaseWorkload<QuantizedLstmQueueDescriptor>(descriptor, info) |
| 20 | { |
| 21 | // Basic parameters |
| 22 | m_InputToInputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 23 | BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo()); |
| 24 | |
| 25 | m_InputToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 26 | BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo()); |
| 27 | |
| 28 | m_InputToCellWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 29 | BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo()); |
| 30 | |
| 31 | m_InputToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 32 | BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo()); |
| 33 | |
| 34 | m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 35 | BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo()); |
| 36 | |
| 37 | m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 38 | BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo()); |
| 39 | |
| 40 | m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 41 | BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo()); |
| 42 | |
| 43 | m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::Tensor>(); |
| 44 | BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo()); |
| 45 | |
| 46 | m_InputGateBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 47 | BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo()); |
| 48 | |
| 49 | m_ForgetGateBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 50 | BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo()); |
| 51 | |
| 52 | m_CellBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 53 | BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo()); |
| 54 | |
| 55 | m_OutputGateBiasTensor = std::make_unique<arm_compute::Tensor>(); |
| 56 | BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo()); |
| 57 | |
| 58 | const arm_compute::ITensor& input = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 59 | arm_compute::ITensor& cell_state_in = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
| 60 | const arm_compute::ITensor& output_state_in = static_cast<IAclTensorHandle*>(m_Data.m_Inputs[2])->GetTensor(); |
| 61 | |
| 62 | arm_compute::ITensor& cell_state_out = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 63 | arm_compute::ITensor& output_state_out = static_cast<IAclTensorHandle*>(m_Data.m_Outputs[1])->GetTensor(); |
| 64 | |
| 65 | m_QuantizedLstmLayer.configure(&input, |
| 66 | m_InputToInputWeightsTensor.get(), |
| 67 | m_InputToForgetWeightsTensor.get(), |
| 68 | m_InputToCellWeightsTensor.get(), |
| 69 | m_InputToOutputWeightsTensor.get(), |
| 70 | m_RecurrentToInputWeightsTensor.get(), |
| 71 | m_RecurrentToForgetWeightsTensor.get(), |
| 72 | m_RecurrentToCellWeightsTensor.get(), |
| 73 | m_RecurrentToOutputWeightsTensor.get(), |
| 74 | m_InputGateBiasTensor.get(), |
| 75 | m_ForgetGateBiasTensor.get(), |
| 76 | m_CellBiasTensor.get(), |
| 77 | m_OutputGateBiasTensor.get(), |
| 78 | &cell_state_in, |
| 79 | &output_state_in, |
| 80 | &cell_state_out, |
| 81 | &output_state_out); |
| 82 | |
| 83 | InitializeArmComputeTensorData(*m_InputToInputWeightsTensor, |
| 84 | m_Data.m_InputToInputWeights); |
| 85 | |
| 86 | InitializeArmComputeTensorData(*m_InputToForgetWeightsTensor, |
| 87 | m_Data.m_InputToForgetWeights); |
| 88 | |
| 89 | InitializeArmComputeTensorData(*m_InputToCellWeightsTensor, |
| 90 | m_Data.m_InputToCellWeights); |
| 91 | |
| 92 | InitializeArmComputeTensorData(*m_InputToOutputWeightsTensor, |
| 93 | m_Data.m_InputToOutputWeights); |
| 94 | |
| 95 | InitializeArmComputeTensorData(*m_RecurrentToInputWeightsTensor, |
| 96 | m_Data.m_RecurrentToInputWeights); |
| 97 | |
| 98 | InitializeArmComputeTensorData(*m_RecurrentToForgetWeightsTensor, |
| 99 | m_Data.m_RecurrentToForgetWeights); |
| 100 | |
| 101 | InitializeArmComputeTensorData(*m_RecurrentToCellWeightsTensor, |
| 102 | m_Data.m_RecurrentToCellWeights); |
| 103 | |
| 104 | InitializeArmComputeTensorData(*m_RecurrentToOutputWeightsTensor, |
| 105 | m_Data.m_RecurrentToOutputWeights); |
| 106 | |
| 107 | InitializeArmComputeTensorData(*m_InputGateBiasTensor, |
| 108 | m_Data.m_InputGateBias); |
| 109 | |
| 110 | InitializeArmComputeTensorData(*m_ForgetGateBiasTensor, |
| 111 | m_Data.m_ForgetGateBias); |
| 112 | |
| 113 | InitializeArmComputeTensorData(*m_CellBiasTensor, |
| 114 | m_Data.m_CellBias); |
| 115 | |
| 116 | InitializeArmComputeTensorData(*m_OutputGateBiasTensor, |
| 117 | m_Data.m_OutputGateBias); |
| 118 | |
| 119 | // Force Compute Library to perform the necessary copying and reshaping, after which |
| 120 | // delete all the input tensors that will no longer be needed |
| 121 | m_QuantizedLstmLayer.prepare(); |
| 122 | FreeUnusedTensors(); |
| 123 | } |
| 124 | |
| 125 | void NeonQuantizedLstmWorkload::Execute() const |
| 126 | { |
| 127 | m_QuantizedLstmLayer.run(); |
| 128 | } |
| 129 | |
| 130 | arm_compute::Status NeonQuantizedLstmWorkloadValidate(const TensorInfo& input, |
| 131 | const TensorInfo& cellStateIn, |
| 132 | const TensorInfo& outputStateIn, |
| 133 | const TensorInfo& cellStateOut, |
| 134 | const TensorInfo& outputStateOut, |
| 135 | const QuantizedLstmInputParamsInfo& paramsInfo) |
| 136 | { |
| 137 | // The inputs and outputs |
| 138 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input); |
| 139 | const arm_compute::TensorInfo aclCellStateInInfo = BuildArmComputeTensorInfo(cellStateIn); |
| 140 | const arm_compute::TensorInfo aclOutputStateInInfo = BuildArmComputeTensorInfo(outputStateIn); |
| 141 | const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut); |
| 142 | const arm_compute::TensorInfo aclOutputStateOutInfo = BuildArmComputeTensorInfo(outputStateOut); |
| 143 | |
| 144 | // Basic parameters |
| 145 | const arm_compute::TensorInfo aclInputToInputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 146 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToInputWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 147 | const arm_compute::TensorInfo aclInputToForgetWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 148 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToForgetWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 149 | const arm_compute::TensorInfo aclInputToCellWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 150 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToCellWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 151 | const arm_compute::TensorInfo aclInputToOutputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 152 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToOutputWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 153 | |
| 154 | const arm_compute::TensorInfo aclRecurrentToInputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 155 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToInputWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 156 | const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 157 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToForgetWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 158 | const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 159 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToCellWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 160 | const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 161 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToOutputWeights()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 162 | |
| 163 | const arm_compute::TensorInfo aclInputGateBiasInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 164 | = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 165 | const arm_compute::TensorInfo aclForgetGateBiasInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 166 | = BuildArmComputeTensorInfo(paramsInfo.GetForgetGateBias()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 167 | const arm_compute::TensorInfo aclCellBiasInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 168 | = BuildArmComputeTensorInfo(paramsInfo.GetCellBias()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 169 | const arm_compute::TensorInfo aclOutputGateBiasInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 170 | = BuildArmComputeTensorInfo(paramsInfo.GetOutputGateBias()); |
Francis Murtagh | 4fc3c48 | 2019-08-02 13:20:54 +0100 | [diff] [blame] | 171 | |
| 172 | return arm_compute::NELSTMLayerQuantized::validate(&aclInputInfo, |
| 173 | &aclInputToInputWeightsInfo, |
| 174 | &aclInputToForgetWeightsInfo, |
| 175 | &aclInputToCellWeightsInfo, |
| 176 | &aclInputToOutputWeightsInfo, |
| 177 | &aclRecurrentToInputWeightsInfo, |
| 178 | &aclRecurrentToForgetWeightsInfo, |
| 179 | &aclRecurrentToCellWeightsInfo, |
| 180 | &aclRecurrentToOutputWeightsInfo, |
| 181 | &aclInputGateBiasInfo, |
| 182 | &aclForgetGateBiasInfo, |
| 183 | &aclCellBiasInfo, |
| 184 | &aclOutputGateBiasInfo, |
| 185 | &aclCellStateInInfo, |
| 186 | &aclOutputStateInInfo, |
| 187 | &aclCellStateOutInfo, |
| 188 | &aclOutputStateOutInfo); |
| 189 | } |
| 190 | |
| 191 | void NeonQuantizedLstmWorkload::FreeUnusedTensors() |
| 192 | { |
| 193 | FreeTensorIfUnused(m_InputToInputWeightsTensor); |
| 194 | FreeTensorIfUnused(m_InputToForgetWeightsTensor); |
| 195 | FreeTensorIfUnused(m_InputToCellWeightsTensor); |
| 196 | FreeTensorIfUnused(m_InputToOutputWeightsTensor); |
| 197 | FreeTensorIfUnused(m_RecurrentToInputWeightsTensor); |
| 198 | FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor); |
| 199 | FreeTensorIfUnused(m_RecurrentToCellWeightsTensor); |
| 200 | FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor); |
| 201 | FreeTensorIfUnused(m_InputGateBiasTensor); |
| 202 | FreeTensorIfUnused(m_ForgetGateBiasTensor); |
| 203 | FreeTensorIfUnused(m_CellBiasTensor); |
| 204 | FreeTensorIfUnused(m_OutputGateBiasTensor); |
| 205 | FreeTensorIfUnused(m_CellStateInTensor); |
| 206 | FreeTensorIfUnused(m_OutputStateInTensor); |
| 207 | FreeTensorIfUnused(m_CellStateOutTensor); |
| 208 | } |
| 209 | |
| 210 | } //namespace armnn |