Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <armnn_delegate.hpp> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 7 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 8 | #include "Version.hpp" |
| 9 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 10 | #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" |
Teresa Charlin | d5c0ed2 | 2022-04-25 18:23:41 +0100 | [diff] [blame] | 21 | #include "GatherNd.hpp" |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 22 | #include "LogicalBinary.hpp" |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 23 | #include "Lstm.hpp" |
| 24 | #include "Normalization.hpp" |
Matthew Sloyan | a7a12f5 | 2021-05-06 10:05:28 +0100 | [diff] [blame] | 25 | #include "Pack.hpp" |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 26 | #include "Pad.hpp" |
| 27 | #include "Pooling.hpp" |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 28 | #include "Prelu.hpp" |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 29 | #include "Quantization.hpp" |
| 30 | #include "Redefine.hpp" |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 31 | #include "Reduce.hpp" |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 32 | #include "Resize.hpp" |
| 33 | #include "Round.hpp" |
Keith Davis | 0176fd8 | 2021-06-01 17:36:32 +0100 | [diff] [blame] | 34 | #include "Shape.hpp" |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 35 | #include "Slice.hpp" |
| 36 | #include "Softmax.hpp" |
| 37 | #include "SpaceDepth.hpp" |
Sadik Armagan | 34fa1bd | 2020-11-27 12:40:52 +0000 | [diff] [blame] | 38 | #include "Split.hpp" |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 39 | #include "Transpose.hpp" |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 40 | #include "UnidirectionalSequenceLstm.hpp" |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 41 | #include "Unpack.hpp" |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 42 | |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 43 | #include <armnnUtils/Filesystem.hpp> |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 44 | #include <armnn/utility/Timer.hpp> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 45 | #include <flatbuffers/flatbuffers.h> |
| 46 | #include <tensorflow/lite/context_util.h> |
Jim Flynn | 4b2f347 | 2021-10-13 21:20:07 +0100 | [diff] [blame] | 47 | #include <tensorflow/lite/schema/schema_generated.h> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 48 | |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 49 | #include <algorithm> |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 50 | #include <iostream> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 51 | #include <sstream> |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 52 | |
| 53 | namespace armnnDelegate |
| 54 | { |
| 55 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 56 | DelegateOptions TfLiteArmnnDelegateOptionsDefault() |
| 57 | { |
| 58 | DelegateOptions options(armnn::Compute::CpuRef); |
| 59 | return options; |
| 60 | } |
| 61 | |
| 62 | TfLiteDelegate* TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options) |
| 63 | { |
| 64 | auto* armnnDelegate = new ::armnnDelegate::Delegate(options); |
| 65 | return armnnDelegate->GetDelegate(); |
| 66 | } |
| 67 | |
| 68 | void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate) |
| 69 | { |
| 70 | if (tfLiteDelegate != nullptr) |
| 71 | { |
| 72 | delete static_cast<::armnnDelegate::Delegate*>(tfLiteDelegate->data_); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | TfLiteStatus DoPrepare(TfLiteContext* tfLiteContext, TfLiteDelegate* tfLiteDelegate) |
| 77 | { |
| 78 | TfLiteIntArray* supportedOperators = |
| 79 | static_cast<::armnnDelegate::Delegate*>(tfLiteDelegate->data_)->IdentifyOperatorsToDelegate(tfLiteContext); |
| 80 | |
| 81 | // ArmNN Delegate Registration |
| 82 | static const TfLiteRegistration kArmnnSubgraphRegistration = { |
| 83 | // ArmnnSubgraph Init |
| 84 | .init = [](TfLiteContext* tfLiteContext, const char* buffer, size_t length) -> void* { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 85 | armnn::IgnoreUnused(length); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 86 | const TfLiteDelegateParams* parameters = reinterpret_cast<const TfLiteDelegateParams*>(buffer); |
| 87 | |
| 88 | return static_cast<void*>(ArmnnSubgraph::Create( |
| 89 | tfLiteContext, parameters, static_cast<::armnnDelegate::Delegate*>(parameters->delegate->data_))); |
| 90 | }, |
| 91 | // ArmnnSubgraph Free |
| 92 | .free = [](TfLiteContext* tfLiteContext, void* buffer) -> void { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 93 | armnn::IgnoreUnused(tfLiteContext); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 94 | if (buffer != nullptr) |
| 95 | { |
| 96 | delete static_cast<ArmnnSubgraph*>(buffer); |
| 97 | } |
| 98 | }, |
| 99 | // ArmnnSubgraph Prepare |
| 100 | .prepare = [](TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode) -> TfLiteStatus { |
| 101 | if (tfLiteNode->user_data == nullptr) |
| 102 | { |
| 103 | return kTfLiteError; |
| 104 | } |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 105 | return static_cast<ArmnnSubgraph*>(tfLiteNode->user_data)->Prepare(tfLiteContext); |
| 106 | }, |
| 107 | // ArmnnSubgraph Invoke |
| 108 | .invoke = [](TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode) -> TfLiteStatus { |
| 109 | if (tfLiteNode->user_data == nullptr) |
| 110 | { |
| 111 | return kTfLiteError; |
| 112 | } |
| 113 | |
| 114 | return static_cast<ArmnnSubgraph*>(tfLiteNode->user_data)->Invoke(tfLiteContext, tfLiteNode); |
| 115 | }, |
| 116 | |
| 117 | .profiling_string = nullptr, |
| 118 | .builtin_code = kTfLiteBuiltinDelegate, |
| 119 | .custom_name = "TfLiteArmNnDelegate", |
| 120 | .version = 1, |
| 121 | }; |
| 122 | |
| 123 | const TfLiteStatus status = |
| 124 | tfLiteContext->ReplaceNodeSubsetsWithDelegateKernels( |
| 125 | tfLiteContext, kArmnnSubgraphRegistration, supportedOperators, tfLiteDelegate); |
| 126 | |
| 127 | TfLiteIntArrayFree(supportedOperators); |
| 128 | return status; |
| 129 | |
| 130 | } |
| 131 | |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 132 | Delegate::Delegate(armnnDelegate::DelegateOptions options) |
| 133 | : m_Runtime(nullptr, nullptr), |
| 134 | m_Options(std::move(options)) |
| 135 | { |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 136 | // Configures logging for ARMNN |
| 137 | if (options.IsLoggingEnabled()) |
| 138 | { |
| 139 | armnn::ConfigureLogging(true, true, options.GetLoggingSeverity()); |
| 140 | } |
| 141 | |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 142 | // Create ArmNN Runtime |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 143 | m_Runtime = armnn::IRuntime::Create(options.GetRuntimeOptions()); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 144 | |
| 145 | std::vector<armnn::BackendId> backends; |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 146 | if (m_Runtime) |
| 147 | { |
| 148 | const armnn::BackendIdSet supportedDevices = m_Runtime->GetDeviceSpec().GetSupportedBackends(); |
| 149 | for (auto& backend : m_Options.GetBackends()) |
| 150 | { |
| 151 | if (std::find(supportedDevices.cbegin(), supportedDevices.cend(), backend) == supportedDevices.cend()) |
| 152 | { |
Sadik Armagan | 0534e03 | 2020-10-27 17:30:18 +0000 | [diff] [blame] | 153 | TFLITE_LOG_PROD(tflite::TFLITE_LOG_INFO, |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 154 | "TfLiteArmnnDelegate: Requested unknown backend %s", backend.Get().c_str()); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | backends.push_back(backend); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (backends.empty()) |
| 164 | { |
| 165 | // No known backend specified |
| 166 | throw armnn::InvalidArgumentException("TfLiteArmnnDelegate: No known backend specified."); |
| 167 | } |
| 168 | m_Options.SetBackends(backends); |
| 169 | |
| 170 | TFLITE_LOG_PROD_ONCE(tflite::TFLITE_LOG_INFO, "TfLiteArmnnDelegate: Created TfLite ArmNN delegate."); |
| 171 | } |
| 172 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 173 | TfLiteIntArray* Delegate::IdentifyOperatorsToDelegate(TfLiteContext* tfLiteContext) |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 174 | { |
| 175 | TfLiteIntArray* executionPlan = nullptr; |
| 176 | if (tfLiteContext->GetExecutionPlan(tfLiteContext, &executionPlan) != kTfLiteOk) |
| 177 | { |
| 178 | TF_LITE_KERNEL_LOG(tfLiteContext, "TfLiteArmnnDelegate: Unable to get graph execution plan."); |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 182 | // Delegate data with null network |
| 183 | DelegateData delegateData(m_Options.GetBackends()); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 184 | |
| 185 | TfLiteIntArray* nodesToDelegate = TfLiteIntArrayCreate(executionPlan->size); |
| 186 | nodesToDelegate->size = 0; |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 187 | |
| 188 | std::set<int32_t> unsupportedOperators; |
| 189 | |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 190 | for (int i = 0; i < executionPlan->size; ++i) |
| 191 | { |
| 192 | const int nodeIndex = executionPlan->data[i]; |
| 193 | |
| 194 | // If TfLite nodes can be delegated to ArmNN |
| 195 | TfLiteNode* tfLiteNode = nullptr; |
| 196 | TfLiteRegistration* tfLiteRegistration = nullptr; |
| 197 | if (tfLiteContext->GetNodeAndRegistration( |
| 198 | tfLiteContext, nodeIndex, &tfLiteNode, &tfLiteRegistration) != kTfLiteOk) |
| 199 | { |
| 200 | TF_LITE_KERNEL_LOG(tfLiteContext, |
| 201 | "TfLiteArmnnDelegate: Unable to get node and registration for node %d.", |
| 202 | nodeIndex); |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | if (ArmnnSubgraph::VisitNode( |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 207 | delegateData, tfLiteContext, tfLiteRegistration, tfLiteNode, nodeIndex) != kTfLiteOk) |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 208 | { |
| 209 | // node is not supported by ArmNN |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 210 | unsupportedOperators.insert(tfLiteRegistration->builtin_code); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 211 | continue; |
| 212 | } |
| 213 | |
| 214 | nodesToDelegate->data[nodesToDelegate->size++] = nodeIndex; |
| 215 | } |
| 216 | |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 217 | for (std::set<int32_t>::iterator it=unsupportedOperators.begin(); it!=unsupportedOperators.end(); ++it) |
| 218 | { |
| 219 | TF_LITE_KERNEL_LOG(tfLiteContext, |
| 220 | "Operator %s [%d] is not supported by armnn_delegate.", |
| 221 | tflite::EnumNameBuiltinOperator(tflite::BuiltinOperator(*it)), |
| 222 | *it); |
| 223 | } |
| 224 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 225 | std::sort(&nodesToDelegate->data[0], &nodesToDelegate->data[nodesToDelegate->size]); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 226 | return nodesToDelegate; |
| 227 | } |
| 228 | |
| 229 | TfLiteDelegate* Delegate::GetDelegate() |
| 230 | { |
| 231 | return &m_Delegate; |
| 232 | } |
| 233 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 234 | const std::string Delegate::GetVersion() |
| 235 | { |
| 236 | return DELEGATE_VERSION; |
| 237 | } |
| 238 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 239 | TfLiteStatus ArmnnSubgraph::AddInputLayer(DelegateData& delegateData, |
| 240 | TfLiteContext* tfLiteContext, |
| 241 | const TfLiteIntArray* inputs, |
| 242 | std::vector<armnn::BindingPointInfo>& inputBindings) |
| 243 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 244 | const size_t numInputs = static_cast<size_t>(inputs->size); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 245 | for (unsigned int i = 0; i < numInputs; ++i) |
| 246 | { |
| 247 | const int32_t tensorId = inputs->data[i]; |
| 248 | const TfLiteTensor tensor = tfLiteContext->tensors[tensorId]; |
Sadik Armagan | 6e36a64 | 2020-11-10 21:18:41 +0000 | [diff] [blame] | 249 | // Do not create bindings for constant inputs |
| 250 | if (tensor.allocation_type == kTfLiteMmapRo) |
| 251 | { |
| 252 | continue; |
| 253 | } |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 254 | |
| 255 | auto bindingId = static_cast<armnn::LayerBindingId>((tensorId)); |
| 256 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddInputLayer(bindingId); |
| 257 | |
| 258 | auto tensorInfo = GetTensorInfoForTfLiteTensor(tensor); |
| 259 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 260 | outputSlot.SetTensorInfo(tensorInfo); |
| 261 | |
| 262 | // Store for creating connections |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 263 | delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)] = &outputSlot; |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 264 | |
Sadik Armagan | 6e36a64 | 2020-11-10 21:18:41 +0000 | [diff] [blame] | 265 | inputBindings.push_back(std::make_pair(bindingId, tensorInfo)); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 266 | } |
Sadik Armagan | 6e36a64 | 2020-11-10 21:18:41 +0000 | [diff] [blame] | 267 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 268 | return kTfLiteOk; |
| 269 | } |
| 270 | |
| 271 | TfLiteStatus ArmnnSubgraph::AddOutputLayer(DelegateData& delegateData, |
| 272 | TfLiteContext* tfLiteContext, |
| 273 | const TfLiteIntArray* outputs, |
| 274 | std::vector<armnn::BindingPointInfo>& outputBindings) |
| 275 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 276 | const size_t numOutputs = static_cast<size_t>(outputs->size); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 277 | for (unsigned int i = 0; i < numOutputs; ++i) |
| 278 | { |
| 279 | const int32_t tensorId = outputs->data[i]; |
| 280 | const TfLiteTensor tensor = tfLiteContext->tensors[tensorId]; |
| 281 | |
| 282 | auto bindingId = static_cast<armnn::LayerBindingId>((tensorId)); |
| 283 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddOutputLayer(bindingId); |
| 284 | |
| 285 | auto tensorInfo = GetTensorInfoForTfLiteTensor(tensor); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 286 | ARMNN_ASSERT(delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)] != nullptr); |
| 287 | delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tensorId)]->Connect(layer->GetInputSlot(0)); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 288 | outputBindings.push_back(std::make_pair(bindingId, tensorInfo)); |
| 289 | } |
| 290 | |
| 291 | return kTfLiteOk; |
| 292 | } |
| 293 | |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 294 | ArmnnSubgraph* ArmnnSubgraph::Create(TfLiteContext* tfLiteContext, |
| 295 | const TfLiteDelegateParams* parameters, |
| 296 | const Delegate* delegate) |
| 297 | { |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 298 | const auto startTime = armnn::GetTimeNow(); |
| 299 | ARMNN_LOG(info) << "ArmnnSubgraph creation"; |
| 300 | |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 301 | TfLiteIntArray* executionPlan; |
| 302 | if (tfLiteContext->GetExecutionPlan(tfLiteContext, &executionPlan) != kTfLiteOk) |
| 303 | { |
| 304 | return nullptr; |
| 305 | } |
| 306 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 307 | // Initialize DelegateData holds network and output slots information |
| 308 | DelegateData delegateData(delegate->m_Options.GetBackends()); |
| 309 | |
| 310 | // Build ArmNN Network |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 311 | armnn::NetworkOptions networkOptions = delegate->m_Options.GetOptimizerOptions().m_ModelOptions; |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 312 | armnn::NetworkId networkId; |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 313 | delegateData.m_Network = armnn::INetwork::Create(networkOptions); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 314 | |
Sadik Armagan | 6e36a64 | 2020-11-10 21:18:41 +0000 | [diff] [blame] | 315 | delegateData.m_OutputSlotForNode = std::vector<armnn::IOutputSlot*>(tfLiteContext->tensors_size, nullptr); |
| 316 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 317 | std::vector<armnn::BindingPointInfo> inputBindings; |
| 318 | std::vector<armnn::BindingPointInfo> outputBindings; |
| 319 | |
| 320 | // Add input layer |
| 321 | auto status = AddInputLayer(delegateData, tfLiteContext, parameters->input_tensors, inputBindings); |
| 322 | if (status != kTfLiteOk) |
| 323 | { |
| 324 | throw armnn::Exception("TfLiteArmnnDelegate: Unable to add Inputs to the network!"); |
| 325 | } |
| 326 | |
| 327 | // Parse TfLite delegate nodes to ArmNN |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 328 | const auto parseStartTime = armnn::GetTimeNow(); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 329 | for (int i = 0; i < parameters->nodes_to_replace->size; ++i) |
| 330 | { |
| 331 | const int nodeIndex = parameters->nodes_to_replace->data[i]; |
| 332 | |
| 333 | TfLiteNode* tfLiteNode = nullptr; |
| 334 | TfLiteRegistration* tfLiteRegistration = nullptr; |
| 335 | if (tfLiteContext->GetNodeAndRegistration( |
| 336 | tfLiteContext, nodeIndex, &tfLiteNode, &tfLiteRegistration) != kTfLiteOk) |
| 337 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 338 | throw armnn::Exception(&"TfLiteArmnnDelegate: Unable to get node registration: " [ nodeIndex]); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 341 | if (VisitNode(delegateData, tfLiteContext, tfLiteRegistration, tfLiteNode, nodeIndex) != kTfLiteOk) |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 342 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 343 | throw armnn::Exception(&"TfLiteArmnnDelegate: Unable to parse node: " [ nodeIndex]); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 344 | } |
| 345 | } |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 346 | ARMNN_LOG(info) << "Parse nodes to ArmNN time: " << std::setprecision(2) |
| 347 | << std::fixed << armnn::GetTimeDuration(parseStartTime).count() << " ms"; |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 348 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 349 | // Add Output layer |
| 350 | status = AddOutputLayer(delegateData, tfLiteContext, parameters->output_tensors, outputBindings); |
| 351 | if (status != kTfLiteOk) |
| 352 | { |
| 353 | throw armnn::Exception("TfLiteArmnnDelegate: Unable to add Outputs to the network!"); |
| 354 | } |
| 355 | |
| 356 | // Optimize ArmNN network |
| 357 | armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr); |
| 358 | try |
| 359 | { |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 360 | const auto optimizeStartTime = armnn::GetTimeNow(); |
Sadik Armagan | 6e36a64 | 2020-11-10 21:18:41 +0000 | [diff] [blame] | 361 | optNet = armnn::Optimize(*(delegateData.m_Network.get()), |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 362 | delegate->m_Options.GetBackends(), |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 363 | delegate->m_Runtime->GetDeviceSpec(), |
| 364 | delegate->m_Options.GetOptimizerOptions()); |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 365 | ARMNN_LOG(info) << "Optimize ArmnnSubgraph time: " << std::setprecision(2) |
| 366 | << std::fixed << armnn::GetTimeDuration(optimizeStartTime).count() << " ms"; |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 367 | } |
| 368 | catch (std::exception &ex) |
| 369 | { |
| 370 | std::stringstream exMessage; |
| 371 | exMessage << "TfLiteArmnnDelegate: Exception (" << ex.what() << ") caught from optimize."; |
| 372 | throw armnn::Exception(exMessage.str()); |
| 373 | } |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 374 | if (!optNet) |
| 375 | { |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 376 | // Optimize failed |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 377 | throw armnn::Exception("TfLiteArmnnDelegate: Unable to optimize the network!"); |
| 378 | } |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 379 | |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 380 | // If set, we will serialize the optimized model into a dot file. |
| 381 | const std::string serializeToDotFile = delegate->m_Options.GetSerializeToDot(); |
| 382 | if (!serializeToDotFile.empty()) |
| 383 | { |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 384 | ARMNN_LOG(info) << "Writing graph to dot file: " << serializeToDotFile; |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 385 | fs::path filename = serializeToDotFile; |
| 386 | std::fstream file(filename.c_str(), std::ios_base::out); |
| 387 | optNet->SerializeToDot(file); |
| 388 | } |
| 389 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 390 | try |
| 391 | { |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 392 | const auto loadStartTime = armnn::GetTimeNow(); |
| 393 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 394 | // Load graph into runtime |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 395 | std::string errorMessage; |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 396 | armnn::Status loadingStatus; |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 397 | armnn::MemorySource inputSource = armnn::MemorySource::Undefined; |
| 398 | armnn::MemorySource outputSource = armnn::MemorySource::Undefined; |
| 399 | // There's a bit of an assumption here that the delegate will only support Malloc memory source. |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 400 | if (delegate->m_Options.GetOptimizerOptions().m_ImportEnabled) |
| 401 | { |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 402 | inputSource = armnn::MemorySource::Malloc; |
| 403 | } |
| 404 | if (delegate->m_Options.GetOptimizerOptions().m_ExportEnabled) |
| 405 | { |
| 406 | outputSource = armnn::MemorySource::Malloc; |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 407 | } |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 408 | armnn::INetworkProperties networkProperties(false, |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 409 | inputSource, |
| 410 | outputSource, |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 411 | delegate->m_Options.GetInternalProfilingState(), |
| 412 | delegate->m_Options.GetInternalProfilingDetail()); |
| 413 | loadingStatus = delegate->m_Runtime->LoadNetwork(networkId, |
| 414 | std::move(optNet), |
| 415 | errorMessage, |
| 416 | networkProperties); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 417 | if (loadingStatus != armnn::Status::Success) |
| 418 | { |
Colm Donelan | 4514228 | 2021-10-21 23:39:52 +0100 | [diff] [blame] | 419 | // Network load failed. |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 420 | throw armnn::Exception("TfLiteArmnnDelegate: Network could not be loaded:" + errorMessage); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 421 | } |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 422 | |
| 423 | ARMNN_LOG(info) << "Load ArmnnSubgraph time: " << std::setprecision(2) |
| 424 | << std::fixed << armnn::GetTimeDuration(loadStartTime).count() << " ms"; |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 425 | } |
| 426 | catch (std::exception& ex) |
| 427 | { |
| 428 | std::stringstream exMessage; |
| 429 | exMessage << "TfLiteArmnnDelegate: Exception (" << ex.what() << ") caught from LoadNetwork."; |
| 430 | throw armnn::Exception(exMessage.str()); |
| 431 | } |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 432 | |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 433 | // Register debug callback function |
| 434 | if (delegate->m_Options.GetDebugCallbackFunction().has_value()) |
| 435 | { |
| 436 | delegate->m_Runtime->RegisterDebugCallback(networkId, delegate->m_Options.GetDebugCallbackFunction().value()); |
| 437 | } |
| 438 | |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 439 | ARMNN_LOG(info) << "Overall ArmnnSubgraph creation time: " << std::setprecision(2) |
| 440 | << std::fixed << armnn::GetTimeDuration(startTime).count() << " ms\n"; |
| 441 | |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 442 | // Create a new SubGraph with networkId and runtime |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 443 | return new ArmnnSubgraph(networkId, delegate->m_Runtime.get(), inputBindings, outputBindings); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | TfLiteStatus ArmnnSubgraph::Prepare(TfLiteContext* tfLiteContext) |
| 447 | { |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 448 | armnn::IgnoreUnused(tfLiteContext); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 449 | return kTfLiteOk; |
| 450 | } |
| 451 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 452 | TfLiteStatus ArmnnSubgraph::Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode) |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 453 | { |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 454 | // Prepare inputs |
| 455 | armnn::InputTensors inputTensors; |
| 456 | size_t inputIndex = 0; |
| 457 | for (auto inputIdx : tflite::TfLiteIntArrayView(tfLiteNode->inputs)) |
| 458 | { |
| 459 | TfLiteTensor* tensor = &tfLiteContext->tensors[inputIdx]; |
| 460 | if (tensor->allocation_type != kTfLiteMmapRo) |
| 461 | { |
| 462 | const armnn::BindingPointInfo& inputBinding = m_InputBindings[inputIndex]; |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 463 | armnn::TensorInfo inputTensorInfo = inputBinding.second; |
| 464 | inputTensorInfo.SetConstant(true); |
| 465 | const armnn::ConstTensor inputTensor(inputTensorInfo, tensor->data.data); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 466 | inputTensors.emplace_back(inputIdx, inputTensor); |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 467 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 468 | ++inputIndex; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Prepare outputs |
| 473 | armnn::OutputTensors outputTensors; |
| 474 | size_t outputIndex = 0; |
| 475 | for (auto outputIdx : tflite::TfLiteIntArrayView(tfLiteNode->outputs)) |
| 476 | { |
| 477 | const armnn::BindingPointInfo& outputBinding = m_OutputBindings[outputIndex]; |
| 478 | TfLiteTensor* tensor = &tfLiteContext->tensors[outputIdx]; |
| 479 | const armnn::Tensor outputTensor(outputBinding.second, tensor->data.data); |
| 480 | outputTensors.emplace_back(outputIdx, outputTensor); |
| 481 | |
| 482 | ++outputIndex; |
| 483 | } |
| 484 | |
| 485 | // Run graph |
| 486 | auto status = m_Runtime->EnqueueWorkload(m_NetworkId, inputTensors, outputTensors); |
Colm Donelan | 4514228 | 2021-10-21 23:39:52 +0100 | [diff] [blame] | 487 | // The delegate holds its own Arm NN runtime so this is our last chance to print internal profiling data. |
| 488 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkId); |
| 489 | if (profiler && profiler->IsProfilingEnabled()) |
| 490 | { |
| 491 | profiler->Print(std::cout); |
| 492 | } |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 493 | return (status == armnn::Status::Success) ? kTfLiteOk : kTfLiteError; |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 496 | TfLiteStatus ArmnnSubgraph::VisitNode(DelegateData& delegateData, |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 497 | TfLiteContext* tfLiteContext, |
| 498 | TfLiteRegistration* tfLiteRegistration, |
| 499 | TfLiteNode* tfLiteNode, |
| 500 | int nodeIndex) |
| 501 | { |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 502 | switch (tfLiteRegistration->builtin_code) |
| 503 | { |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 504 | case kTfLiteBuiltinCustom: |
| 505 | { |
| 506 | #if defined(ARMNN_POST_TFLITE_2_5) |
| 507 | // Custom operators are defined by the name rather than the builtin code. |
| 508 | // Parse the custom_name param in the registration to point to the correct visitor function. |
| 509 | std::string customOperatorName = tfLiteRegistration->custom_name; |
| 510 | if ( customOperatorName == "AveragePool3D" ) |
| 511 | { |
| 512 | return VisitPooling3dOperator(delegateData, |
| 513 | tfLiteContext, |
| 514 | tfLiteNode, |
| 515 | nodeIndex, |
| 516 | customOperatorName); |
| 517 | } |
| 518 | else if (customOperatorName == "MaxPool3D") |
| 519 | { |
| 520 | return VisitPooling3dOperator(delegateData, |
| 521 | tfLiteContext, |
| 522 | tfLiteNode, |
| 523 | nodeIndex, |
| 524 | customOperatorName); |
| 525 | } |
| 526 | #endif |
| 527 | // Invalid or unsupported custom operator |
| 528 | return kTfLiteError; |
| 529 | } |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 530 | case kTfLiteBuiltinAbs: |
| 531 | return VisitElementwiseUnaryOperator(delegateData, |
| 532 | tfLiteContext, |
| 533 | tfLiteNode, |
| 534 | nodeIndex, |
| 535 | armnn::UnaryOperation::Abs); |
| 536 | case kTfLiteBuiltinAdd: |
| 537 | return VisitElementwiseBinaryOperator(delegateData, |
| 538 | tfLiteContext, |
| 539 | tfLiteNode, |
| 540 | nodeIndex, |
| 541 | kTfLiteBuiltinAdd); |
| 542 | case kTfLiteBuiltinArgMax: |
| 543 | return VisitArgMinMaxOperator(delegateData, |
| 544 | tfLiteContext, |
| 545 | tfLiteNode, |
| 546 | nodeIndex, |
| 547 | kTfLiteBuiltinArgMax); |
| 548 | case kTfLiteBuiltinArgMin: |
| 549 | return VisitArgMinMaxOperator(delegateData, |
| 550 | tfLiteContext, |
| 551 | tfLiteNode, |
| 552 | nodeIndex, |
| 553 | kTfLiteBuiltinArgMin); |
| 554 | case kTfLiteBuiltinAveragePool2d: |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 555 | return VisitPooling2dOperator(delegateData, |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 556 | tfLiteContext, |
| 557 | tfLiteNode, |
| 558 | nodeIndex, |
| 559 | kTfLiteBuiltinAveragePool2d); |
| 560 | case kTfLiteBuiltinBatchToSpaceNd: |
| 561 | return VisitBatchToSpaceNdOperator(delegateData, |
| 562 | tfLiteContext, |
| 563 | tfLiteNode, |
| 564 | nodeIndex, |
| 565 | kTfLiteBuiltinBatchToSpaceNd); |
Sadik Armagan | 937565b | 2021-04-21 14:03:28 +0100 | [diff] [blame] | 566 | case kTfLiteBuiltinCast: |
| 567 | return VisitCastOperator(delegateData, |
| 568 | tfLiteContext, |
| 569 | tfLiteNode, |
| 570 | nodeIndex, |
| 571 | kTfLiteBuiltinCast); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 572 | case kTfLiteBuiltinConcatenation: |
| 573 | return VisitControlOperator(delegateData, |
| 574 | tfLiteContext, |
| 575 | tfLiteNode, |
| 576 | nodeIndex, |
| 577 | kTfLiteBuiltinConcatenation); |
| 578 | case kTfLiteBuiltinConv2d: |
| 579 | return VisitConvolutionOperator(delegateData, |
| 580 | tfLiteContext, |
| 581 | tfLiteNode, |
| 582 | nodeIndex, |
| 583 | kTfLiteBuiltinConv2d); |
Matthew Sloyan | 81ec994 | 2021-10-12 10:26:30 +0100 | [diff] [blame] | 584 | // Conv3d is only correctly supported for external delegates from TF Lite v2.6, as there was a breaking bug in v2.5. |
| 585 | #if defined(ARMNN_POST_TFLITE_2_5) |
| 586 | case kTfLiteBuiltinConv3d: |
| 587 | return VisitConvolutionOperator(delegateData, |
| 588 | tfLiteContext, |
| 589 | tfLiteNode, |
| 590 | nodeIndex, |
| 591 | kTfLiteBuiltinConv3d); |
| 592 | #endif |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 593 | case kTfLiteBuiltinDepthToSpace: |
| 594 | return VisitDepthToSpaceOperator(delegateData, |
| 595 | tfLiteContext, |
| 596 | tfLiteNode, |
| 597 | nodeIndex, |
| 598 | kTfLiteBuiltinDepthToSpace); |
| 599 | case kTfLiteBuiltinDepthwiseConv2d: |
| 600 | return VisitConvolutionOperator(delegateData, |
| 601 | tfLiteContext, |
| 602 | tfLiteNode, |
| 603 | nodeIndex, |
| 604 | kTfLiteBuiltinDepthwiseConv2d); |
| 605 | case kTfLiteBuiltinDequantize: |
| 606 | return VisitDequantizeOperator(delegateData, |
| 607 | tfLiteContext, |
| 608 | tfLiteNode, |
| 609 | nodeIndex, |
| 610 | kTfLiteBuiltinDequantize); |
| 611 | case kTfLiteBuiltinDiv: |
| 612 | return VisitElementwiseBinaryOperator(delegateData, |
| 613 | tfLiteContext, |
| 614 | tfLiteNode, |
| 615 | nodeIndex, |
| 616 | kTfLiteBuiltinDiv); |
| 617 | case kTfLiteBuiltinElu: |
| 618 | return VisitActivationOperator(delegateData, |
| 619 | tfLiteContext, |
| 620 | tfLiteNode, |
| 621 | nodeIndex, |
| 622 | kTfLiteBuiltinElu); |
| 623 | case kTfLiteBuiltinEqual: |
| 624 | return VisitComparisonOperator(delegateData, |
| 625 | tfLiteContext, |
| 626 | tfLiteNode, |
| 627 | nodeIndex, |
| 628 | kTfLiteBuiltinEqual); |
| 629 | case kTfLiteBuiltinExp: |
| 630 | return VisitElementwiseUnaryOperator(delegateData, |
| 631 | tfLiteContext, |
| 632 | tfLiteNode, |
| 633 | nodeIndex, |
| 634 | armnn::UnaryOperation::Exp); |
| 635 | case kTfLiteBuiltinExpandDims: |
| 636 | return VisitExpandDimsOperator(delegateData, |
| 637 | tfLiteContext, |
| 638 | tfLiteNode, |
| 639 | nodeIndex, |
| 640 | kTfLiteBuiltinExpandDims); |
| 641 | case kTfLiteBuiltinFill: |
| 642 | return VisitFillOperator(delegateData, |
| 643 | tfLiteContext, |
| 644 | tfLiteNode, |
| 645 | nodeIndex, |
| 646 | kTfLiteBuiltinFill); |
| 647 | case kTfLiteBuiltinFloor: |
| 648 | return VisitFloorOperator(delegateData, |
| 649 | tfLiteContext, |
| 650 | tfLiteNode, |
| 651 | nodeIndex, |
| 652 | kTfLiteBuiltinFloor); |
Jim Flynn | 4b2f347 | 2021-10-13 21:20:07 +0100 | [diff] [blame] | 653 | case kTfLiteBuiltinFloorDiv: |
| 654 | return VisitElementwiseBinaryOperator(delegateData, |
| 655 | tfLiteContext, |
| 656 | tfLiteNode, |
| 657 | nodeIndex, |
| 658 | kTfLiteBuiltinFloorDiv); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 659 | case kTfLiteBuiltinFullyConnected: |
| 660 | return VisitFullyConnectedOperator(delegateData, |
| 661 | tfLiteContext, |
| 662 | tfLiteNode, |
| 663 | nodeIndex, |
| 664 | kTfLiteBuiltinFullyConnected); |
| 665 | case kTfLiteBuiltinGather: |
| 666 | return VisitGatherOperator(delegateData, |
| 667 | tfLiteContext, |
| 668 | tfLiteNode, |
| 669 | nodeIndex, |
| 670 | kTfLiteBuiltinGather); |
Teresa Charlin | d5c0ed2 | 2022-04-25 18:23:41 +0100 | [diff] [blame] | 671 | case kTfLiteBuiltinGatherNd: |
| 672 | return VisitGatherNdOperator(delegateData, |
| 673 | tfLiteContext, |
| 674 | tfLiteNode, |
| 675 | nodeIndex, |
| 676 | kTfLiteBuiltinGatherNd); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 677 | case kTfLiteBuiltinGreater: |
| 678 | return VisitComparisonOperator(delegateData, |
| 679 | tfLiteContext, |
| 680 | tfLiteNode, |
| 681 | nodeIndex, |
| 682 | kTfLiteBuiltinGreater); |
| 683 | case kTfLiteBuiltinGreaterEqual: |
| 684 | return VisitComparisonOperator(delegateData, |
| 685 | tfLiteContext, |
| 686 | tfLiteNode, |
| 687 | nodeIndex, |
| 688 | kTfLiteBuiltinGreaterEqual); |
| 689 | case kTfLiteBuiltinHardSwish: |
| 690 | return VisitActivationOperator(delegateData, |
| 691 | tfLiteContext, |
| 692 | tfLiteNode, |
| 693 | nodeIndex, |
| 694 | kTfLiteBuiltinHardSwish); |
| 695 | case kTfLiteBuiltinL2Normalization: |
Sadik Armagan | 4b227bb | 2021-01-22 10:53:38 +0000 | [diff] [blame] | 696 | return VisitL2NormalizationOperator(delegateData, |
| 697 | tfLiteContext, |
| 698 | tfLiteNode, |
| 699 | nodeIndex, |
| 700 | kTfLiteBuiltinL2Normalization); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 701 | case kTfLiteBuiltinL2Pool2d: |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 702 | return VisitPooling2dOperator(delegateData, |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 703 | tfLiteContext, |
| 704 | tfLiteNode, |
| 705 | nodeIndex, |
| 706 | kTfLiteBuiltinL2Pool2d); |
| 707 | case kTfLiteBuiltinLess: |
| 708 | return VisitComparisonOperator(delegateData, |
| 709 | tfLiteContext, |
| 710 | tfLiteNode, |
| 711 | nodeIndex, |
| 712 | kTfLiteBuiltinLess); |
| 713 | case kTfLiteBuiltinLessEqual: |
| 714 | return VisitComparisonOperator(delegateData, |
| 715 | tfLiteContext, |
| 716 | tfLiteNode, |
| 717 | nodeIndex, |
| 718 | kTfLiteBuiltinLessEqual); |
| 719 | case kTfLiteBuiltinLocalResponseNormalization: |
Sadik Armagan | 4b227bb | 2021-01-22 10:53:38 +0000 | [diff] [blame] | 720 | return VisitLocalResponseNormalizationOperator(delegateData, |
| 721 | tfLiteContext, |
| 722 | tfLiteNode, |
| 723 | nodeIndex, |
| 724 | kTfLiteBuiltinLocalResponseNormalization); |
Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +0000 | [diff] [blame] | 725 | case kTfLiteBuiltinLogicalAnd: |
| 726 | return VisitLogicalBinaryOperator(delegateData, |
| 727 | tfLiteContext, |
| 728 | tfLiteNode, |
| 729 | nodeIndex, |
| 730 | kTfLiteBuiltinLogicalAnd, |
| 731 | armnn::LogicalBinaryOperation::LogicalAnd); |
| 732 | case kTfLiteBuiltinLogicalNot: |
| 733 | return VisitElementwiseUnaryOperator(delegateData, |
| 734 | tfLiteContext, |
| 735 | tfLiteNode, |
| 736 | nodeIndex, |
| 737 | armnn::UnaryOperation::LogicalNot); |
| 738 | case kTfLiteBuiltinLogicalOr: |
| 739 | return VisitLogicalBinaryOperator(delegateData, |
| 740 | tfLiteContext, |
| 741 | tfLiteNode, |
| 742 | nodeIndex, |
| 743 | kTfLiteBuiltinLogicalOr, |
| 744 | armnn::LogicalBinaryOperation::LogicalOr); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 745 | case kTfLiteBuiltinLogistic: |
| 746 | return VisitActivationOperator(delegateData, |
| 747 | tfLiteContext, |
| 748 | tfLiteNode, |
| 749 | nodeIndex, |
| 750 | kTfLiteBuiltinLogistic); |
| 751 | case kTfLiteBuiltinLogSoftmax: |
| 752 | return VisitSoftmaxOperator(delegateData, |
| 753 | tfLiteContext, |
| 754 | tfLiteNode, |
| 755 | nodeIndex, |
| 756 | kTfLiteBuiltinLogSoftmax); |
| 757 | case kTfLiteBuiltinLstm: |
| 758 | return VisitLstmOperator(delegateData, |
| 759 | tfLiteContext, |
| 760 | tfLiteNode, |
| 761 | nodeIndex, |
| 762 | kTfLiteBuiltinLstm); |
| 763 | case kTfLiteBuiltinMaxPool2d: |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 764 | return VisitPooling2dOperator(delegateData, |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 765 | tfLiteContext, |
| 766 | tfLiteNode, |
| 767 | nodeIndex, |
| 768 | kTfLiteBuiltinMaxPool2d); |
| 769 | case kTfLiteBuiltinMaximum: |
| 770 | return VisitElementwiseBinaryOperator(delegateData, |
| 771 | tfLiteContext, |
| 772 | tfLiteNode, |
| 773 | nodeIndex, |
| 774 | kTfLiteBuiltinMaximum); |
| 775 | case kTfLiteBuiltinMean: |
| 776 | return VisitControlOperator(delegateData, |
| 777 | tfLiteContext, |
| 778 | tfLiteNode, |
| 779 | nodeIndex, |
| 780 | kTfLiteBuiltinMean); |
| 781 | case kTfLiteBuiltinMinimum: |
| 782 | return VisitElementwiseBinaryOperator(delegateData, |
| 783 | tfLiteContext, |
| 784 | tfLiteNode, |
| 785 | nodeIndex, |
| 786 | kTfLiteBuiltinMinimum); |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 787 | case kTfLiteBuiltinMirrorPad: |
| 788 | return VisitPadOperator(delegateData, |
| 789 | tfLiteContext, |
| 790 | tfLiteNode, |
| 791 | nodeIndex, |
| 792 | kTfLiteBuiltinMirrorPad); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 793 | case kTfLiteBuiltinMul: |
| 794 | return VisitElementwiseBinaryOperator(delegateData, |
| 795 | tfLiteContext, |
| 796 | tfLiteNode, |
| 797 | nodeIndex, |
| 798 | kTfLiteBuiltinMul); |
| 799 | case kTfLiteBuiltinNeg: |
| 800 | return VisitElementwiseUnaryOperator(delegateData, |
| 801 | tfLiteContext, |
| 802 | tfLiteNode, |
| 803 | nodeIndex, |
| 804 | armnn::UnaryOperation::Neg); |
| 805 | case kTfLiteBuiltinNotEqual: |
| 806 | return VisitComparisonOperator(delegateData, |
| 807 | tfLiteContext, |
| 808 | tfLiteNode, |
| 809 | nodeIndex, |
| 810 | kTfLiteBuiltinNotEqual); |
Matthew Sloyan | a7a12f5 | 2021-05-06 10:05:28 +0100 | [diff] [blame] | 811 | case kTfLiteBuiltinPack: |
| 812 | return VisitPackOperator(delegateData, |
| 813 | tfLiteContext, |
| 814 | tfLiteNode, |
| 815 | nodeIndex, |
| 816 | kTfLiteBuiltinPack); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 817 | case kTfLiteBuiltinPad: |
| 818 | return VisitPadOperator(delegateData, |
| 819 | tfLiteContext, |
| 820 | tfLiteNode, |
| 821 | nodeIndex, |
| 822 | kTfLiteBuiltinPad); |
| 823 | case kTfLiteBuiltinPadv2: |
| 824 | return VisitPadOperator(delegateData, |
| 825 | tfLiteContext, |
| 826 | tfLiteNode, |
| 827 | nodeIndex, |
| 828 | kTfLiteBuiltinPadv2); |
| 829 | case kTfLiteBuiltinPrelu: |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 830 | return VisitPreluOperator(delegateData, |
| 831 | tfLiteContext, |
| 832 | tfLiteNode, |
| 833 | nodeIndex, |
| 834 | kTfLiteBuiltinPrelu); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 835 | case kTfLiteBuiltinQuantize: |
| 836 | return VisitQuantizeOperator(delegateData, |
| 837 | tfLiteContext, |
| 838 | tfLiteNode, |
| 839 | nodeIndex, |
| 840 | kTfLiteBuiltinQuantize); |
| 841 | case kTfLiteBuiltinRank: |
| 842 | return VisitControlOperator(delegateData, |
| 843 | tfLiteContext, |
| 844 | tfLiteNode, |
| 845 | nodeIndex, |
| 846 | kTfLiteBuiltinRank); |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 847 | case kTfLiteBuiltinReduceMax: |
| 848 | return VisitReduceOperator(delegateData, |
| 849 | tfLiteContext, |
| 850 | tfLiteNode, |
| 851 | nodeIndex, |
| 852 | kTfLiteBuiltinReduceMax); |
| 853 | case kTfLiteBuiltinReduceMin: |
| 854 | return VisitReduceOperator(delegateData, |
| 855 | tfLiteContext, |
| 856 | tfLiteNode, |
| 857 | nodeIndex, |
| 858 | kTfLiteBuiltinReduceMin); |
Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 859 | case kTfLiteBuiltinReduceProd: |
| 860 | return VisitReduceOperator(delegateData, |
| 861 | tfLiteContext, |
| 862 | tfLiteNode, |
| 863 | nodeIndex, |
| 864 | kTfLiteBuiltinReduceProd); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 865 | case kTfLiteBuiltinRelu: |
| 866 | return VisitActivationOperator(delegateData, |
| 867 | tfLiteContext, |
| 868 | tfLiteNode, |
| 869 | nodeIndex, |
| 870 | kTfLiteBuiltinRelu); |
| 871 | case kTfLiteBuiltinReluN1To1: |
| 872 | return VisitActivationOperator(delegateData, |
| 873 | tfLiteContext, |
| 874 | tfLiteNode, |
| 875 | nodeIndex, |
| 876 | kTfLiteBuiltinReluN1To1); |
| 877 | case kTfLiteBuiltinRelu6: |
| 878 | return VisitActivationOperator(delegateData, |
| 879 | tfLiteContext, |
| 880 | tfLiteNode, |
| 881 | nodeIndex, |
| 882 | kTfLiteBuiltinRelu6); |
| 883 | case kTfLiteBuiltinReshape: |
| 884 | return VisitReshapeOperator(delegateData, |
| 885 | tfLiteContext, |
| 886 | tfLiteNode, |
| 887 | nodeIndex, |
| 888 | kTfLiteBuiltinReshape); |
| 889 | case kTfLiteBuiltinResizeBilinear: |
| 890 | return VisitResizeOperator(delegateData, |
| 891 | tfLiteContext, |
| 892 | tfLiteNode, |
| 893 | nodeIndex, |
| 894 | kTfLiteBuiltinResizeBilinear); |
| 895 | case kTfLiteBuiltinResizeNearestNeighbor: |
| 896 | return VisitResizeOperator(delegateData, |
| 897 | tfLiteContext, |
| 898 | tfLiteNode, |
| 899 | nodeIndex, |
| 900 | kTfLiteBuiltinResizeNearestNeighbor); |
| 901 | case kTfLiteBuiltinRsqrt: |
| 902 | return VisitElementwiseUnaryOperator(delegateData, |
| 903 | tfLiteContext, |
| 904 | tfLiteNode, |
| 905 | nodeIndex, |
| 906 | armnn::UnaryOperation::Rsqrt); |
Keith Davis | 0176fd8 | 2021-06-01 17:36:32 +0100 | [diff] [blame] | 907 | case kTfLiteBuiltinShape: |
| 908 | return VisitShapeOperator(delegateData, |
| 909 | tfLiteContext, |
| 910 | tfLiteNode, |
| 911 | nodeIndex, |
| 912 | kTfLiteBuiltinShape); |
Sadik Armagan | 34fa1bd | 2020-11-27 12:40:52 +0000 | [diff] [blame] | 913 | case kTfLiteBuiltinSplit: |
| 914 | return VisitSplitOperator(delegateData, |
| 915 | tfLiteContext, |
| 916 | tfLiteNode, |
| 917 | nodeIndex, |
| 918 | kTfLiteBuiltinSplit); |
| 919 | case kTfLiteBuiltinSplitV: |
| 920 | return VisitSplitVOperator(delegateData, |
| 921 | tfLiteContext, |
| 922 | tfLiteNode, |
| 923 | nodeIndex, |
| 924 | kTfLiteBuiltinSplitV); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 925 | case kTfLiteBuiltinSqrt: |
| 926 | return VisitElementwiseUnaryOperator(delegateData, |
| 927 | tfLiteContext, |
| 928 | tfLiteNode, |
| 929 | nodeIndex, |
| 930 | armnn::UnaryOperation::Sqrt); |
| 931 | case kTfLiteBuiltinSqueeze: |
| 932 | return VisitSqueezeOperator(delegateData, |
| 933 | tfLiteContext, |
| 934 | tfLiteNode, |
| 935 | nodeIndex, |
| 936 | kTfLiteBuiltinSqueeze); |
| 937 | case kTfLiteBuiltinStridedSlice: |
| 938 | return VisitSliceOperator(delegateData, |
| 939 | tfLiteContext, |
| 940 | tfLiteNode, |
| 941 | nodeIndex, |
| 942 | kTfLiteBuiltinStridedSlice); |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 943 | case kTfLiteBuiltinSum: |
| 944 | return VisitReduceOperator(delegateData, |
| 945 | tfLiteContext, |
| 946 | tfLiteNode, |
| 947 | nodeIndex, |
| 948 | kTfLiteBuiltinSum); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 949 | case kTfLiteBuiltinTranspose: |
| 950 | return VisitTransposeOperator(delegateData, |
| 951 | tfLiteContext, |
| 952 | tfLiteNode, |
| 953 | nodeIndex, |
| 954 | kTfLiteBuiltinTranspose); |
| 955 | case kTfLiteBuiltinTransposeConv: |
| 956 | return VisitConvolutionOperator(delegateData, |
| 957 | tfLiteContext, |
| 958 | tfLiteNode, |
| 959 | nodeIndex, |
| 960 | kTfLiteBuiltinTransposeConv); |
| 961 | case kTfLiteBuiltinSoftmax: |
| 962 | return VisitSoftmaxOperator(delegateData, |
| 963 | tfLiteContext, |
| 964 | tfLiteNode, |
| 965 | nodeIndex, |
| 966 | kTfLiteBuiltinSoftmax); |
| 967 | case kTfLiteBuiltinSpaceToBatchNd: |
| 968 | return VisitSpaceToBatchNdOperator(delegateData, |
| 969 | tfLiteContext, |
| 970 | tfLiteNode, |
| 971 | nodeIndex, |
| 972 | kTfLiteBuiltinSpaceToBatchNd); |
| 973 | case kTfLiteBuiltinSpaceToDepth: |
| 974 | return VisitSpaceToDepthOperator(delegateData, |
| 975 | tfLiteContext, |
| 976 | tfLiteNode, |
| 977 | nodeIndex, |
| 978 | kTfLiteBuiltinSpaceToDepth); |
| 979 | case kTfLiteBuiltinSub: |
| 980 | return VisitElementwiseBinaryOperator(delegateData, |
| 981 | tfLiteContext, |
| 982 | tfLiteNode, |
| 983 | nodeIndex, |
| 984 | kTfLiteBuiltinSub); |
| 985 | case kTfLiteBuiltinTanh: |
| 986 | return VisitActivationOperator(delegateData, |
| 987 | tfLiteContext, |
| 988 | tfLiteNode, |
| 989 | nodeIndex, |
| 990 | kTfLiteBuiltinTanh); |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 991 | case kTfLiteBuiltinUnidirectionalSequenceLstm: |
| 992 | return VisitUnidirectionalSequenceLstmOperator(delegateData, |
| 993 | tfLiteContext, |
| 994 | tfLiteNode, |
| 995 | nodeIndex, |
| 996 | kTfLiteBuiltinUnidirectionalSequenceLstm); |
Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 997 | case kTfLiteBuiltinUnpack: |
| 998 | return VisitUnpackOperator(delegateData, |
| 999 | tfLiteContext, |
| 1000 | tfLiteNode, |
| 1001 | nodeIndex, |
| 1002 | kTfLiteBuiltinUnpack); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 1003 | default: |
| 1004 | return kTfLiteError; |
| 1005 | } |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | } // armnnDelegate namespace |