Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | 3ad2e14 | 2023-01-13 10:19:20 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matthew Sloyan | 2b04ec3 | 2023-04-26 11:42:46 +0100 | [diff] [blame] | 8 | #include <DelegateUtils.hpp> |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 9 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 10 | #include <tensorflow/lite/builtin_ops.h> |
| 11 | #include <tensorflow/lite/c/builtin_op_data.h> |
| 12 | #include <tensorflow/lite/c/common.h> |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 13 | #include <tensorflow/lite/kernels/internal/tensor_ctypes.h> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 14 | #include <tensorflow/lite/minimal_logging.h> |
| 15 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <iterator> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 21 | namespace armnnDelegate |
| 22 | { |
| 23 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 24 | TfLiteStatus VisitConcatenationOperator(DelegateData& delegateData, |
| 25 | TfLiteContext* tfLiteContext, |
| 26 | TfLiteNode* tfLiteNode, |
| 27 | int nodeIndex, |
| 28 | int32_t tfLiteConcatOperatorCode) |
| 29 | { |
| 30 | unsigned int numInputs = tfLiteNode->inputs->size; |
| 31 | if (numInputs < 2) |
| 32 | { |
| 33 | TF_LITE_MAYBE_KERNEL_LOG( |
| 34 | tfLiteContext, "TfLiteArmnnDelegate: Minimum number of inputs (%d != %d) in node #%d", |
| 35 | 2, numInputs, nodeIndex); |
| 36 | return kTfLiteError; |
| 37 | } |
| 38 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 39 | |
| 40 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 41 | |
| 42 | std::vector<armnn::TensorInfo> inputTensorInfos; |
| 43 | for (unsigned int i = 0; i < numInputs; ++i) |
| 44 | { |
| 45 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[i]]; |
Sadik Armagan | 529195f | 2022-01-14 12:56:35 +0000 | [diff] [blame] | 46 | if (!IsValid(tfLiteContext, tfLiteInputTensor, tfLiteConcatOperatorCode, nodeIndex)) |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 47 | { |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 48 | return kTfLiteError; |
| 49 | } |
| 50 | |
| 51 | armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
| 52 | inputTensorInfos.emplace_back(inputTensorInfo); |
| 53 | } |
| 54 | |
| 55 | // Convert input tensors to const armnn::TensorInfo* type for FORWARD_LAYER_SUPPORT_FUNC. |
| 56 | std::vector<const armnn::TensorInfo*> inputConstTensorInfos; |
| 57 | std::transform(inputTensorInfos.begin(), |
| 58 | inputTensorInfos.end(), |
| 59 | std::back_inserter(inputConstTensorInfos), |
| 60 | [](armnn::TensorInfo& t)->const armnn::TensorInfo*{ return &t; }); |
| 61 | |
| 62 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
Sadik Armagan | 529195f | 2022-01-14 12:56:35 +0000 | [diff] [blame] | 63 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, tfLiteConcatOperatorCode, nodeIndex)) |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 64 | { |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 65 | return kTfLiteError; |
| 66 | } |
| 67 | |
| 68 | // Setup OriginsDescriptor, axis and view origin |
| 69 | unsigned int numConcatView = static_cast<unsigned int>(numInputs); |
| 70 | uint32_t inputRank = tfLiteTensors[tfLiteNode->inputs->data[0]].dims->size; |
| 71 | |
| 72 | auto* concatenationParameters = reinterpret_cast<TfLiteConcatenationParams*>(tfLiteNode->builtin_data); |
Ryan OShea | 3ad2e14 | 2023-01-13 10:19:20 +0000 | [diff] [blame] | 73 | |
| 74 | if(!concatenationParameters) |
| 75 | { |
| 76 | throw armnn::Exception(&"TfLiteArmnnDelegate: Concat parameters are null in: " [ nodeIndex]); |
| 77 | } |
| 78 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 79 | const unsigned int concatDimInput = static_cast<unsigned int>( |
| 80 | (static_cast<int>(inputRank) + concatenationParameters->axis) % static_cast<int>(inputRank)); |
| 81 | |
| 82 | armnn::OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank); |
| 83 | concatDescriptor.SetConcatAxis(concatDimInput); |
| 84 | |
| 85 | unsigned int mergeDimOrigin = 0; |
| 86 | for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex) |
| 87 | { |
| 88 | armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor( |
| 89 | tfLiteTensors[tfLiteNode->inputs->data[viewIndex]]); |
| 90 | |
| 91 | // Sets up concatDescriptor view origin |
| 92 | SetupConcatViewOrigin(inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin); |
| 93 | } |
| 94 | |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 95 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 96 | |
Ryan OShea | 3ad2e14 | 2023-01-13 10:19:20 +0000 | [diff] [blame] | 97 | // Verify we support the fused activation before attempting to create a layer |
| 98 | TfLiteFusedActivation activationType = concatenationParameters->activation; |
| 99 | |
Ryan OShea | 3ad2e14 | 2023-01-13 10:19:20 +0000 | [diff] [blame] | 100 | TfLiteStatus activationStatus = ValidateFusedActivationOperator(delegateData, tfLiteContext, outputTensorInfo, |
| 101 | outputTensorInfo, activationType); |
| 102 | if(activationStatus != kTfLiteOk) |
| 103 | { |
| 104 | return kTfLiteError; |
| 105 | } |
| 106 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 107 | // Check if supported |
| 108 | bool isSupported = false; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 109 | armnn::BackendId setBackend; |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 110 | auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported) |
| 111 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 112 | FORWARD_LAYER_SUPPORT_FUNC("CONCATENATION", |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 113 | tfLiteContext, |
| 114 | IsConcatSupported, |
| 115 | delegateData.m_Backends, |
| 116 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 117 | setBackend, |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 118 | inputConstTensorInfos, |
| 119 | outputTensorInfo, |
| 120 | concatDescriptor); |
| 121 | }; |
| 122 | |
| 123 | if (!delegateData.m_Network) |
| 124 | { |
| 125 | validateFunc(outputTensorInfo, isSupported); |
| 126 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 127 | } |
| 128 | |
| 129 | // Setup layer and connect. |
| 130 | armnn::IConnectableLayer* concatenationLayer = delegateData.m_Network->AddConcatLayer(concatDescriptor); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 131 | concatenationLayer->SetBackendId(setBackend); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 132 | ARMNN_ASSERT(concatenationLayer != nullptr); |
| 133 | |
Sadik Armagan | 529195f | 2022-01-14 12:56:35 +0000 | [diff] [blame] | 134 | // Connect the Constant Inputs |
| 135 | auto inputsTensorsProcess = ProcessInputs(concatenationLayer, |
| 136 | delegateData, |
| 137 | tfLiteContext, |
| 138 | tfLiteNode); |
| 139 | if (inputsTensorsProcess == kTfLiteError) |
| 140 | { |
| 141 | return inputsTensorsProcess; |
| 142 | } |
| 143 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 144 | armnn::IOutputSlot& outputSlot = concatenationLayer->GetOutputSlot(0); |
| 145 | outputSlot.SetTensorInfo(outputTensorInfo); |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 146 | if(Connect(concatenationLayer, tfLiteNode, delegateData) != kTfLiteOk) |
| 147 | { |
| 148 | return kTfLiteError; |
| 149 | } |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 150 | |
Ryan OShea | 3ad2e14 | 2023-01-13 10:19:20 +0000 | [diff] [blame] | 151 | if (activationType == kTfLiteActNone) |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 152 | { |
| 153 | // No Activation |
| 154 | return kTfLiteOk; |
| 155 | } |
| 156 | |
Ryan OShea | 3ad2e14 | 2023-01-13 10:19:20 +0000 | [diff] [blame] | 157 | // Check and Create activation |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 158 | return FusedActivation(tfLiteContext, tfLiteNode, activationType, concatenationLayer, 0, delegateData); |
| 159 | } |
| 160 | |
| 161 | TfLiteStatus VisitMeanOperator(DelegateData& delegateData, |
| 162 | TfLiteContext* tfLiteContext, |
| 163 | TfLiteNode* tfLiteNode, |
| 164 | int nodeIndex, |
| 165 | int32_t tfLiteMeanOperatorCode) |
| 166 | { |
| 167 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 168 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 169 | |
| 170 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 171 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 172 | if(!IsValid(&tfLiteInputTensor)) |
| 173 | { |
| 174 | TF_LITE_MAYBE_KERNEL_LOG( |
| 175 | tfLiteContext, |
| 176 | "TfLiteArmnnDelegate: Invalid input tensor in operator #%d node #%d: ", |
| 177 | tfLiteMeanOperatorCode, nodeIndex); |
| 178 | return kTfLiteError; |
| 179 | } |
| 180 | if (IsDynamicTensor(tfLiteInputTensor)) |
| 181 | { |
| 182 | TF_LITE_MAYBE_KERNEL_LOG( |
| 183 | tfLiteContext, |
| 184 | "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ", |
| 185 | tfLiteMeanOperatorCode, nodeIndex); |
| 186 | return kTfLiteError; |
| 187 | } |
| 188 | |
| 189 | const TfLiteTensor& tfLiteAxisTensor = tfLiteTensors[tfLiteNode->inputs->data[1]]; |
| 190 | if(!IsValid(&tfLiteAxisTensor)) |
| 191 | { |
| 192 | TF_LITE_MAYBE_KERNEL_LOG( |
| 193 | tfLiteContext, |
| 194 | "TfLiteArmnnDelegate: Invalid axis tensor in operator #%d node #%d: ", |
| 195 | tfLiteMeanOperatorCode, nodeIndex); |
| 196 | return kTfLiteError; |
| 197 | } |
| 198 | if (IsDynamicTensor(tfLiteAxisTensor)) |
| 199 | { |
| 200 | TF_LITE_MAYBE_KERNEL_LOG( |
| 201 | tfLiteContext, |
| 202 | "TfLiteArmnnDelegate: Dynamic axis tensors are not supported in operator #%d node #%d: ", |
| 203 | tfLiteMeanOperatorCode, nodeIndex); |
| 204 | return kTfLiteError; |
| 205 | } |
| 206 | |
| 207 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 208 | if(!IsValid(&tfLiteOutputTensor)) |
| 209 | { |
| 210 | TF_LITE_MAYBE_KERNEL_LOG( |
| 211 | tfLiteContext, |
| 212 | "TfLiteArmnnDelegate: Invalid output tensor in operator #%d node #%d: ", |
| 213 | tfLiteAxisTensor, nodeIndex); |
| 214 | return kTfLiteError; |
| 215 | } |
| 216 | if (IsDynamicTensor(tfLiteOutputTensor)) |
| 217 | { |
| 218 | TF_LITE_MAYBE_KERNEL_LOG( |
| 219 | tfLiteContext, |
| 220 | "TfLiteArmnnDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ", |
| 221 | tfLiteMeanOperatorCode, nodeIndex); |
| 222 | return kTfLiteError; |
| 223 | } |
| 224 | |
| 225 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
| 226 | const armnn::TensorInfo& axisTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteAxisTensor); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 227 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 228 | |
| 229 | auto* axisTensorData = tflite::GetTensorData<int32_t>(&tfLiteAxisTensor); |
| 230 | |
| 231 | std::vector<int32_t> axis; |
| 232 | // Add axis data to vector to be converter to unsigned int and assigned to descriptor axis. |
| 233 | for (unsigned int i = 0; i < axisTensorInfo.GetNumElements(); ++i) |
| 234 | { |
| 235 | axis.emplace_back(axisTensorData[i]); |
| 236 | } |
| 237 | |
| 238 | // Convert the axis to unsigned int and remove duplicates. |
| 239 | unsigned int rank = inputTensorInfo.GetNumDimensions(); |
| 240 | std::set<unsigned int> uniqueAxis; |
| 241 | std::transform(axis.begin(), |
| 242 | axis.end(), |
| 243 | std::inserter(uniqueAxis, uniqueAxis.begin()), |
| 244 | [rank](int i)->unsigned int{ return (i + rank) % rank; }); |
| 245 | |
| 246 | // Setup MeanDescriptor and assign axis and keepDims |
| 247 | armnn::MeanDescriptor desc; |
| 248 | desc.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end()); |
| 249 | desc.m_KeepDims = inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? true : false; |
| 250 | |
| 251 | // Check if supported |
| 252 | bool isSupported = false; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 253 | armnn::BackendId setBackend; |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 254 | auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported) |
| 255 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 256 | FORWARD_LAYER_SUPPORT_FUNC("MEAN", |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 257 | tfLiteContext, |
| 258 | IsMeanSupported, |
| 259 | delegateData.m_Backends, |
| 260 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 261 | setBackend, |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 262 | inputTensorInfo, |
| 263 | outputTensorInfo, |
| 264 | desc); |
| 265 | }; |
| 266 | |
| 267 | if (!delegateData.m_Network) |
| 268 | { |
| 269 | validateFunc(outputTensorInfo, isSupported); |
| 270 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 271 | } |
| 272 | |
| 273 | // Setup layer and connect. |
| 274 | armnn::IConnectableLayer* meanLayer = delegateData.m_Network->AddMeanLayer(desc); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 275 | meanLayer->SetBackendId(setBackend); |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 276 | ARMNN_ASSERT(meanLayer != nullptr); |
| 277 | |
| 278 | armnn::IOutputSlot& outputSlot = meanLayer->GetOutputSlot(0); |
| 279 | outputSlot.SetTensorInfo(outputTensorInfo); |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 280 | |
| 281 | // try to connect the Constant Inputs if there are any |
| 282 | if(ProcessInputs(meanLayer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk ) |
| 283 | { |
| 284 | return kTfLiteError; |
| 285 | } |
| 286 | |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 287 | return Connect(meanLayer, tfLiteNode, delegateData); |
| 288 | } |
| 289 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 290 | TfLiteStatus VisitControlOperator(DelegateData& delegateData, |
| 291 | TfLiteContext* tfLiteContext, |
| 292 | TfLiteNode* tfLiteNode, |
| 293 | int nodeIndex, |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 294 | int32_t operatorCode) |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 295 | { |
Matthew Sloyan | 91c4171 | 2020-11-13 09:47:35 +0000 | [diff] [blame] | 296 | switch(operatorCode) |
| 297 | { |
| 298 | case kTfLiteBuiltinConcatenation: |
| 299 | return VisitConcatenationOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode); |
| 300 | case kTfLiteBuiltinMean: |
| 301 | return VisitMeanOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode); |
| 302 | default: |
| 303 | return kTfLiteError; |
| 304 | } |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | } // namespace armnnDelegate |