ArmNN
 20.11
ClLstmFloatWorkload Class Reference

#include <ClLstmFloatWorkload.hpp>

Inheritance diagram for ClLstmFloatWorkload:
TypedWorkload< QueueDescriptor, DataTypes > BaseWorkload< QueueDescriptor > IWorkload

Public Member Functions

 ClLstmFloatWorkload (const LstmQueueDescriptor &descriptor, const WorkloadInfo &info)
 
void Execute () const override
 
- Public Member Functions inherited from TypedWorkload< QueueDescriptor, DataTypes >
 TypedWorkload (const QueueDescriptor &descriptor, const WorkloadInfo &info)
 
- Public Member Functions inherited from BaseWorkload< QueueDescriptor >
 BaseWorkload (const QueueDescriptor &descriptor, const WorkloadInfo &info)
 
void PostAllocationConfigure () override
 
const QueueDescriptorGetData () const
 
profiling::ProfilingGuid GetGuid () const final
 
- Public Member Functions inherited from IWorkload
virtual ~IWorkload ()
 
virtual void RegisterDebugCallback (const DebugCallbackFunction &)
 

Additional Inherited Members

- Protected Attributes inherited from BaseWorkload< QueueDescriptor >
const QueueDescriptor m_Data
 
const profiling::ProfilingGuid m_Guid
 

Detailed Description

Definition at line 18 of file ClLstmFloatWorkload.hpp.

Constructor & Destructor Documentation

◆ ClLstmFloatWorkload()

ClLstmFloatWorkload ( const LstmQueueDescriptor descriptor,
const WorkloadInfo info 
)

Definition at line 22 of file ClLstmFloatWorkload.cpp.

23  : FloatWorkload<LstmQueueDescriptor>(descriptor, info)
24 {
25  arm_compute::LSTMParams<arm_compute::ICLTensor> lstm_param;
26 
27  // Basic parameters
28  m_InputToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
29  BuildArmComputeTensor(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights->GetTensorInfo());
30 
31  m_InputToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
32  BuildArmComputeTensor(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights->GetTensorInfo());
33 
34  m_InputToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
35  BuildArmComputeTensor(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights->GetTensorInfo());
36 
37  m_RecurrentToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
38  BuildArmComputeTensor(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights->GetTensorInfo());
39 
40  m_RecurrentToCellWeightsTensor = std::make_unique<arm_compute::CLTensor>();
41  BuildArmComputeTensor(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights->GetTensorInfo());
42 
43  m_RecurrentToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
44  BuildArmComputeTensor(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights->GetTensorInfo());
45 
46  m_ForgetGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
47  BuildArmComputeTensor(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias->GetTensorInfo());
48 
49  m_CellBiasTensor = std::make_unique<arm_compute::CLTensor>();
50  BuildArmComputeTensor(*m_CellBiasTensor, m_Data.m_CellBias->GetTensorInfo());
51 
52  m_OutputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
53  BuildArmComputeTensor(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias->GetTensorInfo());
54 
55  // for future reference: check the AndroidNN API for the logic here
56  if (!m_Data.m_Parameters.m_CifgEnabled)
57  {
58  m_InputToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
59  BuildArmComputeTensor(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights->GetTensorInfo());
60 
61  m_RecurrentToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
62  BuildArmComputeTensor(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights->GetTensorInfo());
63 
64  m_CellToInputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
65  if (m_Data.m_CellToInputWeights != nullptr)
66  {
67  BuildArmComputeTensor(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights->GetTensorInfo());
68  }
69 
70  m_InputGateBiasTensor = std::make_unique<arm_compute::CLTensor>();
71  BuildArmComputeTensor(*m_InputGateBiasTensor, m_Data.m_InputGateBias->GetTensorInfo());
72 
73  lstm_param.set_cifg_params(m_InputToInputWeightsTensor.get(),
74  m_RecurrentToInputWeightsTensor.get(),
75  m_Data.m_CellToInputWeights != nullptr ? m_CellToInputWeightsTensor.get() : nullptr,
76  m_InputGateBiasTensor.get());
77  }
78 
79  if (m_Data.m_Parameters.m_ProjectionEnabled)
80  {
81  m_ProjectionWeightsTensor = std::make_unique<arm_compute::CLTensor>();
82  BuildArmComputeTensor(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights->GetTensorInfo());
83 
84  m_ProjectionBiasTensor = std::make_unique<arm_compute::CLTensor>();
85  if (m_Data.m_ProjectionBias != nullptr)
86  {
87  BuildArmComputeTensor(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias->GetTensorInfo());
88  }
89 
90  lstm_param.set_projection_params(m_ProjectionWeightsTensor.get(),
91  m_Data.m_ProjectionBias != nullptr ? m_ProjectionBiasTensor.get() : nullptr);
92  }
93 
94  if (m_Data.m_Parameters.m_PeepholeEnabled)
95  {
96  m_CellToForgetWeightsTensor = std::make_unique<arm_compute::CLTensor>();
97  BuildArmComputeTensor(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights->GetTensorInfo());
98 
99  m_CellToOutputWeightsTensor = std::make_unique<arm_compute::CLTensor>();
100  BuildArmComputeTensor(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights->GetTensorInfo());
101 
102  lstm_param.set_peephole_params(m_CellToForgetWeightsTensor.get(), m_CellToOutputWeightsTensor.get());
103  }
104 
105  if (m_Data.m_Parameters.m_LayerNormEnabled)
106  {
107  m_InputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
108  m_ForgetLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
109  m_CellLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
110  m_OutputLayerNormWeightsTensor = std::make_unique<arm_compute::CLTensor>();
111 
112  if (!m_Data.m_Parameters.m_CifgEnabled)
113  {
114  BuildArmComputeTensor(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights->GetTensorInfo());
115  }
116  BuildArmComputeTensor(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights->GetTensorInfo());
117  BuildArmComputeTensor(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights->GetTensorInfo());
118  BuildArmComputeTensor(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights->GetTensorInfo());
119 
120  lstm_param.set_layer_normalization_params(m_Data.m_Parameters.m_CifgEnabled ? nullptr :
121  m_InputLayerNormWeightsTensor.get(),
122  m_ForgetLayerNormWeightsTensor.get(),
123  m_CellLayerNormWeightsTensor.get(),
124  m_OutputLayerNormWeightsTensor.get());
125  }
126 
127  const arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
128  const arm_compute::ICLTensor& output_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[1])->GetTensor();
129  arm_compute::ICLTensor& cell_state_in = static_cast<IClTensorHandle*>(m_Data.m_Inputs[2])->GetTensor();
130 
131  arm_compute::ICLTensor& output_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[1])->GetTensor();
132  arm_compute::ICLTensor& cell_state_out = static_cast<IClTensorHandle*>(m_Data.m_Outputs[2])->GetTensor();
133  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[3])->GetTensor();
134 
135  // Get the batch_size and the num_units from the cellStateIn dimensions
136  const TensorInfo& inputTensorInfo = info.m_InputTensorInfos[2];
137  const unsigned int batch_size = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[0]);
138  const unsigned int num_units = armnn::numeric_cast<unsigned int>(inputTensorInfo.GetShape()[1]);
139 
140  m_ScratchBuffer = std::make_unique<arm_compute::CLTensor>();
141  if (m_Data.m_Parameters.m_CifgEnabled)
142  {
143  // 2D tensor with dimensions [num_units * 3, batch_size] with CIFG
144  armnn::TensorInfo scratchBuffer1({ batch_size, num_units * 3 }, DataType::Float32);
145  BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
146  }
147  else
148  {
149  // scratch_buffer [num_units * 4, batch_size] without CIFG
150  armnn::TensorInfo scratchBuffer2({ batch_size, num_units * 4 }, DataType::Float32);
151  BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer2);
152  }
153 
154  float cell_threshold = m_Data.m_Parameters.m_ClippingThresCell;
155  float projection_threshold = m_Data.m_Parameters.m_ClippingThresProj;
156 
157  // for preparing the object for the class ActivationLayerInfo, we need to consider 5 situations
158  arm_compute::ActivationLayerInfo activationLayerInfo;
159  if (m_Data.m_Parameters.m_ActivationFunc == 0)
160  {
161  // no activation, do nothing
162  }
163  else if (m_Data.m_Parameters.m_ActivationFunc == 1)
164  {
165  activationLayerInfo = arm_compute::ActivationLayerInfo(
166  arm_compute::ActivationLayerInfo::ActivationFunction::RELU);
167  }
168  else if (m_Data.m_Parameters.m_ActivationFunc == 3)
169  {
170  activationLayerInfo = arm_compute::ActivationLayerInfo(
171  arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.0);
172  }
173  else if (m_Data.m_Parameters.m_ActivationFunc == 4)
174  {
175  activationLayerInfo = arm_compute::ActivationLayerInfo(
176  arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0, 1.0);
177  }
178  else if (m_Data.m_Parameters.m_ActivationFunc == 6)
179  {
180  activationLayerInfo = arm_compute::ActivationLayerInfo(
181  arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC);
182  }
183  else
184  {
185  throw armnn::Exception("Wrong Type of Activation Function!");
186  }
187 
188  m_LstmLayer.configure(&input, m_InputToForgetWeightsTensor.get(), m_InputToCellWeightsTensor.get(),
189  m_InputToOutputWeightsTensor.get(), m_RecurrentToForgetWeightsTensor.get(),
190  m_RecurrentToCellWeightsTensor.get(), m_RecurrentToOutputWeightsTensor.get(),
191  m_ForgetGateBiasTensor.get(), m_CellBiasTensor.get(), m_OutputGateBiasTensor.get(),
192  &output_state_in, &cell_state_in, m_ScratchBuffer.get(), &output_state_out,
193  &cell_state_out, &output, lstm_param, activationLayerInfo,
194  cell_threshold, projection_threshold);
195 
196  armcomputetensorutils::InitialiseArmComputeTensorEmpty(*m_ScratchBuffer);
197 
198  InitializeArmComputeClTensorData(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights);
199  InitializeArmComputeClTensorData(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights);
200  InitializeArmComputeClTensorData(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights);
201  InitializeArmComputeClTensorData(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights);
202  InitializeArmComputeClTensorData(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights);
203  InitializeArmComputeClTensorData(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights);
204  InitializeArmComputeClTensorData(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias);
205  InitializeArmComputeClTensorData(*m_CellBiasTensor, m_Data.m_CellBias);
206  InitializeArmComputeClTensorData(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias);
207 
208  if (!m_Data.m_Parameters.m_CifgEnabled)
209  {
210  InitializeArmComputeClTensorData(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights);
211  InitializeArmComputeClTensorData(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights);
212  if (m_Data.m_CellToInputWeights != nullptr)
213  {
214  InitializeArmComputeClTensorData(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights);
215  }
216  InitializeArmComputeClTensorData(*m_InputGateBiasTensor, m_Data.m_InputGateBias);
217  }
218 
219  if (m_Data.m_Parameters.m_ProjectionEnabled)
220  {
221  InitializeArmComputeClTensorData(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights);
222  if (m_Data.m_ProjectionBias != nullptr)
223  {
224  InitializeArmComputeClTensorData(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias);
225  }
226  }
227 
228  if (m_Data.m_Parameters.m_PeepholeEnabled)
229  {
230  InitializeArmComputeClTensorData(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights);
231  InitializeArmComputeClTensorData(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights);
232  }
233 
234  if (m_Data.m_Parameters.m_LayerNormEnabled)
235  {
236  if (!m_Data.m_Parameters.m_CifgEnabled)
237  {
238  InitializeArmComputeClTensorData(*m_InputLayerNormWeightsTensor, m_Data.m_InputLayerNormWeights);
239  }
240 
241  InitializeArmComputeClTensorData(*m_ForgetLayerNormWeightsTensor, m_Data.m_ForgetLayerNormWeights);
242  InitializeArmComputeClTensorData(*m_CellLayerNormWeightsTensor, m_Data.m_CellLayerNormWeights);
243  InitializeArmComputeClTensorData(*m_OutputLayerNormWeightsTensor, m_Data.m_OutputLayerNormWeights);
244  }
245 
246  // Force Compute Library to perform the necessary copying and reshaping, after which
247  // delete all the input tensors that will no longer be needed
248  m_LstmLayer.prepare();
249  FreeUnusedTensors();
250 }
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstCpuTensorHandle *handle)
const QueueDescriptor m_Data
Definition: Workload.hpp:46
std::vector< ITensorHandle * > m_Outputs
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
std::vector< ITensorHandle * > m_Inputs

Member Function Documentation

◆ Execute()

void Execute ( ) const
overridevirtual

Implements IWorkload.

Definition at line 252 of file ClLstmFloatWorkload.cpp.

References ARMNN_SCOPED_PROFILING_EVENT_CL, CHECK_LOCATION, and armnn::RunClFunction().

253 {
254  ARMNN_SCOPED_PROFILING_EVENT_CL("ClLstmFloatWorkload_Execute");
255  RunClFunction(m_LstmLayer, CHECK_LOCATION());
256 }
#define ARMNN_SCOPED_PROFILING_EVENT_CL(name)
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197

The documentation for this class was generated from the following files: