blob: e6779f360a8c9b1ace0ce8b854780fa1059ce74f [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
Ryan OShea3ad2e142023-01-13 10: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
Matthew Sloyan2b04ec32023-04-26 11:42:46 +01008#include <DelegateUtils.hpp>
Finn Williams6f9f9902020-11-13 13:23:15 +00009
Sadik Armagan62483be2020-10-23 17:14:43 +010010#include <tensorflow/lite/builtin_ops.h>
11#include <tensorflow/lite/c/builtin_op_data.h>
12#include <tensorflow/lite/c/common.h>
Matthew Sloyan91c41712020-11-13 09:47:35 +000013#include <tensorflow/lite/kernels/internal/tensor_ctypes.h>
Sadik Armagan62483be2020-10-23 17:14:43 +010014#include <tensorflow/lite/minimal_logging.h>
15
Matthew Sloyan91c41712020-11-13 09:47:35 +000016#include <algorithm>
17#include <iterator>
18#include <string>
19#include <vector>
20
Sadik Armagan62483be2020-10-23 17:14:43 +010021namespace armnnDelegate
22{
23
Matthew Sloyan91c41712020-11-13 09:47:35 +000024TfLiteStatus VisitConcatenationOperator(DelegateData& delegateData,
25 TfLiteContext* tfLiteContext,
26 TfLiteNode* tfLiteNode,
27 int nodeIndex,
28 int32_t tfLiteConcatOperatorCode)
29{
30 unsigned int numInputs = tfLiteNode->inputs->size;
31 if (numInputs < 2)
32 {
33 TF_LITE_MAYBE_KERNEL_LOG(
34 tfLiteContext, "TfLiteArmnnDelegate: Minimum number of inputs (%d != %d) in node #%d",
35 2, numInputs, nodeIndex);
36 return kTfLiteError;
37 }
38 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
39
40 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
41
42 std::vector<armnn::TensorInfo> inputTensorInfos;
43 for (unsigned int i = 0; i < numInputs; ++i)
44 {
45 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[i]];
Sadik Armagan529195f2022-01-14 12:56:35 +000046 if (!IsValid(tfLiteContext, tfLiteInputTensor, tfLiteConcatOperatorCode, nodeIndex))
Matthew Sloyan91c41712020-11-13 09:47:35 +000047 {
Matthew Sloyan91c41712020-11-13 09:47:35 +000048 return kTfLiteError;
49 }
50
51 armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
52 inputTensorInfos.emplace_back(inputTensorInfo);
53 }
54
55 // Convert input tensors to const armnn::TensorInfo* type for FORWARD_LAYER_SUPPORT_FUNC.
56 std::vector<const armnn::TensorInfo*> inputConstTensorInfos;
57 std::transform(inputTensorInfos.begin(),
58 inputTensorInfos.end(),
59 std::back_inserter(inputConstTensorInfos),
60 [](armnn::TensorInfo& t)->const armnn::TensorInfo*{ return &t; });
61
62 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
Sadik Armagan529195f2022-01-14 12:56:35 +000063 if (!IsValid(tfLiteContext, tfLiteOutputTensor, tfLiteConcatOperatorCode, nodeIndex))
Matthew Sloyan91c41712020-11-13 09:47:35 +000064 {
Matthew Sloyan91c41712020-11-13 09:47:35 +000065 return kTfLiteError;
66 }
67
68 // Setup OriginsDescriptor, axis and view origin
69 unsigned int numConcatView = static_cast<unsigned int>(numInputs);
70 uint32_t inputRank = tfLiteTensors[tfLiteNode->inputs->data[0]].dims->size;
71
72 auto* concatenationParameters = reinterpret_cast<TfLiteConcatenationParams*>(tfLiteNode->builtin_data);
Ryan OShea3ad2e142023-01-13 10:19:20 +000073
74 if(!concatenationParameters)
75 {
76 throw armnn::Exception(&"TfLiteArmnnDelegate: Concat parameters are null in: " [ nodeIndex]);
77 }
78
Matthew Sloyan91c41712020-11-13 09:47:35 +000079 const unsigned int concatDimInput = static_cast<unsigned int>(
80 (static_cast<int>(inputRank) + concatenationParameters->axis) % static_cast<int>(inputRank));
81
82 armnn::OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
83 concatDescriptor.SetConcatAxis(concatDimInput);
84
85 unsigned int mergeDimOrigin = 0;
86 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
87 {
88 armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(
89 tfLiteTensors[tfLiteNode->inputs->data[viewIndex]]);
90
91 // Sets up concatDescriptor view origin
92 SetupConcatViewOrigin(inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
93 }
94
Sadik Armagan90a119b2022-08-05 16:12:49 +010095 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyan91c41712020-11-13 09:47:35 +000096
Ryan OShea3ad2e142023-01-13 10:19:20 +000097 // Verify we support the fused activation before attempting to create a layer
98 TfLiteFusedActivation activationType = concatenationParameters->activation;
99
Ryan OShea3ad2e142023-01-13 10:19:20 +0000100 TfLiteStatus activationStatus = ValidateFusedActivationOperator(delegateData, tfLiteContext, outputTensorInfo,
101 outputTensorInfo, activationType);
102 if(activationStatus != kTfLiteOk)
103 {
104 return kTfLiteError;
105 }
106
Matthew Sloyan91c41712020-11-13 09:47:35 +0000107 // Check if supported
108 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +0100109 armnn::BackendId setBackend;
Matthew Sloyan91c41712020-11-13 09:47:35 +0000110 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
111 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000112 FORWARD_LAYER_SUPPORT_FUNC("CONCATENATION",
Matthew Sloyan91c41712020-11-13 09:47:35 +0000113 tfLiteContext,
114 IsConcatSupported,
115 delegateData.m_Backends,
116 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100117 setBackend,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000118 inputConstTensorInfos,
119 outputTensorInfo,
120 concatDescriptor);
121 };
122
123 if (!delegateData.m_Network)
124 {
125 validateFunc(outputTensorInfo, isSupported);
126 return isSupported ? kTfLiteOk : kTfLiteError;
127 }
128
129 // Setup layer and connect.
130 armnn::IConnectableLayer* concatenationLayer = delegateData.m_Network->AddConcatLayer(concatDescriptor);
Cathal Corbett53837672022-09-01 11:34:37 +0100131 concatenationLayer->SetBackendId(setBackend);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000132 ARMNN_ASSERT(concatenationLayer != nullptr);
133
Sadik Armagan529195f2022-01-14 12:56:35 +0000134 // Connect the Constant Inputs
135 auto inputsTensorsProcess = ProcessInputs(concatenationLayer,
136 delegateData,
137 tfLiteContext,
138 tfLiteNode);
139 if (inputsTensorsProcess == kTfLiteError)
140 {
141 return inputsTensorsProcess;
142 }
143
Matthew Sloyan91c41712020-11-13 09:47:35 +0000144 armnn::IOutputSlot& outputSlot = concatenationLayer->GetOutputSlot(0);
145 outputSlot.SetTensorInfo(outputTensorInfo);
Ryan OSheaa544f0f2023-01-25 18:10:20 +0000146 if(Connect(concatenationLayer, tfLiteNode, delegateData) != kTfLiteOk)
147 {
148 return kTfLiteError;
149 }
Matthew Sloyan91c41712020-11-13 09:47:35 +0000150
Ryan OShea3ad2e142023-01-13 10:19:20 +0000151 if (activationType == kTfLiteActNone)
Matthew Sloyan91c41712020-11-13 09:47:35 +0000152 {
153 // No Activation
154 return kTfLiteOk;
155 }
156
Ryan OShea3ad2e142023-01-13 10:19:20 +0000157 // Check and Create activation
Matthew Sloyan91c41712020-11-13 09:47:35 +0000158 return FusedActivation(tfLiteContext, tfLiteNode, activationType, concatenationLayer, 0, delegateData);
159}
160
161TfLiteStatus VisitMeanOperator(DelegateData& delegateData,
162 TfLiteContext* tfLiteContext,
163 TfLiteNode* tfLiteNode,
164 int nodeIndex,
165 int32_t tfLiteMeanOperatorCode)
166{
167 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
168 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
169
170 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
171 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
172 if(!IsValid(&tfLiteInputTensor))
173 {
174 TF_LITE_MAYBE_KERNEL_LOG(
175 tfLiteContext,
176 "TfLiteArmnnDelegate: Invalid input tensor in operator #%d node #%d: ",
177 tfLiteMeanOperatorCode, nodeIndex);
178 return kTfLiteError;
179 }
180 if (IsDynamicTensor(tfLiteInputTensor))
181 {
182 TF_LITE_MAYBE_KERNEL_LOG(
183 tfLiteContext,
184 "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
185 tfLiteMeanOperatorCode, nodeIndex);
186 return kTfLiteError;
187 }
188
189 const TfLiteTensor& tfLiteAxisTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
190 if(!IsValid(&tfLiteAxisTensor))
191 {
192 TF_LITE_MAYBE_KERNEL_LOG(
193 tfLiteContext,
194 "TfLiteArmnnDelegate: Invalid axis tensor in operator #%d node #%d: ",
195 tfLiteMeanOperatorCode, nodeIndex);
196 return kTfLiteError;
197 }
198 if (IsDynamicTensor(tfLiteAxisTensor))
199 {
200 TF_LITE_MAYBE_KERNEL_LOG(
201 tfLiteContext,
202 "TfLiteArmnnDelegate: Dynamic axis tensors are not supported in operator #%d node #%d: ",
203 tfLiteMeanOperatorCode, nodeIndex);
204 return kTfLiteError;
205 }
206
207 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
208 if(!IsValid(&tfLiteOutputTensor))
209 {
210 TF_LITE_MAYBE_KERNEL_LOG(
211 tfLiteContext,
212 "TfLiteArmnnDelegate: Invalid output tensor in operator #%d node #%d: ",
213 tfLiteAxisTensor, nodeIndex);
214 return kTfLiteError;
215 }
216 if (IsDynamicTensor(tfLiteOutputTensor))
217 {
218 TF_LITE_MAYBE_KERNEL_LOG(
219 tfLiteContext,
220 "TfLiteArmnnDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ",
221 tfLiteMeanOperatorCode, nodeIndex);
222 return kTfLiteError;
223 }
224
225 const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
226 const armnn::TensorInfo& axisTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteAxisTensor);
Sadik Armagan90a119b2022-08-05 16:12:49 +0100227 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000228
229 auto* axisTensorData = tflite::GetTensorData<int32_t>(&tfLiteAxisTensor);
230
231 std::vector<int32_t> axis;
232 // Add axis data to vector to be converter to unsigned int and assigned to descriptor axis.
233 for (unsigned int i = 0; i < axisTensorInfo.GetNumElements(); ++i)
234 {
235 axis.emplace_back(axisTensorData[i]);
236 }
237
238 // Convert the axis to unsigned int and remove duplicates.
239 unsigned int rank = inputTensorInfo.GetNumDimensions();
240 std::set<unsigned int> uniqueAxis;
241 std::transform(axis.begin(),
242 axis.end(),
243 std::inserter(uniqueAxis, uniqueAxis.begin()),
244 [rank](int i)->unsigned int{ return (i + rank) % rank; });
245
246 // Setup MeanDescriptor and assign axis and keepDims
247 armnn::MeanDescriptor desc;
248 desc.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
249 desc.m_KeepDims = inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? true : false;
250
251 // Check if supported
252 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +0100253 armnn::BackendId setBackend;
Matthew Sloyan91c41712020-11-13 09:47:35 +0000254 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
255 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000256 FORWARD_LAYER_SUPPORT_FUNC("MEAN",
Matthew Sloyan91c41712020-11-13 09:47:35 +0000257 tfLiteContext,
258 IsMeanSupported,
259 delegateData.m_Backends,
260 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100261 setBackend,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000262 inputTensorInfo,
263 outputTensorInfo,
264 desc);
265 };
266
267 if (!delegateData.m_Network)
268 {
269 validateFunc(outputTensorInfo, isSupported);
270 return isSupported ? kTfLiteOk : kTfLiteError;
271 }
272
273 // Setup layer and connect.
274 armnn::IConnectableLayer* meanLayer = delegateData.m_Network->AddMeanLayer(desc);
Cathal Corbett53837672022-09-01 11:34:37 +0100275 meanLayer->SetBackendId(setBackend);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000276 ARMNN_ASSERT(meanLayer != nullptr);
277
278 armnn::IOutputSlot& outputSlot = meanLayer->GetOutputSlot(0);
279 outputSlot.SetTensorInfo(outputTensorInfo);
Ryan OShea4c231de2023-01-17 15:19:20 +0000280
281 // try to connect the Constant Inputs if there are any
282 if(ProcessInputs(meanLayer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk )
283 {
284 return kTfLiteError;
285 }
286
Matthew Sloyan91c41712020-11-13 09:47:35 +0000287 return Connect(meanLayer, tfLiteNode, delegateData);
288}
289
Sadik Armagan62483be2020-10-23 17:14:43 +0100290TfLiteStatus VisitControlOperator(DelegateData& delegateData,
291 TfLiteContext* tfLiteContext,
292 TfLiteNode* tfLiteNode,
293 int nodeIndex,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000294 int32_t operatorCode)
Sadik Armagan62483be2020-10-23 17:14:43 +0100295{
Matthew Sloyan91c41712020-11-13 09:47:35 +0000296 switch(operatorCode)
297 {
298 case kTfLiteBuiltinConcatenation:
299 return VisitConcatenationOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode);
300 case kTfLiteBuiltinMean:
301 return VisitMeanOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode);
302 default:
303 return kTfLiteError;
304 }
Sadik Armagan62483be2020-10-23 17:14:43 +0100305}
306
307} // namespace armnnDelegate