blob: fb58ffdf7036b3cae1c343d564411865ddad1e99 [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Finn Williams6f9f9902020-11-13 13:23:15 +00008#include <armnn/utility/IgnoreUnused.hpp>
9
David Monahan1670b0c2020-11-18 14:40:27 +000010#include "DelegateUtils.hpp"
11
Sadik Armagan62483be2020-10-23 17:14:43 +010012#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 Monahan1670b0c2020-11-18 14:40:27 +000016#include <numeric>
Sadik Armagan62483be2020-10-23 17:14:43 +010017
18namespace armnnDelegate
19{
20
David Monahan1670b0c2020-11-18 14:40:27 +000021TfLiteStatus 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 Armagan62483be2020-10-23 17:14:43 +010050TfLiteStatus VisitReshapeOperator(DelegateData& delegateData,
51 TfLiteContext* tfLiteContext,
52 TfLiteNode* tfLiteNode,
53 int nodeIndex,
54 int32_t operatorCode)
55{
David Monahan1670b0c2020-11-18 14:40:27 +000056 auto numInputs = tfLiteNode->inputs->size;
Finn Williams6f9f9902020-11-13 13:23:15 +000057
David Monahan1670b0c2020-11-18 14:40:27 +000058 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 Monahane03d9c22020-11-20 09:58:54 +000095 TfLiteReshapeParams* reshapeOptions = reinterpret_cast<TfLiteReshapeParams*>(tfLiteNode->builtin_data);
96 std::vector<int32_t> targetShape;
97 bool targetShapeFound = false;
98
99 if (reshapeOptions != nullptr)
David Monahan1670b0c2020-11-18 14:40:27 +0000100 {
David Monahane03d9c22020-11-20 09:58:54 +0000101 // 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]);
108 elementCounter = elementCounter * reshapeOptions->shape[i];
109 }
110 // Check the number of elements match, otherwise fall back to using the second input tensor.
111 if (elementCounter == inputTensorInfo0.GetNumElements())
112 {
113 targetShapeFound = true;
114 }
115 }
116 }
117 if (!targetShapeFound)
118 {
119 if (numInputs == 2)
120 {
121 const TfLiteTensor& tfLiteShapeInputTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
122 if (IsDynamicTensor(tfLiteShapeInputTensor))
123 {
124 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
125 "TfLiteArmnnDelegate: Dynamic input tensors are not supported in "
126 "operator #%d node #%d: ",
127 operatorCode, nodeIndex);
128 return kTfLiteError;
129 }
130
131 if (tfLiteShapeInputTensor.dims->size != 1)
132 {
133 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
134 "TfLiteArmnnDelegate: Target 'shape' input is not a 1D tensor in "
135 "operator #%d node #%d: ",
136 operatorCode, nodeIndex);
137 return kTfLiteError;
138 }
139
140 // Get the shape data out of the input tensor
141 auto* shapeTensorDataPtr = tflite::GetTensorData<int32_t>(&tfLiteShapeInputTensor);
142 auto shapeTensorNumValues = tfLiteShapeInputTensor.dims->data[0];
143 for (auto i=0; i < shapeTensorNumValues; ++i)
144 {
145 targetShape.push_back(*(shapeTensorDataPtr+i));
146 }
147 }
148 else
David Monahan1670b0c2020-11-18 14:40:27 +0000149 {
150 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
David Monahane03d9c22020-11-20 09:58:54 +0000151 "Target shape not defined in reshape parameters or input tensor. "
152 "At least one method required in operator #%d node #%d: ",
David Monahan1670b0c2020-11-18 14:40:27 +0000153 operatorCode, nodeIndex);
154 return kTfLiteError;
155 }
156 }
David Monahane03d9c22020-11-20 09:58:54 +0000157
158 // Use the data to create the required tensor shape.
159 if (CreateOutputTensorShape(inputTensorInfo0, targetShape, reshapeDesc) != kTfLiteOk)
David Monahan1670b0c2020-11-18 14:40:27 +0000160 {
161 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
David Monahane03d9c22020-11-20 09:58:54 +0000162 "TfLiteArmnnDelegate: At most one component of shape can be -1 in: "
163 "operator #%d node #%d: ",
David Monahan1670b0c2020-11-18 14:40:27 +0000164 operatorCode, nodeIndex);
David Monahane03d9c22020-11-20 09:58:54 +0000165 return kTfLiteError;
166 }
167
168 if (reshapeDesc.m_TargetShape.GetNumElements() != inputTensorInfo0.GetNumElements())
169 {
170 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
171 "TfLiteArmnnDelegate: Reshape, number of elements in output shape does not match input "
172 "operator #%d node #%d: ",
173 operatorCode, nodeIndex);
174 return kTfLiteError;
David Monahan1670b0c2020-11-18 14:40:27 +0000175 }
176
177 bool isSupported = false;
178 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
179 {
180 FORWARD_LAYER_SUPPORT_FUNC(__func__,
181 tfLiteContext,
182 IsReshapeSupported,
183 delegateData.m_Backends,
184 isSupported,
185 inputTensorInfo0,
186 outInfo,
187 reshapeDesc);
188 };
189
190 if (!delegateData.m_Network)
191 {
192 validateFunc(outputTensorInfo, isSupported);
193 return isSupported ? kTfLiteOk : kTfLiteError;
194 }
195
196 armnn::IConnectableLayer* layer = delegateData.m_Network->AddReshapeLayer(reshapeDesc);
197 ARMNN_ASSERT(layer != nullptr);
198
199 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
200 outputSlot.SetTensorInfo(outputTensorInfo);
201
202 // Connect
203 return Connect(layer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100204}
205
206TfLiteStatus VisitSqueezeOperator(DelegateData& delegateData,
207 TfLiteContext* tfLiteContext,
208 TfLiteNode* tfLiteNode,
209 int nodeIndex,
210 int32_t operatorCode)
211{
Finn Williams6f9f9902020-11-13 13:23:15 +0000212 armnn::IgnoreUnused(delegateData,
213 tfLiteContext,
214 tfLiteNode,
215 nodeIndex,
216 operatorCode);
217
Sadik Armagan62483be2020-10-23 17:14:43 +0100218 return kTfLiteError;
219}
220
221TfLiteStatus VisitExpandDimsOperator(DelegateData& delegateData,
222 TfLiteContext* tfLiteContext,
223 TfLiteNode* tfLiteNode,
224 int nodeIndex,
225 int32_t operatorCode)
226{
Finn Williams6f9f9902020-11-13 13:23:15 +0000227 armnn::IgnoreUnused(delegateData,
228 tfLiteContext,
229 tfLiteNode,
230 nodeIndex,
231 operatorCode);
232
Sadik Armagan62483be2020-10-23 17:14:43 +0100233 return kTfLiteError;
234}
235
236} // namespace armnnDelegate