blob: 33a07f244da01d6582fb36c4fd430de7e1ea4061 [file] [log] [blame]
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +00001//
2// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <armnn_delegate.hpp>
Ryan OSheaac9607f2023-04-03 11:33:33 +01007#include <OpaqueDelegateUtils.hpp>
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +00008
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +00009#include "Activation.hpp"
10#include "ArgMinMax.hpp"
11#include "BatchMatMul.hpp"
12#include "BatchSpace.hpp"
Idriss Chaouchcbf79292023-09-08 11:18:16 +010013#include "BroadcastTo.hpp"
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000014#include "Comparison.hpp"
15#include "Convolution.hpp"
16#include "Control.hpp"
17#include "ElementwiseBinary.hpp"
18#include "ElementwiseUnary.hpp"
19#include "Fill.hpp"
20#include "FullyConnected.hpp"
21#include "Gather.hpp"
22#include "GatherNd.hpp"
23#include "LogicalBinary.hpp"
24#include "Lstm.hpp"
25#include "Normalization.hpp"
26#include "Pack.hpp"
27#include "Pad.hpp"
28#include "Pooling.hpp"
29#include "Prelu.hpp"
30#include "Quantization.hpp"
31#include "Redefine.hpp"
32#include "Reduce.hpp"
33#include "Resize.hpp"
Tracy Narine7306bbe2023-07-17 16:06:26 +010034#include "ReverseV2.hpp"
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000035#include "Round.hpp"
36#include "Shape.hpp"
37#include "Slice.hpp"
38#include "StridedSlice.hpp"
39#include "Softmax.hpp"
40#include "SpaceDepth.hpp"
41#include "Split.hpp"
Tianle Cheng92ce35c2023-07-25 16:41:00 +010042#include "Tile.hpp"
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000043#include "Transpose.hpp"
44#include "UnidirectionalSequenceLstm.hpp"
45#include "Unpack.hpp"
46
47#include <armnn/utility/IgnoreUnused.hpp>
48#include <armnnUtils/Filesystem.hpp>
49#include <armnn/utility/Timer.hpp>
50#include <flatbuffers/flatbuffers.h>
51#include <tensorflow/lite/context_util.h>
52#include <tensorflow/lite/schema/schema_generated.h>
53#include <tensorflow/lite/minimal_logging.h>
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000054
55#include <algorithm>
56#include <iostream>
57#include <sstream>
Teresa Charlin19ad8162023-10-04 11:17:03 +010058#include <regex>
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000059
60namespace armnnOpaqueDelegate
61{
62
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +010063static auto* g_delegate_plugin_ArmnnDelegatePlugin_ =
Ryan OShea59f8f652023-05-11 20:37:53 +010064 new tflite::delegates::DelegatePluginRegistry::Register("armnn_delegate",
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +010065 ArmnnDelegatePlugin::New);
66
Teresa Charlin19ad8162023-10-04 11:17:03 +010067armnnDelegate::DelegateOptions ParseArmNNSettings(const tflite::TFLiteSettings* tfLiteSettings)
68{
69 const tflite::ArmNNSettings* settings = tfLiteSettings->armnn_settings();
70 ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(settings,
71 "The passed TFLiteSettings did not contain a valid ArmNNSettings");
72
73 // Extract settings fields
74 bool fastmath = settings->fastmath();
75 std::string backends_str = (settings->backends()) ? settings->backends()->str() : "";
76 const ::flatbuffers::String* additional_parameters = settings->additional_parameters();
77
78 // Build additional parameters string
79 std::string additional_parameters_str;
80 if (additional_parameters)
81 {
82 additional_parameters_str = additional_parameters->str();
83
84 // Apply a regex to remove spaces around the = and , signs
85 std::regex regex_equals_str("[ ]*=[ ]*");
86 std::regex regex_comma_str("[ ]*,[ ]*");
87 additional_parameters_str = std::regex_replace(additional_parameters_str, regex_equals_str, "=");
88 additional_parameters_str = std::regex_replace(additional_parameters_str, regex_comma_str, ",");
89 }
90
91 // Build a std::pair list of option names and values
92 std::vector<std::pair<std::string, std::string>> options;
93 options.emplace_back(std::pair<std::string, std::string>("backends", backends_str));
94 options.emplace_back(std::pair<std::string, std::string>("enable-fast-math", (fastmath) ? "true" : "false"));
95
96 std::stringstream additional_parameters_ss(additional_parameters_str);
97 while (additional_parameters_ss.good())
98 {
99 std::string option_str;
100 getline( additional_parameters_ss, option_str, ',' );
101 size_t n = option_str.find("=");
102 if (n != std::string::npos)
103 {
104 std::string name = option_str.substr(0, n);
105 std::string value = option_str.substr(n + 1, std::string::npos);
106 options.emplace_back(std::pair<std::string, std::string>(name, value));
107 }
108 }
109
110 // Build the key and value lists to pass into the constructor of the DelegateOptions
111 size_t num_options = options.size();
112 std::unique_ptr<const char*> options_keys = std::unique_ptr<const char*>(new const char*[num_options + 1]);
113 std::unique_ptr<const char*> options_values = std::unique_ptr<const char*>(new const char*[num_options + 1]);
114
115 for (size_t i=0; i<num_options; ++i)
116 {
117 options_keys.get()[i] = options[i].first.c_str();
118 options_values.get()[i] = options[i].second.c_str();
119 }
120
121 // Finally call the constructor
122 armnnDelegate::DelegateOptions delegateOptions = armnnDelegate::DelegateOptions(options_keys.get(),
123 options_values.get(),
124 num_options,
125 nullptr);
126
127 return delegateOptions;
128}
129
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000130ArmnnOpaqueDelegate::ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options)
131 : m_Options(std::move(options))
132{
133 // Configures logging for ARMNN
134 if (m_Options.IsLoggingEnabled())
135 {
136 armnn::ConfigureLogging(true, true, m_Options.GetLoggingSeverity());
137 }
138 // Create/Get the static ArmNN Runtime. Note that the m_Runtime will be shared by all armnn_delegate
139 // instances so the RuntimeOptions cannot be altered for different armnn_delegate instances.
140 m_Runtime = GetRuntime(m_Options.GetRuntimeOptions());
141 std::vector<armnn::BackendId> backends;
142 if (m_Runtime)
143 {
144 const armnn::BackendIdSet supportedDevices = m_Runtime->GetDeviceSpec().GetSupportedBackends();
145 for (auto& backend : m_Options.GetBackends())
146 {
147 if (std::find(supportedDevices.cbegin(), supportedDevices.cend(), backend) == supportedDevices.cend())
148 {
149 TFLITE_LOG_PROD(tflite::TFLITE_LOG_INFO,
Teresa Charlinf69ae562023-04-27 14:42:23 +0100150 "TfLiteArmnnOpaqueDelegate: Requested unknown backend %s", backend.Get().c_str());
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000151 }
152 else
153 {
154 backends.push_back(backend);
Kevin May0425a372023-11-03 12:06:04 +0000155 TFLITE_LOG_PROD(tflite::TFLITE_LOG_INFO,
156 "TfLiteArmnnOpaqueDelegate: Added backend %s", backend.Get().c_str());
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000157 }
158 }
159 }
160
161 if (backends.empty())
162 {
163 // No known backend specified
164 throw armnn::InvalidArgumentException("TfLiteArmnnOpaqueDelegate: No known backend specified.");
165 }
166 m_Options.SetBackends(backends);
167
168 TFLITE_LOG_PROD_ONCE(tflite::TFLITE_LOG_INFO, "TfLiteArmnnOpaqueDelegate: Created TfLite ArmNN delegate.");
169}
170
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100171TfLiteStatus DoPrepare(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueDelegate* tfLiteDelegate, void* data)
Matthew Sloyan54cf0112023-04-03 16:32:57 +0100172{
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100173 // We are required to have the void* data parameter in the function signature, but we don't actually use it.
174 armnn::IgnoreUnused(data);
175
Matthew Sloyan54cf0112023-04-03 16:32:57 +0100176 TfLiteIntArray* supportedOperators =
177 static_cast<::armnnOpaqueDelegate::ArmnnOpaqueDelegate*>
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100178 (TfLiteOpaqueDelegateGetData(tfLiteDelegate))->IdentifyOperatorsToDelegate(tfLiteContext);
Matthew Sloyan54cf0112023-04-03 16:32:57 +0100179 if(supportedOperators == nullptr)
180 {
181 return kTfLiteError;
182 }
183
184 // ArmNN Opaque Delegate Registration
185 TfLiteRegistrationExternal* kernelRegistration =
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +0100186 TfLiteRegistrationExternalCreate(kTfLiteBuiltinDelegate,
Ryan OShea59f8f652023-05-11 20:37:53 +0100187 "armnn_delegate",
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +0100188 /*version=*/OPAQUE_DELEGATE_MAJOR_VERSION);
Matthew Sloyan54cf0112023-04-03 16:32:57 +0100189 if(kernelRegistration == nullptr)
190 {
191 return kTfLiteError;
192 }
193
194 TfLiteRegistrationExternalSetInit(
195 kernelRegistration,
196 [](TfLiteOpaqueContext* tfLiteContext, const char* buffer, size_t length) -> void*
197 {
198 armnn::IgnoreUnused(length);
199 const TfLiteOpaqueDelegateParams* parameters =
200 reinterpret_cast<const TfLiteOpaqueDelegateParams*>(buffer);
201 if(parameters == nullptr)
202 {
203 TF_LITE_OPAQUE_KERNEL_LOG(tfLiteContext,
204 "TfLiteArmnnOpaqueDelegate: Unable to get parameters.");
205 return nullptr;
206 }
207
208 return static_cast<void*>(
209 ArmnnSubgraph::Create(tfLiteContext,
210 parameters,
211 static_cast<::armnnOpaqueDelegate::ArmnnOpaqueDelegate*>(
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100212 parameters->delegate->opaque_delegate_builder->data)));
Matthew Sloyan54cf0112023-04-03 16:32:57 +0100213 }
214 );
215
216 TfLiteRegistrationExternalSetFree(
217 kernelRegistration,
218 [](TfLiteOpaqueContext* tfLiteContext, void* buffer) -> void
219 {
220 armnn::IgnoreUnused(tfLiteContext);
221 if (buffer != nullptr)
222 {
223 delete static_cast<ArmnnSubgraph*>(buffer);
224 }
225 }
226 );
227
228 TfLiteRegistrationExternalSetPrepare(
229 kernelRegistration,
230 [](TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode) -> TfLiteStatus
231 {
232 void* userData = TfLiteOpaqueNodeGetUserData(tfLiteNode);
233 if (userData == nullptr)
234 {
235 return kTfLiteError;
236 }
237 return static_cast<ArmnnSubgraph*>(userData)->Prepare(tfLiteContext);
238 }
239 );
240
241 TfLiteRegistrationExternalSetInvoke(
242 kernelRegistration,
243 [](TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode) -> TfLiteStatus
244 {
245 void* userData = TfLiteOpaqueNodeGetUserData(tfLiteNode);
246 if (userData == nullptr)
247 {
248 return kTfLiteError;
249 }
250
251 return static_cast<ArmnnSubgraph*>(userData)->Invoke(tfLiteContext, tfLiteNode);
252 }
253 );
254
255 const TfLiteStatus status =
256 TfLiteOpaqueContextReplaceNodeSubsetsWithDelegateKernels(
257 tfLiteContext, kernelRegistration, supportedOperators, tfLiteDelegate);
258
259 TfLiteIntArrayFree(supportedOperators);
260 return status;
261}
262
Teresa Charlin3e4b6082023-10-19 19:13:29 +0100263TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(armnnDelegate::DelegateOptions options)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000264{
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000265 auto* armnnDelegate = new ::armnnOpaqueDelegate::ArmnnOpaqueDelegate(options);
266 return TfLiteOpaqueDelegateCreate(armnnDelegate->GetDelegateBuilder());
267}
268
269::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault()
270{
271 ::armnnDelegate::DelegateOptions options(armnn::Compute::CpuRef);
272 return options;
273}
274
275void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate)
276{
277 if (tfLiteDelegate != nullptr)
278 {
279 delete static_cast<::armnnOpaqueDelegate::ArmnnOpaqueDelegate*>(TfLiteOpaqueDelegateGetData(tfLiteDelegate));
280 TfLiteOpaqueDelegateDelete(tfLiteDelegate);
281 }
282}
283
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000284const std::string ArmnnOpaqueDelegate::GetVersion() {
285 return OPAQUE_DELEGATE_VERSION;
286}
287
Matthew Sloyan54cf0112023-04-03 16:32:57 +0100288TfLiteIntArray* ArmnnOpaqueDelegate::IdentifyOperatorsToDelegate(TfLiteOpaqueContext* tfLiteContext)
289{
290 TfLiteIntArray* executionPlan = nullptr;
291 if (TfLiteOpaqueContextGetExecutionPlan(tfLiteContext, &executionPlan) != kTfLiteOk)
292 {
293 TF_LITE_OPAQUE_KERNEL_LOG(tfLiteContext, "TfLiteArmnnOpaqueDelegate: Unable to get graph execution plan.");
294 return nullptr;
295 }
296
297 // Delegate data with null network
298 DelegateData delegateData(m_Options.GetBackends());
299
300 TfLiteIntArray* nodesToDelegate = TfLiteIntArrayCreate(executionPlan->size);
301 if (nodesToDelegate == nullptr)
302 {
303 TF_LITE_OPAQUE_KERNEL_LOG(tfLiteContext,
304 "TfLiteArmnnOpaqueDelegate: Unable to create int array from execution plan.");
305 return nullptr;
306 }
307 nodesToDelegate->size = 0;
308
309 std::set<int32_t> unsupportedOperators;
310
311 for (int i = 0; i < executionPlan->size; ++i)
312 {
313 const int nodeIndex = executionPlan->data[i];
314
315 // If TfLiteOpaqueNodes can be delegated to ArmNN
316 TfLiteOpaqueNode* tfLiteNode = nullptr;
317 TfLiteRegistrationExternal* tfLiteRegistration = nullptr;
318
319 if (TfLiteOpaqueContextGetNodeAndRegistration(
320 tfLiteContext, nodeIndex, &tfLiteNode, &tfLiteRegistration) != kTfLiteOk)
321 {
322 TF_LITE_OPAQUE_KERNEL_LOG(tfLiteContext,
323 "TfLiteArmnnOpaqueDelegate: Unable to get node and registration for node %d.",
324 nodeIndex);
325 continue;
326 }
327
328 TfLiteStatus visitStatus;
329 try
330 {
331 visitStatus = ArmnnSubgraph::VisitNode(
332 delegateData, tfLiteContext, tfLiteRegistration, tfLiteNode, nodeIndex);
333 }
334 catch(std::exception& ex)
335 {
336 ARMNN_LOG(error) << "ArmNN Failed to visit node with error: " << ex.what();
337 visitStatus = kTfLiteError;
Ciara Sookarry39436152023-10-31 15:44:41 +0000338 TF_LITE_OPAQUE_KERNEL_LOG(tfLiteContext,
339 "Exception text: %s",
340 ex.what());
Matthew Sloyan54cf0112023-04-03 16:32:57 +0100341 }
342
343 if (visitStatus != kTfLiteOk)
344 {
345 // node is not supported by ArmNN
346 unsupportedOperators.insert(TfLiteRegistrationExternalGetBuiltInCode(tfLiteRegistration));
347 continue;
348 }
349
350 nodesToDelegate->data[nodesToDelegate->size++] = nodeIndex;
351 }
352
353 for (std::set<int32_t>::iterator it=unsupportedOperators.begin(); it!=unsupportedOperators.end(); ++it)
354 {
355 TF_LITE_OPAQUE_KERNEL_LOG(tfLiteContext,
356 "Operator %s [%d] is not supported by armnn_opaque_delegate.",
357 tflite::EnumNameBuiltinOperator(tflite::BuiltinOperator(*it)),
358 *it);
359 }
360
361 if (!unsupportedOperators.empty() && m_Options.TfLiteRuntimeFallbackDisabled())
362 {
363 std::stringstream exMessage;
364 exMessage << "TfLiteArmnnOpaqueDelegate: There are unsupported operators in the model. ";
365 exMessage << "Not falling back to TfLite Runtime as fallback is disabled. ";
366 exMessage << "This should only be disabled under test conditions.";
367 throw armnn::Exception(exMessage.str());
368 }
369 if (nodesToDelegate->size == 0)
370 {
371 ARMNN_LOG(info) << "No operators in this model are supported by the Arm NN TfLite delegate." <<
372 " The model will be executed entirely by TfLite runtime.";
373 }
374
375 std::sort(&nodesToDelegate->data[0], &nodesToDelegate->data[nodesToDelegate->size]);
376 return nodesToDelegate;
377}
378
Ryan OSheaac9607f2023-04-03 11:33:33 +0100379TfLiteStatus ArmnnSubgraph::AddInputLayer(DelegateData& delegateData,
380 TfLiteOpaqueContext* tfLiteContext,
381 const TfLiteIntArray* inputs,
382 std::vector<armnn::BindingPointInfo>& inputBindings)
383{
384 const size_t numInputs = static_cast<size_t>(inputs->size);
385 for (unsigned int i = 0; i < numInputs; ++i)
386 {
387 const int32_t tensorId = inputs->data[i];
388 const TfLiteOpaqueTensor* tensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, tensorId);
389
390 if(!tensor)
391 {
392 return kTfLiteError;
393 }
394
395 // Do not create bindings for constant inputs
396 if (TfLiteOpaqueTensorGetAllocationType(tensor) == kTfLiteMmapRo)
397 {
398 continue;
399 }
400
401 auto bindingId = static_cast<armnn::LayerBindingId>((tensorId));
402 armnn::IConnectableLayer* layer = delegateData.m_Network->AddInputLayer(bindingId);
403
404 auto tensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tensor);
405 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
406 outputSlot.SetTensorInfo(tensorInfo);
407
408 // Store for creating connections
409 delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)] = &outputSlot;
410
411 inputBindings.push_back(std::make_pair(bindingId, tensorInfo));
412 }
413
414 return kTfLiteOk;
415}
416
417TfLiteStatus ArmnnSubgraph::AddOutputLayer(DelegateData& delegateData,
418 TfLiteOpaqueContext* tfLiteContext,
419 const TfLiteIntArray* outputs,
420 std::vector<armnn::BindingPointInfo>& outputBindings)
421{
422 const size_t numOutputs = static_cast<size_t>(outputs->size);
423 for (unsigned int i = 0; i < numOutputs; ++i)
424 {
425 const int32_t tensorId = outputs->data[i];
426 const TfLiteOpaqueTensor* tensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, tensorId);
427
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100428 if(!IsValid(tensor))
Ryan OSheaac9607f2023-04-03 11:33:33 +0100429 {
430 return kTfLiteError;
431 }
432
433 auto bindingId = static_cast<armnn::LayerBindingId>((tensorId));
434 armnn::IConnectableLayer* layer = delegateData.m_Network->AddOutputLayer(bindingId);
435
436 auto tensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tensor);
Ryan OSheac229b3f2023-06-27 22:34:54 +0100437
438 if (delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)] == nullptr)
439 {
440 return kTfLiteError;
441 }
442
Ryan OSheaac9607f2023-04-03 11:33:33 +0100443 delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)]->Connect(layer->GetInputSlot(0));
444 outputBindings.push_back(std::make_pair(bindingId, tensorInfo));
445 }
446
447 return kTfLiteOk;
448}
449
450ArmnnSubgraph* ArmnnSubgraph::Create(TfLiteOpaqueContext* tfLiteContext,
451 const TfLiteOpaqueDelegateParams* parameters,
452 const ArmnnOpaqueDelegate* delegate)
453{
454 const auto startTime = armnn::GetTimeNow();
455 ARMNN_LOG(info) << "ArmnnSubgraph creation";
456
457 TfLiteIntArray* executionPlan;
458 if (TfLiteOpaqueContextGetExecutionPlan(tfLiteContext, &executionPlan) != kTfLiteOk)
459 {
460 return nullptr;
461 }
462
463 // Initialize DelegateData holds network and output slots information
464 DelegateData delegateData(delegate->m_Options.GetBackends());
465
466 // Build ArmNN Network
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000467 armnn::NetworkOptions networkOptions = delegate->m_Options.GetOptimizerOptions().GetModelOptions();
Ryan OSheaac9607f2023-04-03 11:33:33 +0100468 armnn::NetworkId networkId;
469 delegateData.m_Network = armnn::INetwork::Create(networkOptions);
470
471 delegateData.m_OutputSlotForNode = std::vector<armnn::IOutputSlot*>(
472 TfLiteOpaqueContextGetNumTensors(tfLiteContext), nullptr);
473
474 std::vector<armnn::BindingPointInfo> inputBindings;
475 std::vector<armnn::BindingPointInfo> outputBindings;
476
477 // Add input layer
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100478 if (AddInputLayer(delegateData, tfLiteContext, parameters->input_tensors, inputBindings) != kTfLiteOk)
Ryan OSheaac9607f2023-04-03 11:33:33 +0100479 {
480 throw armnn::Exception("TfLiteArmnnOpaqueDelegate: Unable to add Inputs to the network!");
481 }
482
483 // Parse TfLite delegate nodes to ArmNN
484 const auto parseStartTime = armnn::GetTimeNow();
485 for (int i = 0; i < parameters->nodes_to_replace->size; ++i)
486 {
487 const int nodeIndex = parameters->nodes_to_replace->data[i];
488
489 TfLiteOpaqueNode* tfLiteNode = nullptr;
490 TfLiteRegistrationExternal* tfLiteRegistration = nullptr;
491 if (TfLiteOpaqueContextGetNodeAndRegistration(
492 tfLiteContext, nodeIndex, &tfLiteNode, &tfLiteRegistration) != kTfLiteOk)
493 {
494 throw armnn::Exception(&"TfLiteArmnnOpaqueDelegate: Unable to get node registration: " [ nodeIndex]);
495 }
496
497 if (VisitNode(delegateData, tfLiteContext, tfLiteRegistration, tfLiteNode, nodeIndex) != kTfLiteOk)
498 {
499 throw armnn::Exception(&"TfLiteArmnnOpaqueDelegate: Unable to parse node: " [ nodeIndex]);
500 }
501 }
502 ARMNN_LOG(info) << "Parse nodes to ArmNN time: " << std::setprecision(2)
503 << std::fixed << armnn::GetTimeDuration(parseStartTime).count() << " ms";
504
505 // Add Output layer
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100506 if (AddOutputLayer(delegateData, tfLiteContext, parameters->output_tensors, outputBindings) != kTfLiteOk)
Ryan OSheaac9607f2023-04-03 11:33:33 +0100507 {
508 throw armnn::Exception("TfLiteArmnnOpaqueDelegate: Unable to add Outputs to the network!");
509 }
510
511 // Optimize ArmNN network
512 armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr);
513 try
514 {
515 const auto optimizeStartTime = armnn::GetTimeNow();
516 optNet = armnn::Optimize(*(delegateData.m_Network.get()),
517 delegate->m_Options.GetBackends(),
518 delegate->m_Runtime->GetDeviceSpec(),
519 delegate->m_Options.GetOptimizerOptions());
520 ARMNN_LOG(info) << "Optimize ArmnnSubgraph time: " << std::setprecision(2)
521 << std::fixed << armnn::GetTimeDuration(optimizeStartTime).count() << " ms";
522 }
523 catch (std::exception& ex)
524 {
525 std::stringstream exMessage;
526 exMessage << "TfLiteArmnnOpaqueDelegate: Exception (" << ex.what() << ") caught from optimize.";
527 throw armnn::Exception(exMessage.str());
528 }
529 if (!optNet)
530 {
531 // Optimize failed
532 throw armnn::Exception("TfLiteArmnnOpaqueDelegate: Unable to optimize the network!");
533 }
534
535 // If set, we will serialize the optimized model into a dot file.
536 const std::string serializeToDotFile = delegate->m_Options.GetSerializeToDot();
537 if (!serializeToDotFile.empty())
538 {
539 ARMNN_LOG(info) << "Writing graph to dot file: " << serializeToDotFile;
540 fs::path filename = serializeToDotFile;
541 std::fstream file(filename.c_str(), std::ios_base::out);
542 optNet->SerializeToDot(file);
543 }
544
545 try
546 {
547 const auto loadStartTime = armnn::GetTimeNow();
548
549 // Load graph into runtime
550 std::string errorMessage;
551 armnn::Status loadingStatus;
552 armnn::MemorySource inputSource = armnn::MemorySource::Undefined;
553 armnn::MemorySource outputSource = armnn::MemorySource::Undefined;
554 // There's a bit of an assumption here that the delegate will only support Malloc memory source.
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000555 if (delegate->m_Options.GetOptimizerOptions().GetImportEnabled())
Ryan OSheaac9607f2023-04-03 11:33:33 +0100556 {
557 inputSource = armnn::MemorySource::Malloc;
558 }
John Mcloughlinc5ee0d72023-03-24 12:07:25 +0000559 if (delegate->m_Options.GetOptimizerOptions().GetExportEnabled())
Ryan OSheaac9607f2023-04-03 11:33:33 +0100560 {
561 outputSource = armnn::MemorySource::Malloc;
562 }
563 armnn::INetworkProperties networkProperties(false,
564 inputSource,
565 outputSource,
566 delegate->m_Options.GetInternalProfilingState(),
567 delegate->m_Options.GetInternalProfilingDetail());
568 loadingStatus = delegate->m_Runtime->LoadNetwork(networkId,
569 std::move(optNet),
570 errorMessage,
571 networkProperties);
572 if (loadingStatus != armnn::Status::Success)
573 {
574 // Network load failed.
575 throw armnn::Exception("TfLiteArmnnOpaqueDelegate: Network could not be loaded: " + errorMessage);
576 }
577
578 ARMNN_LOG(info) << "Load ArmnnSubgraph time: " << std::setprecision(2)
579 << std::fixed << armnn::GetTimeDuration(loadStartTime).count() << " ms";
580 }
581 catch (std::exception& ex)
582 {
583 std::stringstream exMessage;
584 exMessage << "TfLiteArmnnOpaqueDelegate: Exception (" << ex.what() << ") caught from LoadNetwork.";
585 throw armnn::Exception(exMessage.str());
586 }
587
588 // Register debug callback function
589 if (delegate->m_Options.GetDebugCallbackFunction().has_value())
590 {
591 delegate->m_Runtime->RegisterDebugCallback(networkId, delegate->m_Options.GetDebugCallbackFunction().value());
592 }
593
594 ARMNN_LOG(info) << "Overall ArmnnSubgraph creation time: " << std::setprecision(2)
595 << std::fixed << armnn::GetTimeDuration(startTime).count() << " ms\n";
596
597 // Create a new SubGraph with networkId and runtime
598 return new ArmnnSubgraph(networkId, delegate->m_Runtime, inputBindings, outputBindings);
599}
600
601TfLiteStatus ArmnnSubgraph::Prepare(TfLiteOpaqueContext* tfLiteContext)
602{
603 armnn::IgnoreUnused(tfLiteContext);
604 return kTfLiteOk;
605}
606
607TfLiteStatus ArmnnSubgraph::Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode)
608{
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100609 // Get array of input indices, inputIndexArray is set from the TfLiteOpaqueNodeInputs function
610 // This function turns inputIndexArray into an int array of indices. These indices point to the tensors for
611 // each input slot in the node.
612 const int* inputIndexArray;
Ryan OSheaac9607f2023-04-03 11:33:33 +0100613 int numInputs;
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100614 if(TfLiteOpaqueNodeInputs(tfLiteNode, &inputIndexArray, &numInputs) != kTfLiteOk)
Ryan OSheaac9607f2023-04-03 11:33:33 +0100615 {
616 throw armnn::Exception("TfLiteArmnnOpaqueDelegate: Unable to load subgraph inputs!");
617 }
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100618 // Prepare inputs
619 armnn::InputTensors inputTensors;
620 size_t inputIndex = 0;
Ryan OSheaac9607f2023-04-03 11:33:33 +0100621 for (int inputIdx = 0; inputIdx < numInputs; inputIdx++)
622 {
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100623 TfLiteOpaqueTensor* tensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputIndexArray[inputIdx]);
Ryan OSheaac9607f2023-04-03 11:33:33 +0100624
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100625 if(!IsValid(tensor))
Ryan OSheaac9607f2023-04-03 11:33:33 +0100626 {
627 return kTfLiteError;
628 }
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100629 // If tensor is not read only
Ryan OSheaac9607f2023-04-03 11:33:33 +0100630 if (TfLiteOpaqueTensorGetAllocationType(tensor) != kTfLiteMmapRo)
631 {
632 const armnn::BindingPointInfo& inputBinding = m_InputBindings[inputIndex];
633 armnn::TensorInfo inputTensorInfo = inputBinding.second;
634 inputTensorInfo.SetConstant(true);
635 const armnn::ConstTensor inputTensor(inputTensorInfo, TfLiteOpaqueTensorData(tensor));
Narumol Prangnawarat46e574e2023-05-05 16:39:05 +0100636 inputTensors.emplace_back(inputIndexArray[inputIdx], inputTensor);
Ryan OSheaac9607f2023-04-03 11:33:33 +0100637
638 ++inputIndex;
639 }
640 }
641
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100642 // Get array of output indices, outputIndexArray is set from the TfLiteOpaqueNodeOutputs function
643 // This function turns outputIndexArray into an int array of indices. These indices point to the tensors for
644 // each output slot in the node.
645 const int* outputIndexArray;
Ryan OSheaac9607f2023-04-03 11:33:33 +0100646 int numOutputs;
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100647 if(TfLiteOpaqueNodeOutputs(tfLiteNode, &outputIndexArray, &numOutputs) != kTfLiteOk)
Ryan OSheaac9607f2023-04-03 11:33:33 +0100648 {
649 throw armnn::Exception("TfLiteArmnnOpaqueDelegate: Unable to load subgraph outputs!");
650 }
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100651 // Assign the tensors from the outputIndexArray to the armnn BindingPointInfo
652 armnn::OutputTensors outputTensors;
Ryan OSheaac9607f2023-04-03 11:33:33 +0100653 for (int outputIdx = 0; outputIdx < numOutputs; outputIdx++)
654 {
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100655 const armnn::BindingPointInfo& outputBinding = m_OutputBindings[outputIdx];
656 TfLiteOpaqueTensor* tensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, outputIndexArray[outputIdx]);
657 if(!IsValid(tensor))
Ryan OSheaac9607f2023-04-03 11:33:33 +0100658 {
659 return kTfLiteError;
660 }
661
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100662 const armnn::Tensor outputTensor(outputBinding.second, reinterpret_cast<TfLiteTensor*>(tensor)->data
663 .data);
664 outputTensors.emplace_back(outputIndexArray[outputIdx], outputTensor);
Ryan OSheaac9607f2023-04-03 11:33:33 +0100665 }
666
667 // Run graph
David Monahan727d0172023-10-04 10:16:24 +0100668 try
Ryan OSheaac9607f2023-04-03 11:33:33 +0100669 {
David Monahan727d0172023-10-04 10:16:24 +0100670 auto status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors);
671 // The delegate holds its own Arm NN runtime so this is our last chance to print internal profiling data.
672 std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId);
673 if (profiler && profiler->IsProfilingEnabled())
674 {
675 profiler->Print(std::cout);
676 }
677 return (status == armnn::Status::Success) ? kTfLiteOk : kTfLiteError;
Ryan OSheaac9607f2023-04-03 11:33:33 +0100678 }
David Monahan727d0172023-10-04 10:16:24 +0100679 catch (armnn::InvalidArgumentException& ex)
680 {
681 ARMNN_LOG(error) << "ArmNN Failed to EnqueueWorkload with error: " << ex.what();
682 // This should really be kTfLiteDelegateError but the Delegate Test Suite expects kTfLiteError so we return
683 // that instead
684 return kTfLiteError;
685 }
686
Ryan OSheaac9607f2023-04-03 11:33:33 +0100687}
688
689TfLiteStatus ArmnnSubgraph::VisitNode(DelegateData& delegateData,
690 TfLiteOpaqueContext* tfLiteContext,
691 TfLiteRegistrationExternal* tfLiteRegistration,
692 TfLiteOpaqueNode* tfLiteNode,
693 int nodeIndex)
694{
695 switch (TfLiteRegistrationExternalGetBuiltInCode(tfLiteRegistration))
696 {
Teresa Charlinf69ae562023-04-27 14:42:23 +0100697 case kTfLiteBuiltinAbs:
698 return VisitElementwiseUnaryOperator(delegateData,
699 tfLiteContext,
700 tfLiteNode,
701 nodeIndex,
702 kTfLiteBuiltinAbs,
703 armnn::UnaryOperation::Abs);
David Monahan6c53f9f2023-04-27 15:21:19 +0100704 case kTfLiteBuiltinAdd:
705 return VisitElementwiseBinaryOperator(delegateData,
706 tfLiteContext,
707 tfLiteNode,
708 nodeIndex,
709 kTfLiteBuiltinAdd);
John Mcloughlin559d9092023-04-26 20:14:47 +0100710 case kTfLiteBuiltinArgMax:
711 return VisitArgMinMaxOperator(delegateData,
712 tfLiteContext,
713 tfLiteNode,
714 nodeIndex,
715 kTfLiteBuiltinArgMax);
716 case kTfLiteBuiltinArgMin:
717 return VisitArgMinMaxOperator(delegateData,
718 tfLiteContext,
719 tfLiteNode,
720 nodeIndex,
721 kTfLiteBuiltinArgMin);
Matthew Sloyan48ec8132023-04-27 17:04:47 +0100722 case kTfLiteBuiltinAveragePool2d:
723 return VisitPooling2dOperator(delegateData,
724 tfLiteContext,
725 tfLiteNode,
726 nodeIndex,
727 kTfLiteBuiltinAveragePool2d);
John Mcloughlin0422cf22023-04-27 16:55:00 +0100728 case kTfLiteBuiltinBatchMatmul:
729 return VisitBatchMatMulOperator(delegateData,
730 tfLiteContext,
731 tfLiteNode,
732 nodeIndex,
733 kTfLiteBuiltinBatchMatmul);
Idriss Chaouchcbf79292023-09-08 11:18:16 +0100734 case kTfLiteBuiltinBroadcastTo:
735 return VisitBroadcastToOperator(delegateData,
736 tfLiteContext,
737 tfLiteNode,
738 nodeIndex,
739 kTfLiteBuiltinBroadcastTo);
Kevin May81b66f32023-04-26 14:55:36 +0100740 case kTfLiteBuiltinBatchToSpaceNd:
741 return VisitBatchToSpaceNdOperator(delegateData,
742 tfLiteContext,
743 tfLiteNode,
744 nodeIndex,
745 kTfLiteBuiltinBatchToSpaceNd);
Ryan OSheaa37ccb02023-04-11 10:54:07 +0100746 case kTfLiteBuiltinCast:
747 return VisitCastOperator(delegateData,
748 tfLiteContext,
749 tfLiteNode,
750 nodeIndex,
751 kTfLiteBuiltinCast);
Teresa Charlinf69ae562023-04-27 14:42:23 +0100752 case kTfLiteBuiltinCeil:
753 return VisitElementwiseUnaryOperator(delegateData,
754 tfLiteContext,
755 tfLiteNode,
756 nodeIndex,
757 kTfLiteBuiltinCeil,
758 armnn::UnaryOperation::Ceil);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +0100759 case kTfLiteBuiltinConcatenation:
760 return VisitControlOperator(delegateData,
761 tfLiteContext,
762 tfLiteNode,
763 nodeIndex,
764 kTfLiteBuiltinConcatenation);
Matthew Sloyan080ffd82023-04-24 12:53:04 +0100765 case kTfLiteBuiltinConv2d:
766 return VisitConvolutionOperator(delegateData,
767 tfLiteContext,
768 tfLiteNode,
769 nodeIndex,
770 kTfLiteBuiltinConv2d);
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +0100771 case kTfLiteBuiltinConv3d:
772 return VisitConvolutionOperator(delegateData,
773 tfLiteContext,
774 tfLiteNode,
775 nodeIndex,
776 kTfLiteBuiltinConv3d);
Matthew Sloyan48ec8132023-04-27 17:04:47 +0100777 case kTfLiteBuiltinCustom:
778 {
779 // Custom operators are defined by the name rather than the builtin code.
780 // Parse the custom_name param in the registration to point to the correct visitor function.
781 std::string customOperatorName = TfLiteRegistrationExternalGetCustomName(tfLiteRegistration);
782 if ( customOperatorName == "AveragePool3D" )
783 {
784 return VisitPooling3dOperator(delegateData,
785 tfLiteContext,
786 tfLiteNode,
787 nodeIndex,
788 customOperatorName);
789 }
790 else if (customOperatorName == "MaxPool3D")
791 {
792 return VisitPooling3dOperator(delegateData,
793 tfLiteContext,
794 tfLiteNode,
795 nodeIndex,
796 customOperatorName);
797 }
798 // Invalid or unsupported custom operator
799 return kTfLiteError;
800 }
Matthew Sloyan080ffd82023-04-24 12:53:04 +0100801 case kTfLiteBuiltinDepthwiseConv2d:
802 return VisitConvolutionOperator(delegateData,
803 tfLiteContext,
804 tfLiteNode,
805 nodeIndex,
806 kTfLiteBuiltinDepthwiseConv2d);
Francis Murtagh36d94ef2023-04-28 14:05:43 +0100807 case kTfLiteBuiltinDequantize:
808 return VisitDequantizeOperator(delegateData,
809 tfLiteContext,
810 tfLiteNode,
811 nodeIndex,
812 kTfLiteBuiltinDequantize);
David Monahan6c53f9f2023-04-27 15:21:19 +0100813 case kTfLiteBuiltinDiv:
814 return VisitElementwiseBinaryOperator(delegateData,
815 tfLiteContext,
816 tfLiteNode,
817 nodeIndex,
818 kTfLiteBuiltinDiv);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +0100819 case kTfLiteBuiltinEqual:
820 return VisitComparisonOperator(delegateData,
821 tfLiteContext,
822 tfLiteNode,
823 nodeIndex,
Teresa Charlinf69ae562023-04-27 14:42:23 +0100824 kTfLiteBuiltinEqual,
825 armnn::ComparisonOperation::Equal);
Teresa Charlin42362962023-04-28 14:23:33 +0100826 case kTfLiteBuiltinDepthToSpace:
827 return VisitDepthToSpaceOperator(delegateData,
828 tfLiteContext,
829 tfLiteNode,
830 nodeIndex,
831 kTfLiteBuiltinDepthToSpace);
832 case kTfLiteBuiltinElu:
833 return VisitActivationOperator(delegateData,
834 tfLiteContext,
835 tfLiteNode,
836 nodeIndex,
837 kTfLiteBuiltinElu);
Teresa Charlinf69ae562023-04-27 14:42:23 +0100838 case kTfLiteBuiltinExp:
839 return VisitElementwiseUnaryOperator(delegateData,
840 tfLiteContext,
841 tfLiteNode,
842 nodeIndex,
843 kTfLiteBuiltinExp,
844 armnn::UnaryOperation::Exp);
Matthew Sloyan3504e422023-05-03 13:53:02 +0100845 case kTfLiteBuiltinExpandDims:
846 return VisitExpandDimsOperator(delegateData,
847 tfLiteContext,
848 tfLiteNode,
849 nodeIndex,
850 kTfLiteBuiltinExpandDims);
Ryan OShea59f8f652023-05-11 20:37:53 +0100851 case kTfLiteBuiltinFill:
852 return VisitFillOperator(delegateData,
853 tfLiteContext,
854 tfLiteNode,
855 nodeIndex,
856 kTfLiteBuiltinFill);
Matthew Sloyan48ec8132023-04-27 17:04:47 +0100857 case kTfLiteBuiltinFloor:
858 return VisitFloorOperator(delegateData,
859 tfLiteContext,
860 tfLiteNode,
861 nodeIndex,
862 kTfLiteBuiltinFloor);
David Monahan6c53f9f2023-04-27 15:21:19 +0100863 case kTfLiteBuiltinFloorDiv:
864 return VisitElementwiseBinaryOperator(delegateData,
865 tfLiteContext,
866 tfLiteNode,
867 nodeIndex,
868 kTfLiteBuiltinFloorDiv);
Matthew Sloyan0bd4c622023-04-27 11:48:26 +0100869 case kTfLiteBuiltinFullyConnected:
870 return VisitFullyConnectedOperator(delegateData,
871 tfLiteContext,
872 tfLiteNode,
873 nodeIndex,
874 kTfLiteBuiltinFullyConnected);
Kevin Mayb2831c52023-04-26 17:27:24 +0100875 case kTfLiteBuiltinGather:
876 return VisitGatherOperator(delegateData,
877 tfLiteContext,
878 tfLiteNode,
879 nodeIndex,
880 kTfLiteBuiltinGather);
881 case kTfLiteBuiltinGatherNd:
882 return VisitGatherNdOperator(delegateData,
883 tfLiteContext,
884 tfLiteNode,
885 nodeIndex,
886 kTfLiteBuiltinGatherNd);
Teresa Charlin077cddb2023-09-15 15:19:21 +0100887 case kTfLiteBuiltinGelu:
888 return VisitActivationOperator(delegateData,
889 tfLiteContext,
890 tfLiteNode,
891 nodeIndex,
892 kTfLiteBuiltinGelu);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +0100893 case kTfLiteBuiltinGreater:
894 return VisitComparisonOperator(delegateData,
895 tfLiteContext,
896 tfLiteNode,
897 nodeIndex,
Teresa Charlinf69ae562023-04-27 14:42:23 +0100898 kTfLiteBuiltinGreater,
899 armnn::ComparisonOperation::Greater);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +0100900 case kTfLiteBuiltinGreaterEqual:
901 return VisitComparisonOperator(delegateData,
902 tfLiteContext,
903 tfLiteNode,
904 nodeIndex,
Teresa Charlinf69ae562023-04-27 14:42:23 +0100905 kTfLiteBuiltinGreaterEqual,
906 armnn::ComparisonOperation::GreaterOrEqual);
Matthew Sloyan0bd4c622023-04-27 11:48:26 +0100907 case kTfLiteBuiltinHardSwish:
908 return VisitActivationOperator(delegateData,
909 tfLiteContext,
910 tfLiteNode,
911 nodeIndex,
912 kTfLiteBuiltinHardSwish);
Teresa Charlinf69ae562023-04-27 14:42:23 +0100913 case kTfLiteBuiltinL2Normalization:
914 return VisitL2NormalizationOperator(delegateData,
915 tfLiteContext,
916 tfLiteNode,
917 nodeIndex,
918 kTfLiteBuiltinL2Normalization);
Matthew Sloyan48ec8132023-04-27 17:04:47 +0100919 case kTfLiteBuiltinL2Pool2d:
920 return VisitPooling2dOperator(delegateData,
921 tfLiteContext,
922 tfLiteNode,
923 nodeIndex,
924 kTfLiteBuiltinL2Pool2d);
Tianle Chengae931732023-07-28 11:53:04 +0100925 case kTfLiteBuiltinLeakyRelu:
926 return VisitActivationOperator(delegateData,
927 tfLiteContext,
928 tfLiteNode,
929 nodeIndex,
930 kTfLiteBuiltinLeakyRelu);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +0100931 case kTfLiteBuiltinLess:
932 return VisitComparisonOperator(delegateData,
933 tfLiteContext,
934 tfLiteNode,
935 nodeIndex,
Teresa Charlinf69ae562023-04-27 14:42:23 +0100936 kTfLiteBuiltinLess,
937 armnn::ComparisonOperation::Less);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +0100938 case kTfLiteBuiltinLessEqual:
939 return VisitComparisonOperator(delegateData,
940 tfLiteContext,
941 tfLiteNode,
942 nodeIndex,
Teresa Charlinf69ae562023-04-27 14:42:23 +0100943 kTfLiteBuiltinLessEqual,
944 armnn::ComparisonOperation::LessOrEqual);
Matthew Sloyan0bd4c622023-04-27 11:48:26 +0100945 case kTfLiteBuiltinLogistic:
946 return VisitActivationOperator(delegateData,
947 tfLiteContext,
948 tfLiteNode,
949 nodeIndex,
950 kTfLiteBuiltinLogistic);
Teresa Charlinf69ae562023-04-27 14:42:23 +0100951 case kTfLiteBuiltinLocalResponseNormalization:
952 return VisitLocalResponseNormalizationOperator(delegateData,
953 tfLiteContext,
954 tfLiteNode,
955 nodeIndex,
956 kTfLiteBuiltinLocalResponseNormalization);
957 case kTfLiteBuiltinLog:
958 return VisitElementwiseUnaryOperator(delegateData,
959 tfLiteContext,
960 tfLiteNode,
961 nodeIndex,
962 kTfLiteBuiltinLog,
963 armnn::UnaryOperation::Log);
964 case kTfLiteBuiltinLogicalAnd:
965 return VisitLogicalBinaryOperator(delegateData,
966 tfLiteContext,
967 tfLiteNode,
968 nodeIndex,
969 kTfLiteBuiltinLogicalAnd,
970 armnn::LogicalBinaryOperation::LogicalAnd);
971 case kTfLiteBuiltinLogicalNot:
972 return VisitElementwiseUnaryOperator(delegateData,
973 tfLiteContext,
974 tfLiteNode,
975 nodeIndex,
976 kTfLiteBuiltinLogicalNot,
977 armnn::UnaryOperation::LogicalNot);
978 case kTfLiteBuiltinLogicalOr:
979 return VisitLogicalBinaryOperator(delegateData,
980 tfLiteContext,
981 tfLiteNode,
982 nodeIndex,
983 kTfLiteBuiltinLogicalOr,
984 armnn::LogicalBinaryOperation::LogicalOr);
Teresa Charlin42362962023-04-28 14:23:33 +0100985 case kTfLiteBuiltinLogSoftmax:
986 return VisitSoftmaxOperator(delegateData,
987 tfLiteContext,
988 tfLiteNode,
989 nodeIndex,
990 kTfLiteBuiltinLogSoftmax);
Matthew Sloyan48ec8132023-04-27 17:04:47 +0100991 case kTfLiteBuiltinLstm:
992 return VisitLstmOperator(delegateData,
993 tfLiteContext,
994 tfLiteNode,
995 nodeIndex,
996 kTfLiteBuiltinLstm);
997 case kTfLiteBuiltinMaxPool2d:
998 return VisitPooling2dOperator(delegateData,
999 tfLiteContext,
1000 tfLiteNode,
1001 nodeIndex,
1002 kTfLiteBuiltinMaxPool2d);
David Monahan6c53f9f2023-04-27 15:21:19 +01001003 case kTfLiteBuiltinMaximum:
1004 return VisitElementwiseBinaryOperator(delegateData,
1005 tfLiteContext,
1006 tfLiteNode,
1007 nodeIndex,
1008 kTfLiteBuiltinMaximum);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +01001009 case kTfLiteBuiltinMean:
1010 return VisitControlOperator(delegateData,
1011 tfLiteContext,
1012 tfLiteNode,
1013 nodeIndex,
1014 kTfLiteBuiltinMean);
David Monahan6c53f9f2023-04-27 15:21:19 +01001015 case kTfLiteBuiltinMinimum:
1016 return VisitElementwiseBinaryOperator(delegateData,
1017 tfLiteContext,
1018 tfLiteNode,
1019 nodeIndex,
1020 kTfLiteBuiltinMinimum);
Ryan OShea59f8f652023-05-11 20:37:53 +01001021 case kTfLiteBuiltinMirrorPad:
1022 return VisitPadOperator(delegateData,
1023 tfLiteContext,
1024 tfLiteNode,
1025 nodeIndex,
1026 kTfLiteBuiltinMirrorPad);
David Monahan6c53f9f2023-04-27 15:21:19 +01001027 case kTfLiteBuiltinMul:
1028 return VisitElementwiseBinaryOperator(delegateData,
1029 tfLiteContext,
1030 tfLiteNode,
1031 nodeIndex,
1032 kTfLiteBuiltinMul);
Teresa Charlinf69ae562023-04-27 14:42:23 +01001033 case kTfLiteBuiltinNeg:
1034 return VisitElementwiseUnaryOperator(delegateData,
1035 tfLiteContext,
1036 tfLiteNode,
1037 nodeIndex,
1038 kTfLiteBuiltinNeg,
1039 armnn::UnaryOperation::Neg);
Matthew Sloyan2b04ec32023-04-26 11:42:46 +01001040 case kTfLiteBuiltinNotEqual:
1041 return VisitComparisonOperator(delegateData,
1042 tfLiteContext,
1043 tfLiteNode,
1044 nodeIndex,
Teresa Charlinf69ae562023-04-27 14:42:23 +01001045 kTfLiteBuiltinNotEqual,
1046 armnn::ComparisonOperation::NotEqual);
Teresa Charlinecebb0f2023-04-27 21:37:56 +01001047 case kTfLiteBuiltinPack:
1048 return VisitPackOperator(delegateData,
1049 tfLiteContext,
1050 tfLiteNode,
1051 nodeIndex,
1052 kTfLiteBuiltinPack);
1053 case kTfLiteBuiltinPad:
1054 return VisitPadOperator(delegateData,
1055 tfLiteContext,
1056 tfLiteNode,
1057 nodeIndex,
1058 kTfLiteBuiltinPad);
1059 case kTfLiteBuiltinPadv2:
1060 return VisitPadOperator(delegateData,
1061 tfLiteContext,
1062 tfLiteNode,
1063 nodeIndex,
1064 kTfLiteBuiltinPadv2);
John Mcloughlin0ec00872023-05-15 17:03:49 +01001065 case kTfLiteBuiltinPow:
1066 return VisitElementwiseBinaryOperator(delegateData,
1067 tfLiteContext,
1068 tfLiteNode,
1069 nodeIndex,
1070 kTfLiteBuiltinPow);
Matthew Sloyan0bd4c622023-04-27 11:48:26 +01001071 case kTfLiteBuiltinPrelu:
1072 return VisitPreluOperator(delegateData,
1073 tfLiteContext,
1074 tfLiteNode,
1075 nodeIndex,
1076 kTfLiteBuiltinPrelu);
Francis Murtagh36d94ef2023-04-28 14:05:43 +01001077 case kTfLiteBuiltinQuantize:
1078 return VisitQuantizeOperator(delegateData,
1079 tfLiteContext,
1080 tfLiteNode,
1081 nodeIndex,
1082 kTfLiteBuiltinQuantize);
John Mcloughlin083586d2023-04-28 18:36:52 +01001083 case kTfLiteBuiltinReduceMax:
1084 return VisitReduceOperator(delegateData,
1085 tfLiteContext,
1086 tfLiteNode,
1087 nodeIndex,
1088 kTfLiteBuiltinReduceMax);
1089 case kTfLiteBuiltinReduceMin:
1090 return VisitReduceOperator(delegateData,
1091 tfLiteContext,
1092 tfLiteNode,
1093 nodeIndex,
1094 kTfLiteBuiltinReduceMin);
1095 case kTfLiteBuiltinReduceProd:
1096 return VisitReduceOperator(delegateData,
1097 tfLiteContext,
1098 tfLiteNode,
1099 nodeIndex,
1100 kTfLiteBuiltinReduceProd);
Matthew Sloyan0bd4c622023-04-27 11:48:26 +01001101 case kTfLiteBuiltinRelu:
1102 return VisitActivationOperator(delegateData,
1103 tfLiteContext,
1104 tfLiteNode,
1105 nodeIndex,
1106 kTfLiteBuiltinRelu);
1107 case kTfLiteBuiltinReluN1To1:
1108 return VisitActivationOperator(delegateData,
1109 tfLiteContext,
1110 tfLiteNode,
1111 nodeIndex,
1112 kTfLiteBuiltinReluN1To1);
1113 case kTfLiteBuiltinRelu6:
1114 return VisitActivationOperator(delegateData,
1115 tfLiteContext,
1116 tfLiteNode,
1117 nodeIndex,
1118 kTfLiteBuiltinRelu6);
Matthew Sloyanc49aacc2023-04-28 17:27:26 +01001119 case kTfLiteBuiltinReshape:
1120 return VisitReshapeOperator(delegateData,
1121 tfLiteContext,
1122 tfLiteNode,
1123 nodeIndex,
1124 kTfLiteBuiltinReshape);
John Mcloughlin083586d2023-04-28 18:36:52 +01001125 case kTfLiteBuiltinResizeNearestNeighbor:
1126 return VisitResizeOperator(delegateData,
1127 tfLiteContext,
1128 tfLiteNode,
1129 nodeIndex,
1130 kTfLiteBuiltinResizeNearestNeighbor);
1131 case kTfLiteBuiltinResizeBilinear:
1132 return VisitResizeOperator(delegateData,
1133 tfLiteContext,
1134 tfLiteNode,
1135 nodeIndex,
1136 kTfLiteBuiltinResizeBilinear);
Tracy Narine7306bbe2023-07-17 16:06:26 +01001137 case kTfLiteBuiltinReverseV2:
1138 return VisitReverseV2Operator(delegateData,
1139 tfLiteContext,
1140 tfLiteNode,
1141 nodeIndex,
1142 kTfLiteBuiltinReverseV2);
Teresa Charlinf69ae562023-04-27 14:42:23 +01001143 case kTfLiteBuiltinRsqrt:
1144 return VisitElementwiseUnaryOperator(delegateData,
1145 tfLiteContext,
1146 tfLiteNode,
1147 nodeIndex,
1148 kTfLiteBuiltinRsqrt,
1149 armnn::UnaryOperation::Rsqrt);
John Mcloughlin0422cf22023-04-27 16:55:00 +01001150 case kTfLiteBuiltinShape:
1151 return VisitShapeOperator(delegateData,
1152 tfLiteContext,
1153 tfLiteNode,
1154 nodeIndex,
1155 kTfLiteBuiltinShape);
Teresa Charlinf69ae562023-04-27 14:42:23 +01001156 case kTfLiteBuiltinSin:
1157 return VisitElementwiseUnaryOperator(delegateData,
1158 tfLiteContext,
1159 tfLiteNode,
1160 nodeIndex,
1161 kTfLiteBuiltinSin,
1162 armnn::UnaryOperation::Sin);
Teresa Charlin86b03572023-04-28 13:19:12 +01001163 case kTfLiteBuiltinSlice:
1164 return VisitSliceOperator(delegateData,
1165 tfLiteContext,
1166 tfLiteNode,
1167 nodeIndex,
1168 kTfLiteBuiltinSlice);
Teresa Charlin42362962023-04-28 14:23:33 +01001169 case kTfLiteBuiltinSoftmax:
1170 return VisitSoftmaxOperator(delegateData,
1171 tfLiteContext,
1172 tfLiteNode,
1173 nodeIndex,
1174 kTfLiteBuiltinSoftmax);
Kevin May81b66f32023-04-26 14:55:36 +01001175 case kTfLiteBuiltinSpaceToBatchNd:
1176 return VisitSpaceToBatchNdOperator(delegateData,
1177 tfLiteContext,
1178 tfLiteNode,
1179 nodeIndex,
1180 kTfLiteBuiltinSpaceToBatchNd);
Teresa Charlin42362962023-04-28 14:23:33 +01001181 case kTfLiteBuiltinSpaceToDepth:
1182 return VisitSpaceToDepthOperator(delegateData,
1183 tfLiteContext,
1184 tfLiteNode,
1185 nodeIndex,
1186 kTfLiteBuiltinSpaceToDepth);
David Monahanc833cef2023-05-03 15:53:03 +01001187 case kTfLiteBuiltinSplit:
1188 return VisitSplitOperator(delegateData,
1189 tfLiteContext,
1190 tfLiteNode,
1191 nodeIndex,
1192 kTfLiteBuiltinSplit);
1193 case kTfLiteBuiltinSplitV:
1194 return VisitSplitVOperator(delegateData,
1195 tfLiteContext,
1196 tfLiteNode,
1197 nodeIndex,
1198 kTfLiteBuiltinSplitV);
John Mcloughlin0ec00872023-05-15 17:03:49 +01001199 case kTfLiteBuiltinSquaredDifference:
1200 return VisitElementwiseBinaryOperator(delegateData,
1201 tfLiteContext,
1202 tfLiteNode,
1203 nodeIndex,
1204 kTfLiteBuiltinSquaredDifference);
David Monahan6c53f9f2023-04-27 15:21:19 +01001205 case kTfLiteBuiltinSub:
1206 return VisitElementwiseBinaryOperator(delegateData,
1207 tfLiteContext,
1208 tfLiteNode,
1209 nodeIndex,
1210 kTfLiteBuiltinSub);
Teresa Charlinf69ae562023-04-27 14:42:23 +01001211 case kTfLiteBuiltinSqrt:
1212 return VisitElementwiseUnaryOperator(delegateData,
1213 tfLiteContext,
1214 tfLiteNode,
1215 nodeIndex,
1216 kTfLiteBuiltinSqrt,
1217 armnn::UnaryOperation::Sqrt);
Matthew Sloyan3504e422023-05-03 13:53:02 +01001218 case kTfLiteBuiltinSqueeze:
1219 return VisitSqueezeOperator(delegateData,
1220 tfLiteContext,
1221 tfLiteNode,
1222 nodeIndex,
1223 kTfLiteBuiltinSqueeze);
Teresa Charlin86b03572023-04-28 13:19:12 +01001224 case kTfLiteBuiltinStridedSlice:
1225 return VisitStridedSliceOperator(delegateData,
1226 tfLiteContext,
1227 tfLiteNode,
1228 nodeIndex,
1229 kTfLiteBuiltinStridedSlice);
John Mcloughlin083586d2023-04-28 18:36:52 +01001230 case kTfLiteBuiltinSum:
1231 return VisitReduceOperator(delegateData,
1232 tfLiteContext,
1233 tfLiteNode,
1234 nodeIndex,
1235 kTfLiteBuiltinSum);
Matthew Sloyan0bd4c622023-04-27 11:48:26 +01001236 case kTfLiteBuiltinTanh:
1237 return VisitActivationOperator(delegateData,
1238 tfLiteContext,
1239 tfLiteNode,
1240 nodeIndex,
1241 kTfLiteBuiltinTanh);
Tianle Cheng92ce35c2023-07-25 16:41:00 +01001242 case kTfLiteBuiltinTile:
1243 return VisitTileOperator(delegateData,
1244 tfLiteContext,
1245 tfLiteNode,
1246 nodeIndex,
1247 kTfLiteBuiltinTile);
Teresa Charlin42362962023-04-28 14:23:33 +01001248 case kTfLiteBuiltinTranspose:
1249 return VisitTransposeOperator(delegateData,
Tianle Cheng92ce35c2023-07-25 16:41:00 +01001250 tfLiteContext,
1251 tfLiteNode,
1252 nodeIndex,
1253 kTfLiteBuiltinTranspose);
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +01001254 case kTfLiteBuiltinTransposeConv:
1255 return VisitConvolutionOperator(delegateData,
1256 tfLiteContext,
1257 tfLiteNode,
1258 nodeIndex,
1259 kTfLiteBuiltinTransposeConv);
Matthew Sloyan74be13e2023-05-03 17:34:00 +01001260 case kTfLiteBuiltinUnidirectionalSequenceLstm:
1261 return VisitUnidirectionalSequenceLstmOperator(delegateData,
1262 tfLiteContext,
1263 tfLiteNode,
1264 nodeIndex,
1265 kTfLiteBuiltinUnidirectionalSequenceLstm);
Teresa Charlinecebb0f2023-04-27 21:37:56 +01001266 case kTfLiteBuiltinUnpack:
1267 return VisitUnpackOperator(delegateData,
1268 tfLiteContext,
1269 tfLiteNode,
1270 nodeIndex,
1271 kTfLiteBuiltinUnpack);
Ryan OSheaac9607f2023-04-03 11:33:33 +01001272 default:
1273 return kTfLiteError;
1274 }
1275}
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +01001276} // armnnOpaqueDelegate namespace