ArmNN
 21.02
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, const arm_compute::CLCompileContext &clCompileContext)
 
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,
const arm_compute::CLCompileContext &  clCompileContext 
)

Definition at line 22 of file ClLstmFloatWorkload.cpp.

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

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

256 {
257  ARMNN_SCOPED_PROFILING_EVENT_CL("ClLstmFloatWorkload_Execute");
258  RunClFunction(m_LstmLayer, CHECK_LOCATION());
259 }
#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: