blob: a923874a745d47b4275ffbf12420f8b86f4ea155 [file] [log] [blame]
Narumol Prangnawarat7684b182021-08-12 14:48:15 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "DelegateUtils.hpp"
9
10#include <armnn/LstmParams.hpp>
11#include <armnn/Tensor.hpp>
12#include <armnn/utility/IgnoreUnused.hpp>
13
14#include <tensorflow/lite/builtin_ops.h>
15#include <tensorflow/lite/c/builtin_op_data.h>
16#include <tensorflow/lite/c/common.h>
17#include <tensorflow/lite/minimal_logging.h>
18
19namespace armnnDelegate
20{
21
22TfLiteStatus VisitUnidirectionalSequenceLstmOperator(DelegateData& delegateData,
23 TfLiteContext* tfLiteContext,
24 TfLiteNode* tfLiteNode,
25 int nodeIndex,
26 int32_t operatorCode)
27{
28 auto numInputs = tfLiteNode->inputs->size;
29 if (numInputs < 2)
30 {
31 TF_LITE_MAYBE_KERNEL_LOG(
32 tfLiteContext, "TfLiteArmnnDelegate: Minimum number of inputs (%d != %d) in node #%d",
33 2, numInputs, nodeIndex);
34 return kTfLiteError;
35 }
36
37 const auto nodeParams = reinterpret_cast<TfLiteUnidirectionalSequenceLSTMParams *>(tfLiteNode->builtin_data);
38 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
39
40 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
41 if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex))
42 {
43 return kTfLiteError;
44 }
45
46 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
47 if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex))
48 {
49 return kTfLiteError;
50 }
51
52 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
53 // Please refer to each operand at
54 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
55 armnn::LstmInputParams params;
56
57 if (!IsOptionalOperandPresent(tfLiteNode, 1))
58 {
59 params.m_InputToInputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 1);
60 }
61
62 params.m_InputToForgetWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 2);
63 params.m_InputToCellWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 3);
64 params.m_InputToOutputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 4);
65
66 // Recurrent weight tensors of size {n_cell, n_output}
67 if (!IsOptionalOperandPresent(tfLiteNode, 5))
68 {
69 params.m_RecurrentToInputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 5);
70 }
71
72 params.m_RecurrentToForgetWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 6);
73 params.m_RecurrentToCellWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 7);
74 params.m_RecurrentToOutputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 8);
75
76 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
77 if (!IsOptionalOperandPresent(tfLiteNode, 9))
78 {
79 params.m_CellToInputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 9);
80 }
81
82 if (!IsOptionalOperandPresent(tfLiteNode, 10))
83 {
84 params.m_CellToForgetWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 10);
85 }
86
87 if (!IsOptionalOperandPresent(tfLiteNode, 11))
88 {
89 params.m_CellToOutputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 11);
90 }
91
92 // Gates bias tensors of size {n_cell}
93 if (!IsOptionalOperandPresent(tfLiteNode, 12))
94 {
95 params.m_InputGateBias = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 12);
96 }
97
98 params.m_ForgetGateBias = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 13);
99 params.m_CellBias = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 14);
100 params.m_OutputGateBias = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 15);
101
102 // Projection weight tensor of size {n_output, n_cell}
103 if (!IsOptionalOperandPresent(tfLiteNode, 16))
104 {
105 params.m_ProjectionWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 16);
106 }
107 // Projection bias tensor of size {n_output}
108 if (!IsOptionalOperandPresent(tfLiteNode, 17))
109 {
110 params.m_ProjectionBias = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 17);
111 }
112
113 // These state tensors are defined as variable tensors, and will be modified by this op.
114 armnn::TensorInfo outputStateInInfo = GetTensorInfoForTfLiteTensor(tfLiteTensors[tfLiteNode->inputs->data[18]]);
115 armnn::TensorInfo cellStateInInfo = GetTensorInfoForTfLiteTensor(tfLiteTensors[tfLiteNode->inputs->data[19]]);
116
117 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
118 if (tfLiteNode->inputs->size >= 21 && !IsOptionalOperandPresent(tfLiteNode, 20))
119 {
120 params.m_InputLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 20);
121 }
122
123 if (tfLiteNode->inputs->size >= 22 && !IsOptionalOperandPresent(tfLiteNode, 21))
124 {
125 params.m_ForgetLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 21);
126 }
127
128 if (tfLiteNode->inputs->size >= 23 && !IsOptionalOperandPresent(tfLiteNode, 22))
129 {
130 params.m_CellLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 22);
131 }
132
133 if (tfLiteNode->inputs->size >= 24 && !IsOptionalOperandPresent(tfLiteNode, 23))
134 {
135 params.m_OutputLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 23);
136 }
137
138 // set the layer descriptor
139 armnn::UnidirectionalSequenceLstmDescriptor desc;
140 desc.m_ActivationFunc = NonNegative(nodeParams->activation, nodeIndex);
141 desc.m_ClippingThresCell = nodeParams->cell_clip;
142 desc.m_ClippingThresProj = nodeParams->proj_clip;
143 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
144 || params.m_RecurrentToInputWeights == nullptr
145 || params.m_InputGateBias == nullptr);
146 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
147 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
148 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
149 || params.m_ForgetLayerNormWeights != nullptr
150 || params.m_CellLayerNormWeights != nullptr
151 || params.m_OutputLayerNormWeights != nullptr);
152 desc.m_TimeMajor = nodeParams->time_major;
153
154 const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
155 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor);
156
157 unsigned int batchSize = inputTensorInfo.GetShape()[0];
158 unsigned int outputSize = outputTensorInfo.GetShape()[2];
159 unsigned int numUnits = cellStateInInfo.GetShape()[1];
160
161 armnn::DataType dataType = inputTensorInfo.GetDataType();
162 float qScale = inputTensorInfo.GetQuantizationScale();
163 float qOffset = inputTensorInfo.GetQuantizationOffset();
164
165 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
166 if (!desc.m_CifgEnabled)
167 {
168 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
169 }
170 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits}, dataType, qScale, qOffset);
171 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
172
173 armnn::LstmInputParamsInfo paramsInfo;
174 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
175 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
176 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
177 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
178 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
179 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
180 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
181 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
182 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
183
184 if (!desc.m_CifgEnabled)
185 {
186 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
187 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
188 if (params.m_CellToInputWeights != nullptr)
189 {
190 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
191 }
192 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
193 }
194
195 if (desc.m_ProjectionEnabled)
196 {
197 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
198 if (params.m_ProjectionBias != nullptr)
199 {
200 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
201 }
202 }
203
204 if (desc.m_PeepholeEnabled)
205 {
206 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
207 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
208 }
209
210 if (desc.m_LayerNormEnabled)
211 {
212 if(!desc.m_CifgEnabled)
213 {
214 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
215 }
216 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
217 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
218 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
219 }
220
221 // hiddenStateOutput and cellStateOutput do not present in TfLite UnidirectionalSequenceLstm
222 armnn::Optional<armnn::TensorInfo> optionalTensor;
223
224 bool isSupported = false;
225 auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported)
226 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000227 FORWARD_LAYER_SUPPORT_FUNC("UNIDIRECTIONAL_SEQUENCE_LSTM",
Narumol Prangnawarat7684b182021-08-12 14:48:15 +0100228 tfLiteContext,
229 IsUnidirectionalSequenceLstmSupported,
230 delegateData.m_Backends,
231 isSupported,
232 inputTensorInfo,
233 outputStateInInfo,
234 cellStateInInfo,
235 outputInfo,
236 optionalTensor,
237 optionalTensor,
238 desc,
239 paramsInfo);
240 };
241
242 if (!delegateData.m_Network)
243 {
244 validateFunc(outputTensorInfo, isSupported);
245 return isSupported ? kTfLiteOk : kTfLiteError;
246 }
247
248 armnn::IConnectableLayer* layer = delegateData.m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
249 ARMNN_ASSERT(layer != nullptr);
250
251 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
252
253 // Connect the inputs
254 // input_layer
255 delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[0]]->Connect(layer->GetInputSlot(0));
256 // cellStateIn
257 delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[18]]->Connect(layer->GetInputSlot(1));
258 //outputStateIn
259 delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[19]]->Connect(layer->GetInputSlot(2));
260
261 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
262 delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tfLiteNode->outputs->data[0])] = &outputSlot;
263 return kTfLiteOk;
264}
265
266} // namespace armnnDelegate