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