Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 1 | // |
| 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 Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 11 | #include <tensorflow/lite/kernels/internal/tensor_ctypes.h> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 12 | #include <tensorflow/lite/minimal_logging.h> |
| 13 | |
| 14 | namespace armnnDelegate |
| 15 | { |
| 16 | |
| 17 | TfLiteStatus VisitArgMinMaxOperator(DelegateData& delegateData, |
| 18 | TfLiteContext* tfLiteContext, |
| 19 | TfLiteNode* tfLiteNode, |
| 20 | int nodeIndex, |
| 21 | int32_t argMinMaxOperatorCode) |
| 22 | { |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 23 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 24 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 25 | |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 26 | 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 Kelly | 1f140f7 | 2021-04-06 12:25:55 +0100 | [diff] [blame] | 70 | if (argMaxParameters->output_type != kTfLiteInt32 && argMaxParameters->output_type != kTfLiteInt64) |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 71 | { |
Mike Kelly | 1f140f7 | 2021-04-06 12:25:55 +0100 | [diff] [blame] | 72 | 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 Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | desc.m_Function = armnn::ArgMinMaxFunction::Min; |
| 82 | auto* argMinParameters = reinterpret_cast<TfLiteArgMinParams*>(tfLiteNode->builtin_data); |
Mike Kelly | 1f140f7 | 2021-04-06 12:25:55 +0100 | [diff] [blame] | 83 | if (argMinParameters->output_type != kTfLiteInt32 && argMinParameters->output_type != kTfLiteInt64) |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 84 | { |
Mike Kelly | 1f140f7 | 2021-04-06 12:25:55 +0100 | [diff] [blame] | 85 | TF_LITE_MAYBE_KERNEL_LOG( |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 86 | tfLiteContext, |
| 87 | "TfLiteArmnnDelegate: output_type data type is not supported in operator #%d node #%d: ", |
| 88 | argMinMaxOperatorCode, nodeIndex); |
Mike Kelly | 1f140f7 | 2021-04-06 12:25:55 +0100 | [diff] [blame] | 89 | return kTfLiteError; |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | bool isSupported = false; |
| 94 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 95 | { |
| 96 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 97 | 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 Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace armnnDelegate |