blob: 190c38666622f1f283ba511bdbeb1964beb354c3 [file] [log] [blame]
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +00001//
2// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <armnn_delegate.hpp>
7
8#include <Version.hpp>
9
10#include "Activation.hpp"
11#include "ArgMinMax.hpp"
12#include "BatchMatMul.hpp"
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"
22#include "GatherNd.hpp"
23#include "LogicalBinary.hpp"
24#include "Lstm.hpp"
25#include "Normalization.hpp"
26#include "Pack.hpp"
27#include "Pad.hpp"
28#include "Pooling.hpp"
29#include "Prelu.hpp"
30#include "Quantization.hpp"
31#include "Redefine.hpp"
32#include "Reduce.hpp"
33#include "Resize.hpp"
34#include "Round.hpp"
35#include "Shape.hpp"
36#include "Slice.hpp"
37#include "StridedSlice.hpp"
38#include "Softmax.hpp"
39#include "SpaceDepth.hpp"
40#include "Split.hpp"
41#include "Transpose.hpp"
42#include "UnidirectionalSequenceLstm.hpp"
43#include "Unpack.hpp"
44
45#include <armnn/utility/IgnoreUnused.hpp>
46#include <armnnUtils/Filesystem.hpp>
47#include <armnn/utility/Timer.hpp>
48#include <flatbuffers/flatbuffers.h>
49#include <tensorflow/lite/context_util.h>
50#include <tensorflow/lite/schema/schema_generated.h>
51#include <tensorflow/lite/minimal_logging.h>
52#include <tensorflow/lite/logger.h>
53
54#include <algorithm>
55#include <iostream>
56#include <sstream>
57
58namespace armnnOpaqueDelegate
59{
60
61ArmnnOpaqueDelegate::ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options)
62 : m_Options(std::move(options))
63{
64 // Configures logging for ARMNN
65 if (m_Options.IsLoggingEnabled())
66 {
67 armnn::ConfigureLogging(true, true, m_Options.GetLoggingSeverity());
68 }
69 // Create/Get the static ArmNN Runtime. Note that the m_Runtime will be shared by all armnn_delegate
70 // instances so the RuntimeOptions cannot be altered for different armnn_delegate instances.
71 m_Runtime = GetRuntime(m_Options.GetRuntimeOptions());
72 std::vector<armnn::BackendId> backends;
73 if (m_Runtime)
74 {
75 const armnn::BackendIdSet supportedDevices = m_Runtime->GetDeviceSpec().GetSupportedBackends();
76 for (auto& backend : m_Options.GetBackends())
77 {
78 if (std::find(supportedDevices.cbegin(), supportedDevices.cend(), backend) == supportedDevices.cend())
79 {
80 TFLITE_LOG_PROD(tflite::TFLITE_LOG_INFO,
81 "TfLiteArmnnDelegate: Requested unknown backend %s", backend.Get().c_str());
82 }
83 else
84 {
85 backends.push_back(backend);
86 }
87 }
88 }
89
90 if (backends.empty())
91 {
92 // No known backend specified
93 throw armnn::InvalidArgumentException("TfLiteArmnnOpaqueDelegate: No known backend specified.");
94 }
95 m_Options.SetBackends(backends);
96
97 TFLITE_LOG_PROD_ONCE(tflite::TFLITE_LOG_INFO, "TfLiteArmnnOpaqueDelegate: Created TfLite ArmNN delegate.");
98}
99
100TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings)
101{
102 // This method will always create Opaque Delegate with default settings until
103 // we have a DelegateOptions Constructor which can parse the void* settings
104 armnn::IgnoreUnused(settings);
105 auto options = TfLiteArmnnDelegateOptionsDefault();
106 auto* armnnDelegate = new ::armnnOpaqueDelegate::ArmnnOpaqueDelegate(options);
107 return TfLiteOpaqueDelegateCreate(armnnDelegate->GetDelegateBuilder());
108}
109
110::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault()
111{
112 ::armnnDelegate::DelegateOptions options(armnn::Compute::CpuRef);
113 return options;
114}
115
116void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate)
117{
118 if (tfLiteDelegate != nullptr)
119 {
120 delete static_cast<::armnnOpaqueDelegate::ArmnnOpaqueDelegate*>(TfLiteOpaqueDelegateGetData(tfLiteDelegate));
121 TfLiteOpaqueDelegateDelete(tfLiteDelegate);
122 }
123}
124
125const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi()
126{
127 static constexpr TfLiteOpaqueDelegatePlugin armnnPlugin{
128 TfLiteArmnnOpaqueDelegateCreate, TfLiteArmnnOpaqueDelegateDelete, TfLiteArmnnOpaqueDelegateErrno};
129 return &armnnPlugin;
130}
131
132const std::string ArmnnOpaqueDelegate::GetVersion() {
133 return OPAQUE_DELEGATE_VERSION;
134}
135
136} // armnnOpaqueDelegate namespace