blob: 688ebf918464b3bf2ea8aed039dfdd6814375e3c [file] [log] [blame]
Ferran Balaguer737d9ff2019-08-01 09:58:08 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ClQuantizedLstmWorkload.hpp"
7#include "ClWorkloadUtils.hpp"
8
9#include <backendsCommon/CpuTensorHandle.hpp>
10#include <aclCommon/ArmComputeTensorUtils.hpp>
11#include <cl/ClTensorHandle.hpp>
12
13namespace armnn
14{
15
16using namespace armcomputetensorutils;
17
18arm_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 Murtaghbb590b42019-08-14 09:51:36 +010034 = BuildArmComputeTensorInfo(paramsInfo.GetInputToInputWeights());
Ferran Balaguer737d9ff2019-08-01 09:58:08 +010035 const arm_compute::TensorInfo aclInputToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +010036 = BuildArmComputeTensorInfo(paramsInfo.GetInputToForgetWeights());
Ferran Balaguer737d9ff2019-08-01 09:58:08 +010037 const arm_compute::TensorInfo aclInputToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +010038 = BuildArmComputeTensorInfo(paramsInfo.GetInputToCellWeights());
Ferran Balaguer737d9ff2019-08-01 09:58:08 +010039 const arm_compute::TensorInfo aclInputToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +010040 = BuildArmComputeTensorInfo(paramsInfo.GetInputToOutputWeights());
Ferran Balaguer737d9ff2019-08-01 09:58:08 +010041 const arm_compute::TensorInfo aclRecurrentToInputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +010042 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToInputWeights());
Ferran Balaguer737d9ff2019-08-01 09:58:08 +010043 const arm_compute::TensorInfo aclRecurrentToForgetWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +010044 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToForgetWeights());
Ferran Balaguer737d9ff2019-08-01 09:58:08 +010045 const arm_compute::TensorInfo aclRecurrentToCellWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +010046 = BuildArmComputeTensorInfo(paramsInfo.GetRecurrentToCellWeights());
Ferran Balaguer737d9ff2019-08-01 09:58:08 +010047 const arm_compute::TensorInfo aclRecurrentToOutputWeightsInfo
Francis Murtaghbb590b42019-08-14 09:51:36 +010048 = 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 Balaguer737d9ff2019-08-01 09:58:08 +010053
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
64ClQuantizedLstmWorkload::ClQuantizedLstmWorkload(const QuantizedLstmQueueDescriptor &descriptor,
65 const WorkloadInfo &info):
66 BaseWorkload<QuantizedLstmQueueDescriptor>(descriptor, info)
67{
68 m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
69 BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo());
70
71 m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
72 BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo());
73
74 m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
75 BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo());
76
77 m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
78 BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo());
79
80 m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
81 BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo());
82
83 m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
84 BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
85
86 m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
87 BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo());
88
89 m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
90 BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
91
92 m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
93 BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo());
94
95 m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
96 BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo());
97
98 m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
99 BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo());
100
101 m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
102 BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo());
103
104 const arm_compute::ICLTensor& inputTensor = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
105 arm_compute::ICLTensor& cellStateInTensor = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
106 const arm_compute::ICLTensor& outputStateInTensor = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
107
108 arm_compute::ICLTensor& cellStateOutTensor = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
109 arm_compute::ICLTensor& outputStateOutTensor = static_cast<IClTensorHandle*>(m_Data.m_Outputs[1])->GetTensor();
110
111 m_QuantizedLstmLayer.configure(&inputTensor, m_InputToInputWeightsTensor.get(), m_InputToForgetWeightsTensor.get(),
112 m_InputToCellWeightsTensor.get(), m_InputToOutputWeightsTensor.get(),
113 m_RecurrentToInputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
114 m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
115 m_InputGateBiasTensor.get(), m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(),
116 m_OutputGateBiasTensor.get(), &cellStateInTensor, &outputStateInTensor,
117 &cellStateOutTensor, &outputStateOutTensor);
118
119 InitializeArmComputeClTensorData(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights);
120 InitializeArmComputeClTensorData(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights);
121 InitializeArmComputeClTensorData(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights);
122 InitializeArmComputeClTensorData(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights);
123 InitializeArmComputeClTensorData(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights);
124 InitializeArmComputeClTensorData(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights);
125 InitializeArmComputeClTensorData(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights);
126 InitializeArmComputeClTensorData(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights);
127 InitializeArmComputeClTensorData(*m_InputGateBiasTensor, m_Data.m_InputGateBias);
128 InitializeArmComputeClTensorData(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias);
129 InitializeArmComputeClTensorData(*m_CellBiasTensor, m_Data.m_CellBias);
130 InitializeArmComputeClTensorData(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias);
131
132 m_QuantizedLstmLayer.prepare();
133 FreeUnusedTensors();
134}
135
136void ClQuantizedLstmWorkload::Execute() const
137{
138 ARMNN_SCOPED_PROFILING_EVENT_CL("ClQuantizedLstmWorkload_Execute");
139 RunClFunction(m_QuantizedLstmLayer, CHECK_LOCATION());
140}
141
142void ClQuantizedLstmWorkload::FreeUnusedTensors()
143{
144 FreeTensorIfUnused(m_InputToInputWeightsTensor);
145 FreeTensorIfUnused(m_InputToForgetWeightsTensor);
146 FreeTensorIfUnused(m_InputToCellWeightsTensor);
147 FreeTensorIfUnused(m_InputToOutputWeightsTensor);
148 FreeTensorIfUnused(m_RecurrentToInputWeightsTensor);
149 FreeTensorIfUnused(m_RecurrentToForgetWeightsTensor);
150 FreeTensorIfUnused(m_RecurrentToCellWeightsTensor);
151 FreeTensorIfUnused(m_RecurrentToOutputWeightsTensor);
152 FreeTensorIfUnused(m_InputGateBiasTensor);
153 FreeTensorIfUnused(m_ForgetGateBiasTensor);
154 FreeTensorIfUnused(m_CellBiasTensor);
155 FreeTensorIfUnused(m_OutputGateBiasTensor);
156}
157
158} // namespace armnn