blob: 0b0b13d6ec00a005487d1487cc0582819b50ea0c [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
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>
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);
40 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor);
41
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;
94 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
95 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000096 FORWARD_LAYER_SUPPORT_FUNC("ARGMINMAX",
Sadik Armagandc032fc2021-01-19 17:24:21 +000097 tfLiteContext,
98 IsArgMinMaxSupported,
99 delegateData.m_Backends,
100 isSupported,
101 inputTensorInfo,
102 outInfo,
103 desc);
104 };
105
106 if (!delegateData.m_Network)
107 {
108 validateFunc(outputTensorInfo, isSupported);
109 return isSupported ? kTfLiteOk : kTfLiteError;
110 }
111
112 // Add an ArgMinMax layer
113 armnn::IConnectableLayer* layer = delegateData.m_Network->AddArgMinMaxLayer(desc);
114 ARMNN_ASSERT(layer != nullptr);
115
116 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
117 outputSlot.SetTensorInfo(outputTensorInfo);
118
119 // Connect
120 return Connect(layer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100121}
122
123} // namespace armnnDelegate