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 | |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 10 | #include "DelegateUtils.hpp" |
| 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> |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 16 | #include <numeric> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 17 | |
| 18 | namespace armnnDelegate |
| 19 | { |
| 20 | |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 21 | TfLiteStatus CreateOutputTensorShape(const armnn::TensorInfo& inputTensorInfo, |
| 22 | const std::vector<int32_t>& targetShape, |
| 23 | armnn::ReshapeDescriptor& reshapeDesc) |
| 24 | { |
| 25 | std::vector<unsigned int> outputDims(targetShape.begin(), targetShape.end()); |
| 26 | const auto stretchDim = std::find(targetShape.begin(), targetShape.end(), -1); |
| 27 | |
| 28 | if (stretchDim != targetShape.end()) |
| 29 | { |
| 30 | if (std::find(std::next(stretchDim), targetShape.end(), -1) != targetShape.end()) |
| 31 | { |
| 32 | // Return kTfLiteError and log the error after returning |
| 33 | return kTfLiteError; |
| 34 | } |
| 35 | |
| 36 | auto targetNumElements = |
| 37 | armnn::numeric_cast<unsigned int>( |
| 38 | std::accumulate(targetShape.begin(), targetShape.end(), -1, std::multiplies<int32_t>())); |
| 39 | |
| 40 | auto stretchIndex = static_cast<size_t>(std::distance(targetShape.begin(), stretchDim)); |
| 41 | outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements; |
| 42 | } |
| 43 | |
| 44 | armnn::TensorShape outputShape = armnn::TensorShape(static_cast<unsigned int>(outputDims.size()), |
| 45 | outputDims.data()); |
| 46 | reshapeDesc.m_TargetShape = outputShape; |
| 47 | return kTfLiteOk; |
| 48 | } |
| 49 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 50 | TfLiteStatus VisitReshapeOperator(DelegateData& delegateData, |
| 51 | TfLiteContext* tfLiteContext, |
| 52 | TfLiteNode* tfLiteNode, |
| 53 | int nodeIndex, |
| 54 | int32_t operatorCode) |
| 55 | { |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 56 | auto numInputs = tfLiteNode->inputs->size; |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 57 | |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 58 | if (numInputs == 2) |
| 59 | { |
| 60 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 65 | } |
| 66 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 67 | |
| 68 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 69 | const TfLiteTensor& tfLiteInputTensor0 = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
| 70 | if (IsDynamicTensor(tfLiteInputTensor0)) |
| 71 | { |
| 72 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 73 | "TfLiteArmnnDelegate: Dynamic input tensors are not supported in " |
| 74 | "operator #%d node #%d: ", |
| 75 | operatorCode, nodeIndex); |
| 76 | return kTfLiteError; |
| 77 | } |
| 78 | |
| 79 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 80 | if (IsDynamicTensor(tfLiteOutputTensor)) |
| 81 | { |
| 82 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 83 | "TfLiteArmnnDelegate: Dynamic output tensors are not supported in " |
| 84 | "operator #%d node #%d: ", |
| 85 | operatorCode, nodeIndex); |
| 86 | return kTfLiteError; |
| 87 | } |
| 88 | |
| 89 | const armnn::TensorInfo& inputTensorInfo0 = GetTensorInfoForTfLiteTensor(tfLiteInputTensor0); |
| 90 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); |
| 91 | |
| 92 | armnn::ReshapeDescriptor reshapeDesc; |
| 93 | |
| 94 | // The new shape can be defined by either a second input tensor or by a builtin option, we need to check for both. |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 95 | TfLiteReshapeParams* reshapeOptions = reinterpret_cast<TfLiteReshapeParams*>(tfLiteNode->builtin_data); |
| 96 | std::vector<int32_t> targetShape; |
| 97 | bool targetShapeFound = false; |
| 98 | |
| 99 | if (reshapeOptions != nullptr) |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 100 | { |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 101 | // Options might be set without valid data. we need to check the dimensions are in a valid range. |
| 102 | if (reshapeOptions->num_dimensions > 0 && reshapeOptions->num_dimensions <= 8) |
| 103 | { |
| 104 | uint64_t elementCounter = 1; |
| 105 | for (int i=0; i < reshapeOptions->num_dimensions; ++i) |
| 106 | { |
| 107 | targetShape.push_back(reshapeOptions->shape[i]); |
Narumol Prangnawarat | 8c2f651 | 2020-11-20 16:17:48 +0000 | [diff] [blame] | 108 | if (reshapeOptions->shape[i] > 0) |
| 109 | { |
| 110 | elementCounter = elementCounter * reshapeOptions->shape[i]; |
| 111 | } |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 112 | } |
| 113 | // Check the number of elements match, otherwise fall back to using the second input tensor. |
Narumol Prangnawarat | 8c2f651 | 2020-11-20 16:17:48 +0000 | [diff] [blame] | 114 | if (elementCounter <= inputTensorInfo0.GetNumElements()) |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 115 | { |
| 116 | targetShapeFound = true; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | if (!targetShapeFound) |
| 121 | { |
| 122 | if (numInputs == 2) |
| 123 | { |
| 124 | const TfLiteTensor& tfLiteShapeInputTensor = tfLiteTensors[tfLiteNode->inputs->data[1]]; |
| 125 | if (IsDynamicTensor(tfLiteShapeInputTensor)) |
| 126 | { |
| 127 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 128 | "TfLiteArmnnDelegate: Dynamic input tensors are not supported in " |
| 129 | "operator #%d node #%d: ", |
| 130 | operatorCode, nodeIndex); |
| 131 | return kTfLiteError; |
| 132 | } |
| 133 | |
| 134 | if (tfLiteShapeInputTensor.dims->size != 1) |
| 135 | { |
| 136 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 137 | "TfLiteArmnnDelegate: Target 'shape' input is not a 1D tensor in " |
| 138 | "operator #%d node #%d: ", |
| 139 | operatorCode, nodeIndex); |
| 140 | return kTfLiteError; |
| 141 | } |
| 142 | |
| 143 | // Get the shape data out of the input tensor |
| 144 | auto* shapeTensorDataPtr = tflite::GetTensorData<int32_t>(&tfLiteShapeInputTensor); |
| 145 | auto shapeTensorNumValues = tfLiteShapeInputTensor.dims->data[0]; |
| 146 | for (auto i=0; i < shapeTensorNumValues; ++i) |
| 147 | { |
| 148 | targetShape.push_back(*(shapeTensorDataPtr+i)); |
| 149 | } |
| 150 | } |
| 151 | else |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 152 | { |
| 153 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 154 | "Target shape not defined in reshape parameters or input tensor. " |
| 155 | "At least one method required in operator #%d node #%d: ", |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 156 | operatorCode, nodeIndex); |
| 157 | return kTfLiteError; |
| 158 | } |
| 159 | } |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 160 | |
| 161 | // Use the data to create the required tensor shape. |
| 162 | if (CreateOutputTensorShape(inputTensorInfo0, targetShape, reshapeDesc) != kTfLiteOk) |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 163 | { |
| 164 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 165 | "TfLiteArmnnDelegate: At most one component of shape can be -1 in: " |
| 166 | "operator #%d node #%d: ", |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 167 | operatorCode, nodeIndex); |
David Monahan | 195361c | 2020-11-20 09:58:54 +0000 | [diff] [blame] | 168 | return kTfLiteError; |
| 169 | } |
| 170 | |
| 171 | if (reshapeDesc.m_TargetShape.GetNumElements() != inputTensorInfo0.GetNumElements()) |
| 172 | { |
| 173 | TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext, |
| 174 | "TfLiteArmnnDelegate: Reshape, number of elements in output shape does not match input " |
| 175 | "operator #%d node #%d: ", |
| 176 | operatorCode, nodeIndex); |
| 177 | return kTfLiteError; |
David Monahan | 1670b0c | 2020-11-18 14:40:27 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | bool isSupported = false; |
| 181 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 182 | { |
| 183 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 184 | tfLiteContext, |
| 185 | IsReshapeSupported, |
| 186 | delegateData.m_Backends, |
| 187 | isSupported, |
| 188 | inputTensorInfo0, |
| 189 | outInfo, |
| 190 | reshapeDesc); |
| 191 | }; |
| 192 | |
| 193 | if (!delegateData.m_Network) |
| 194 | { |
| 195 | validateFunc(outputTensorInfo, isSupported); |
| 196 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 197 | } |
| 198 | |
| 199 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddReshapeLayer(reshapeDesc); |
| 200 | ARMNN_ASSERT(layer != nullptr); |
| 201 | |
| 202 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 203 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 204 | |
| 205 | // Connect |
| 206 | return Connect(layer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TfLiteStatus VisitSqueezeOperator(DelegateData& delegateData, |
| 210 | TfLiteContext* tfLiteContext, |
| 211 | TfLiteNode* tfLiteNode, |
| 212 | int nodeIndex, |
| 213 | int32_t operatorCode) |
| 214 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 215 | armnn::IgnoreUnused(delegateData, |
| 216 | tfLiteContext, |
| 217 | tfLiteNode, |
| 218 | nodeIndex, |
| 219 | operatorCode); |
| 220 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 221 | return kTfLiteError; |
| 222 | } |
| 223 | |
| 224 | TfLiteStatus VisitExpandDimsOperator(DelegateData& delegateData, |
| 225 | TfLiteContext* tfLiteContext, |
| 226 | TfLiteNode* tfLiteNode, |
| 227 | int nodeIndex, |
| 228 | int32_t operatorCode) |
| 229 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 230 | armnn::IgnoreUnused(delegateData, |
| 231 | tfLiteContext, |
| 232 | tfLiteNode, |
| 233 | nodeIndex, |
| 234 | operatorCode); |
| 235 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 236 | return kTfLiteError; |
| 237 | } |
| 238 | |
| 239 | } // namespace armnnDelegate |