blob: d71618ee9c5fc0b7eb796b47aed7089c34f5455b [file] [log] [blame]
Matthew Sloyanc8eb9552020-11-26 10:54:22 +00001//
Ryan OSheaa544f0f2023-01-25 18:10:20 +00002// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyanc8eb9552020-11-26 10:54:22 +00003// 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
13namespace armnnDelegate
14{
15
16TfLiteStatus 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 Armagan90a119b2022-08-05 16:12:49 +010047 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000048
Ryan OSheaa544f0f2023-01-25 18:10:20 +000049 // 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 Sloyanc8eb9552020-11-26 10:54:22 +000056 // Setup descriptor and assign operation
57 armnn::LogicalBinaryDescriptor desc;
58 desc.m_Operation = binaryOperation;
59
60 // Check if supported
61 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +010062 armnn::BackendId setBackend;
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000063 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
64 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000065 FORWARD_LAYER_SUPPORT_FUNC("LOGICAL_BINARY",
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000066 tfLiteContext,
67 IsLogicalBinarySupported,
68 delegateData.m_Backends,
69 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +010070 setBackend,
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000071 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
83 armnn::IConnectableLayer* logicalBinaryLayer = delegateData.m_Network->AddLogicalBinaryLayer(desc);
Cathal Corbett53837672022-09-01 11:34:37 +010084 logicalBinaryLayer->SetBackendId(setBackend);
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000085 ARMNN_ASSERT(logicalBinaryLayer != nullptr);
86
87 armnn::IOutputSlot& outputSlot = logicalBinaryLayer->GetOutputSlot(0);
88 outputSlot.SetTensorInfo(outputTensorInfo);
89
Sadik Armaganf7ac72c2021-05-05 15:03:50 +010090 auto inputsTensorsProcess = ProcessInputs(logicalBinaryLayer,
91 delegateData,
92 tfLiteContext,
93 tfLiteNode);
94 if (inputsTensorsProcess == kTfLiteError)
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000095 {
Sadik Armaganf7ac72c2021-05-05 15:03:50 +010096 return inputsTensorsProcess;
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000097 }
98
Ryan OSheaa544f0f2023-01-25 18:10:20 +000099 return Connect(logicalBinaryLayer, tfLiteNode, delegateData);
Matthew Sloyanc8eb9552020-11-26 10:54:22 +0000100}
101
102} // namespace armnnDelegate