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 | |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 8 | #include <armnn/utility/IgnoreUnused.hpp> |
| 9 | |
Matthew Sloyan | 1157232 | 2023-03-16 10:17:51 +0000 | [diff] [blame] | 10 | #include <ClassicDelegateUtils.hpp> |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 11 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 12 | #include <tensorflow/lite/builtin_ops.h> |
| 13 | #include <tensorflow/lite/c/builtin_op_data.h> |
| 14 | #include <tensorflow/lite/c/common.h> |
| 15 | #include <tensorflow/lite/minimal_logging.h> |
| 16 | |
| 17 | namespace armnnDelegate |
| 18 | { |
| 19 | |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 20 | TfLiteStatus VisitCastOperator(DelegateData& delegateData, |
| 21 | TfLiteContext* tfLiteContext, |
| 22 | TfLiteNode* tfLiteNode, |
| 23 | int nodeIndex, |
| 24 | int32_t operatorCode) |
| 25 | { |
| 26 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 27 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 28 | |
| 29 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 30 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 31 | if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) |
| 32 | { |
| 33 | return kTfLiteError; |
| 34 | } |
| 35 | |
| 36 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 37 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
| 38 | { |
| 39 | return kTfLiteError; |
| 40 | } |
| 41 | |
| 42 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 43 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 44 | |
| 45 | bool isSupported = false; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 46 | armnn::BackendId setBackend; |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [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("CAST", |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 50 | tfLiteContext, |
| 51 | IsCastSupported, |
| 52 | delegateData.m_Backends, |
| 53 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 54 | setBackend, |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 55 | inputTensorInfo, |
| 56 | outInfo); |
| 57 | }; |
| 58 | |
| 59 | // If the m_Network is a nullptr, this signals that a prerequisite TfLite callback is required to clarify the |
| 60 | // support for the operator |
| 61 | // If supported, VisitCastOperator will be called again to add the layer to the network as seen further below |
| 62 | if (!delegateData.m_Network) |
| 63 | { |
| 64 | validateFunc(outputTensorInfo, isSupported); |
| 65 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 66 | } |
| 67 | |
| 68 | // Add a Cast layer |
| 69 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddCastLayer(); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 70 | layer->SetBackendId(setBackend); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 71 | ARMNN_ASSERT(layer != nullptr); |
| 72 | |
| 73 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 74 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 75 | |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 76 | // try to connect the Constant Inputs if there are any |
| 77 | if(ProcessInputs(layer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk ) |
| 78 | { |
| 79 | return kTfLiteError; |
| 80 | } |
| 81 | |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 82 | // Connect |
| 83 | return Connect(layer, tfLiteNode, delegateData); |
| 84 | } |
| 85 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 86 | TfLiteStatus VisitReshapeOperator(DelegateData& delegateData, |
| 87 | TfLiteContext* tfLiteContext, |
| 88 | TfLiteNode* tfLiteNode, |
| 89 | int nodeIndex, |
| 90 | int32_t operatorCode) |
| 91 | { |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 92 | auto numInputs = tfLiteNode->inputs->size; |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 93 | |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 94 | if (numInputs == 2) |
| 95 | { |
| 96 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 101 | } |
| 102 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 103 | |
| 104 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 105 | const TfLiteTensor& tfLiteInputTensor0 = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 106 | if (!IsValid(tfLiteContext, tfLiteInputTensor0, operatorCode, nodeIndex)) |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 107 | { |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 108 | return kTfLiteError; |
| 109 | } |
| 110 | |
| 111 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 112 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 113 | { |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 114 | return kTfLiteError; |
| 115 | } |
| 116 | |
| 117 | const armnn::TensorInfo& inputTensorInfo0 = GetTensorInfoForTfLiteTensor(tfLiteInputTensor0); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 118 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 119 | |
| 120 | armnn::ReshapeDescriptor reshapeDesc; |
Narumol Prangnawarat | 7f6c667 | 2020-11-24 18:40:42 +0000 | [diff] [blame] | 121 | std::vector<int32_t> targetShape; |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 122 | |
| 123 | TfLiteReshapeParams* reshapeOptions = reinterpret_cast<TfLiteReshapeParams*>(tfLiteNode->builtin_data); |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 124 | |
| 125 | // The new shape can be defined by either a second input tensor or by a builtin option, we need to check for both. |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 126 | // Options might be set without valid data. we need to check the dimensions are in a valid range. |
| 127 | if (reshapeOptions && reshapeOptions->num_dimensions > 0 && reshapeOptions->num_dimensions <= 8) |
| 128 | { |
| 129 | for (int i=0; i < reshapeOptions->num_dimensions; ++i) |
| 130 | { |
| 131 | targetShape.push_back(reshapeOptions->shape[i]); |
| 132 | } |
| 133 | } |
| 134 | else if (numInputs == 2) |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 135 | { |
Narumol Prangnawarat | 7f6c667 | 2020-11-24 18:40:42 +0000 | [diff] [blame] | 136 | // Get shape from the second input tensor |
| 137 | const TfLiteTensor& tfLiteShapeInputTensor = tfLiteTensors[tfLiteNode->inputs->data[1]]; |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 138 | if (!IsValid(tfLiteContext, tfLiteShapeInputTensor, operatorCode, nodeIndex)) |
David Monahan | e03d9c2 | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 139 | { |
Narumol Prangnawarat | 7f6c667 | 2020-11-24 18:40:42 +0000 | [diff] [blame] | 140 | return kTfLiteError; |
| 141 | } |
| 142 | |
| 143 | if (tfLiteShapeInputTensor.dims->size != 1) |
| 144 | { |
| 145 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 146 | "TfLiteArmnnDelegate: Target 'shape' input is not a 1D tensor in " |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 147 | "operator #%d node #%d: Falling back to TfLiteOptions.", |
Narumol Prangnawarat | 7f6c667 | 2020-11-24 18:40:42 +0000 | [diff] [blame] | 148 | operatorCode, nodeIndex); |
Narumol Prangnawarat | 7f6c667 | 2020-11-24 18:40:42 +0000 | [diff] [blame] | 149 | } |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 150 | else |
Narumol Prangnawarat | 7f6c667 | 2020-11-24 18:40:42 +0000 | [diff] [blame] | 151 | { |
Matthew Sloyan | f00f6c2 | 2020-12-07 13:33:24 +0000 | [diff] [blame] | 152 | // Get the shape data out of the input tensor |
| 153 | auto* shapeTensorDataPtr = tflite::GetTensorData<int32_t>(&tfLiteShapeInputTensor); |
| 154 | auto shapeTensorNumValues = tfLiteShapeInputTensor.dims->data[0]; |
| 155 | for (auto i=0; i < shapeTensorNumValues; ++i) |
| 156 | { |
| 157 | targetShape.push_back(*(shapeTensorDataPtr+i)); |
| 158 | } |
David Monahan | e03d9c2 | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 161 | else |
David Monahan | e03d9c2 | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 162 | { |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 163 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 164 | "Target shape not defined in reshape parameters or input tensor. " |
| 165 | "At least one method required in operator #%d node #%d: ", |
| 166 | operatorCode, nodeIndex); |
| 167 | return kTfLiteError; |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 168 | } |
David Monahan | e03d9c2 | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 169 | |
| 170 | // Use the data to create the required tensor shape. |
| 171 | if (CreateOutputTensorShape(inputTensorInfo0, targetShape, reshapeDesc) != kTfLiteOk) |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 172 | { |
| 173 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
David Monahan | e03d9c2 | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 174 | "TfLiteArmnnDelegate: At most one component of shape can be -1 in: " |
| 175 | "operator #%d node #%d: ", |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 176 | operatorCode, nodeIndex); |
David Monahan | e03d9c2 | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 177 | return kTfLiteError; |
| 178 | } |
| 179 | |
| 180 | if (reshapeDesc.m_TargetShape.GetNumElements() != inputTensorInfo0.GetNumElements()) |
| 181 | { |
Narumol Prangnawarat | 7f6c667 | 2020-11-24 18:40:42 +0000 | [diff] [blame] | 182 | TF_LITE_MAYBE_KERNEL_LOG( |
| 183 | tfLiteContext, |
| 184 | "TfLiteArmnnDelegate: Reshape, number of elements in output shape does not match input " |
| 185 | "operator #%d node #%d: ", |
| 186 | operatorCode, nodeIndex); |
David Monahan | e03d9c2 | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 187 | return kTfLiteError; |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | bool isSupported = false; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 191 | armnn::BackendId setBackend; |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 192 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 193 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 194 | FORWARD_LAYER_SUPPORT_FUNC("RESHAPE", |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 195 | tfLiteContext, |
| 196 | IsReshapeSupported, |
| 197 | delegateData.m_Backends, |
| 198 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 199 | setBackend, |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 200 | inputTensorInfo0, |
| 201 | outInfo, |
| 202 | reshapeDesc); |
| 203 | }; |
| 204 | |
| 205 | if (!delegateData.m_Network) |
| 206 | { |
| 207 | validateFunc(outputTensorInfo, isSupported); |
| 208 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 209 | } |
| 210 | |
| 211 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddReshapeLayer(reshapeDesc); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 212 | layer->SetBackendId(setBackend); |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 213 | ARMNN_ASSERT(layer != nullptr); |
| 214 | |
| 215 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 216 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 217 | |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 218 | // try to connect the Constant Inputs if there are any |
| 219 | if(ProcessInputs(layer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk ) |
| 220 | { |
| 221 | return kTfLiteError; |
| 222 | } |
| 223 | |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 224 | // Connect |
| 225 | return Connect(layer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | TfLiteStatus VisitSqueezeOperator(DelegateData& delegateData, |
| 229 | TfLiteContext* tfLiteContext, |
| 230 | TfLiteNode* tfLiteNode, |
| 231 | int nodeIndex, |
| 232 | int32_t operatorCode) |
| 233 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 234 | armnn::IgnoreUnused(delegateData, |
| 235 | tfLiteContext, |
| 236 | tfLiteNode, |
| 237 | nodeIndex, |
| 238 | operatorCode); |
| 239 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 240 | return kTfLiteError; |
| 241 | } |
| 242 | |
| 243 | TfLiteStatus VisitExpandDimsOperator(DelegateData& delegateData, |
| 244 | TfLiteContext* tfLiteContext, |
| 245 | TfLiteNode* tfLiteNode, |
| 246 | int nodeIndex, |
| 247 | int32_t operatorCode) |
| 248 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 249 | armnn::IgnoreUnused(delegateData, |
| 250 | tfLiteContext, |
| 251 | tfLiteNode, |
| 252 | nodeIndex, |
| 253 | operatorCode); |
| 254 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 255 | return kTfLiteError; |
| 256 | } |
| 257 | |
| 258 | } // namespace armnnDelegate |