ArmNN
 21.02
NeonLstmFloatWorkload Class Reference

#include <NeonLstmFloatWorkload.hpp>

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

Public Member Functions

 NeonLstmFloatWorkload (const LstmQueueDescriptor &descriptor, const WorkloadInfo &info)
 
virtual 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 19 of file NeonLstmFloatWorkload.hpp.

Constructor & Destructor Documentation

◆ NeonLstmFloatWorkload()

NeonLstmFloatWorkload ( const LstmQueueDescriptor descriptor,
const WorkloadInfo info 
)

Definition at line 19 of file NeonLstmFloatWorkload.cpp.

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

269 {
270  m_LstmLayer.run();
271 }

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