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 | |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 8 | #include <armnn/utility/IgnoreUnused.hpp> |
| 9 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 10 | #include <tensorflow/lite/builtin_ops.h> |
| 11 | #include <tensorflow/lite/c/builtin_op_data.h> |
| 12 | #include <tensorflow/lite/c/common.h> |
| 13 | #include <tensorflow/lite/minimal_logging.h> |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 14 | #include <tensorflow/lite/kernels/internal/tensor_ctypes.h> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 15 | |
| 16 | namespace armnnDelegate |
| 17 | { |
| 18 | |
| 19 | TfLiteStatus VisitTransposeOperator(DelegateData& delegateData, |
| 20 | TfLiteContext* tfLiteContext, |
| 21 | TfLiteNode* tfLiteNode, |
| 22 | int nodeIndex, |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 23 | int32_t tfliteTransposeOperatorCode) |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 24 | { |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 25 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 26 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 27 | |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 28 | const TfLiteTensor *tfLiteTensors = tfLiteContext->tensors; |
| 29 | const TfLiteTensor& tfLiteInputTensor0 = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 30 | if (IsDynamicTensor(tfLiteInputTensor0)) |
| 31 | { |
| 32 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 33 | "TfLiteArmnnDelegate: Dynamic input tensors are not supported in " |
| 34 | "operator #%d node #%d: ", |
| 35 | tfliteTransposeOperatorCode, nodeIndex); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 36 | |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 37 | return kTfLiteError; |
| 38 | } |
| 39 | |
| 40 | const TfLiteTensor& tfLiteInputTensor1 = tfLiteTensors[tfLiteNode->inputs->data[1]]; |
| 41 | if (IsDynamicTensor(tfLiteInputTensor1)) |
| 42 | { |
| 43 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 44 | "TfLiteArmnnDelegate: Dynamic input tensors are not supported in " |
| 45 | "operator #%d node #%d: ", |
| 46 | tfliteTransposeOperatorCode, 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(tfLiteContext, |
| 54 | "TfLiteArmnnDelegate: Dynamic output tensors are not supported in " |
| 55 | "operator #%d node #%d: ", |
| 56 | tfliteTransposeOperatorCode, nodeIndex); |
| 57 | return kTfLiteError; |
| 58 | } |
| 59 | |
| 60 | const armnn::TensorInfo& inputTensorInfo0 = GetTensorInfoForTfLiteTensor(tfLiteInputTensor0); |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 61 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); |
| 62 | |
| 63 | auto* permTensorDataPtr = tflite::GetTensorData<int32_t>(&tfLiteInputTensor1); |
James Ward | 7c0e3fd | 2020-11-16 18:46:12 +0000 | [diff] [blame] | 64 | unsigned int numEl = tfLiteInputTensor1.dims->data[0]; |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 65 | |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 66 | ARMNN_ASSERT( numEl <= static_cast<int>(armnn::MaxNumOfTensorDimensions)); |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 67 | ARMNN_ASSERT( tfLiteInputTensor1.dims->size == 1); // ensure only single dimension to the permutation tensor |
| 68 | |
| 69 | armnn::TransposeDescriptor descriptor(armnn::PermutationVector( |
| 70 | reinterpret_cast<const armnn::PermutationVector::ValueType *> (permTensorDataPtr), |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 71 | static_cast<armnn::PermutationVector::SizeType>(numEl))); |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 72 | |
| 73 | bool isSupported = false; |
| 74 | |
| 75 | auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported) |
| 76 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 77 | FORWARD_LAYER_SUPPORT_FUNC("TRANSPOSE", |
James Ward | f89964e | 2020-11-09 11:57:47 +0000 | [diff] [blame] | 78 | tfLiteContext, |
| 79 | IsTransposeSupported, |
| 80 | delegateData.m_Backends, |
| 81 | isSupported, |
| 82 | inputTensorInfo0, |
| 83 | outputTensorInfo, |
| 84 | descriptor); |
| 85 | }; |
| 86 | |
| 87 | if (!delegateData.m_Network) |
| 88 | { |
| 89 | validateFunc(outputTensorInfo, isSupported); |
| 90 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 91 | } |
| 92 | |
| 93 | armnn::IConnectableLayer* transposeLayer = delegateData.m_Network->AddTransposeLayer(descriptor); |
| 94 | ARMNN_ASSERT(transposeLayer != nullptr); |
| 95 | ARMNN_ASSERT(transposeLayer->GetNumInputSlots() == 1); // permutation vector given to descriptor object |
| 96 | |
| 97 | armnn::IOutputSlot& outputSlot = transposeLayer->GetOutputSlot(0); |
| 98 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 99 | |
| 100 | return Connect(transposeLayer, tfLiteNode, delegateData); |
| 101 | } |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 102 | } // namespace armnnDelegate |