• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
16 #include <vector>
17 
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/delegates/utils/dummy_delegate/dummy_delegate.h"
20 #include "tensorflow/lite/tools/command_line_flags.h"
21 #include "tensorflow/lite/tools/logging.h"
22 
23 namespace tflite {
24 namespace tools {
25 
CreateDummyDelegateFromOptions(char ** options_keys,char ** options_values,size_t num_options)26 TfLiteDelegate* CreateDummyDelegateFromOptions(char** options_keys,
27                                                char** options_values,
28                                                size_t num_options) {
29   DummyDelegateOptions options = TfLiteDummyDelegateOptionsDefault();
30 
31   // Parse key-values options to DummyDelegateOptions by mimicking them as
32   // command-line flags.
33   std::vector<const char*> argv;
34   argv.reserve(num_options + 1);
35   constexpr char kDummyDelegateParsing[] = "dummy_delegate_parsing";
36   argv.push_back(kDummyDelegateParsing);
37 
38   std::vector<std::string> option_args;
39   option_args.reserve(num_options);
40   for (int i = 0; i < num_options; ++i) {
41     option_args.emplace_back("--");
42     option_args.rbegin()->append(options_keys[i]);
43     option_args.rbegin()->push_back('=');
44     option_args.rbegin()->append(options_values[i]);
45     argv.push_back(option_args.rbegin()->c_str());
46   }
47 
48   constexpr char kAllowedBuiltinOp[] = "allowed_builtin_code";
49   constexpr char kReportErrorDuingInit[] = "error_during_init";
50   constexpr char kReportErrorDuingPrepare[] = "error_during_prepare";
51   constexpr char kReportErrorDuingInvoke[] = "error_during_invoke";
52 
53   std::vector<tflite::Flag> flag_list = {
54       tflite::Flag::CreateFlag(kAllowedBuiltinOp, &options.allowed_builtin_code,
55                                "Allowed builtin code."),
56       tflite::Flag::CreateFlag(kReportErrorDuingInit,
57                                &options.error_during_init,
58                                "Report error during init."),
59       tflite::Flag::CreateFlag(kReportErrorDuingPrepare,
60                                &options.error_during_prepare,
61                                "Report error during prepare."),
62       tflite::Flag::CreateFlag(kReportErrorDuingInvoke,
63                                &options.error_during_invoke,
64                                "Report error during invoke."),
65   };
66 
67   int argc = num_options + 1;
68   if (!tflite::Flags::Parse(&argc, argv.data(), flag_list)) {
69     return nullptr;
70   }
71 
72   TFLITE_LOG(INFO) << "Dummy delegate: allowed_builtin_code set to "
73                    << options.allowed_builtin_code << ".";
74   TFLITE_LOG(INFO) << "Dummy delegate: error_during_init set to "
75                    << options.error_during_init << ".";
76   TFLITE_LOG(INFO) << "Dummy delegate: error_during_prepare set to "
77                    << options.error_during_prepare << ".";
78   TFLITE_LOG(INFO) << "Dummy delegate: error_during_invoke set to "
79                    << options.error_during_invoke << ".";
80 
81   return TfLiteDummyDelegateCreate(&options);
82 }
83 
84 }  // namespace tools
85 }  // namespace tflite
86 
87 extern "C" {
88 
89 // Defines two symbols that need to be exported to use the TFLite external
90 // delegate. See tensorflow/lite/delegates/external for details.
tflite_plugin_create_delegate(char ** options_keys,char ** options_values,size_t num_options,void (* report_error)(const char *))91 TFL_CAPI_EXPORT TfLiteDelegate* tflite_plugin_create_delegate(
92     char** options_keys, char** options_values, size_t num_options,
93     void (*report_error)(const char*)) {
94   return tflite::tools::CreateDummyDelegateFromOptions(
95       options_keys, options_values, num_options);
96 }
97 
tflite_plugin_destroy_delegate(TfLiteDelegate * delegate)98 TFL_CAPI_EXPORT void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate) {
99   TfLiteDummyDelegateDelete(delegate);
100 }
101 
102 }  // extern "C"
103