Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClQuantizedLstmWorkload.hpp" |
| 7 | #include "ClWorkloadUtils.hpp" |
| 8 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 9 | #include <backendsCommon/TensorHandle.hpp> |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 10 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 11 | #include <cl/ClTensorHandle.hpp> |
| 12 | |
| 13 | namespace armnn |
| 14 | { |
| 15 | |
| 16 | using namespace armcomputetensorutils; |
| 17 | |
| 18 | arm_compute::Status ClQuantizedLstmWorkloadValidate(const TensorInfo& input, const TensorInfo& previousCellStateIn, |
| 19 | const TensorInfo& previousOutputIn, const TensorInfo& cellStateOut, |
| 20 | const TensorInfo& output, |
| 21 | const QuantizedLstmInputParamsInfo& paramsInfo) |
| 22 | { |
| 23 | // Inputs |
| 24 | const arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input); |
| 25 | const arm_compute::TensorInfo aclPreviousCellStateInInfo = BuildArmComputeTensorInfo(previousCellStateIn); |
| 26 | const arm_compute::TensorInfo aclPreviousOutputInInfo = BuildArmComputeTensorInfo(previousOutputIn); |
| 27 | |
| 28 | // Outputs |
| 29 | const arm_compute::TensorInfo aclCellStateOutInfo = BuildArmComputeTensorInfo(cellStateOut); |
| 30 | const arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output); |
| 31 | |
| 32 | // Basic parameters |
| 33 | const arm_compute::TensorInfo aclInputToInputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 34 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToInputWeights()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 35 | const arm_compute::TensorInfo aclInputToForgetWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 36 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToForgetWeights()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 37 | const arm_compute::TensorInfo aclInputToCellWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 38 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToCellWeights()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 39 | const arm_compute::TensorInfo aclInputToOutputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 40 | = BuildArmComputeTensorInfo(paramsInfo.GetInputToOutputWeights()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 41 | const arm_compute::TensorInfo aclRecurrentToInputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 42 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToInputWeights()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 43 | const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 44 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToForgetWeights()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 45 | const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 46 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToCellWeights()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 47 | const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 48 | = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToOutputWeights()); |
| 49 | const arm_compute::TensorInfo aclInputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetInputGateBias()); |
| 50 | const arm_compute::TensorInfo aclForgetGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetForgetGateBias()); |
| 51 | const arm_compute::TensorInfo aclCellBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetCellBias()); |
| 52 | const arm_compute::TensorInfo aclOutputGateBiasInfo = BuildArmComputeTensorInfo(paramsInfo.GetOutputGateBias()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 53 | |
| 54 | return arm_compute::CLLSTMLayerQuantized::validate(&aclInputInfo, &aclInputToInputWeightsInfo, |
| 55 | &aclInputToForgetWeightsInfo, &aclInputToCellWeightsInfo, |
| 56 | &aclInputToOutputWeightsInfo, &aclRecurrentToInputWeightsInfo, |
| 57 | &aclRecurrentToForgetWeightsInfo, &aclRecurrentToCellWeightsInfo, |
| 58 | &aclRecurrentToOutputWeightsInfo, &aclInputGateBiasInfo, |
| 59 | &aclForgetGateBiasInfo, &aclCellBiasInfo, &aclOutputGateBiasInfo, |
| 60 | &aclPreviousCellStateInInfo, &aclPreviousOutputInInfo, |
| 61 | &aclCellStateOutInfo, &aclOutputInfo); |
| 62 | } |
| 63 | |
| 64 | ClQuantizedLstmWorkload::ClQuantizedLstmWorkload(const QuantizedLstmQueueDescriptor &descriptor, |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 65 | const WorkloadInfo &info, |
| 66 | const arm_compute::CLCompileContext& clCompileContext): |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 67 | BaseWorkload<QuantizedLstmQueueDescriptor>(descriptor, info) |
| 68 | { |
| 69 | m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 70 | BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo()); |
| 71 | |
| 72 | m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 73 | BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo()); |
| 74 | |
| 75 | m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 76 | BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo()); |
| 77 | |
| 78 | m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 79 | BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo()); |
| 80 | |
| 81 | m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 82 | BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo()); |
| 83 | |
| 84 | m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 85 | BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo()); |
| 86 | |
| 87 | m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 88 | BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo()); |
| 89 | |
| 90 | m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>(); |
| 91 | BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo()); |
| 92 | |
| 93 | m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>(); |
| 94 | BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo()); |
| 95 | |
| 96 | m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>(); |
| 97 | BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo()); |
| 98 | |
| 99 | m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>(); |
| 100 | BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo()); |
| 101 | |
| 102 | m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>(); |
| 103 | BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo()); |
| 104 | |
| 105 | const arm_compute::ICLTensor& inputTensor = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
| 106 | arm_compute::ICLTensor& cellStateInTensor = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor(); |
| 107 | const arm_compute::ICLTensor& outputStateInTensor = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor(); |
| 108 | |
| 109 | arm_compute::ICLTensor& cellStateOutTensor = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
| 110 | arm_compute::ICLTensor& outputStateOutTensor = static_cast<IClTensorHandle*>(m_Data.m_Outputs[1])->GetTensor(); |
| 111 | |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 112 | m_QuantizedLstmLayer.configure(clCompileContext, &inputTensor, m_InputToInputWeightsTensor.get(), |
| 113 | m_InputToForgetWeightsTensor.get(), |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 114 | m_InputToCellWeightsTensor.get(), m_InputToOutputWeightsTensor.get(), |
| 115 | m_RecurrentToInputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(), |
| 116 | m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(), |
| 117 | m_InputGateBiasTensor.get(), m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), |
| 118 | m_OutputGateBiasTensor.get(), &cellStateInTensor, &outputStateInTensor, |
| 119 | &cellStateOutTensor, &outputStateOutTensor); |
| 120 | |
| 121 | InitializeArmComputeClTensorData(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights); |
| 122 | InitializeArmComputeClTensorData(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights); |
| 123 | InitializeArmComputeClTensorData(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights); |
| 124 | InitializeArmComputeClTensorData(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights); |
| 125 | InitializeArmComputeClTensorData(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights); |
| 126 | InitializeArmComputeClTensorData(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights); |
| 127 | InitializeArmComputeClTensorData(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights); |
| 128 | InitializeArmComputeClTensorData(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights); |
| 129 | InitializeArmComputeClTensorData(*m_InputGateBiasTensor, m_Data.m_InputGateBias); |
| 130 | InitializeArmComputeClTensorData(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias); |
| 131 | InitializeArmComputeClTensorData(*m_CellBiasTensor, m_Data.m_CellBias); |
| 132 | InitializeArmComputeClTensorData(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias); |
| 133 | |
| 134 | m_QuantizedLstmLayer.prepare(); |
| 135 | FreeUnusedTensors(); |
| 136 | } |
| 137 | |
| 138 | void ClQuantizedLstmWorkload::Execute() const |
| 139 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 140 | ARMNN_SCOPED_PROFILING_EVENT_CL_GUID("ClQuantizedLstmWorkload_Execute", this->GetGuid()); |
Ferran Balaguer | 737d9ff | 2019-08-01 09:58:08 +0100 | [diff] [blame] | 141 | RunClFunction(m_QuantizedLstmLayer, CHECK_LOCATION()); |
| 142 | } |
| 143 | |
| 144 | void ClQuantizedLstmWorkload::FreeUnusedTensors() |
| 145 | { |
| 146 | FreeTensorIfUnused(m_InputToInputWeightsTensor); |
| 147 | FreeTensorIfUnused(m_InputToForgetWeightsTensor); |
| 148 | FreeTensorIfUnused(m_InputToCellWeightsTensor); |
| 149 | FreeTensorIfUnused(m_InputToOutputWeightsTensor); |
| 150 | FreeTensorIfUnused(m_RecurrentToInputWeightsTensor); |
| 151 | FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor); |
| 152 | FreeTensorIfUnused(m_RecurrentToCellWeightsTensor); |
| 153 | FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor); |
| 154 | FreeTensorIfUnused(m_InputGateBiasTensor); |
| 155 | FreeTensorIfUnused(m_ForgetGateBiasTensor); |
| 156 | FreeTensorIfUnused(m_CellBiasTensor); |
| 157 | FreeTensorIfUnused(m_OutputGateBiasTensor); |
| 158 | } |
| 159 | |
| 160 | } // namespace armnn |