blob: 2b07fc7098b89a168ca6171507ec1b70c4405c0f [file] [log] [blame]
Sadik Armagan3c24f432020-10-19 17:35:30 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <armnn_delegate.hpp>
Sadik Armagan62483be2020-10-23 17:14:43 +01007
Matthew Sloyanac001ee2021-02-03 10:43:04 +00008#include "Version.hpp"
9
Sadik Armagan62483be2020-10-23 17:14:43 +010010#include "Activation.hpp"
11#include "ArgMinMax.hpp"
12#include "BatchSpace.hpp"
13#include "Comparison.hpp"
14#include "Convolution.hpp"
15#include "Control.hpp"
16#include "ElementwiseBinary.hpp"
17#include "ElementwiseUnary.hpp"
18#include "Fill.hpp"
19#include "FullyConnected.hpp"
20#include "Gather.hpp"
Matthew Sloyanc8eb9552020-11-26 10:54:22 +000021#include "LogicalBinary.hpp"
Sadik Armagan62483be2020-10-23 17:14:43 +010022#include "Lstm.hpp"
23#include "Normalization.hpp"
24#include "Pad.hpp"
25#include "Pooling.hpp"
26#include "Quantization.hpp"
27#include "Redefine.hpp"
Sadik Armagana2747482021-02-09 10:28:54 +000028#include "Reduce.hpp"
Sadik Armagan62483be2020-10-23 17:14:43 +010029#include "Resize.hpp"
30#include "Round.hpp"
31#include "Slice.hpp"
32#include "Softmax.hpp"
33#include "SpaceDepth.hpp"
Sadik Armagan34fa1bd2020-11-27 12:40:52 +000034#include "Split.hpp"
Sadik Armagan62483be2020-10-23 17:14:43 +010035#include "Transpose.hpp"
36
37#include <flatbuffers/flatbuffers.h>
38#include <tensorflow/lite/context_util.h>
39
Sadik Armagan3c24f432020-10-19 17:35:30 +010040#include <algorithm>
Matthew Sloyanac001ee2021-02-03 10:43:04 +000041#include <iostream>
Sadik Armagan62483be2020-10-23 17:14:43 +010042#include <sstream>
Sadik Armagan3c24f432020-10-19 17:35:30 +010043
44namespace armnnDelegate
45{
46
Sadik Armagan62483be2020-10-23 17:14:43 +010047DelegateOptions TfLiteArmnnDelegateOptionsDefault()
48{
49 DelegateOptions options(armnn::Compute::CpuRef);
50 return options;
51}
52
53TfLiteDelegate* TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options)
54{
55 auto* armnnDelegate = new ::armnnDelegate::Delegate(options);
56 return armnnDelegate->GetDelegate();
57}
58
59void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate)
60{
61 if (tfLiteDelegate != nullptr)
62 {
63 delete static_cast<::armnnDelegate::Delegate*>(tfLiteDelegate->data_);
64 }
65}
66
67TfLiteStatus DoPrepare(TfLiteContext* tfLiteContext, TfLiteDelegate* tfLiteDelegate)
68{
69 TfLiteIntArray* supportedOperators =
70 static_cast<::armnnDelegate::Delegate*>(tfLiteDelegate->data_)->IdentifyOperatorsToDelegate(tfLiteContext);
71
72 // ArmNN Delegate Registration
73 static const TfLiteRegistration kArmnnSubgraphRegistration = {
74 // ArmnnSubgraph Init
75 .init = [](TfLiteContext* tfLiteContext, const char* buffer, size_t length) -> void* {
Finn Williams6f9f9902020-11-13 13:23:15 +000076 armnn::IgnoreUnused(length);
Sadik Armagan62483be2020-10-23 17:14:43 +010077 const TfLiteDelegateParams* parameters = reinterpret_cast<const TfLiteDelegateParams*>(buffer);
78
79 return static_cast<void*>(ArmnnSubgraph::Create(
80 tfLiteContext, parameters, static_cast<::armnnDelegate::Delegate*>(parameters->delegate->data_)));
81 },
82 // ArmnnSubgraph Free
83 .free = [](TfLiteContext* tfLiteContext, void* buffer) -> void {
Finn Williams6f9f9902020-11-13 13:23:15 +000084 armnn::IgnoreUnused(tfLiteContext);
Sadik Armagan62483be2020-10-23 17:14:43 +010085 if (buffer != nullptr)
86 {
87 delete static_cast<ArmnnSubgraph*>(buffer);
88 }
89 },
90 // ArmnnSubgraph Prepare
91 .prepare = [](TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode) -> TfLiteStatus {
92 if (tfLiteNode->user_data == nullptr)
93 {
94 return kTfLiteError;
95 }
Sadik Armagan62483be2020-10-23 17:14:43 +010096 return static_cast<ArmnnSubgraph*>(tfLiteNode->user_data)->Prepare(tfLiteContext);
97 },
98 // ArmnnSubgraph Invoke
99 .invoke = [](TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode) -> TfLiteStatus {
100 if (tfLiteNode->user_data == nullptr)
101 {
102 return kTfLiteError;
103 }
104
105 return static_cast<ArmnnSubgraph*>(tfLiteNode->user_data)->Invoke(tfLiteContext, tfLiteNode);
106 },
107
108 .profiling_string = nullptr,
109 .builtin_code = kTfLiteBuiltinDelegate,
110 .custom_name = "TfLiteArmNnDelegate",
111 .version = 1,
112 };
113
114 const TfLiteStatus status =
115 tfLiteContext->ReplaceNodeSubsetsWithDelegateKernels(
116 tfLiteContext, kArmnnSubgraphRegistration, supportedOperators, tfLiteDelegate);
117
118 TfLiteIntArrayFree(supportedOperators);
119 return status;
120
121}
122
Sadik Armagan3c24f432020-10-19 17:35:30 +0100123Delegate::Delegate(armnnDelegate::DelegateOptions options)
124 : m_Runtime(nullptr, nullptr),
125 m_Options(std::move(options))
126{
Jan Eilers2cd18472020-12-15 10:42:38 +0000127 // Configures logging for ARMNN
128 if (options.IsLoggingEnabled())
129 {
130 armnn::ConfigureLogging(true, true, options.GetLoggingSeverity());
131 }
132
Sadik Armagan3c24f432020-10-19 17:35:30 +0100133 // Create ArmNN Runtime
134 armnn::IRuntime::CreationOptions runtimeOptions;
Sadik Armagan4189cc52020-11-11 18:01:48 +0000135
136 auto backendOptions = m_Options.GetBackendOptions();
137 if (!backendOptions.empty())
138 {
139 runtimeOptions.m_BackendOptions = backendOptions;
140 }
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +0000141 else if (!m_Options.GetOptimizerOptions().m_ModelOptions.empty())
142 {
143 runtimeOptions.m_BackendOptions = m_Options.GetOptimizerOptions().m_ModelOptions;
144 }
Sadik Armagan3c24f432020-10-19 17:35:30 +0100145 m_Runtime = armnn::IRuntime::Create(runtimeOptions);
146
147 std::vector<armnn::BackendId> backends;
Sadik Armagan3c24f432020-10-19 17:35:30 +0100148 if (m_Runtime)
149 {
150 const armnn::BackendIdSet supportedDevices = m_Runtime->GetDeviceSpec().GetSupportedBackends();
151 for (auto& backend : m_Options.GetBackends())
152 {
153 if (std::find(supportedDevices.cbegin(), supportedDevices.cend(), backend) == supportedDevices.cend())
154 {
Sadik Armagan0534e032020-10-27 17:30:18 +0000155 TFLITE_LOG_PROD(tflite::TFLITE_LOG_INFO,
Sadik Armagan3c24f432020-10-19 17:35:30 +0100156 "TfLiteArmnnDelegate: Requested unknown backend %s", backend.Get().c_str());
157 }
158 else
159 {
160 backends.push_back(backend);
161 }
162 }
163 }
164
165 if (backends.empty())
166 {
167 // No known backend specified
168 throw armnn::InvalidArgumentException("TfLiteArmnnDelegate: No known backend specified.");
169 }
170 m_Options.SetBackends(backends);
171
172 TFLITE_LOG_PROD_ONCE(tflite::TFLITE_LOG_INFO, "TfLiteArmnnDelegate: Created TfLite ArmNN delegate.");
173}
174
Sadik Armagan62483be2020-10-23 17:14:43 +0100175TfLiteIntArray* Delegate::IdentifyOperatorsToDelegate(TfLiteContext* tfLiteContext)
Sadik Armagan3c24f432020-10-19 17:35:30 +0100176{
177 TfLiteIntArray* executionPlan = nullptr;
178 if (tfLiteContext->GetExecutionPlan(tfLiteContext, &executionPlan) != kTfLiteOk)
179 {
180 TF_LITE_KERNEL_LOG(tfLiteContext, "TfLiteArmnnDelegate: Unable to get graph execution plan.");
181 return nullptr;
182 }
183
Sadik Armagan62483be2020-10-23 17:14:43 +0100184 // Delegate data with null network
185 DelegateData delegateData(m_Options.GetBackends());
Sadik Armagan3c24f432020-10-19 17:35:30 +0100186
187 TfLiteIntArray* nodesToDelegate = TfLiteIntArrayCreate(executionPlan->size);
188 nodesToDelegate->size = 0;
189 for (int i = 0; i < executionPlan->size; ++i)
190 {
191 const int nodeIndex = executionPlan->data[i];
192
193 // If TfLite nodes can be delegated to ArmNN
194 TfLiteNode* tfLiteNode = nullptr;
195 TfLiteRegistration* tfLiteRegistration = nullptr;
196 if (tfLiteContext->GetNodeAndRegistration(
197 tfLiteContext, nodeIndex, &tfLiteNode, &tfLiteRegistration) != kTfLiteOk)
198 {
199 TF_LITE_KERNEL_LOG(tfLiteContext,
200 "TfLiteArmnnDelegate: Unable to get node and registration for node %d.",
201 nodeIndex);
202 continue;
203 }
204
205 if (ArmnnSubgraph::VisitNode(
Sadik Armagan62483be2020-10-23 17:14:43 +0100206 delegateData, tfLiteContext, tfLiteRegistration, tfLiteNode, nodeIndex) != kTfLiteOk)
Sadik Armagan3c24f432020-10-19 17:35:30 +0100207 {
208 // node is not supported by ArmNN
209 continue;
210 }
211
212 nodesToDelegate->data[nodesToDelegate->size++] = nodeIndex;
213 }
214
Sadik Armagan62483be2020-10-23 17:14:43 +0100215 std::sort(&nodesToDelegate->data[0], &nodesToDelegate->data[nodesToDelegate->size]);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100216 return nodesToDelegate;
217}
218
219TfLiteDelegate* Delegate::GetDelegate()
220{
221 return &m_Delegate;
222}
223
Matthew Sloyanac001ee2021-02-03 10:43:04 +0000224const std::string Delegate::GetVersion()
225{
226 return DELEGATE_VERSION;
227}
228
Sadik Armagan62483be2020-10-23 17:14:43 +0100229TfLiteStatus ArmnnSubgraph::AddInputLayer(DelegateData& delegateData,
230 TfLiteContext* tfLiteContext,
231 const TfLiteIntArray* inputs,
232 std::vector<armnn::BindingPointInfo>& inputBindings)
233{
Finn Williams6f9f9902020-11-13 13:23:15 +0000234 const size_t numInputs = static_cast<size_t>(inputs->size);
Sadik Armagan62483be2020-10-23 17:14:43 +0100235 for (unsigned int i = 0; i < numInputs; ++i)
236 {
237 const int32_t tensorId = inputs->data[i];
238 const TfLiteTensor tensor = tfLiteContext->tensors[tensorId];
Sadik Armagan6e36a642020-11-10 21:18:41 +0000239 // Do not create bindings for constant inputs
240 if (tensor.allocation_type == kTfLiteMmapRo)
241 {
242 continue;
243 }
Sadik Armagan62483be2020-10-23 17:14:43 +0100244
245 auto bindingId = static_cast<armnn::LayerBindingId>((tensorId));
246 armnn::IConnectableLayer* layer = delegateData.m_Network->AddInputLayer(bindingId);
247
248 auto tensorInfo = GetTensorInfoForTfLiteTensor(tensor);
249 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
250 outputSlot.SetTensorInfo(tensorInfo);
251
252 // Store for creating connections
Finn Williams6f9f9902020-11-13 13:23:15 +0000253 delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)] = &outputSlot;
Sadik Armagan62483be2020-10-23 17:14:43 +0100254
Sadik Armagan6e36a642020-11-10 21:18:41 +0000255 inputBindings.push_back(std::make_pair(bindingId, tensorInfo));
Sadik Armagan62483be2020-10-23 17:14:43 +0100256 }
Sadik Armagan6e36a642020-11-10 21:18:41 +0000257
Sadik Armagan62483be2020-10-23 17:14:43 +0100258 return kTfLiteOk;
259}
260
261TfLiteStatus ArmnnSubgraph::AddOutputLayer(DelegateData& delegateData,
262 TfLiteContext* tfLiteContext,
263 const TfLiteIntArray* outputs,
264 std::vector<armnn::BindingPointInfo>& outputBindings)
265{
Finn Williams6f9f9902020-11-13 13:23:15 +0000266 const size_t numOutputs = static_cast<size_t>(outputs->size);
Sadik Armagan62483be2020-10-23 17:14:43 +0100267 for (unsigned int i = 0; i < numOutputs; ++i)
268 {
269 const int32_t tensorId = outputs->data[i];
270 const TfLiteTensor tensor = tfLiteContext->tensors[tensorId];
271
272 auto bindingId = static_cast<armnn::LayerBindingId>((tensorId));
273 armnn::IConnectableLayer* layer = delegateData.m_Network->AddOutputLayer(bindingId);
274
275 auto tensorInfo = GetTensorInfoForTfLiteTensor(tensor);
Finn Williams6f9f9902020-11-13 13:23:15 +0000276 ARMNN_ASSERT(delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)] != nullptr);
277 delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)]->Connect(layer->GetInputSlot(0));
Sadik Armagan62483be2020-10-23 17:14:43 +0100278 outputBindings.push_back(std::make_pair(bindingId, tensorInfo));
279 }
280
281 return kTfLiteOk;
282}
283
Sadik Armagan3c24f432020-10-19 17:35:30 +0100284ArmnnSubgraph* ArmnnSubgraph::Create(TfLiteContext* tfLiteContext,
285 const TfLiteDelegateParams* parameters,
286 const Delegate* delegate)
287{
288 TfLiteIntArray* executionPlan;
289 if (tfLiteContext->GetExecutionPlan(tfLiteContext, &executionPlan) != kTfLiteOk)
290 {
291 return nullptr;
292 }
293
Sadik Armagan62483be2020-10-23 17:14:43 +0100294 // Initialize DelegateData holds network and output slots information
295 DelegateData delegateData(delegate->m_Options.GetBackends());
296
297 // Build ArmNN Network
Sadik Armagan3c24f432020-10-19 17:35:30 +0100298 armnn::NetworkOptions networkOptions = {};
299 armnn::NetworkId networkId;
Sadik Armagan62483be2020-10-23 17:14:43 +0100300 delegateData.m_Network = armnn::INetwork::Create(networkOptions);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100301
Sadik Armagan6e36a642020-11-10 21:18:41 +0000302 delegateData.m_OutputSlotForNode = std::vector<armnn::IOutputSlot*>(tfLiteContext->tensors_size, nullptr);
303
Sadik Armagan62483be2020-10-23 17:14:43 +0100304 std::vector<armnn::BindingPointInfo> inputBindings;
305 std::vector<armnn::BindingPointInfo> outputBindings;
306
307 // Add input layer
308 auto status = AddInputLayer(delegateData, tfLiteContext, parameters->input_tensors, inputBindings);
309 if (status != kTfLiteOk)
310 {
311 throw armnn::Exception("TfLiteArmnnDelegate: Unable to add Inputs to the network!");
312 }
313
314 // Parse TfLite delegate nodes to ArmNN
Sadik Armagan3c24f432020-10-19 17:35:30 +0100315 for (int i = 0; i < parameters->nodes_to_replace->size; ++i)
316 {
317 const int nodeIndex = parameters->nodes_to_replace->data[i];
318
319 TfLiteNode* tfLiteNode = nullptr;
320 TfLiteRegistration* tfLiteRegistration = nullptr;
321 if (tfLiteContext->GetNodeAndRegistration(
322 tfLiteContext, nodeIndex, &tfLiteNode, &tfLiteRegistration) != kTfLiteOk)
323 {
Finn Williams6f9f9902020-11-13 13:23:15 +0000324 throw armnn::Exception(&"TfLiteArmnnDelegate: Unable to get node registration: " [ nodeIndex]);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100325 }
326
Sadik Armagan62483be2020-10-23 17:14:43 +0100327 if (VisitNode(delegateData, tfLiteContext, tfLiteRegistration, tfLiteNode, nodeIndex) != kTfLiteOk)
Sadik Armagan3c24f432020-10-19 17:35:30 +0100328 {
Finn Williams6f9f9902020-11-13 13:23:15 +0000329 throw armnn::Exception(&"TfLiteArmnnDelegate: Unable to parse node: " [ nodeIndex]);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100330 }
331 }
332
Sadik Armagan62483be2020-10-23 17:14:43 +0100333 // Add Output layer
334 status = AddOutputLayer(delegateData, tfLiteContext, parameters->output_tensors, outputBindings);
335 if (status != kTfLiteOk)
336 {
337 throw armnn::Exception("TfLiteArmnnDelegate: Unable to add Outputs to the network!");
338 }
339
340 // Optimize ArmNN network
341 armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr);
342 try
343 {
Sadik Armagan6e36a642020-11-10 21:18:41 +0000344 optNet = armnn::Optimize(*(delegateData.m_Network.get()),
Sadik Armagan62483be2020-10-23 17:14:43 +0100345 delegate->m_Options.GetBackends(),
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +0000346 delegate->m_Runtime->GetDeviceSpec(),
347 delegate->m_Options.GetOptimizerOptions());
Sadik Armagan62483be2020-10-23 17:14:43 +0100348 }
349 catch (std::exception &ex)
350 {
351 std::stringstream exMessage;
352 exMessage << "TfLiteArmnnDelegate: Exception (" << ex.what() << ") caught from optimize.";
353 throw armnn::Exception(exMessage.str());
354 }
Sadik Armagan3c24f432020-10-19 17:35:30 +0100355 if (!optNet)
356 {
Sadik Armagan62483be2020-10-23 17:14:43 +0100357 // Optimize failed
Sadik Armagan3c24f432020-10-19 17:35:30 +0100358 throw armnn::Exception("TfLiteArmnnDelegate: Unable to optimize the network!");
359 }
Sadik Armagan62483be2020-10-23 17:14:43 +0100360
361 try
362 {
363 // Load graph into runtime
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +0000364 std::string errorMessage;
Narumol Prangnawarat74a3cf52021-01-29 15:38:54 +0000365 armnn::Status loadingStatus;
366 if (delegate->m_Options.GetOptimizerOptions().m_ImportEnabled)
367 {
368 armnn::INetworkProperties networkProperties(true, true);
369 loadingStatus = delegate->m_Runtime->LoadNetwork(networkId,
370 std::move(optNet),
371 errorMessage,
372 networkProperties);
373 }
374 else
375 {
376 loadingStatus = delegate->m_Runtime->LoadNetwork(networkId,
377 std::move(optNet),
378 errorMessage);
379 }
Sadik Armagan62483be2020-10-23 17:14:43 +0100380 if (loadingStatus != armnn::Status::Success)
381 {
382 // Optimize failed
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +0000383 throw armnn::Exception("TfLiteArmnnDelegate: Network could not be loaded:" + errorMessage);
Sadik Armagan62483be2020-10-23 17:14:43 +0100384 }
385 }
386 catch (std::exception& ex)
387 {
388 std::stringstream exMessage;
389 exMessage << "TfLiteArmnnDelegate: Exception (" << ex.what() << ") caught from LoadNetwork.";
390 throw armnn::Exception(exMessage.str());
391 }
Sadik Armagan3c24f432020-10-19 17:35:30 +0100392
Narumol Prangnawarat0b51d5a2021-01-20 15:58:29 +0000393 // Register debug callback function
394 if (delegate->m_Options.GetDebugCallbackFunction().has_value())
395 {
396 delegate->m_Runtime->RegisterDebugCallback(networkId, delegate->m_Options.GetDebugCallbackFunction().value());
397 }
398
Sadik Armagan3c24f432020-10-19 17:35:30 +0100399 // Create a new SubGraph with networkId and runtime
Sadik Armagan62483be2020-10-23 17:14:43 +0100400 return new ArmnnSubgraph(networkId, delegate->m_Runtime.get(), inputBindings, outputBindings);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100401}
402
403TfLiteStatus ArmnnSubgraph::Prepare(TfLiteContext* tfLiteContext)
404{
Finn Williams6f9f9902020-11-13 13:23:15 +0000405 armnn::IgnoreUnused(tfLiteContext);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100406 return kTfLiteOk;
407}
408
Sadik Armagan62483be2020-10-23 17:14:43 +0100409TfLiteStatus ArmnnSubgraph::Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode)
Sadik Armagan3c24f432020-10-19 17:35:30 +0100410{
Sadik Armagan62483be2020-10-23 17:14:43 +0100411 // Prepare inputs
412 armnn::InputTensors inputTensors;
413 size_t inputIndex = 0;
414 for (auto inputIdx : tflite::TfLiteIntArrayView(tfLiteNode->inputs))
415 {
416 TfLiteTensor* tensor = &tfLiteContext->tensors[inputIdx];
417 if (tensor->allocation_type != kTfLiteMmapRo)
418 {
419 const armnn::BindingPointInfo& inputBinding = m_InputBindings[inputIndex];
420 const armnn::ConstTensor inputTensor(inputBinding.second, tensor->data.data);
421 inputTensors.emplace_back(inputIdx, inputTensor);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100422
Sadik Armagan62483be2020-10-23 17:14:43 +0100423 ++inputIndex;
424 }
425 }
426
427 // Prepare outputs
428 armnn::OutputTensors outputTensors;
429 size_t outputIndex = 0;
430 for (auto outputIdx : tflite::TfLiteIntArrayView(tfLiteNode->outputs))
431 {
432 const armnn::BindingPointInfo& outputBinding = m_OutputBindings[outputIndex];
433 TfLiteTensor* tensor = &tfLiteContext->tensors[outputIdx];
434 const armnn::Tensor outputTensor(outputBinding.second, tensor->data.data);
435 outputTensors.emplace_back(outputIdx, outputTensor);
436
437 ++outputIndex;
438 }
439
440 // Run graph
441 auto status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
442 return (status == armnn::Status::Success) ? kTfLiteOk : kTfLiteError;
Sadik Armagan3c24f432020-10-19 17:35:30 +0100443}
444
Sadik Armagan62483be2020-10-23 17:14:43 +0100445TfLiteStatus ArmnnSubgraph::VisitNode(DelegateData& delegateData,
Sadik Armagan3c24f432020-10-19 17:35:30 +0100446 TfLiteContext* tfLiteContext,
447 TfLiteRegistration* tfLiteRegistration,
448 TfLiteNode* tfLiteNode,
449 int nodeIndex)
450{
Sadik Armagan62483be2020-10-23 17:14:43 +0100451 switch (tfLiteRegistration->builtin_code)
452 {
453 case kTfLiteBuiltinAbs:
454 return VisitElementwiseUnaryOperator(delegateData,
455 tfLiteContext,
456 tfLiteNode,
457 nodeIndex,
458 armnn::UnaryOperation::Abs);
459 case kTfLiteBuiltinAdd:
460 return VisitElementwiseBinaryOperator(delegateData,
461 tfLiteContext,
462 tfLiteNode,
463 nodeIndex,
464 kTfLiteBuiltinAdd);
465 case kTfLiteBuiltinArgMax:
466 return VisitArgMinMaxOperator(delegateData,
467 tfLiteContext,
468 tfLiteNode,
469 nodeIndex,
470 kTfLiteBuiltinArgMax);
471 case kTfLiteBuiltinArgMin:
472 return VisitArgMinMaxOperator(delegateData,
473 tfLiteContext,
474 tfLiteNode,
475 nodeIndex,
476 kTfLiteBuiltinArgMin);
477 case kTfLiteBuiltinAveragePool2d:
478 return VisitPoolingOperator(delegateData,
479 tfLiteContext,
480 tfLiteNode,
481 nodeIndex,
482 kTfLiteBuiltinAveragePool2d);
483 case kTfLiteBuiltinBatchToSpaceNd:
484 return VisitBatchToSpaceNdOperator(delegateData,
485 tfLiteContext,
486 tfLiteNode,
487 nodeIndex,
488 kTfLiteBuiltinBatchToSpaceNd);
489 case kTfLiteBuiltinConcatenation:
490 return VisitControlOperator(delegateData,
491 tfLiteContext,
492 tfLiteNode,
493 nodeIndex,
494 kTfLiteBuiltinConcatenation);
495 case kTfLiteBuiltinConv2d:
496 return VisitConvolutionOperator(delegateData,
497 tfLiteContext,
498 tfLiteNode,
499 nodeIndex,
500 kTfLiteBuiltinConv2d);
501 case kTfLiteBuiltinDepthToSpace:
502 return VisitDepthToSpaceOperator(delegateData,
503 tfLiteContext,
504 tfLiteNode,
505 nodeIndex,
506 kTfLiteBuiltinDepthToSpace);
507 case kTfLiteBuiltinDepthwiseConv2d:
508 return VisitConvolutionOperator(delegateData,
509 tfLiteContext,
510 tfLiteNode,
511 nodeIndex,
512 kTfLiteBuiltinDepthwiseConv2d);
513 case kTfLiteBuiltinDequantize:
514 return VisitDequantizeOperator(delegateData,
515 tfLiteContext,
516 tfLiteNode,
517 nodeIndex,
518 kTfLiteBuiltinDequantize);
519 case kTfLiteBuiltinDiv:
520 return VisitElementwiseBinaryOperator(delegateData,
521 tfLiteContext,
522 tfLiteNode,
523 nodeIndex,
524 kTfLiteBuiltinDiv);
525 case kTfLiteBuiltinElu:
526 return VisitActivationOperator(delegateData,
527 tfLiteContext,
528 tfLiteNode,
529 nodeIndex,
530 kTfLiteBuiltinElu);
531 case kTfLiteBuiltinEqual:
532 return VisitComparisonOperator(delegateData,
533 tfLiteContext,
534 tfLiteNode,
535 nodeIndex,
536 kTfLiteBuiltinEqual);
537 case kTfLiteBuiltinExp:
538 return VisitElementwiseUnaryOperator(delegateData,
539 tfLiteContext,
540 tfLiteNode,
541 nodeIndex,
542 armnn::UnaryOperation::Exp);
543 case kTfLiteBuiltinExpandDims:
544 return VisitExpandDimsOperator(delegateData,
545 tfLiteContext,
546 tfLiteNode,
547 nodeIndex,
548 kTfLiteBuiltinExpandDims);
549 case kTfLiteBuiltinFill:
550 return VisitFillOperator(delegateData,
551 tfLiteContext,
552 tfLiteNode,
553 nodeIndex,
554 kTfLiteBuiltinFill);
555 case kTfLiteBuiltinFloor:
556 return VisitFloorOperator(delegateData,
557 tfLiteContext,
558 tfLiteNode,
559 nodeIndex,
560 kTfLiteBuiltinFloor);
561 case kTfLiteBuiltinFullyConnected:
562 return VisitFullyConnectedOperator(delegateData,
563 tfLiteContext,
564 tfLiteNode,
565 nodeIndex,
566 kTfLiteBuiltinFullyConnected);
567 case kTfLiteBuiltinGather:
568 return VisitGatherOperator(delegateData,
569 tfLiteContext,
570 tfLiteNode,
571 nodeIndex,
572 kTfLiteBuiltinGather);
573 case kTfLiteBuiltinGatherNd:
574 return VisitGatherOperator(delegateData,
575 tfLiteContext,
576 tfLiteNode,
577 nodeIndex,
578 kTfLiteBuiltinGatherNd);
579 case kTfLiteBuiltinGreater:
580 return VisitComparisonOperator(delegateData,
581 tfLiteContext,
582 tfLiteNode,
583 nodeIndex,
584 kTfLiteBuiltinGreater);
585 case kTfLiteBuiltinGreaterEqual:
586 return VisitComparisonOperator(delegateData,
587 tfLiteContext,
588 tfLiteNode,
589 nodeIndex,
590 kTfLiteBuiltinGreaterEqual);
591 case kTfLiteBuiltinHardSwish:
592 return VisitActivationOperator(delegateData,
593 tfLiteContext,
594 tfLiteNode,
595 nodeIndex,
596 kTfLiteBuiltinHardSwish);
597 case kTfLiteBuiltinL2Normalization:
Sadik Armagan4b227bb2021-01-22 10:53:38 +0000598 return VisitL2NormalizationOperator(delegateData,
599 tfLiteContext,
600 tfLiteNode,
601 nodeIndex,
602 kTfLiteBuiltinL2Normalization);
Sadik Armagan62483be2020-10-23 17:14:43 +0100603 case kTfLiteBuiltinL2Pool2d:
604 return VisitPoolingOperator(delegateData,
605 tfLiteContext,
606 tfLiteNode,
607 nodeIndex,
608 kTfLiteBuiltinL2Pool2d);
609 case kTfLiteBuiltinLess:
610 return VisitComparisonOperator(delegateData,
611 tfLiteContext,
612 tfLiteNode,
613 nodeIndex,
614 kTfLiteBuiltinLess);
615 case kTfLiteBuiltinLessEqual:
616 return VisitComparisonOperator(delegateData,
617 tfLiteContext,
618 tfLiteNode,
619 nodeIndex,
620 kTfLiteBuiltinLessEqual);
621 case kTfLiteBuiltinLocalResponseNormalization:
Sadik Armagan4b227bb2021-01-22 10:53:38 +0000622 return VisitLocalResponseNormalizationOperator(delegateData,
623 tfLiteContext,
624 tfLiteNode,
625 nodeIndex,
626 kTfLiteBuiltinLocalResponseNormalization);
Matthew Sloyanc8eb9552020-11-26 10:54:22 +0000627 case kTfLiteBuiltinLogicalAnd:
628 return VisitLogicalBinaryOperator(delegateData,
629 tfLiteContext,
630 tfLiteNode,
631 nodeIndex,
632 kTfLiteBuiltinLogicalAnd,
633 armnn::LogicalBinaryOperation::LogicalAnd);
634 case kTfLiteBuiltinLogicalNot:
635 return VisitElementwiseUnaryOperator(delegateData,
636 tfLiteContext,
637 tfLiteNode,
638 nodeIndex,
639 armnn::UnaryOperation::LogicalNot);
640 case kTfLiteBuiltinLogicalOr:
641 return VisitLogicalBinaryOperator(delegateData,
642 tfLiteContext,
643 tfLiteNode,
644 nodeIndex,
645 kTfLiteBuiltinLogicalOr,
646 armnn::LogicalBinaryOperation::LogicalOr);
Sadik Armagan62483be2020-10-23 17:14:43 +0100647 case kTfLiteBuiltinLogistic:
648 return VisitActivationOperator(delegateData,
649 tfLiteContext,
650 tfLiteNode,
651 nodeIndex,
652 kTfLiteBuiltinLogistic);
653 case kTfLiteBuiltinLogSoftmax:
654 return VisitSoftmaxOperator(delegateData,
655 tfLiteContext,
656 tfLiteNode,
657 nodeIndex,
658 kTfLiteBuiltinLogSoftmax);
659 case kTfLiteBuiltinLstm:
660 return VisitLstmOperator(delegateData,
661 tfLiteContext,
662 tfLiteNode,
663 nodeIndex,
664 kTfLiteBuiltinLstm);
665 case kTfLiteBuiltinMaxPool2d:
666 return VisitPoolingOperator(delegateData,
667 tfLiteContext,
668 tfLiteNode,
669 nodeIndex,
670 kTfLiteBuiltinMaxPool2d);
671 case kTfLiteBuiltinMaximum:
672 return VisitElementwiseBinaryOperator(delegateData,
673 tfLiteContext,
674 tfLiteNode,
675 nodeIndex,
676 kTfLiteBuiltinMaximum);
677 case kTfLiteBuiltinMean:
678 return VisitControlOperator(delegateData,
679 tfLiteContext,
680 tfLiteNode,
681 nodeIndex,
682 kTfLiteBuiltinMean);
683 case kTfLiteBuiltinMinimum:
684 return VisitElementwiseBinaryOperator(delegateData,
685 tfLiteContext,
686 tfLiteNode,
687 nodeIndex,
688 kTfLiteBuiltinMinimum);
689 case kTfLiteBuiltinMul:
690 return VisitElementwiseBinaryOperator(delegateData,
691 tfLiteContext,
692 tfLiteNode,
693 nodeIndex,
694 kTfLiteBuiltinMul);
695 case kTfLiteBuiltinNeg:
696 return VisitElementwiseUnaryOperator(delegateData,
697 tfLiteContext,
698 tfLiteNode,
699 nodeIndex,
700 armnn::UnaryOperation::Neg);
701 case kTfLiteBuiltinNotEqual:
702 return VisitComparisonOperator(delegateData,
703 tfLiteContext,
704 tfLiteNode,
705 nodeIndex,
706 kTfLiteBuiltinNotEqual);
707 case kTfLiteBuiltinPad:
708 return VisitPadOperator(delegateData,
709 tfLiteContext,
710 tfLiteNode,
711 nodeIndex,
712 kTfLiteBuiltinPad);
713 case kTfLiteBuiltinPadv2:
714 return VisitPadOperator(delegateData,
715 tfLiteContext,
716 tfLiteNode,
717 nodeIndex,
718 kTfLiteBuiltinPadv2);
719 case kTfLiteBuiltinPrelu:
720 return VisitActivationOperator(delegateData,
721 tfLiteContext,
722 tfLiteNode,
723 nodeIndex,
724 kTfLiteBuiltinPrelu);
725 case kTfLiteBuiltinQuantize:
726 return VisitQuantizeOperator(delegateData,
727 tfLiteContext,
728 tfLiteNode,
729 nodeIndex,
730 kTfLiteBuiltinQuantize);
731 case kTfLiteBuiltinRank:
732 return VisitControlOperator(delegateData,
733 tfLiteContext,
734 tfLiteNode,
735 nodeIndex,
736 kTfLiteBuiltinRank);
Sadik Armagana2747482021-02-09 10:28:54 +0000737 case kTfLiteBuiltinReduceMax:
738 return VisitReduceOperator(delegateData,
739 tfLiteContext,
740 tfLiteNode,
741 nodeIndex,
742 kTfLiteBuiltinReduceMax);
743 case kTfLiteBuiltinReduceMin:
744 return VisitReduceOperator(delegateData,
745 tfLiteContext,
746 tfLiteNode,
747 nodeIndex,
748 kTfLiteBuiltinReduceMin);
Sadik Armagan62483be2020-10-23 17:14:43 +0100749 case kTfLiteBuiltinRelu:
750 return VisitActivationOperator(delegateData,
751 tfLiteContext,
752 tfLiteNode,
753 nodeIndex,
754 kTfLiteBuiltinRelu);
755 case kTfLiteBuiltinReluN1To1:
756 return VisitActivationOperator(delegateData,
757 tfLiteContext,
758 tfLiteNode,
759 nodeIndex,
760 kTfLiteBuiltinReluN1To1);
761 case kTfLiteBuiltinRelu6:
762 return VisitActivationOperator(delegateData,
763 tfLiteContext,
764 tfLiteNode,
765 nodeIndex,
766 kTfLiteBuiltinRelu6);
767 case kTfLiteBuiltinReshape:
768 return VisitReshapeOperator(delegateData,
769 tfLiteContext,
770 tfLiteNode,
771 nodeIndex,
772 kTfLiteBuiltinReshape);
773 case kTfLiteBuiltinResizeBilinear:
774 return VisitResizeOperator(delegateData,
775 tfLiteContext,
776 tfLiteNode,
777 nodeIndex,
778 kTfLiteBuiltinResizeBilinear);
779 case kTfLiteBuiltinResizeNearestNeighbor:
780 return VisitResizeOperator(delegateData,
781 tfLiteContext,
782 tfLiteNode,
783 nodeIndex,
784 kTfLiteBuiltinResizeNearestNeighbor);
785 case kTfLiteBuiltinRsqrt:
786 return VisitElementwiseUnaryOperator(delegateData,
787 tfLiteContext,
788 tfLiteNode,
789 nodeIndex,
790 armnn::UnaryOperation::Rsqrt);
Sadik Armagan34fa1bd2020-11-27 12:40:52 +0000791 case kTfLiteBuiltinSplit:
792 return VisitSplitOperator(delegateData,
793 tfLiteContext,
794 tfLiteNode,
795 nodeIndex,
796 kTfLiteBuiltinSplit);
797 case kTfLiteBuiltinSplitV:
798 return VisitSplitVOperator(delegateData,
799 tfLiteContext,
800 tfLiteNode,
801 nodeIndex,
802 kTfLiteBuiltinSplitV);
Sadik Armagan62483be2020-10-23 17:14:43 +0100803 case kTfLiteBuiltinSqrt:
804 return VisitElementwiseUnaryOperator(delegateData,
805 tfLiteContext,
806 tfLiteNode,
807 nodeIndex,
808 armnn::UnaryOperation::Sqrt);
809 case kTfLiteBuiltinSqueeze:
810 return VisitSqueezeOperator(delegateData,
811 tfLiteContext,
812 tfLiteNode,
813 nodeIndex,
814 kTfLiteBuiltinSqueeze);
815 case kTfLiteBuiltinStridedSlice:
816 return VisitSliceOperator(delegateData,
817 tfLiteContext,
818 tfLiteNode,
819 nodeIndex,
820 kTfLiteBuiltinStridedSlice);
Sadik Armagana2747482021-02-09 10:28:54 +0000821 case kTfLiteBuiltinSum:
822 return VisitReduceOperator(delegateData,
823 tfLiteContext,
824 tfLiteNode,
825 nodeIndex,
826 kTfLiteBuiltinSum);
Sadik Armagan62483be2020-10-23 17:14:43 +0100827 case kTfLiteBuiltinTranspose:
828 return VisitTransposeOperator(delegateData,
829 tfLiteContext,
830 tfLiteNode,
831 nodeIndex,
832 kTfLiteBuiltinTranspose);
833 case kTfLiteBuiltinTransposeConv:
834 return VisitConvolutionOperator(delegateData,
835 tfLiteContext,
836 tfLiteNode,
837 nodeIndex,
838 kTfLiteBuiltinTransposeConv);
839 case kTfLiteBuiltinSoftmax:
840 return VisitSoftmaxOperator(delegateData,
841 tfLiteContext,
842 tfLiteNode,
843 nodeIndex,
844 kTfLiteBuiltinSoftmax);
845 case kTfLiteBuiltinSpaceToBatchNd:
846 return VisitSpaceToBatchNdOperator(delegateData,
847 tfLiteContext,
848 tfLiteNode,
849 nodeIndex,
850 kTfLiteBuiltinSpaceToBatchNd);
851 case kTfLiteBuiltinSpaceToDepth:
852 return VisitSpaceToDepthOperator(delegateData,
853 tfLiteContext,
854 tfLiteNode,
855 nodeIndex,
856 kTfLiteBuiltinSpaceToDepth);
857 case kTfLiteBuiltinSub:
858 return VisitElementwiseBinaryOperator(delegateData,
859 tfLiteContext,
860 tfLiteNode,
861 nodeIndex,
862 kTfLiteBuiltinSub);
863 case kTfLiteBuiltinTanh:
864 return VisitActivationOperator(delegateData,
865 tfLiteContext,
866 tfLiteNode,
867 nodeIndex,
868 kTfLiteBuiltinTanh);
869 default:
870 return kTfLiteError;
871 }
Sadik Armagan3c24f432020-10-19 17:35:30 +0100872}
873
874} // armnnDelegate namespace