Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2023 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Matthew Sloyan | 48ec813 | 2023-04-27 17:04:47 +0100 | [diff] [blame] | 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <OpaqueDelegateUtils.hpp> |
| 9 | |
| 10 | namespace armnnOpaqueDelegate |
| 11 | { |
| 12 | |
| 13 | TfLiteStatus VisitLstmOperator(DelegateData& delegateData, |
| 14 | TfLiteOpaqueContext* tfLiteContext, |
| 15 | TfLiteOpaqueNode* tfLiteNode, |
| 16 | int nodeIndex, |
| 17 | int32_t operatorCode) |
| 18 | { |
| 19 | auto numInputs = TfLiteOpaqueNodeNumberOfInputs(tfLiteNode); |
| 20 | if (numInputs < 2) |
| 21 | { |
| 22 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 23 | tfLiteContext, |
| 24 | "TfLiteArmnnOpaqueDelegate: Minimum number of inputs (%d != %d) in node #%d", |
| 25 | 2, numInputs, nodeIndex); |
| 26 | return kTfLiteError; |
| 27 | } |
| 28 | |
| 29 | // Gather input indices and use to get input tensor. |
| 30 | const int* inputTensors; |
| 31 | if (TfLiteOpaqueNodeInputs(tfLiteNode, &inputTensors, &numInputs) != kTfLiteOk) |
| 32 | { |
| 33 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 34 | tfLiteContext, |
| 35 | "TfLiteArmnnOpaqueDelegate: Unable to gather input tensor indices from node #%d: ", |
| 36 | nodeIndex); |
| 37 | return kTfLiteError; |
| 38 | } |
| 39 | |
| 40 | const TfLiteOpaqueTensor* tfLiteInputTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[0]); |
| 41 | if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) |
| 42 | { |
| 43 | return kTfLiteError; |
| 44 | } |
| 45 | |
| 46 | // Gather output indices and use to get output tensors. |
| 47 | int numOutputs = 0; |
| 48 | const int* outputTensors; |
| 49 | if (TfLiteOpaqueNodeOutputs(tfLiteNode, &outputTensors, &numOutputs) != kTfLiteOk) |
| 50 | { |
| 51 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 52 | tfLiteContext, |
| 53 | "TfLiteArmnnOpaqueDelegate: Unable to gather output tensor indices from node #%d: ", |
| 54 | nodeIndex); |
| 55 | return kTfLiteError; |
| 56 | } |
| 57 | |
| 58 | const TfLiteOpaqueTensor* tfLiteOutputTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, outputTensors[0]); |
| 59 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
| 60 | { |
| 61 | return kTfLiteError; |
| 62 | } |
| 63 | |
| 64 | // Set the params structure for the AddLstmLayer call |
| 65 | armnn::LstmInputParams params; |
| 66 | |
| 67 | if (IsOptionalOperandPresent(tfLiteNode, 1)) |
| 68 | { |
| 69 | params.m_InputToInputWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 1); |
| 70 | } |
| 71 | |
| 72 | params.m_InputToForgetWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 2); |
| 73 | params.m_InputToCellWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 3); |
| 74 | params.m_InputToOutputWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 4); |
| 75 | |
| 76 | // Recurrent weight tensors of size {n_cell, n_output} |
| 77 | if (IsOptionalOperandPresent(tfLiteNode, 5)) |
| 78 | { |
| 79 | params.m_RecurrentToInputWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 5); |
| 80 | } |
| 81 | |
| 82 | params.m_RecurrentToForgetWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 6); |
| 83 | params.m_RecurrentToCellWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 7); |
| 84 | params.m_RecurrentToOutputWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 8); |
| 85 | |
| 86 | // Peephole weights tensors of size {n_cell}, representing a diagonal matrix. |
| 87 | if (IsOptionalOperandPresent(tfLiteNode, 9)) |
| 88 | { |
| 89 | params.m_CellToInputWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 9); |
| 90 | } |
| 91 | |
| 92 | if (IsOptionalOperandPresent(tfLiteNode, 10)) |
| 93 | { |
| 94 | params.m_CellToForgetWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 10); |
| 95 | } |
| 96 | |
| 97 | if (IsOptionalOperandPresent(tfLiteNode, 11)) |
| 98 | { |
| 99 | params.m_CellToOutputWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 11); |
| 100 | } |
| 101 | |
| 102 | // Gates bias tensors of size {n_cell} |
| 103 | if (IsOptionalOperandPresent(tfLiteNode, 12)) |
| 104 | { |
| 105 | params.m_InputGateBias = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 12); |
| 106 | } |
| 107 | |
| 108 | params.m_ForgetGateBias = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 13); |
| 109 | params.m_CellBias = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 14); |
| 110 | params.m_OutputGateBias = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 15); |
| 111 | |
| 112 | // Projection weight tensor of size {n_output, n_cell} |
| 113 | if (IsOptionalOperandPresent(tfLiteNode, 16)) |
| 114 | { |
| 115 | params.m_ProjectionWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 16); |
| 116 | } |
| 117 | // Projection bias tensor of size {n_output} |
| 118 | if (IsOptionalOperandPresent(tfLiteNode, 17)) |
| 119 | { |
| 120 | params.m_ProjectionBias = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 17); |
| 121 | } |
| 122 | |
| 123 | // These state tensors are defined as variable tensors, and will be modified by this op. |
| 124 | const TfLiteOpaqueTensor* tfLiteOutputStateIn = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[18]); |
| 125 | if (!IsValid(tfLiteContext, tfLiteOutputStateIn, operatorCode, nodeIndex)) |
| 126 | { |
| 127 | return kTfLiteError; |
| 128 | } |
| 129 | const TfLiteOpaqueTensor* cellStateIn = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[19]); |
| 130 | if (!IsValid(tfLiteContext, cellStateIn, operatorCode, nodeIndex)) |
| 131 | { |
| 132 | return kTfLiteError; |
| 133 | } |
| 134 | armnn::TensorInfo outputStateInInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteOutputStateIn); |
| 135 | armnn::TensorInfo cellStateInInfo = GetTensorInfoForTfLiteOpaqueTensor(cellStateIn); |
| 136 | |
| 137 | // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix. |
| 138 | if (IsOptionalOperandPresent(tfLiteNode, 20)) |
| 139 | { |
| 140 | params.m_InputLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 20); |
| 141 | } |
| 142 | |
| 143 | if (IsOptionalOperandPresent(tfLiteNode, 21)) |
| 144 | { |
| 145 | params.m_ForgetLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 21); |
| 146 | } |
| 147 | |
| 148 | if (IsOptionalOperandPresent(tfLiteNode, 22)) |
| 149 | { |
| 150 | params.m_CellLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 22); |
| 151 | } |
| 152 | |
| 153 | if (IsOptionalOperandPresent(tfLiteNode, 23)) |
| 154 | { |
| 155 | params.m_OutputLayerNormWeights = GetConstTensorForTfLiteTensor(tfLiteContext, tfLiteNode, 23); |
| 156 | } |
| 157 | |
| 158 | const auto nodeParams = reinterpret_cast<TfLiteLSTMParams*>(TfLiteOpaqueNodeGetBuiltinData(tfLiteNode)); |
| 159 | |
| 160 | // set the layer descriptor |
| 161 | armnn::LstmDescriptor desc; |
| 162 | desc.m_ActivationFunc = NonNegative(nodeParams->activation, nodeIndex); |
| 163 | desc.m_ClippingThresCell = nodeParams->cell_clip; |
| 164 | desc.m_ClippingThresProj = nodeParams->proj_clip; |
| 165 | desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr |
| 166 | || params.m_RecurrentToInputWeights == nullptr |
| 167 | || params.m_InputGateBias == nullptr); |
| 168 | desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr); |
| 169 | desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr); |
| 170 | desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr |
| 171 | || params.m_ForgetLayerNormWeights != nullptr |
| 172 | || params.m_CellLayerNormWeights != nullptr |
| 173 | || params.m_OutputLayerNormWeights != nullptr); |
| 174 | |
| 175 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteInputTensor); |
| 176 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteOutputTensor, true); |
| 177 | |
| 178 | unsigned int batchSize = inputTensorInfo.GetShape()[0]; |
| 179 | unsigned int outputSize = outputTensorInfo.GetShape()[1]; |
| 180 | unsigned int numUnits = cellStateInInfo.GetShape()[1]; |
| 181 | |
| 182 | armnn::DataType dataType = inputTensorInfo.GetDataType(); |
| 183 | float qScale = inputTensorInfo.GetQuantizationScale(); |
| 184 | float qOffset = inputTensorInfo.GetQuantizationOffset(); |
| 185 | |
| 186 | armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset); |
| 187 | if (!desc.m_CifgEnabled) |
| 188 | { |
| 189 | scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset); |
| 190 | } |
| 191 | armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits}, dataType, qScale, qOffset); |
| 192 | armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset); |
| 193 | |
| 194 | armnn::LstmInputParamsInfo paramsInfo; |
| 195 | paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo()); |
| 196 | paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo()); |
| 197 | paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo()); |
| 198 | paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo()); |
| 199 | paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo()); |
| 200 | paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo()); |
| 201 | paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo()); |
| 202 | paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo()); |
| 203 | paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo()); |
| 204 | |
| 205 | if (!desc.m_CifgEnabled) |
| 206 | { |
| 207 | paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo()); |
| 208 | paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo()); |
| 209 | if (params.m_CellToInputWeights != nullptr) |
| 210 | { |
| 211 | paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo()); |
| 212 | } |
| 213 | paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo()); |
| 214 | } |
| 215 | |
| 216 | if (desc.m_ProjectionEnabled) |
| 217 | { |
| 218 | paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo()); |
| 219 | if (params.m_ProjectionBias != nullptr) |
| 220 | { |
| 221 | paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (desc.m_PeepholeEnabled) |
| 226 | { |
| 227 | paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo()); |
| 228 | paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo()); |
| 229 | } |
| 230 | |
| 231 | if (desc.m_LayerNormEnabled) |
| 232 | { |
| 233 | if(!desc.m_CifgEnabled) |
| 234 | { |
| 235 | paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo()); |
| 236 | } |
| 237 | paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo()); |
| 238 | paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo()); |
| 239 | paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo()); |
| 240 | } |
| 241 | |
| 242 | bool isSupported = false; |
| 243 | armnn::BackendId setBackend; |
| 244 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
| 245 | { |
| 246 | FORWARD_LAYER_OPAQUE_SUPPORT_FUNC("LSTM", |
| 247 | tfLiteContext, |
| 248 | IsLstmSupported, |
| 249 | delegateData.m_Backends, |
| 250 | isSupported, |
| 251 | setBackend, |
| 252 | inputTensorInfo, |
| 253 | outputStateInInfo, |
| 254 | cellStateInInfo, |
| 255 | scratchBufferTensorInfo, |
| 256 | outputStateOutTensorInfo, |
| 257 | cellStateOutTensorInfo, |
| 258 | outputInfo, |
| 259 | desc, |
| 260 | paramsInfo); |
| 261 | }; |
| 262 | |
| 263 | if (!delegateData.m_Network) |
| 264 | { |
| 265 | validateFunc(outputTensorInfo, isSupported); |
| 266 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 267 | } |
| 268 | |
| 269 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddLstmLayer(desc, params); |
| 270 | layer->SetBackendId(setBackend); |
| 271 | ARMNN_ASSERT(layer != nullptr); |
| 272 | |
| 273 | layer->GetOutputSlot(0).SetTensorInfo(scratchBufferTensorInfo); |
| 274 | layer->GetOutputSlot(1).SetTensorInfo(outputStateOutTensorInfo); |
| 275 | layer->GetOutputSlot(2).SetTensorInfo(cellStateOutTensorInfo); |
| 276 | layer->GetOutputSlot(3).SetTensorInfo(outputTensorInfo); |
| 277 | |
| 278 | // Connect the inputs |
| 279 | // input_layer |
| 280 | delegateData.m_OutputSlotForNode[inputTensors[0]]->Connect(layer->GetInputSlot(0)); |
| 281 | // cellStateIn |
| 282 | delegateData.m_OutputSlotForNode[inputTensors[18]]->Connect(layer->GetInputSlot(1)); |
| 283 | //outputStateIn |
| 284 | delegateData.m_OutputSlotForNode[inputTensors[19]]->Connect(layer->GetInputSlot(2)); |
| 285 | |
| 286 | // In the test_model there is only 1 Output |
| 287 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(1); |
| 288 | delegateData.m_OutputSlotForNode[static_cast<unsigned long>(outputTensors[0])] = &outputSlot; |
| 289 | |
| 290 | return kTfLiteOk; |
| 291 | } |
| 292 | |
| 293 | } // namespace armnnOpaqueDelegate |