Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 1 | // |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <tensorflow/lite/builtin_ops.h> |
| 9 | #include <tensorflow/lite/c/builtin_op_data.h> |
| 10 | #include <tensorflow/lite/c/common.h> |
| 11 | #include <tensorflow/lite/minimal_logging.h> |
| 12 | |
| 13 | namespace armnnDelegate |
| 14 | { |
| 15 | |
| 16 | TfLiteStatus VisitLogicalBinaryOperator(DelegateData& delegateData, |
| 17 | TfLiteContext* tfLiteContext, |
| 18 | TfLiteNode* tfLiteNode, |
| 19 | int nodeIndex, |
| 20 | int32_t logicalOperatorCode, |
| 21 | armnn::LogicalBinaryOperation binaryOperation) |
| 22 | { |
| 23 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 24 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 25 | |
| 26 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 27 | const TfLiteTensor& tfLiteInputTensor0 = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 28 | if (!IsValid(tfLiteContext, tfLiteInputTensor0, logicalOperatorCode, nodeIndex)) |
| 29 | { |
| 30 | return kTfLiteError; |
| 31 | } |
| 32 | |
| 33 | const TfLiteTensor& tfLiteInputTensor1 = tfLiteTensors[tfLiteNode->inputs->data[1]]; |
| 34 | if (!IsValid(tfLiteContext, tfLiteInputTensor1, logicalOperatorCode, nodeIndex)) |
| 35 | { |
| 36 | return kTfLiteError; |
| 37 | } |
| 38 | |
| 39 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 40 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, logicalOperatorCode, nodeIndex)) |
| 41 | { |
| 42 | return kTfLiteError; |
| 43 | } |
| 44 | |
| 45 | armnn::TensorInfo inputTensorInfo0 = GetTensorInfoForTfLiteTensor(tfLiteInputTensor0); |
| 46 | armnn::TensorInfo inputTensorInfo1 = GetTensorInfoForTfLiteTensor(tfLiteInputTensor1); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 47 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 48 | |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 49 | // Check if we need to expand the dims of any of the input tensor infos. |
| 50 | // This is required for a few of the backends. |
| 51 | if(inputTensorInfo0.GetNumDimensions() != inputTensorInfo1.GetNumDimensions()) |
| 52 | { |
| 53 | ExpandTensorRankToEqual(inputTensorInfo0, inputTensorInfo1); |
| 54 | } |
| 55 | |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 56 | // Setup descriptor and assign operation |
| 57 | armnn::LogicalBinaryDescriptor desc; |
| 58 | desc.m_Operation = binaryOperation; |
| 59 | |
| 60 | // Check if supported |
| 61 | bool isSupported = false; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 62 | armnn::BackendId setBackend; |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 63 | auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported) |
| 64 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 65 | FORWARD_LAYER_SUPPORT_FUNC("LOGICAL_BINARY", |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 66 | tfLiteContext, |
| 67 | IsLogicalBinarySupported, |
| 68 | delegateData.m_Backends, |
| 69 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 70 | setBackend, |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 71 | inputTensorInfo0, |
| 72 | inputTensorInfo1, |
| 73 | outputTensorInfo, |
| 74 | desc); |
| 75 | }; |
| 76 | |
| 77 | if (!delegateData.m_Network) |
| 78 | { |
| 79 | validateFunc(outputTensorInfo, isSupported); |
| 80 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 81 | } |
| 82 | |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 83 | auto layerName = GetLayerName(desc.m_Operation, nodeIndex); |
| 84 | armnn::IConnectableLayer* logicalBinaryLayer = delegateData.m_Network->AddLogicalBinaryLayer(desc, |
| 85 | layerName.c_str()); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 86 | logicalBinaryLayer->SetBackendId(setBackend); |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 87 | ARMNN_ASSERT(logicalBinaryLayer != nullptr); |
| 88 | |
| 89 | armnn::IOutputSlot& outputSlot = logicalBinaryLayer->GetOutputSlot(0); |
| 90 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 91 | |
Sadik Armagan | f7ac72c | 2021-05-05 15:03:50 +0100 | [diff] [blame] | 92 | auto inputsTensorsProcess = ProcessInputs(logicalBinaryLayer, |
| 93 | delegateData, |
| 94 | tfLiteContext, |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 95 | tfLiteNode, |
| 96 | nodeIndex); |
Sadik Armagan | f7ac72c | 2021-05-05 15:03:50 +0100 | [diff] [blame] | 97 | if (inputsTensorsProcess == kTfLiteError) |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 98 | { |
Sadik Armagan | f7ac72c | 2021-05-05 15:03:50 +0100 | [diff] [blame] | 99 | return inputsTensorsProcess; |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 102 | return Connect(logicalBinaryLayer, tfLiteNode, delegateData); |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace armnnDelegate |