blob: b6a8f5d5f6754d9825ccc9bdafb4f9019a2b99e6 [file] [log] [blame]
Matthew Sloyanc8eb9552020-11-26 10:54:22 +00001//
Sadik Armagan90a119b2022-08-05 16:12:49 +01002// Copyright © 2022 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
49 // Setup descriptor and assign operation
50 armnn::LogicalBinaryDescriptor desc;
51 desc.m_Operation = binaryOperation;
52
53 // Check if supported
54 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +010055 armnn::BackendId setBackend;
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000056 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
57 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000058 FORWARD_LAYER_SUPPORT_FUNC("LOGICAL_BINARY",
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000059 tfLiteContext,
60 IsLogicalBinarySupported,
61 delegateData.m_Backends,
62 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +010063 setBackend,
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000064 inputTensorInfo0,
65 inputTensorInfo1,
66 outputTensorInfo,
67 desc);
68 };
69
70 if (!delegateData.m_Network)
71 {
72 validateFunc(outputTensorInfo, isSupported);
73 return isSupported ? kTfLiteOk : kTfLiteError;
74 }
75
76 armnn::IConnectableLayer* logicalBinaryLayer = delegateData.m_Network->AddLogicalBinaryLayer(desc);
Cathal Corbett53837672022-09-01 11:34:37 +010077 logicalBinaryLayer->SetBackendId(setBackend);
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000078 ARMNN_ASSERT(logicalBinaryLayer != nullptr);
79
80 armnn::IOutputSlot& outputSlot = logicalBinaryLayer->GetOutputSlot(0);
81 outputSlot.SetTensorInfo(outputTensorInfo);
82
Sadik Armaganf7ac72c2021-05-05 15:03:50 +010083 auto inputsTensorsProcess = ProcessInputs(logicalBinaryLayer,
84 delegateData,
85 tfLiteContext,
86 tfLiteNode);
87 if (inputsTensorsProcess == kTfLiteError)
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000088 {
Sadik Armaganf7ac72c2021-05-05 15:03:50 +010089 return inputsTensorsProcess;
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000090 }
91
92 // LogicalBinary operators support broadcasting
93 auto reshapeLayer = BroadcastTensor(inputTensorInfo0,
94 inputTensorInfo1,
95 logicalBinaryLayer,
96 tfLiteContext,
97 tfLiteNode,
98 delegateData);
99 if (!reshapeLayer)
100 {
101 return kTfLiteError;
102 }
103 return kTfLiteOk;
104}
105
106} // namespace armnnDelegate