-
Notifications
You must be signed in to change notification settings - Fork 23
/
vx_delegate_adaptor.cc
150 lines (130 loc) · 6.13 KB
/
vx_delegate_adaptor.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/****************************************************************************
*
* Copyright (c) 2021 Vivante Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <string>
#include <vector>
#include <string.h>
#include "tensorflow/lite/c/common.h"
#include "delegate_main.h"
#include "tensorflow/lite/tools/command_line_flags.h"
#include "tensorflow/lite/tools/logging.h"
namespace vx {
namespace delegate {
/*
This adaptor is a customized version of tensorflow/lite/delegates/utils/dummy_delegate
*/
TfLiteDelegate* CreateVxDelegateFromOptions(char** options_keys,
char** options_values,
size_t num_options) {
VxDelegateOptions options = VxDelegateOptionsDefault();
// Parse key-values options to VxDelegateOptions by mimicking them as
// command-line flags.
const char** argv;
argv = new const char*[num_options + 1];
constexpr char kVxDelegateParsing[] = "vx_delegate_parsing";
argv[0] = kVxDelegateParsing;
std::vector<std::string> option_args;
option_args.reserve(num_options);
for (int i = 0; i < num_options; ++i) {
option_args.emplace_back("--");
option_args.rbegin()->append(options_keys[i]);
option_args.rbegin()->push_back('=');
option_args.rbegin()->append(options_values[i]);
argv[i + 1] = option_args.rbegin()->c_str();
}
constexpr char kAllowedSaveLoadNBG[] = "allowed_cache_mode";
constexpr char kDeviceId[] = "device_id";
constexpr char kAllowedBuiltinOp[] = "allowed_builtin_code";
constexpr char kReportErrorDuingInit[] = "error_during_init";
constexpr char kReportErrorDuingPrepare[] = "error_during_prepare";
constexpr char kReportErrorDuingInvoke[] = "error_during_invoke";
std::vector<tflite::Flag> flag_list = {
tflite::Flag::CreateFlag(kAllowedSaveLoadNBG, &options.allowed_cache_mode,
"Allowed save load nbg."),
tflite::Flag::CreateFlag(kDeviceId, &options.device_id,
"device id"),
tflite::Flag::CreateFlag(kAllowedBuiltinOp, &options.allowed_builtin_code,
"Allowed builtin code."),
tflite::Flag::CreateFlag(kReportErrorDuingInit,
&options.error_during_init,
"Report error during init."),
tflite::Flag::CreateFlag(kReportErrorDuingPrepare,
&options.error_during_prepare,
"Report error during prepare."),
tflite::Flag::CreateFlag(kReportErrorDuingInvoke,
&options.error_during_invoke,
"Report error during invoke."),
};
int argc = num_options + 1;
if (!tflite::Flags::Parse(&argc, argv, flag_list)) {
return nullptr;
}
TFLITE_LOG(INFO) << "Vx delegate: allowed_cache_mode set to "
<< options.allowed_cache_mode << ".";
TFLITE_LOG(INFO) << "Vx delegate: device num set to "
<< options.device_id << ".";
TFLITE_LOG(INFO) << "Vx delegate: allowed_builtin_code set to "
<< options.allowed_builtin_code << ".";
TFLITE_LOG(INFO) << "Vx delegate: error_during_init set to "
<< options.error_during_init << ".";
TFLITE_LOG(INFO) << "Vx delegate: error_during_prepare set to "
<< options.error_during_prepare << ".";
TFLITE_LOG(INFO) << "Vx delegate: error_during_invoke set to "
<< options.error_during_invoke << ".";
if (options.allowed_cache_mode) {
for (int i = 0; i < num_options; ++i) {
if(strcmp(options_keys[i],"cache_file_path") == 0){
options.cache_file_path = options_values[i];
break;
}
}
}
delete []argv;
return VxDelegateCreate(&options);
}
} // namespace delegate
} // namespace vx
extern "C" {
// Defines two symbols that need to be exported to use the TFLite external
// delegate. See tensorflow/lite/delegates/external for details.
TFL_CAPI_EXPORT TfLiteDelegate* tflite_plugin_create_delegate(
char** options_keys, char** options_values, size_t num_options,
void (*report_error)(const char*)) {
return vx::delegate::CreateVxDelegateFromOptions(
options_keys, options_values, num_options);
}
TFL_CAPI_EXPORT void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate) {
vx::delegate::VxDelegateDelete(delegate);
}
} // extern "C"