blob: 17f23d81adb0981dbc16d3555f5e3f9099f20bd1 [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
Finn Williams6f9f9902020-11-13 13:23:15 +00008#include <armnn/utility/IgnoreUnused.hpp>
9
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 +000024void SetupConcatViewOrigin(const armnn::TensorInfo& inputTensorInfo,
25 armnn::OriginsDescriptor& concatDescriptor,
26 const unsigned int concatAxis,
27 unsigned int inputIndex,
28 unsigned int& mergeDimOrigin)
29{
30 const uint32_t inputRank = concatDescriptor.GetNumDimensions();
31
32 // double check dimensions of the tensors
33 if (inputTensorInfo.GetNumDimensions() != inputRank)
34 {
35 throw armnn::ParseException("The number of dimensions for input tensors "
36 "of the concatenation operator should be: " + std::to_string(inputRank));
37 }
38
39 for (unsigned int j = 0; j < concatAxis; ++j)
40 {
41 concatDescriptor.SetViewOriginCoord(inputIndex, j, 0);
42 }
43
44 concatDescriptor.SetViewOriginCoord(inputIndex, concatAxis, mergeDimOrigin);
45 mergeDimOrigin += inputTensorInfo.GetShape()[concatAxis];
46
47 for (unsigned int j = concatAxis + 1; j < inputRank; ++j)
48 {
49 concatDescriptor.SetViewOriginCoord(inputIndex, j, 0);
50 }
51}
52
53TfLiteStatus VisitConcatenationOperator(DelegateData& delegateData,
54 TfLiteContext* tfLiteContext,
55 TfLiteNode* tfLiteNode,
56 int nodeIndex,
57 int32_t tfLiteConcatOperatorCode)
58{
59 unsigned int numInputs = tfLiteNode->inputs->size;
60 if (numInputs < 2)
61 {
62 TF_LITE_MAYBE_KERNEL_LOG(
63 tfLiteContext, "TfLiteArmnnDelegate: Minimum number of inputs (%d != %d) in node #%d",
64 2, numInputs, nodeIndex);
65 return kTfLiteError;
66 }
67 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
68
69 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
70
71 std::vector<armnn::TensorInfo> inputTensorInfos;
72 for (unsigned int i = 0; i < numInputs; ++i)
73 {
74 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[i]];
Sadik Armagan529195f2022-01-14 12:56:35 +000075 if (!IsValid(tfLiteContext, tfLiteInputTensor, tfLiteConcatOperatorCode, nodeIndex))
Matthew Sloyan91c41712020-11-13 09:47:35 +000076 {
Matthew Sloyan91c41712020-11-13 09:47:35 +000077 return kTfLiteError;
78 }
79
80 armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
81 inputTensorInfos.emplace_back(inputTensorInfo);
82 }
83
84 // Convert input tensors to const armnn::TensorInfo* type for FORWARD_LAYER_SUPPORT_FUNC.
85 std::vector<const armnn::TensorInfo*> inputConstTensorInfos;
86 std::transform(inputTensorInfos.begin(),
87 inputTensorInfos.end(),
88 std::back_inserter(inputConstTensorInfos),
89 [](armnn::TensorInfo& t)->const armnn::TensorInfo*{ return &t; });
90
91 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
Sadik Armagan529195f2022-01-14 12:56:35 +000092 if (!IsValid(tfLiteContext, tfLiteOutputTensor, tfLiteConcatOperatorCode, nodeIndex))
Matthew Sloyan91c41712020-11-13 09:47:35 +000093 {
Matthew Sloyan91c41712020-11-13 09:47:35 +000094 return kTfLiteError;
95 }
96
97 // Setup OriginsDescriptor, axis and view origin
98 unsigned int numConcatView = static_cast<unsigned int>(numInputs);
99 uint32_t inputRank = tfLiteTensors[tfLiteNode->inputs->data[0]].dims->size;
100
101 auto* concatenationParameters = reinterpret_cast<TfLiteConcatenationParams*>(tfLiteNode->builtin_data);
Ryan OShea3ad2e142023-01-13 10:19:20 +0000102
103 if(!concatenationParameters)
104 {
105 throw armnn::Exception(&"TfLiteArmnnDelegate: Concat parameters are null in: " [ nodeIndex]);
106 }
107
Matthew Sloyan91c41712020-11-13 09:47:35 +0000108 const unsigned int concatDimInput = static_cast<unsigned int>(
109 (static_cast<int>(inputRank) + concatenationParameters->axis) % static_cast<int>(inputRank));
110
111 armnn::OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
112 concatDescriptor.SetConcatAxis(concatDimInput);
113
114 unsigned int mergeDimOrigin = 0;
115 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
116 {
117 armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(
118 tfLiteTensors[tfLiteNode->inputs->data[viewIndex]]);
119
120 // Sets up concatDescriptor view origin
121 SetupConcatViewOrigin(inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
122 }
123
Sadik Armagan90a119b2022-08-05 16:12:49 +0100124 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000125
Ryan OShea3ad2e142023-01-13 10:19:20 +0000126 // Verify we support the fused activation before attempting to create a layer
127 TfLiteFusedActivation activationType = concatenationParameters->activation;
128
Ryan OShea3ad2e142023-01-13 10:19:20 +0000129 TfLiteStatus activationStatus = ValidateFusedActivationOperator(delegateData, tfLiteContext, outputTensorInfo,
130 outputTensorInfo, activationType);
131 if(activationStatus != kTfLiteOk)
132 {
133 return kTfLiteError;
134 }
135
Matthew Sloyan91c41712020-11-13 09:47:35 +0000136 // Check if supported
137 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +0100138 armnn::BackendId setBackend;
Matthew Sloyan91c41712020-11-13 09:47:35 +0000139 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
140 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000141 FORWARD_LAYER_SUPPORT_FUNC("CONCATENATION",
Matthew Sloyan91c41712020-11-13 09:47:35 +0000142 tfLiteContext,
143 IsConcatSupported,
144 delegateData.m_Backends,
145 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100146 setBackend,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000147 inputConstTensorInfos,
148 outputTensorInfo,
149 concatDescriptor);
150 };
151
152 if (!delegateData.m_Network)
153 {
154 validateFunc(outputTensorInfo, isSupported);
155 return isSupported ? kTfLiteOk : kTfLiteError;
156 }
157
158 // Setup layer and connect.
159 armnn::IConnectableLayer* concatenationLayer = delegateData.m_Network->AddConcatLayer(concatDescriptor);
Cathal Corbett53837672022-09-01 11:34:37 +0100160 concatenationLayer->SetBackendId(setBackend);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000161 ARMNN_ASSERT(concatenationLayer != nullptr);
162
Sadik Armagan529195f2022-01-14 12:56:35 +0000163 // Connect the Constant Inputs
164 auto inputsTensorsProcess = ProcessInputs(concatenationLayer,
165 delegateData,
166 tfLiteContext,
167 tfLiteNode);
168 if (inputsTensorsProcess == kTfLiteError)
169 {
170 return inputsTensorsProcess;
171 }
172
Matthew Sloyan91c41712020-11-13 09:47:35 +0000173 armnn::IOutputSlot& outputSlot = concatenationLayer->GetOutputSlot(0);
174 outputSlot.SetTensorInfo(outputTensorInfo);
175 Connect(concatenationLayer, tfLiteNode, delegateData);
176
Ryan OShea3ad2e142023-01-13 10:19:20 +0000177 if (activationType == kTfLiteActNone)
Matthew Sloyan91c41712020-11-13 09:47:35 +0000178 {
179 // No Activation
180 return kTfLiteOk;
181 }
182
Ryan OShea3ad2e142023-01-13 10:19:20 +0000183 // Check and Create activation
Matthew Sloyan91c41712020-11-13 09:47:35 +0000184 return FusedActivation(tfLiteContext, tfLiteNode, activationType, concatenationLayer, 0, delegateData);
185}
186
187TfLiteStatus VisitMeanOperator(DelegateData& delegateData,
188 TfLiteContext* tfLiteContext,
189 TfLiteNode* tfLiteNode,
190 int nodeIndex,
191 int32_t tfLiteMeanOperatorCode)
192{
193 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
194 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
195
196 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
197 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
198 if(!IsValid(&tfLiteInputTensor))
199 {
200 TF_LITE_MAYBE_KERNEL_LOG(
201 tfLiteContext,
202 "TfLiteArmnnDelegate: Invalid input tensor in operator #%d node #%d: ",
203 tfLiteMeanOperatorCode, nodeIndex);
204 return kTfLiteError;
205 }
206 if (IsDynamicTensor(tfLiteInputTensor))
207 {
208 TF_LITE_MAYBE_KERNEL_LOG(
209 tfLiteContext,
210 "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
211 tfLiteMeanOperatorCode, nodeIndex);
212 return kTfLiteError;
213 }
214
215 const TfLiteTensor& tfLiteAxisTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
216 if(!IsValid(&tfLiteAxisTensor))
217 {
218 TF_LITE_MAYBE_KERNEL_LOG(
219 tfLiteContext,
220 "TfLiteArmnnDelegate: Invalid axis tensor in operator #%d node #%d: ",
221 tfLiteMeanOperatorCode, nodeIndex);
222 return kTfLiteError;
223 }
224 if (IsDynamicTensor(tfLiteAxisTensor))
225 {
226 TF_LITE_MAYBE_KERNEL_LOG(
227 tfLiteContext,
228 "TfLiteArmnnDelegate: Dynamic axis tensors are not supported in operator #%d node #%d: ",
229 tfLiteMeanOperatorCode, nodeIndex);
230 return kTfLiteError;
231 }
232
233 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
234 if(!IsValid(&tfLiteOutputTensor))
235 {
236 TF_LITE_MAYBE_KERNEL_LOG(
237 tfLiteContext,
238 "TfLiteArmnnDelegate: Invalid output tensor in operator #%d node #%d: ",
239 tfLiteAxisTensor, nodeIndex);
240 return kTfLiteError;
241 }
242 if (IsDynamicTensor(tfLiteOutputTensor))
243 {
244 TF_LITE_MAYBE_KERNEL_LOG(
245 tfLiteContext,
246 "TfLiteArmnnDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ",
247 tfLiteMeanOperatorCode, nodeIndex);
248 return kTfLiteError;
249 }
250
251 const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
252 const armnn::TensorInfo& axisTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteAxisTensor);
Sadik Armagan90a119b2022-08-05 16:12:49 +0100253 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000254
255 auto* axisTensorData = tflite::GetTensorData<int32_t>(&tfLiteAxisTensor);
256
257 std::vector<int32_t> axis;
258 // Add axis data to vector to be converter to unsigned int and assigned to descriptor axis.
259 for (unsigned int i = 0; i < axisTensorInfo.GetNumElements(); ++i)
260 {
261 axis.emplace_back(axisTensorData[i]);
262 }
263
264 // Convert the axis to unsigned int and remove duplicates.
265 unsigned int rank = inputTensorInfo.GetNumDimensions();
266 std::set<unsigned int> uniqueAxis;
267 std::transform(axis.begin(),
268 axis.end(),
269 std::inserter(uniqueAxis, uniqueAxis.begin()),
270 [rank](int i)->unsigned int{ return (i + rank) % rank; });
271
272 // Setup MeanDescriptor and assign axis and keepDims
273 armnn::MeanDescriptor desc;
274 desc.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
275 desc.m_KeepDims = inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? true : false;
276
277 // Check if supported
278 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +0100279 armnn::BackendId setBackend;
Matthew Sloyan91c41712020-11-13 09:47:35 +0000280 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
281 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000282 FORWARD_LAYER_SUPPORT_FUNC("MEAN",
Matthew Sloyan91c41712020-11-13 09:47:35 +0000283 tfLiteContext,
284 IsMeanSupported,
285 delegateData.m_Backends,
286 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100287 setBackend,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000288 inputTensorInfo,
289 outputTensorInfo,
290 desc);
291 };
292
293 if (!delegateData.m_Network)
294 {
295 validateFunc(outputTensorInfo, isSupported);
296 return isSupported ? kTfLiteOk : kTfLiteError;
297 }
298
299 // Setup layer and connect.
300 armnn::IConnectableLayer* meanLayer = delegateData.m_Network->AddMeanLayer(desc);
Cathal Corbett53837672022-09-01 11:34:37 +0100301 meanLayer->SetBackendId(setBackend);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000302 ARMNN_ASSERT(meanLayer != nullptr);
303
304 armnn::IOutputSlot& outputSlot = meanLayer->GetOutputSlot(0);
305 outputSlot.SetTensorInfo(outputTensorInfo);
Ryan OShea4c231de2023-01-17 15:19:20 +0000306
307 // try to connect the Constant Inputs if there are any
308 if(ProcessInputs(meanLayer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk )
309 {
310 return kTfLiteError;
311 }
312
Matthew Sloyan91c41712020-11-13 09:47:35 +0000313 return Connect(meanLayer, tfLiteNode, delegateData);
314}
315
Sadik Armagan62483be2020-10-23 17:14:43 +0100316TfLiteStatus VisitControlOperator(DelegateData& delegateData,
317 TfLiteContext* tfLiteContext,
318 TfLiteNode* tfLiteNode,
319 int nodeIndex,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000320 int32_t operatorCode)
Sadik Armagan62483be2020-10-23 17:14:43 +0100321{
Finn Williams6f9f9902020-11-13 13:23:15 +0000322 armnn::IgnoreUnused(delegateData,
323 tfLiteContext,
324 tfLiteNode,
325 nodeIndex,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000326 operatorCode);
327
328 switch(operatorCode)
329 {
330 case kTfLiteBuiltinConcatenation:
331 return VisitConcatenationOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode);
332 case kTfLiteBuiltinMean:
333 return VisitMeanOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode);
334 default:
335 return kTfLiteError;
336 }
Sadik Armagan62483be2020-10-23 17:14:43 +0100337}
338
339} // namespace armnnDelegate