Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 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); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 39 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 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; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 46 | armnn::BackendId setBackend; |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 47 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 48 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 49 | FORWARD_LAYER_SUPPORT_FUNC("SPACE_TO_DEPTH", |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 50 | tfLiteContext, |
| 51 | IsSpaceToDepthSupported, |
| 52 | delegateData.m_Backends, |
| 53 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 54 | setBackend, |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 55 | inputTensorInfo, |
| 56 | outInfo, |
| 57 | descriptor); |
| 58 | }; |
| 59 | |
| 60 | if (!delegateData.m_Network) |
| 61 | { |
| 62 | validateFunc(outputTensorInfo, isSupported); |
| 63 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 64 | } |
| 65 | |
| 66 | // Add a SpaceToDepth layer |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 67 | auto layerName = GetLayerName(armnn::LayerType::SpaceToDepth, nodeIndex); |
| 68 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str()); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 69 | layer->SetBackendId(setBackend); |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 70 | ARMNN_ASSERT(layer != nullptr); |
| 71 | |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 72 | // try to connect the Constant Inputs if there are any |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 73 | if (ProcessInputs(layer, delegateData, tfLiteContext, tfLiteNode, nodeIndex) != kTfLiteOk) |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 74 | { |
| 75 | return kTfLiteError; |
| 76 | } |
| 77 | |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 78 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 79 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 80 | |
| 81 | // Connect |
| 82 | return Connect(layer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TfLiteStatus VisitDepthToSpaceOperator(DelegateData& delegateData, |
| 86 | TfLiteContext* tfLiteContext, |
| 87 | TfLiteNode* tfLiteNode, |
| 88 | int nodeIndex, |
| 89 | int32_t operatorCode) |
| 90 | { |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 91 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 92 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 93 | |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 94 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 95 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 96 | if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) |
| 97 | { |
| 98 | return kTfLiteError; |
| 99 | } |
| 100 | |
| 101 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 102 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
| 103 | { |
| 104 | return kTfLiteError; |
| 105 | } |
| 106 | |
| 107 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 108 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 109 | |
| 110 | armnn::DepthToSpaceDescriptor descriptor; |
| 111 | auto* params = reinterpret_cast<TfLiteDepthToSpaceParams*>(tfLiteNode->builtin_data); |
| 112 | descriptor.m_BlockSize = params->block_size; |
| 113 | |
| 114 | bool isSupported = false; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 115 | armnn::BackendId setBackend; |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 116 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 117 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 118 | FORWARD_LAYER_SUPPORT_FUNC("DEPTH_TO_SPACE", |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 119 | tfLiteContext, |
| 120 | IsDepthToSpaceSupported, |
| 121 | delegateData.m_Backends, |
| 122 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 123 | setBackend, |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 124 | inputTensorInfo, |
| 125 | outInfo, |
| 126 | descriptor); |
| 127 | }; |
| 128 | |
| 129 | if (!delegateData.m_Network) |
| 130 | { |
| 131 | validateFunc(outputTensorInfo, isSupported); |
| 132 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 133 | } |
| 134 | |
| 135 | // Add a DepthToSpace layer |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 136 | auto layerName = GetLayerName(armnn::LayerType::DepthToSpace, nodeIndex); |
| 137 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str()); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 138 | layer->SetBackendId(setBackend); |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 139 | ARMNN_ASSERT(layer != nullptr); |
| 140 | |
| 141 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 142 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 143 | |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 144 | // try to connect the Constant Inputs if there are any |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 145 | if (ProcessInputs(layer, delegateData, tfLiteContext, tfLiteNode, nodeIndex) != kTfLiteOk) |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 146 | { |
| 147 | return kTfLiteError; |
| 148 | } |
| 149 | |
Sadik Armagan | 89c5a9e | 2021-01-20 17:48:07 +0000 | [diff] [blame] | 150 | // Connect |
| 151 | return Connect(layer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } // namespace armnnDelegate |