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> |
| 11 | #include <tensorflow/lite/minimal_logging.h> |
| 12 | |
| 13 | namespace armnnDelegate |
| 14 | { |
| 15 | |
| 16 | TfLiteStatus VisitPadOperator(DelegateData& delegateData, |
| 17 | TfLiteContext* tfLiteContext, |
| 18 | TfLiteNode* tfLiteNode, |
| 19 | int nodeIndex, |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 20 | int32_t tfLitePadOperatorCode) |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 21 | { |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 22 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 23 | |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 24 | switch(tfLitePadOperatorCode) |
| 25 | { |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 26 | case kTfLiteBuiltinMirrorPad: |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 27 | case kTfLiteBuiltinPad: |
| 28 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 29 | break; |
| 30 | case kTfLiteBuiltinPadv2: |
| 31 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 3, nodeIndex)); |
| 32 | break; |
| 33 | default: |
| 34 | return kTfLiteError; |
| 35 | } |
| 36 | |
| 37 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 38 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 39 | const TfLiteTensor& tfLitepaddingTensor = tfLiteTensors[tfLiteNode->inputs->data[1]]; |
| 40 | |
| 41 | if (IsDynamicTensor(tfLiteInputTensor)) |
| 42 | { |
| 43 | TF_LITE_MAYBE_KERNEL_LOG( |
| 44 | tfLiteContext, |
| 45 | "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ", |
| 46 | tfLitePadOperatorCode, nodeIndex); |
| 47 | return kTfLiteError; |
| 48 | } |
| 49 | |
| 50 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 51 | if (IsDynamicTensor(tfLiteOutputTensor)) |
| 52 | { |
| 53 | TF_LITE_MAYBE_KERNEL_LOG( |
| 54 | tfLiteContext, |
| 55 | "TfLiteArmnnDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ", |
| 56 | tfLitePadOperatorCode, nodeIndex); |
| 57 | return kTfLiteError; |
| 58 | } |
| 59 | |
| 60 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
| 61 | const armnn::TensorInfo& paddingTensorInfo = GetTensorInfoForTfLiteTensor(tfLitepaddingTensor); |
| 62 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); |
| 63 | |
| 64 | // Get the padding data from the input tensor |
| 65 | auto* paddingData = tflite::GetTensorData<int32_t>(&tfLitepaddingTensor); |
| 66 | |
| 67 | size_t step = 2; |
| 68 | armnn::PadDescriptor descriptor; |
| 69 | for (unsigned int i = 0; i < paddingTensorInfo.GetNumElements() / step; ++i) |
| 70 | { |
| 71 | descriptor.m_PadList.emplace_back(paddingData[i * step], paddingData[i * step + 1]); |
| 72 | } |
| 73 | |
| 74 | if (tfLitePadOperatorCode == kTfLiteBuiltinPad && inputTensorInfo.IsQuantized()) |
| 75 | { |
| 76 | descriptor.m_PadValue = inputTensorInfo.GetQuantizationOffset(); |
| 77 | } |
| 78 | else if (tfLitePadOperatorCode == kTfLiteBuiltinPadv2) |
| 79 | { |
| 80 | const TfLiteTensor& tfLitepaddingValue = tfLiteTensors[tfLiteNode->inputs->data[2]]; |
| 81 | armnn::TensorInfo paddingValueTensorInfo = GetTensorInfoForTfLiteTensor(tfLitepaddingValue); |
| 82 | if (paddingValueTensorInfo.GetNumElements() != 1) |
| 83 | { |
| 84 | TF_LITE_MAYBE_KERNEL_LOG( |
| 85 | tfLiteContext, |
| 86 | "TfLiteArmnnDelegate: Multiple padding value are not supported in operator #%d node #%d: ", |
| 87 | tfLitePadOperatorCode, nodeIndex); |
| 88 | return kTfLiteError; |
| 89 | } |
| 90 | // Get the padding value from the input tensor |
| 91 | switch (tfLitepaddingValue.type) |
| 92 | { |
| 93 | case kTfLiteFloat32: |
| 94 | descriptor.m_PadValue = tflite::GetTensorData<float>(&tfLitepaddingValue)[0]; |
| 95 | break; |
| 96 | case kTfLiteUInt8: |
| 97 | descriptor.m_PadValue = tflite::GetTensorData<uint8>(&tfLitepaddingValue)[0]; |
| 98 | break; |
| 99 | case kTfLiteInt8: |
| 100 | descriptor.m_PadValue = tflite::GetTensorData<int8>(&tfLitepaddingValue)[0]; |
| 101 | break; |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 102 | default: |
| 103 | TF_LITE_MAYBE_KERNEL_LOG( |
| 104 | tfLiteContext, |
| 105 | "TfLiteArmnnDelegate: Padding value datatype is not supported in operator #%d node #%d: ", |
| 106 | tfLitePadOperatorCode, nodeIndex); |
| 107 | return kTfLiteError; |
| 108 | } |
| 109 | } |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 110 | else if (tfLitePadOperatorCode == kTfLiteBuiltinMirrorPad) |
| 111 | { |
| 112 | TfLiteMirrorPaddingParams* options = reinterpret_cast<TfLiteMirrorPaddingParams*>(tfLiteNode->builtin_data); |
| 113 | |
| 114 | |
| 115 | if (options->mode == TfLiteMirrorPaddingMode::kTfLiteMirrorPaddingReflect) |
| 116 | { |
| 117 | descriptor.m_PaddingMode = armnn::PaddingMode::Reflect; |
| 118 | } |
| 119 | else if (options->mode == TfLiteMirrorPaddingMode::kTfLiteMirrorPaddingSymmetric) |
| 120 | { |
| 121 | descriptor.m_PaddingMode = armnn::PaddingMode::Symmetric; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | TF_LITE_MAYBE_KERNEL_LOG( |
| 126 | tfLiteContext, |
| 127 | "TfLiteArmnnDelegate: PaddingMode must be either REFLECT or SYMMETRIC in operator #%d node #%d: ", |
| 128 | tfLitePadOperatorCode, nodeIndex); |
| 129 | } |
| 130 | |
| 131 | // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1. |
| 132 | // If padding mode is Symmetric then both paddings must be no greater than inputShape(i). |
| 133 | auto inputShape = inputTensorInfo.GetShape(); |
| 134 | auto padList = descriptor.m_PadList; |
| 135 | |
| 136 | const unsigned int isReflect = |
| 137 | static_cast<unsigned int>(descriptor.m_PaddingMode == armnn::PaddingMode::Reflect); |
| 138 | for(unsigned int i = 0; i < padList.size(); ++i) |
| 139 | { |
| 140 | if(padList.at(i).first > (inputShape[i] - isReflect) || |
| 141 | padList.at(i).second > (inputShape[i] - isReflect)) |
| 142 | { |
| 143 | TF_LITE_MAYBE_KERNEL_LOG( |
| 144 | tfLiteContext, |
| 145 | "TfLiteArmnnDelegate: Padding values must be less (Reflect) or " |
| 146 | "equal (Symmetric) to the dimension size in operator #%d node #%d: ", |
| 147 | tfLitePadOperatorCode, nodeIndex); |
| 148 | } |
| 149 | } |
| 150 | } |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 151 | |
| 152 | if (!delegateData.m_Network) |
| 153 | { |
| 154 | bool isSupported = false; |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 155 | FORWARD_LAYER_SUPPORT_FUNC("PAD", |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 156 | tfLiteContext, |
| 157 | IsPadSupported, |
| 158 | delegateData.m_Backends, |
| 159 | isSupported, |
| 160 | inputTensorInfo, |
| 161 | outputTensorInfo, |
| 162 | descriptor); |
| 163 | |
| 164 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 165 | } |
| 166 | |
| 167 | armnn::IConnectableLayer* padLayer = delegateData.m_Network->AddPadLayer(descriptor); |
| 168 | ARMNN_ASSERT(padLayer != nullptr); |
| 169 | |
| 170 | armnn::IOutputSlot& outputSlot = padLayer->GetOutputSlot(0); |
| 171 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 172 | |
| 173 | return Connect(padLayer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // namespace armnnDelegate |