blob: 864fb7af67a1b60a7719309d503b4142ce2d87d8 [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
Ryan OShea4c231de2023-01-17 15:19:20 +00002// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan62483be2020-10-23 17:14:43 +01003// 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
Sadik Armagan937565b2021-04-21 14:03:28 +010021TfLiteStatus VisitCastOperator(DelegateData& delegateData,
22 TfLiteContext* tfLiteContext,
23 TfLiteNode* tfLiteNode,
24 int nodeIndex,
25 int32_t operatorCode)
26{
27 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
28 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
29
30 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
31 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
32 if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex))
33 {
34 return kTfLiteError;
35 }
36
37 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
38 if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex))
39 {
40 return kTfLiteError;
41 }
42
43 const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
Sadik Armagan90a119b2022-08-05 16:12:49 +010044 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Sadik Armagan937565b2021-04-21 14:03:28 +010045
46 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +010047 armnn::BackendId setBackend;
Sadik Armagan937565b2021-04-21 14:03:28 +010048 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
49 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000050 FORWARD_LAYER_SUPPORT_FUNC("CAST",
Sadik Armagan937565b2021-04-21 14:03:28 +010051 tfLiteContext,
52 IsCastSupported,
53 delegateData.m_Backends,
54 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +010055 setBackend,
Sadik Armagan937565b2021-04-21 14:03:28 +010056 inputTensorInfo,
57 outInfo);
58 };
59
60 // If the m_Network is a nullptr, this signals that a prerequisite TfLite callback is required to clarify the
61 // support for the operator
62 // If supported, VisitCastOperator will be called again to add the layer to the network as seen further below
63 if (!delegateData.m_Network)
64 {
65 validateFunc(outputTensorInfo, isSupported);
66 return isSupported ? kTfLiteOk : kTfLiteError;
67 }
68
69 // Add a Cast layer
70 armnn::IConnectableLayer* layer = delegateData.m_Network->AddCastLayer();
Cathal Corbett53837672022-09-01 11:34:37 +010071 layer->SetBackendId(setBackend);
Sadik Armagan937565b2021-04-21 14:03:28 +010072 ARMNN_ASSERT(layer != nullptr);
73
74 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
75 outputSlot.SetTensorInfo(outputTensorInfo);
76
Ryan OShea4c231de2023-01-17 15:19:20 +000077 // try to connect the Constant Inputs if there are any
78 if(ProcessInputs(layer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk )
79 {
80 return kTfLiteError;
81 }
82
Sadik Armagan937565b2021-04-21 14:03:28 +010083 // Connect
84 return Connect(layer, tfLiteNode, delegateData);
85}
86
87
David Monahan1670b0c2020-11-18 14:40:27 +000088TfLiteStatus CreateOutputTensorShape(const armnn::TensorInfo& inputTensorInfo,
Matthew Sloyanf00f6c22020-12-07 13:33:24 +000089 const std::vector<int32_t>& targetShape,
90 armnn::ReshapeDescriptor& reshapeDesc)
David Monahan1670b0c2020-11-18 14:40:27 +000091{
92 std::vector<unsigned int> outputDims(targetShape.begin(), targetShape.end());
93 const auto stretchDim = std::find(targetShape.begin(), targetShape.end(), -1);
94
95 if (stretchDim != targetShape.end())
96 {
97 if (std::find(std::next(stretchDim), targetShape.end(), -1) != targetShape.end())
98 {
99 // Return kTfLiteError and log the error after returning
100 return kTfLiteError;
101 }
102
103 auto targetNumElements =
104 armnn::numeric_cast<unsigned int>(
105 std::accumulate(targetShape.begin(), targetShape.end(), -1, std::multiplies<int32_t>()));
106
107 auto stretchIndex = static_cast<size_t>(std::distance(targetShape.begin(), stretchDim));
108 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
109 }
110
111 armnn::TensorShape outputShape = armnn::TensorShape(static_cast<unsigned int>(outputDims.size()),
112 outputDims.data());
113 reshapeDesc.m_TargetShape = outputShape;
114 return kTfLiteOk;
115}
116
Sadik Armagan62483be2020-10-23 17:14:43 +0100117TfLiteStatus VisitReshapeOperator(DelegateData& delegateData,
118 TfLiteContext* tfLiteContext,
119 TfLiteNode* tfLiteNode,
120 int nodeIndex,
121 int32_t operatorCode)
122{
David Monahan1670b0c2020-11-18 14:40:27 +0000123 auto numInputs = tfLiteNode->inputs->size;
Finn Williams6f9f9902020-11-13 13:23:15 +0000124
David Monahan1670b0c2020-11-18 14:40:27 +0000125 if (numInputs == 2)
126 {
127 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
128 }
129 else
130 {
131 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
132 }
133 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
134
135 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
136 const TfLiteTensor& tfLiteInputTensor0 = tfLiteTensors[tfLiteNode->inputs->data[0]];
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000137 if (!IsValid(tfLiteContext, tfLiteInputTensor0, operatorCode, nodeIndex))
David Monahan1670b0c2020-11-18 14:40:27 +0000138 {
David Monahan1670b0c2020-11-18 14:40:27 +0000139 return kTfLiteError;
140 }
141
142 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000143 if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex))
David Monahan1670b0c2020-11-18 14:40:27 +0000144 {
David Monahan1670b0c2020-11-18 14:40:27 +0000145 return kTfLiteError;
146 }
147
148 const armnn::TensorInfo& inputTensorInfo0 = GetTensorInfoForTfLiteTensor(tfLiteInputTensor0);
Sadik Armagan90a119b2022-08-05 16:12:49 +0100149 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
David Monahan1670b0c2020-11-18 14:40:27 +0000150
151 armnn::ReshapeDescriptor reshapeDesc;
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000152 std::vector<int32_t> targetShape;
Finn Williamsf806c4d2021-02-22 15:13:12 +0000153
154 TfLiteReshapeParams* reshapeOptions = reinterpret_cast<TfLiteReshapeParams*>(tfLiteNode->builtin_data);
David Monahan1670b0c2020-11-18 14:40:27 +0000155
156 // The new shape can be defined by either a second input tensor or by a builtin option, we need to check for both.
Finn Williamsf806c4d2021-02-22 15:13:12 +0000157 // Options might be set without valid data. we need to check the dimensions are in a valid range.
158 if (reshapeOptions && reshapeOptions->num_dimensions > 0 && reshapeOptions->num_dimensions <= 8)
159 {
160 for (int i=0; i < reshapeOptions->num_dimensions; ++i)
161 {
162 targetShape.push_back(reshapeOptions->shape[i]);
163 }
164 }
165 else if (numInputs == 2)
David Monahan1670b0c2020-11-18 14:40:27 +0000166 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000167 // Get shape from the second input tensor
168 const TfLiteTensor& tfLiteShapeInputTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000169 if (!IsValid(tfLiteContext, tfLiteShapeInputTensor, operatorCode, nodeIndex))
David Monahane03d9c22020-11-20 09:58:54 +0000170 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000171 return kTfLiteError;
172 }
173
174 if (tfLiteShapeInputTensor.dims->size != 1)
175 {
176 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
177 "TfLiteArmnnDelegate: Target 'shape' input is not a 1D tensor in "
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000178 "operator #%d node #%d: Falling back to TfLiteOptions.",
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000179 operatorCode, nodeIndex);
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000180 }
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000181 else
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000182 {
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000183 // Get the shape data out of the input tensor
184 auto* shapeTensorDataPtr = tflite::GetTensorData<int32_t>(&tfLiteShapeInputTensor);
185 auto shapeTensorNumValues = tfLiteShapeInputTensor.dims->data[0];
186 for (auto i=0; i < shapeTensorNumValues; ++i)
187 {
188 targetShape.push_back(*(shapeTensorDataPtr+i));
189 }
David Monahane03d9c22020-11-20 09:58:54 +0000190 }
191 }
Finn Williamsf806c4d2021-02-22 15:13:12 +0000192 else
David Monahane03d9c22020-11-20 09:58:54 +0000193 {
Finn Williamsf806c4d2021-02-22 15:13:12 +0000194 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
195 "Target shape not defined in reshape parameters or input tensor. "
196 "At least one method required in operator #%d node #%d: ",
197 operatorCode, nodeIndex);
198 return kTfLiteError;
David Monahan1670b0c2020-11-18 14:40:27 +0000199 }
David Monahane03d9c22020-11-20 09:58:54 +0000200
201 // Use the data to create the required tensor shape.
202 if (CreateOutputTensorShape(inputTensorInfo0, targetShape, reshapeDesc) != kTfLiteOk)
David Monahan1670b0c2020-11-18 14:40:27 +0000203 {
204 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
David Monahane03d9c22020-11-20 09:58:54 +0000205 "TfLiteArmnnDelegate: At most one component of shape can be -1 in: "
206 "operator #%d node #%d: ",
David Monahan1670b0c2020-11-18 14:40:27 +0000207 operatorCode, nodeIndex);
David Monahane03d9c22020-11-20 09:58:54 +0000208 return kTfLiteError;
209 }
210
211 if (reshapeDesc.m_TargetShape.GetNumElements() != inputTensorInfo0.GetNumElements())
212 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000213 TF_LITE_MAYBE_KERNEL_LOG(
214 tfLiteContext,
215 "TfLiteArmnnDelegate: Reshape, number of elements in output shape does not match input "
216 "operator #%d node #%d: ",
217 operatorCode, nodeIndex);
David Monahane03d9c22020-11-20 09:58:54 +0000218 return kTfLiteError;
David Monahan1670b0c2020-11-18 14:40:27 +0000219 }
220
221 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +0100222 armnn::BackendId setBackend;
David Monahan1670b0c2020-11-18 14:40:27 +0000223 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
224 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000225 FORWARD_LAYER_SUPPORT_FUNC("RESHAPE",
David Monahan1670b0c2020-11-18 14:40:27 +0000226 tfLiteContext,
227 IsReshapeSupported,
228 delegateData.m_Backends,
229 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100230 setBackend,
David Monahan1670b0c2020-11-18 14:40:27 +0000231 inputTensorInfo0,
232 outInfo,
233 reshapeDesc);
234 };
235
236 if (!delegateData.m_Network)
237 {
238 validateFunc(outputTensorInfo, isSupported);
239 return isSupported ? kTfLiteOk : kTfLiteError;
240 }
241
242 armnn::IConnectableLayer* layer = delegateData.m_Network->AddReshapeLayer(reshapeDesc);
Cathal Corbett53837672022-09-01 11:34:37 +0100243 layer->SetBackendId(setBackend);
David Monahan1670b0c2020-11-18 14:40:27 +0000244 ARMNN_ASSERT(layer != nullptr);
245
246 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
247 outputSlot.SetTensorInfo(outputTensorInfo);
248
Ryan OShea4c231de2023-01-17 15:19:20 +0000249 // try to connect the Constant Inputs if there are any
250 if(ProcessInputs(layer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk )
251 {
252 return kTfLiteError;
253 }
254
David Monahan1670b0c2020-11-18 14:40:27 +0000255 // Connect
256 return Connect(layer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100257}
258
259TfLiteStatus VisitSqueezeOperator(DelegateData& delegateData,
260 TfLiteContext* tfLiteContext,
261 TfLiteNode* tfLiteNode,
262 int nodeIndex,
263 int32_t operatorCode)
264{
Finn Williams6f9f9902020-11-13 13:23:15 +0000265 armnn::IgnoreUnused(delegateData,
266 tfLiteContext,
267 tfLiteNode,
268 nodeIndex,
269 operatorCode);
270
Sadik Armagan62483be2020-10-23 17:14:43 +0100271 return kTfLiteError;
272}
273
274TfLiteStatus VisitExpandDimsOperator(DelegateData& delegateData,
275 TfLiteContext* tfLiteContext,
276 TfLiteNode* tfLiteNode,
277 int nodeIndex,
278 int32_t operatorCode)
279{
Finn Williams6f9f9902020-11-13 13:23:15 +0000280 armnn::IgnoreUnused(delegateData,
281 tfLiteContext,
282 tfLiteNode,
283 nodeIndex,
284 operatorCode);
285
Sadik Armagan62483be2020-10-23 17:14:43 +0100286 return kTfLiteError;
287}
288
289} // namespace armnnDelegate