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