blob: 02426a561676d2ebf454607038012d8109848135 [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
Sadik Armagan90a119b2022-08-05 16:12:49 +01002// Copyright © 2022 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);
102 const unsigned int concatDimInput = static_cast<unsigned int>(
103 (static_cast<int>(inputRank) + concatenationParameters->axis) % static_cast<int>(inputRank));
104
105 armnn::OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
106 concatDescriptor.SetConcatAxis(concatDimInput);
107
108 unsigned int mergeDimOrigin = 0;
109 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
110 {
111 armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(
112 tfLiteTensors[tfLiteNode->inputs->data[viewIndex]]);
113
114 // Sets up concatDescriptor view origin
115 SetupConcatViewOrigin(inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
116 }
117
Sadik Armagan90a119b2022-08-05 16:12:49 +0100118 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000119
120 // Check if supported
121 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +0100122 armnn::BackendId setBackend;
Matthew Sloyan91c41712020-11-13 09:47:35 +0000123 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
124 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000125 FORWARD_LAYER_SUPPORT_FUNC("CONCATENATION",
Matthew Sloyan91c41712020-11-13 09:47:35 +0000126 tfLiteContext,
127 IsConcatSupported,
128 delegateData.m_Backends,
129 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100130 setBackend,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000131 inputConstTensorInfos,
132 outputTensorInfo,
133 concatDescriptor);
134 };
135
136 if (!delegateData.m_Network)
137 {
138 validateFunc(outputTensorInfo, isSupported);
139 return isSupported ? kTfLiteOk : kTfLiteError;
140 }
141
142 // Setup layer and connect.
143 armnn::IConnectableLayer* concatenationLayer = delegateData.m_Network->AddConcatLayer(concatDescriptor);
Cathal Corbett53837672022-09-01 11:34:37 +0100144 concatenationLayer->SetBackendId(setBackend);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000145 ARMNN_ASSERT(concatenationLayer != nullptr);
146
Sadik Armagan529195f2022-01-14 12:56:35 +0000147 // Connect the Constant Inputs
148 auto inputsTensorsProcess = ProcessInputs(concatenationLayer,
149 delegateData,
150 tfLiteContext,
151 tfLiteNode);
152 if (inputsTensorsProcess == kTfLiteError)
153 {
154 return inputsTensorsProcess;
155 }
156
Matthew Sloyan91c41712020-11-13 09:47:35 +0000157 armnn::IOutputSlot& outputSlot = concatenationLayer->GetOutputSlot(0);
158 outputSlot.SetTensorInfo(outputTensorInfo);
159 Connect(concatenationLayer, tfLiteNode, delegateData);
160
161 if (!concatenationParameters)
162 {
163 // No Activation
164 return kTfLiteOk;
165 }
166
167 // Check activation
168 TfLiteFusedActivation activationType = concatenationParameters->activation;
169 return FusedActivation(tfLiteContext, tfLiteNode, activationType, concatenationLayer, 0, delegateData);
170}
171
172TfLiteStatus VisitMeanOperator(DelegateData& delegateData,
173 TfLiteContext* tfLiteContext,
174 TfLiteNode* tfLiteNode,
175 int nodeIndex,
176 int32_t tfLiteMeanOperatorCode)
177{
178 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
179 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
180
181 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
182 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
183 if(!IsValid(&tfLiteInputTensor))
184 {
185 TF_LITE_MAYBE_KERNEL_LOG(
186 tfLiteContext,
187 "TfLiteArmnnDelegate: Invalid input tensor in operator #%d node #%d: ",
188 tfLiteMeanOperatorCode, nodeIndex);
189 return kTfLiteError;
190 }
191 if (IsDynamicTensor(tfLiteInputTensor))
192 {
193 TF_LITE_MAYBE_KERNEL_LOG(
194 tfLiteContext,
195 "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
196 tfLiteMeanOperatorCode, nodeIndex);
197 return kTfLiteError;
198 }
199
200 const TfLiteTensor& tfLiteAxisTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
201 if(!IsValid(&tfLiteAxisTensor))
202 {
203 TF_LITE_MAYBE_KERNEL_LOG(
204 tfLiteContext,
205 "TfLiteArmnnDelegate: Invalid axis tensor in operator #%d node #%d: ",
206 tfLiteMeanOperatorCode, nodeIndex);
207 return kTfLiteError;
208 }
209 if (IsDynamicTensor(tfLiteAxisTensor))
210 {
211 TF_LITE_MAYBE_KERNEL_LOG(
212 tfLiteContext,
213 "TfLiteArmnnDelegate: Dynamic axis tensors are not supported in operator #%d node #%d: ",
214 tfLiteMeanOperatorCode, nodeIndex);
215 return kTfLiteError;
216 }
217
218 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
219 if(!IsValid(&tfLiteOutputTensor))
220 {
221 TF_LITE_MAYBE_KERNEL_LOG(
222 tfLiteContext,
223 "TfLiteArmnnDelegate: Invalid output tensor in operator #%d node #%d: ",
224 tfLiteAxisTensor, nodeIndex);
225 return kTfLiteError;
226 }
227 if (IsDynamicTensor(tfLiteOutputTensor))
228 {
229 TF_LITE_MAYBE_KERNEL_LOG(
230 tfLiteContext,
231 "TfLiteArmnnDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ",
232 tfLiteMeanOperatorCode, nodeIndex);
233 return kTfLiteError;
234 }
235
236 const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
237 const armnn::TensorInfo& axisTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteAxisTensor);
Sadik Armagan90a119b2022-08-05 16:12:49 +0100238 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000239
240 auto* axisTensorData = tflite::GetTensorData<int32_t>(&tfLiteAxisTensor);
241
242 std::vector<int32_t> axis;
243 // Add axis data to vector to be converter to unsigned int and assigned to descriptor axis.
244 for (unsigned int i = 0; i < axisTensorInfo.GetNumElements(); ++i)
245 {
246 axis.emplace_back(axisTensorData[i]);
247 }
248
249 // Convert the axis to unsigned int and remove duplicates.
250 unsigned int rank = inputTensorInfo.GetNumDimensions();
251 std::set<unsigned int> uniqueAxis;
252 std::transform(axis.begin(),
253 axis.end(),
254 std::inserter(uniqueAxis, uniqueAxis.begin()),
255 [rank](int i)->unsigned int{ return (i + rank) % rank; });
256
257 // Setup MeanDescriptor and assign axis and keepDims
258 armnn::MeanDescriptor desc;
259 desc.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
260 desc.m_KeepDims = inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? true : false;
261
262 // Check if supported
263 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +0100264 armnn::BackendId setBackend;
Matthew Sloyan91c41712020-11-13 09:47:35 +0000265 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
266 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +0000267 FORWARD_LAYER_SUPPORT_FUNC("MEAN",
Matthew Sloyan91c41712020-11-13 09:47:35 +0000268 tfLiteContext,
269 IsMeanSupported,
270 delegateData.m_Backends,
271 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +0100272 setBackend,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000273 inputTensorInfo,
274 outputTensorInfo,
275 desc);
276 };
277
278 if (!delegateData.m_Network)
279 {
280 validateFunc(outputTensorInfo, isSupported);
281 return isSupported ? kTfLiteOk : kTfLiteError;
282 }
283
284 // Setup layer and connect.
285 armnn::IConnectableLayer* meanLayer = delegateData.m_Network->AddMeanLayer(desc);
Cathal Corbett53837672022-09-01 11:34:37 +0100286 meanLayer->SetBackendId(setBackend);
Matthew Sloyan91c41712020-11-13 09:47:35 +0000287 ARMNN_ASSERT(meanLayer != nullptr);
288
289 armnn::IOutputSlot& outputSlot = meanLayer->GetOutputSlot(0);
290 outputSlot.SetTensorInfo(outputTensorInfo);
291 return Connect(meanLayer, tfLiteNode, delegateData);
292}
293
Sadik Armagan62483be2020-10-23 17:14:43 +0100294TfLiteStatus VisitControlOperator(DelegateData& delegateData,
295 TfLiteContext* tfLiteContext,
296 TfLiteNode* tfLiteNode,
297 int nodeIndex,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000298 int32_t operatorCode)
Sadik Armagan62483be2020-10-23 17:14:43 +0100299{
Finn Williams6f9f9902020-11-13 13:23:15 +0000300 armnn::IgnoreUnused(delegateData,
301 tfLiteContext,
302 tfLiteNode,
303 nodeIndex,
Matthew Sloyan91c41712020-11-13 09:47:35 +0000304 operatorCode);
305
306 switch(operatorCode)
307 {
308 case kTfLiteBuiltinConcatenation:
309 return VisitConcatenationOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode);
310 case kTfLiteBuiltinMean:
311 return VisitMeanOperator(delegateData, tfLiteContext, tfLiteNode, nodeIndex, operatorCode);
312 default:
313 return kTfLiteError;
314 }
Sadik Armagan62483be2020-10-23 17:14:43 +0100315}
316
317} // namespace armnnDelegate