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 VisitSpaceToDepthOperator(DelegateData& delegateData, |
| 17 | TfLiteContext* tfLiteContext, |
| 18 | TfLiteNode* tfLiteNode, |
| 19 | int nodeIndex, |
| 20 | int32_t operatorCode) |
| 21 | { |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 22 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 23 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 24 | |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 25 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 26 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 27 | if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) |
| 28 | { |
| 29 | return kTfLiteError; |
| 30 | } |
| 31 | |
| 32 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 33 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
| 34 | { |
| 35 | return kTfLiteError; |
| 36 | } |
| 37 | |
| 38 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
| 39 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); |
| 40 | |
| 41 | armnn::SpaceToDepthDescriptor descriptor; |
| 42 | auto* params = reinterpret_cast<TfLiteSpaceToDepthParams*>(tfLiteNode->builtin_data); |
| 43 | descriptor.m_BlockSize = params->block_size; |
| 44 | |
| 45 | bool isSupported = false; |
| 46 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 47 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 48 | FORWARD_LAYER_SUPPORT_FUNC("SPACE_TO_DEPTH", |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 49 | tfLiteContext, |
| 50 | IsSpaceToDepthSupported, |
| 51 | delegateData.m_Backends, |
| 52 | isSupported, |
| 53 | inputTensorInfo, |
| 54 | outInfo, |
| 55 | descriptor); |
| 56 | }; |
| 57 | |
| 58 | if (!delegateData.m_Network) |
| 59 | { |
| 60 | validateFunc(outputTensorInfo, isSupported); |
| 61 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 62 | } |
| 63 | |
| 64 | // Add a SpaceToDepth layer |
| 65 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddSpaceToDepthLayer(descriptor); |
| 66 | ARMNN_ASSERT(layer != nullptr); |
| 67 | |
| 68 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 69 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 70 | |
| 71 | // Connect |
| 72 | return Connect(layer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TfLiteStatus VisitDepthToSpaceOperator(DelegateData& delegateData, |
| 76 | TfLiteContext* tfLiteContext, |
| 77 | TfLiteNode* tfLiteNode, |
| 78 | int nodeIndex, |
| 79 | int32_t operatorCode) |
| 80 | { |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 81 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 82 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 83 | |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 84 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 85 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 86 | if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) |
| 87 | { |
| 88 | return kTfLiteError; |
| 89 | } |
| 90 | |
| 91 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 92 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
| 93 | { |
| 94 | return kTfLiteError; |
| 95 | } |
| 96 | |
| 97 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
| 98 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); |
| 99 | |
| 100 | armnn::DepthToSpaceDescriptor descriptor; |
| 101 | auto* params = reinterpret_cast<TfLiteDepthToSpaceParams*>(tfLiteNode->builtin_data); |
| 102 | descriptor.m_BlockSize = params->block_size; |
| 103 | |
| 104 | bool isSupported = false; |
| 105 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 106 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 107 | FORWARD_LAYER_SUPPORT_FUNC("DEPTH_TO_SPACE", |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 108 | tfLiteContext, |
| 109 | IsDepthToSpaceSupported, |
| 110 | delegateData.m_Backends, |
| 111 | isSupported, |
| 112 | inputTensorInfo, |
| 113 | outInfo, |
| 114 | descriptor); |
| 115 | }; |
| 116 | |
| 117 | if (!delegateData.m_Network) |
| 118 | { |
| 119 | validateFunc(outputTensorInfo, isSupported); |
| 120 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 121 | } |
| 122 | |
| 123 | // Add a DepthToSpace layer |
| 124 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddDepthToSpaceLayer(descriptor); |
| 125 | ARMNN_ASSERT(layer != nullptr); |
| 126 | |
| 127 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 128 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 129 | |
| 130 | // Connect |
| 131 | return Connect(layer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | } // namespace armnnDelegate |