Tracy Narine | 7306bbe | 2023-07-17 16:06:26 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2023 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <OpaqueDelegateUtils.hpp> |
| 9 | |
| 10 | namespace armnnOpaqueDelegate |
| 11 | { |
| 12 | |
| 13 | TfLiteStatus ValidateReverseV2Operator(DelegateData& delegateData, |
| 14 | TfLiteOpaqueContext* tfLiteContext, |
| 15 | const armnn::TensorInfo& inputInfo0, |
| 16 | const armnn::TensorInfo& inputInfo1, |
| 17 | const armnn::TensorInfo& outputInfo) |
| 18 | { |
| 19 | bool isSupported = false; |
| 20 | FORWARD_LAYER_OPAQUE_SUPPORT_FUNC("REVERSEV2", |
| 21 | tfLiteContext, |
| 22 | IsReverseV2Supported, |
| 23 | delegateData.m_Backends, |
| 24 | isSupported, |
| 25 | armnn::BackendId(), |
| 26 | inputInfo0, |
| 27 | inputInfo1, |
| 28 | outputInfo); |
| 29 | |
| 30 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 31 | } |
| 32 | |
| 33 | TfLiteStatus VisitReverseV2Operator(DelegateData& delegateData, |
| 34 | TfLiteOpaqueContext* tfLiteContext, |
| 35 | TfLiteOpaqueNode* tfLiteNode, |
| 36 | int nodeIndex, |
| 37 | int32_t reverseV2OperatorCode) |
| 38 | { |
| 39 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 40 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 41 | |
| 42 | // Gather input indices and use to get input tensor. |
| 43 | auto numInputs = TfLiteOpaqueNodeNumberOfInputs(tfLiteNode); |
| 44 | const int* inputTensors; |
| 45 | if (TfLiteOpaqueNodeInputs(tfLiteNode, &inputTensors, &numInputs) != kTfLiteOk) |
| 46 | { |
| 47 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 48 | tfLiteContext, |
| 49 | "TfLiteArmnnOpaqueDelegate: Unable to gather input tensor indices from node #%d: ", |
| 50 | nodeIndex); |
| 51 | return kTfLiteError; |
| 52 | } |
| 53 | |
| 54 | // The first input contains the data to be reversed |
| 55 | const TfLiteOpaqueTensor* tfLiteInputTensor = |
| 56 | TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[0]); |
| 57 | if (IsDynamicTensor(tfLiteInputTensor)) |
| 58 | { |
| 59 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 60 | tfLiteContext, |
| 61 | "TfLiteArmnnOpaqueDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ", |
| 62 | reverseV2OperatorCode, nodeIndex); |
| 63 | return kTfLiteError; |
| 64 | } |
| 65 | |
| 66 | // The second input contains the axis tensor |
| 67 | const TfLiteOpaqueTensor* tfLiteAxisTensor = |
| 68 | TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[1]); |
| 69 | if (IsDynamicTensor(tfLiteAxisTensor)) |
| 70 | { |
| 71 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 72 | tfLiteContext, |
| 73 | "TfLiteArmnnOpaqueDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ", |
| 74 | reverseV2OperatorCode, nodeIndex); |
| 75 | return kTfLiteError; |
| 76 | } |
| 77 | |
| 78 | // Gather output indices and use to get output tensors. |
| 79 | int numOutputs = 0; |
| 80 | const int* outputTensors; |
| 81 | if (TfLiteOpaqueNodeOutputs(tfLiteNode, &outputTensors, &numOutputs) != kTfLiteOk) |
| 82 | { |
| 83 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 84 | tfLiteContext, |
| 85 | "TfLiteArmnnOpaqueDelegate: Unable to gather output tensor indices from node #%d: ", |
| 86 | nodeIndex); |
| 87 | return kTfLiteError; |
| 88 | } |
| 89 | |
| 90 | // Get the output tensor |
| 91 | const TfLiteOpaqueTensor* tfLiteOutputTensor = |
| 92 | TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, outputTensors[0]); |
| 93 | if (IsDynamicTensor(tfLiteOutputTensor)) |
| 94 | { |
| 95 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 96 | tfLiteContext, |
| 97 | "TfLiteArmnnOpaqueDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ", |
| 98 | reverseV2OperatorCode, nodeIndex); |
| 99 | return kTfLiteError; |
| 100 | } |
| 101 | |
| 102 | const armnn::TensorInfo& inputTensorInfo0 = |
| 103 | GetTensorInfoForTfLiteOpaqueTensor(tfLiteInputTensor); |
| 104 | const armnn::TensorInfo& inputTensorInfo1 = |
| 105 | GetTensorInfoForTfLiteOpaqueTensor(tfLiteAxisTensor); |
| 106 | const armnn::TensorInfo& outputTensorInfo = |
| 107 | GetTensorInfoForTfLiteOpaqueTensor(tfLiteOutputTensor, true); |
| 108 | |
| 109 | if (inputTensorInfo0.GetNumDimensions() != outputTensorInfo.GetNumDimensions()) |
| 110 | { |
| 111 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 112 | tfLiteContext, |
| 113 | "TfLiteArmnnOpaqueDelegate: input tensor dimension and output tensor dimension differ #%d node #%d: ", |
| 114 | reverseV2OperatorCode, nodeIndex); |
| 115 | return kTfLiteError; |
| 116 | } |
| 117 | |
| 118 | for (unsigned i=0; i < inputTensorInfo0.GetNumDimensions(); i++) |
| 119 | { |
| 120 | if (inputTensorInfo0.GetShape()[i] != outputTensorInfo.GetShape()[i]) |
| 121 | { |
| 122 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 123 | tfLiteContext, |
| 124 | "TfLiteArmnnOpaqueDelegate: input tensor dimension and output tensor differ #%d node #%d: ", |
| 125 | reverseV2OperatorCode, nodeIndex); |
| 126 | return kTfLiteError; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | std::string layerName("ReverseV2"); |
| 131 | |
| 132 | // Get axis tensor data |
| 133 | auto axisTensorNumValues = static_cast<unsigned int>(TfLiteOpaqueTensorDim(tfLiteAxisTensor,0)); |
| 134 | |
| 135 | const auto maxDimension = 4; |
| 136 | |
| 137 | if (axisTensorNumValues > maxDimension) |
| 138 | { |
| 139 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 140 | tfLiteContext, |
| 141 | "TfLiteArmnnOpaqueDelegate: The Axis-Input-Tensor of the ReverseV2 operation requires a " |
| 142 | "dimension of <= %d but a tensor with a dimension of %d was given. " |
| 143 | "Operator: #%d node #%d: ", |
| 144 | maxDimension, axisTensorNumValues, reverseV2OperatorCode, nodeIndex); |
| 145 | return kTfLiteError; |
| 146 | } |
| 147 | |
| 148 | // No network pointer indicates that only support for this operator should be checked |
| 149 | if (!delegateData.m_Network) |
| 150 | { |
| 151 | return ValidateReverseV2Operator(delegateData, |
| 152 | tfLiteContext, |
| 153 | inputTensorInfo0, |
| 154 | inputTensorInfo1, |
| 155 | outputTensorInfo); |
| 156 | } |
| 157 | |
| 158 | armnn::IConnectableLayer* reverseV2Layer = delegateData.m_Network->AddReverseV2Layer(layerName.c_str()); |
| 159 | |
| 160 | armnn::IOutputSlot& outputSlot = reverseV2Layer->GetOutputSlot(0); |
| 161 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 162 | |
| 163 | // try to connect the Constant Inputs if there are any |
| 164 | if(ProcessInputs(reverseV2Layer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk ) |
| 165 | { |
| 166 | return kTfLiteError; |
| 167 | } |
| 168 | |
| 169 | ARMNN_ASSERT(reverseV2Layer != nullptr); |
| 170 | |
| 171 | return Connect(reverseV2Layer, tfLiteContext, tfLiteNode, delegateData); |
| 172 | } |
| 173 | |
| 174 | } // namespace armnnOpaqueDelegate |