1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/delegates/utils/simple_delegate.h"
16
17 #include <limits>
18 #include <memory>
19 #include <vector>
20
21 #include "tensorflow/lite/builtin_ops.h"
22 #include "tensorflow/lite/c/common.h"
23 #include "tensorflow/lite/context_util.h"
24 #include "tensorflow/lite/delegates/utils.h"
25 #include "tensorflow/lite/kernels/internal/compatibility.h"
26 #include "tensorflow/lite/minimal_logging.h"
27
28 namespace tflite {
29 namespace {
GetDelegateKernelRegistration(SimpleDelegateInterface * delegate)30 TfLiteRegistration GetDelegateKernelRegistration(
31 SimpleDelegateInterface* delegate) {
32 TfLiteRegistration kernel_registration;
33 kernel_registration.profiling_string = nullptr;
34 kernel_registration.builtin_code = kTfLiteBuiltinDelegate;
35 kernel_registration.custom_name = delegate->Name();
36 kernel_registration.version = 1;
37 kernel_registration.free = [](TfLiteContext* context, void* buffer) -> void {
38 delete reinterpret_cast<SimpleDelegateKernelInterface*>(buffer);
39 };
40 kernel_registration.init = [](TfLiteContext* context, const char* buffer,
41 size_t length) -> void* {
42 const TfLiteDelegateParams* params =
43 reinterpret_cast<const TfLiteDelegateParams*>(buffer);
44 if (params == nullptr) {
45 TF_LITE_KERNEL_LOG(context, "NULL TfLiteDelegateParams passed.");
46 return nullptr;
47 }
48 auto* delegate =
49 reinterpret_cast<SimpleDelegateInterface*>(params->delegate->data_);
50 std::unique_ptr<SimpleDelegateKernelInterface> delegate_kernel(
51 delegate->CreateDelegateKernelInterface());
52 if (delegate_kernel->Init(context, params) != kTfLiteOk) {
53 return nullptr;
54 }
55 return delegate_kernel.release();
56 };
57 kernel_registration.prepare = [](TfLiteContext* context,
58 TfLiteNode* node) -> TfLiteStatus {
59 if (node->user_data == nullptr) {
60 TF_LITE_KERNEL_LOG(context, "Delegate kernel was not initialized");
61 return kTfLiteError;
62 }
63 SimpleDelegateKernelInterface* delegate_kernel =
64 reinterpret_cast<SimpleDelegateKernelInterface*>(node->user_data);
65 return delegate_kernel->Prepare(context, node);
66 };
67 kernel_registration.invoke = [](TfLiteContext* context,
68 TfLiteNode* node) -> TfLiteStatus {
69 SimpleDelegateKernelInterface* delegate_kernel =
70 reinterpret_cast<SimpleDelegateKernelInterface*>(node->user_data);
71 TFLITE_DCHECK(delegate_kernel != nullptr);
72 return delegate_kernel->Eval(context, node);
73 };
74
75 return kernel_registration;
76 }
77
DelegatePrepare(TfLiteContext * context,TfLiteDelegate * base_delegate)78 TfLiteStatus DelegatePrepare(TfLiteContext* context,
79 TfLiteDelegate* base_delegate) {
80 auto* delegate =
81 reinterpret_cast<SimpleDelegateInterface*>(base_delegate->data_);
82 auto delegate_options = delegate->DelegateOptions();
83 if (delegate_options.max_delegated_partitions <= 0)
84 delegate_options.max_delegated_partitions = std::numeric_limits<int>::max();
85
86 TF_LITE_ENSURE_STATUS(delegate->Initialize(context));
87 delegates::IsNodeSupportedFn node_supported_fn =
88 [=](TfLiteContext* context, TfLiteNode* node,
89 TfLiteRegistration* registration,
90 std::string* unsupported_details) -> bool {
91 return delegate->IsNodeSupportedByDelegate(registration, node, context);
92 };
93 // TODO(b/149484598): Update to have method that gets all supported nodes.
94 delegates::GraphPartitionHelper helper(context, node_supported_fn);
95 TF_LITE_ENSURE_STATUS(helper.Partition(nullptr));
96
97 std::vector<int> supported_nodes = helper.GetNodesOfFirstNLargestPartitions(
98 delegate_options.max_delegated_partitions,
99 delegate_options.min_nodes_per_partition);
100
101 TFLITE_LOG_PROD(tflite::TFLITE_LOG_INFO,
102 "%s delegate: %d nodes delegated out of %d nodes with "
103 "%d partitions.\n",
104 delegate->Name(), supported_nodes.size(),
105 helper.num_total_nodes(), helper.num_partitions());
106 TfLiteRegistration delegate_kernel_registration =
107 GetDelegateKernelRegistration(delegate);
108
109 return context->ReplaceNodeSubsetsWithDelegateKernels(
110 context, delegate_kernel_registration,
111 BuildTfLiteIntArray(supported_nodes).get(), base_delegate);
112 }
113 } // namespace
114
CreateSimpleDelegate(std::unique_ptr<SimpleDelegateInterface> simple_delegate,int64_t flag)115 TfLiteDelegate* TfLiteDelegateFactory::CreateSimpleDelegate(
116 std::unique_ptr<SimpleDelegateInterface> simple_delegate, int64_t flag) {
117 if (simple_delegate == nullptr) {
118 return nullptr;
119 }
120 auto delegate = new TfLiteDelegate();
121 delegate->Prepare = &DelegatePrepare;
122 delegate->flags = flag;
123 delegate->CopyFromBufferHandle = nullptr;
124 delegate->CopyToBufferHandle = nullptr;
125 delegate->FreeBufferHandle = nullptr;
126 delegate->data_ = simple_delegate.release();
127 return delegate;
128 }
129
DeleteSimpleDelegate(TfLiteDelegate * delegate)130 void TfLiteDelegateFactory::DeleteSimpleDelegate(TfLiteDelegate* delegate) {
131 if (!delegate) return;
132 SimpleDelegateInterface* simple_delegate =
133 reinterpret_cast<SimpleDelegateInterface*>(delegate->data_);
134 delete simple_delegate;
135 delete delegate;
136 }
137 } // namespace tflite
138