blob: e08f466f0958d2fcdca97b7e1faa533401eeb1ea [file] [log] [blame]
Teresa Charlinad1b3d72023-03-14 12:10:28 +00001//
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +00002// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
Teresa Charlinad1b3d72023-03-14 12:10:28 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <DelegateOptions.hpp>
9
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000010#include <tensorflow/lite/c/c_api_opaque.h>
11#include <tensorflow/lite/core/experimental/acceleration/configuration/c/stable_delegate.h>
Teresa Charlinad1b3d72023-03-14 12:10:28 +000012
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000013namespace armnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000014{
15
16struct DelegateData
17{
18 DelegateData(const std::vector<armnn::BackendId>& backends)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000019 : m_Backends(backends)
20 , m_Network(nullptr, nullptr)
Teresa Charlinad1b3d72023-03-14 12:10:28 +000021 {}
22
23 const std::vector<armnn::BackendId> m_Backends;
24 armnn::INetworkPtr m_Network;
25 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
26};
27
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000028/// Forward declaration for functions initializing the ArmNN Delegate
29::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault();
Teresa Charlinad1b3d72023-03-14 12:10:28 +000030
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000031TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000032
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000033void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000034
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000035TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000036
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000037/// ArmNN Opaque Delegate
38class ArmnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000039{
40 friend class ArmnnSubgraph;
41public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000042 explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000043
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000044 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000045
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000046 TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
Teresa Charlinad1b3d72023-03-14 12:10:28 +000047
48 /// Retrieve version in X.Y.Z form
49 static const std::string GetVersion();
50
51private:
52 /**
53 * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates.
54 */
55 armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options)
56 {
57 static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options);
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000058 /// Instantiated on first use.
Teresa Charlinad1b3d72023-03-14 12:10:28 +000059 return instance.get();
60 }
61
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000062 TfLiteOpaqueDelegateBuilder m_Builder =
63 {
Teresa Charlinad1b3d72023-03-14 12:10:28 +000064 reinterpret_cast<void*>(this), // .data_
Ryan OSheaa37ccb02023-04-11 10:54:07 +010065 DoPrepare, // .Prepare
Teresa Charlinad1b3d72023-03-14 12:10:28 +000066 nullptr, // .CopyFromBufferHandle
67 nullptr, // .CopyToBufferHandle
68 nullptr, // .FreeBufferHandle
69 kTfLiteDelegateFlagsNone, // .flags
Teresa Charlinad1b3d72023-03-14 12:10:28 +000070 };
71
72 /// ArmNN Runtime pointer
73 armnn::IRuntime* m_Runtime;
74 /// ArmNN Delegate Options
75 armnnDelegate::DelegateOptions m_Options;
76};
77
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000078static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
79
Matthew Sloyan65c21a12023-04-04 12:06:14 +010080/// In order for the delegate to be loaded by TfLite
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000081const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
82
Matthew Sloyan65c21a12023-04-04 12:06:14 +010083extern const TfLiteStableDelegate TFL_TheStableDelegate;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000084
Teresa Charlinad1b3d72023-03-14 12:10:28 +000085/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
86class ArmnnSubgraph
87{
88public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000089 static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext,
90 const TfLiteOpaqueDelegateParams* parameters,
91 const ArmnnOpaqueDelegate* delegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000092
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000093 TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000094
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000095 TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000096
97 static TfLiteStatus VisitNode(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000098 TfLiteOpaqueContext* tfLiteContext,
99 TfLiteRegistrationExternal* tfLiteRegistration,
100 TfLiteOpaqueNode* tfLiteNode,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000101 int nodeIndex);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000102private:
103 ArmnnSubgraph(armnn::NetworkId networkId,
104 armnn::IRuntime* runtime,
105 std::vector<armnn::BindingPointInfo>& inputBindings,
106 std::vector<armnn::BindingPointInfo>& outputBindings)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000107 : m_NetworkId(networkId)
108 , m_Runtime(runtime)
109 , m_InputBindings(inputBindings)
110 , m_OutputBindings(outputBindings)
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000111 {}
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000112 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000113 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000114 const TfLiteIntArray* inputs,
115 std::vector<armnn::BindingPointInfo>& inputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000116 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000117 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000118 const TfLiteIntArray* outputs,
119 std::vector<armnn::BindingPointInfo>& outputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000120 /// The Network Id
121 armnn::NetworkId m_NetworkId;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000122 /// ArmNN Runtime
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000123 armnn::IRuntime* m_Runtime;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000124 /// Binding information for inputs and outputs
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000125 std::vector<armnn::BindingPointInfo> m_InputBindings;
126 std::vector<armnn::BindingPointInfo> m_OutputBindings;
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000127};
128
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000129} // armnnOpaqueDelegate namespace