blob: 4e4a2a3f3a9627ce179512ff5e10aa6f55a8c4d5 [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
Ryan OShea4c231de2023-01-17 15:19:20 +00002// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan62483be2020-10-23 17:14:43 +01003// 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>
Sadik Armagandc032fc2021-01-19 17:24:21 +000011#include <tensorflow/lite/kernels/internal/tensor_ctypes.h>
Sadik Armagan62483be2020-10-23 17:14:43 +010012#include <tensorflow/lite/minimal_logging.h>
13
14namespace armnnDelegate
15{
16
17TfLiteStatus VisitArgMinMaxOperator(DelegateData& delegateData,
18 TfLiteContext* tfLiteContext,
19 TfLiteNode* tfLiteNode,
20 int nodeIndex,
21 int32_t argMinMaxOperatorCode)
22{
Sadik Armagandc032fc2021-01-19 17:24:21 +000023 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
24 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
Finn Williams6f9f9902020-11-13 13:23:15 +000025
Sadik Armagandc032fc2021-01-19 17:24:21 +000026 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
27 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
28 if (!IsValid(tfLiteContext, tfLiteInputTensor, argMinMaxOperatorCode, nodeIndex))
29 {
30 return kTfLiteError;
31 }
32
33 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
34 if (!IsValid(tfLiteContext, tfLiteOutputTensor, argMinMaxOperatorCode, nodeIndex))
35 {
36 return kTfLiteError;
37 }
38
39 const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
Sadik Armagan90a119b2022-08-05 16:12:49 +010040 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Sadik Armagandc032fc2021-01-19 17:24:21 +000041
42 // Get const axis value from model and set it to descriptor.
43 const TfLiteTensor& tfLiteAxisTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
44 if (!IsValid(tfLiteContext, tfLiteAxisTensor, argMinMaxOperatorCode, nodeIndex))
45 {
46 return kTfLiteError;
47 }
48
49 armnn::ArgMinMaxDescriptor desc;
50 // Get the axis value from the input tensor
51 switch (tfLiteAxisTensor.type)
52 {
53 case kTfLiteInt32:
54 case kTfLiteInt64:
55 desc.m_Axis = tflite::GetTensorData<int>(&tfLiteAxisTensor)[0];
56 break;
57 default:
58 TF_LITE_MAYBE_KERNEL_LOG(
59 tfLiteContext,
60 "TfLiteArmnnDelegate: Axis value data type is not supported in operator #%d node #%d: ",
61 argMinMaxOperatorCode, nodeIndex);
62 return kTfLiteError;
63 }
64
65 // If output_type is int32 then set Signed32 else Signed64. Default type is Signed64.
66 if (argMinMaxOperatorCode == kTfLiteBuiltinArgMax)
67 {
68 desc.m_Function = armnn::ArgMinMaxFunction::Max;
69 auto* argMaxParameters = reinterpret_cast<TfLiteArgMaxParams*>(tfLiteNode->builtin_data);
Mike Kelly1f140f72021-04-06 12:25:55 +010070 if (argMaxParameters->output_type != kTfLiteInt32 && argMaxParameters->output_type != kTfLiteInt64)
Sadik Armagandc032fc2021-01-19 17:24:21 +000071 {
Mike Kelly1f140f72021-04-06 12:25:55 +010072 TF_LITE_MAYBE_KERNEL_LOG(
73 tfLiteContext,
74 "TfLiteArmnnDelegate: output_type data type is not supported in operator #%d node #%d: ",
75 argMinMaxOperatorCode, nodeIndex);
76 return kTfLiteError;
Sadik Armagandc032fc2021-01-19 17:24:21 +000077 }
78 }
79 else
80 {
81 desc.m_Function = armnn::ArgMinMaxFunction::Min;
82 auto* argMinParameters = reinterpret_cast<TfLiteArgMinParams*>(tfLiteNode->builtin_data);
Mike Kelly1f140f72021-04-06 12:25:55 +010083 if (argMinParameters->output_type != kTfLiteInt32 && argMinParameters->output_type != kTfLiteInt64)
Sadik Armagandc032fc2021-01-19 17:24:21 +000084 {
Mike Kelly1f140f72021-04-06 12:25:55 +010085 TF_LITE_MAYBE_KERNEL_LOG(
Sadik Armagandc032fc2021-01-19 17:24:21 +000086 tfLiteContext,
87 "TfLiteArmnnDelegate: output_type data type is not supported in operator #%d node #%d: ",
88 argMinMaxOperatorCode, nodeIndex);
Mike Kelly1f140f72021-04-06 12:25:55 +010089 return kTfLiteError;
Sadik Armagandc032fc2021-01-19 17:24:21 +000090 }
91 }
92
93 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +010094 armnn::BackendId setBackend;
Sadik Armagandc032fc2021-01-19 17:24:21 +000095 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
96 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000097 FORWARD_LAYER_SUPPORT_FUNC("ARGMINMAX",
Sadik Armagandc032fc2021-01-19 17:24:21 +000098 tfLiteContext,
99 IsArgMinMaxSupported,
100 delegateData.m_Backends,
101 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100102 setBackend,
Sadik Armagandc032fc2021-01-19 17:24:21 +0000103 inputTensorInfo,
104 outInfo,
105 desc);
106 };
107
108 if (!delegateData.m_Network)
109 {
110 validateFunc(outputTensorInfo, isSupported);
111 return isSupported ? kTfLiteOk : kTfLiteError;
112 }
113
114 // Add an ArgMinMax layer
115 armnn::IConnectableLayer* layer = delegateData.m_Network->AddArgMinMaxLayer(desc);
Cathal Corbett53837672022-09-01 11:34:37 +0100116 layer->SetBackendId(setBackend);
Sadik Armagandc032fc2021-01-19 17:24:21 +0000117 ARMNN_ASSERT(layer != nullptr);
118
119 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
120 outputSlot.SetTensorInfo(outputTensorInfo);
121
Ryan OShea4c231de2023-01-17 15:19:20 +0000122 // try to connect the Constant Inputs if there are any
123 if(ProcessInputs(layer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk )
124 {
125 return kTfLiteError;
126 }
127
Sadik Armagandc032fc2021-01-19 17:24:21 +0000128 // Connect
129 return Connect(layer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100130}
131
132} // namespace armnnDelegate