Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 1 | // |
| 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 | |
| 19 | namespace armnnDelegate |
| 20 | { |
| 21 | |
| 22 | TfLiteStatus 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 | |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 57 | if (IsOptionalOperandPresent(tfLiteNode, 1)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 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} |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 67 | if (IsOptionalOperandPresent(tfLiteNode, 5)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 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. |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 77 | if (IsOptionalOperandPresent(tfLiteNode, 9)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 78 | { |
| 79 | params.m_CellToInputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 9); |
| 80 | } |
| 81 | |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 82 | if (IsOptionalOperandPresent(tfLiteNode, 10)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 83 | { |
| 84 | params.m_CellToForgetWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 10); |
| 85 | } |
| 86 | |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 87 | if (IsOptionalOperandPresent(tfLiteNode, 11)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 88 | { |
| 89 | params.m_CellToOutputWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 11); |
| 90 | } |
| 91 | |
| 92 | // Gates bias tensors of size {n_cell} |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 93 | if (IsOptionalOperandPresent(tfLiteNode, 12)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 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} |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 103 | if (IsOptionalOperandPresent(tfLiteNode, 16)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 104 | { |
| 105 | params.m_ProjectionWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 16); |
| 106 | } |
| 107 | // Projection bias tensor of size {n_output} |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 108 | if (IsOptionalOperandPresent(tfLiteNode, 17)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 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. |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 118 | if (IsOptionalOperandPresent(tfLiteNode, 20)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 119 | { |
| 120 | params.m_InputLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 20); |
| 121 | } |
| 122 | |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 123 | if (IsOptionalOperandPresent(tfLiteNode, 21)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 124 | { |
| 125 | params.m_ForgetLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 21); |
| 126 | } |
| 127 | |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 128 | if (IsOptionalOperandPresent(tfLiteNode, 22)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 129 | { |
| 130 | params.m_CellLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteTensors, tfLiteNode, 22); |
| 131 | } |
| 132 | |
Mike Kelly | 84d6378 | 2022-05-06 12:14:16 +0100 | [diff] [blame] | 133 | if (IsOptionalOperandPresent(tfLiteNode, 23)) |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 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 | |
Mike Kelly | 1299496 | 2022-04-21 11:57:09 +0100 | [diff] [blame] | 154 | if (tfLiteNode->intermediates->size > 3 && desc.m_LayerNormEnabled) |
| 155 | { |
| 156 | auto inputIntermediateTensorInfo = GetTensorInfoForTfLiteTensor( |
| 157 | tfLiteTensors[tfLiteNode->intermediates->data[0]]); |
| 158 | auto forgetIntermediateTensorInfo = GetTensorInfoForTfLiteTensor( |
| 159 | tfLiteTensors[tfLiteNode->intermediates->data[1]]); |
| 160 | auto cellIntermediateTensorInfo = GetTensorInfoForTfLiteTensor( |
| 161 | tfLiteTensors[tfLiteNode->intermediates->data[2]]); |
| 162 | auto outputIntermediateTensorInfo = GetTensorInfoForTfLiteTensor( |
| 163 | tfLiteTensors[tfLiteNode->intermediates->data[3]]); |
| 164 | |
| 165 | desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale(); |
| 166 | desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale(); |
| 167 | desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale(); |
| 168 | desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale(); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | float defaultIntermediate = std::pow(2, -12); |
| 173 | desc.m_InputIntermediateScale = defaultIntermediate; |
| 174 | desc.m_ForgetIntermediateScale = defaultIntermediate; |
| 175 | desc.m_CellIntermediateScale = defaultIntermediate; |
| 176 | desc.m_OutputIntermediateScale = defaultIntermediate; |
| 177 | } |
| 178 | if (tfLiteNode->intermediates->size > 4) |
| 179 | { |
| 180 | auto hiddentensorInfo = GetTensorInfoForTfLiteTensor(tfLiteTensors[tfLiteNode->intermediates->data[4]]); |
| 181 | desc.m_HiddenStateScale = hiddentensorInfo.GetQuantizationScale(); |
| 182 | desc.m_HiddenStateZeroPoint = hiddentensorInfo.GetQuantizationOffset(); |
| 183 | } |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 184 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
| 185 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); |
| 186 | |
| 187 | unsigned int batchSize = inputTensorInfo.GetShape()[0]; |
| 188 | unsigned int outputSize = outputTensorInfo.GetShape()[2]; |
| 189 | unsigned int numUnits = cellStateInInfo.GetShape()[1]; |
| 190 | |
| 191 | armnn::DataType dataType = inputTensorInfo.GetDataType(); |
| 192 | float qScale = inputTensorInfo.GetQuantizationScale(); |
| 193 | float qOffset = inputTensorInfo.GetQuantizationOffset(); |
| 194 | |
| 195 | armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset); |
| 196 | if (!desc.m_CifgEnabled) |
| 197 | { |
| 198 | scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset); |
| 199 | } |
Mike Kelly | 1299496 | 2022-04-21 11:57:09 +0100 | [diff] [blame] | 200 | armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits}, |
| 201 | cellStateInInfo.GetDataType(), |
| 202 | cellStateInInfo.GetQuantizationScale(), |
| 203 | cellStateInInfo.GetQuantizationOffset()); |
| 204 | |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 205 | armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset); |
| 206 | |
| 207 | armnn::LstmInputParamsInfo paramsInfo; |
| 208 | paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo()); |
| 209 | paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo()); |
| 210 | paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo()); |
| 211 | paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo()); |
| 212 | paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo()); |
| 213 | paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo()); |
| 214 | paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo()); |
| 215 | paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo()); |
| 216 | paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo()); |
| 217 | |
| 218 | if (!desc.m_CifgEnabled) |
| 219 | { |
| 220 | paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo()); |
| 221 | paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo()); |
| 222 | if (params.m_CellToInputWeights != nullptr) |
| 223 | { |
| 224 | paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo()); |
| 225 | } |
| 226 | paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo()); |
| 227 | } |
| 228 | |
| 229 | if (desc.m_ProjectionEnabled) |
| 230 | { |
| 231 | paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo()); |
| 232 | if (params.m_ProjectionBias != nullptr) |
| 233 | { |
| 234 | paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (desc.m_PeepholeEnabled) |
| 239 | { |
| 240 | paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo()); |
| 241 | paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo()); |
| 242 | } |
| 243 | |
| 244 | if (desc.m_LayerNormEnabled) |
| 245 | { |
| 246 | if(!desc.m_CifgEnabled) |
| 247 | { |
| 248 | paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo()); |
| 249 | } |
| 250 | paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo()); |
| 251 | paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo()); |
| 252 | paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo()); |
| 253 | } |
| 254 | |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 255 | bool isSupported = false; |
| 256 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 257 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 258 | FORWARD_LAYER_SUPPORT_FUNC("UNIDIRECTIONAL_SEQUENCE_LSTM", |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 259 | tfLiteContext, |
| 260 | IsUnidirectionalSequenceLstmSupported, |
| 261 | delegateData.m_Backends, |
| 262 | isSupported, |
| 263 | inputTensorInfo, |
| 264 | outputStateInInfo, |
| 265 | cellStateInInfo, |
Mike Kelly | 1299496 | 2022-04-21 11:57:09 +0100 | [diff] [blame] | 266 | outputStateOutTensorInfo, |
| 267 | cellStateOutTensorInfo, |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 268 | outputInfo, |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 269 | desc, |
| 270 | paramsInfo); |
| 271 | }; |
| 272 | |
| 273 | if (!delegateData.m_Network) |
| 274 | { |
| 275 | validateFunc(outputTensorInfo, isSupported); |
| 276 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 277 | } |
| 278 | |
| 279 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddUnidirectionalSequenceLstmLayer(desc, params); |
| 280 | ARMNN_ASSERT(layer != nullptr); |
| 281 | |
Mike Kelly | 1299496 | 2022-04-21 11:57:09 +0100 | [diff] [blame] | 282 | layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo); |
| 283 | layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo); |
| 284 | layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo); |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 285 | |
| 286 | // Connect the inputs |
| 287 | // input_layer |
| 288 | delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[0]]->Connect(layer->GetInputSlot(0)); |
| 289 | // cellStateIn |
| 290 | delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[18]]->Connect(layer->GetInputSlot(1)); |
| 291 | //outputStateIn |
| 292 | delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[19]]->Connect(layer->GetInputSlot(2)); |
| 293 | |
Mike Kelly | 1299496 | 2022-04-21 11:57:09 +0100 | [diff] [blame] | 294 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(2); |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 295 | delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tfLiteNode->outputs->data[0])] = &outputSlot; |
| 296 | return kTfLiteOk; |
| 297 | } |
| 298 | |
| 299 | } // namespace armnnDelegate |