ArmNN
 20.05
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 20 of file ClLstmFloatWorkload.cpp.

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 
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 
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  {
141  // 2D tensor with dimensions [num_units * 3, batch_size] with CIFG
142  armnn::TensorInfo scratchBuffer1({ batch_size, num_units * 3 }, DataType::Float32);
143  BuildArmComputeTensor(*m_ScratchBuffer, scratchBuffer1);
144  }
145  else
146  {
147  // scratch_buffer [num_units * 4, batch_size] without CIFG
148  armnn::TensorInfo scratchBuffer2({ batch_size, num_units * 4 }, DataType::Float32);
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 
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 
196  InitializeArmComputeClTensorData(*m_InputToForgetWeightsTensor, m_Data.m_InputToForgetWeights);
197  InitializeArmComputeClTensorData(*m_InputToCellWeightsTensor, m_Data.m_InputToCellWeights);
198  InitializeArmComputeClTensorData(*m_InputToOutputWeightsTensor, m_Data.m_InputToOutputWeights);
199  InitializeArmComputeClTensorData(*m_RecurrentToForgetWeightsTensor, m_Data.m_RecurrentToForgetWeights);
200  InitializeArmComputeClTensorData(*m_RecurrentToCellWeightsTensor, m_Data.m_RecurrentToCellWeights);
201  InitializeArmComputeClTensorData(*m_RecurrentToOutputWeightsTensor, m_Data.m_RecurrentToOutputWeights);
202  InitializeArmComputeClTensorData(*m_ForgetGateBiasTensor, m_Data.m_ForgetGateBias);
203  InitializeArmComputeClTensorData(*m_CellBiasTensor, m_Data.m_CellBias);
204  InitializeArmComputeClTensorData(*m_OutputGateBiasTensor, m_Data.m_OutputGateBias);
205 
206  if (!m_Data.m_Parameters.m_CifgEnabled)
207  {
208  InitializeArmComputeClTensorData(*m_InputToInputWeightsTensor, m_Data.m_InputToInputWeights);
209  InitializeArmComputeClTensorData(*m_RecurrentToInputWeightsTensor, m_Data.m_RecurrentToInputWeights);
210  if (m_Data.m_CellToInputWeights != nullptr)
211  {
212  InitializeArmComputeClTensorData(*m_CellToInputWeightsTensor, m_Data.m_CellToInputWeights);
213  }
214  InitializeArmComputeClTensorData(*m_InputGateBiasTensor, m_Data.m_InputGateBias);
215  }
216 
217  if (m_Data.m_Parameters.m_ProjectionEnabled)
218  {
219  InitializeArmComputeClTensorData(*m_ProjectionWeightsTensor, m_Data.m_ProjectionWeights);
220  if (m_Data.m_ProjectionBias != nullptr)
221  {
222  InitializeArmComputeClTensorData(*m_ProjectionBiasTensor, m_Data.m_ProjectionBias);
223  }
224  }
225 
226  if (m_Data.m_Parameters.m_PeepholeEnabled)
227  {
228  InitializeArmComputeClTensorData(*m_CellToForgetWeightsTensor, m_Data.m_CellToForgetWeights);
229  InitializeArmComputeClTensorData(*m_CellToOutputWeightsTensor, m_Data.m_CellToOutputWeights);
230  }
231 
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 
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 }
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstCpuTensorHandle *handle)
const QueueDescriptor m_Data
Definition: Workload.hpp:46
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
std::vector< ITensorHandle * > m_Outputs
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
std::vector< ITensorHandle * > m_Inputs

Member Function Documentation

◆ Execute()

void Execute ( ) const
overridevirtual

Implements IWorkload.

Definition at line 250 of file ClLstmFloatWorkload.cpp.

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

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

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