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 | |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 8 | #include "DelegateUtils.hpp" |
| 9 | #include <algorithm> |
| 10 | #include <iterator> |
| 11 | #include <string> |
| 12 | #include <vector> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 13 | |
| 14 | namespace armnnDelegate |
| 15 | { |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 16 | TfLiteStatus VisitGatherOperator(DelegateData& delegateData, |
| 17 | TfLiteContext* tfLiteContext, |
| 18 | TfLiteNode* tfLiteNode, |
| 19 | int nodeIndex, |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 20 | int32_t operatorCode) |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 21 | { |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 22 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 23 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 24 | |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 25 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 26 | |
| 27 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 28 | if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) |
| 29 | { |
| 30 | return kTfLiteError; |
| 31 | } |
| 32 | |
| 33 | const TfLiteTensor& tfLiteIndicesTensor = tfLiteTensors[tfLiteNode->inputs->data[1]]; |
| 34 | if (!IsValid(tfLiteContext, tfLiteIndicesTensor, operatorCode, nodeIndex)) |
| 35 | { |
| 36 | return kTfLiteError; |
| 37 | } |
| 38 | |
| 39 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 40 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
| 41 | { |
| 42 | return kTfLiteError; |
| 43 | } |
| 44 | |
| 45 | auto* gatherParameters = reinterpret_cast<TfLiteGatherParams*>(tfLiteNode->builtin_data); |
| 46 | auto axis = gatherParameters->axis; |
| 47 | |
| 48 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
| 49 | const armnn::TensorInfo& indicesTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteIndicesTensor); |
| 50 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); |
| 51 | armnn::GatherDescriptor gatherDescriptor; |
| 52 | gatherDescriptor.m_Axis = axis; |
| 53 | |
| 54 | auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions()); |
| 55 | auto indicesDimensions = indicesTensorInfo.GetNumDimensions(); |
| 56 | auto outputDimensions = outputTensorInfo.GetNumDimensions(); |
| 57 | if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0))) |
| 58 | { |
| 59 | TF_LITE_MAYBE_KERNEL_LOG( tfLiteContext, |
| 60 | "TfLiteArmnnDelegate: Operation has invalid axis: %d. It is out of bounds [-%d, %d))", |
| 61 | axis, inputDimensions, inputDimensions); |
| 62 | return kTfLiteError; |
| 63 | } |
| 64 | if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1) |
| 65 | { |
| 66 | TF_LITE_MAYBE_KERNEL_LOG( tfLiteContext, |
| 67 | "Operation has invalid output dimensions: %d. Output must be an (%d + %d - 1)-D tensor", |
| 68 | outputDimensions, inputDimensions, indicesDimensions); |
| 69 | return kTfLiteError; |
| 70 | } |
| 71 | |
| 72 | if (!delegateData.m_Network) |
| 73 | { |
| 74 | // Check if supported |
| 75 | bool isSupported = false; |
| 76 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 77 | tfLiteContext, |
| 78 | IsGatherSupported, |
| 79 | delegateData.m_Backends, |
| 80 | isSupported, |
| 81 | inputTensorInfo, |
| 82 | indicesTensorInfo, |
| 83 | outputTensorInfo, |
| 84 | gatherDescriptor); |
| 85 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 86 | } |
| 87 | |
| 88 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddGatherLayer(gatherDescriptor); |
| 89 | ARMNN_ASSERT(layer != nullptr); |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 90 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 91 | |
Sadik Armagan | f7ac72c | 2021-05-05 15:03:50 +0100 | [diff] [blame] | 92 | auto inputsTensorsProcess = ProcessInputs(layer, |
| 93 | delegateData, |
| 94 | tfLiteContext, |
| 95 | tfLiteNode); |
| 96 | if (inputsTensorsProcess == kTfLiteError) |
| 97 | { |
| 98 | return inputsTensorsProcess; |
| 99 | } |
| 100 | |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 101 | Connect(layer, tfLiteNode, delegateData); |
| 102 | |
| 103 | return kTfLiteOk; |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 104 | } |
Teresa Charlin | 98427a1 | 2020-11-25 18:22:57 +0000 | [diff] [blame] | 105 | } // namespace armnnDelegate |