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