-
Notifications
You must be signed in to change notification settings - Fork 74k
/
default_execution_provider.cc
112 lines (99 loc) · 4.92 KB
/
default_execution_provider.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
/* 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 <utility>
#include "tensorflow/lite/tools/delegates/delegate_provider.h"
namespace tflite {
namespace tools {
// This class actually doesn't provide any TFLite delegate instances, it simply
// provides common params and flags that are common to all actual delegate
// providers.
class DefaultExecutionProvider : public DelegateProvider {
public:
DefaultExecutionProvider() {
default_params_.AddParam("help", ToolParam::Create<bool>(false));
default_params_.AddParam("num_threads", ToolParam::Create<int32_t>(-1));
default_params_.AddParam("max_delegated_partitions",
ToolParam::Create<int32_t>(0));
default_params_.AddParam("min_nodes_per_partition",
ToolParam::Create<int32_t>(0));
default_params_.AddParam("delegate_serialize_dir",
ToolParam::Create<std::string>(""));
default_params_.AddParam("delegate_serialize_token",
ToolParam::Create<std::string>(""));
}
std::vector<Flag> CreateFlags(ToolParams* params) const final;
void LogParams(const ToolParams& params, bool verbose) const final;
TfLiteDelegatePtr CreateTfLiteDelegate(const ToolParams& params) const final;
std::pair<TfLiteDelegatePtr, int> CreateRankedTfLiteDelegate(
const ToolParams& params) const final;
std::string GetName() const final { return "Default-NoDelegate"; }
};
REGISTER_DELEGATE_PROVIDER(DefaultExecutionProvider);
std::vector<Flag> DefaultExecutionProvider::CreateFlags(
ToolParams* params) const {
std::vector<Flag> flags = {
CreateFlag<bool>("help", params,
"Print out all supported flags if true."),
CreateFlag<int32_t>("num_threads", params,
"number of threads used for inference on CPU."),
CreateFlag<int32_t>("max_delegated_partitions", params,
"Max number of partitions to be delegated."),
CreateFlag<int32_t>(
"min_nodes_per_partition", params,
"The minimal number of TFLite graph nodes of a partition that has to "
"be reached for it to be delegated.A negative value or 0 means to "
"use the default choice of each delegate."),
CreateFlag<std::string>(
"delegate_serialize_dir", params,
"Directory to be used by delegates for serializing any model data. "
"This allows the delegate to save data into this directory to reduce "
"init time after the first run. Currently supported by NNAPI "
"delegate with specific backends on Android. Note that "
"delegate_serialize_token is also required to enable this feature."),
CreateFlag<std::string>(
"delegate_serialize_token", params,
"Model-specific token acting as a namespace for delegate "
"serialization. Unique tokens ensure that the delegate doesn't read "
"inapplicable/invalid data. Note that delegate_serialize_dir is also "
"required to enable this feature."),
};
return flags;
}
void DefaultExecutionProvider::LogParams(const ToolParams& params,
bool verbose) const {
LOG_TOOL_PARAM(params, bool, "help", "print out all supported flags",
verbose);
LOG_TOOL_PARAM(params, int32_t, "num_threads",
"#threads used for CPU inference", verbose);
LOG_TOOL_PARAM(params, int32_t, "max_delegated_partitions",
"Max number of delegated partitions", verbose);
LOG_TOOL_PARAM(params, int32_t, "min_nodes_per_partition",
"Min nodes per partition", verbose);
LOG_TOOL_PARAM(params, std::string, "delegate_serialize_dir",
"Directory for delegate serialization", verbose);
LOG_TOOL_PARAM(params, std::string, "delegate_serialize_token",
"Model-specific token/key for delegate serialization.",
verbose);
}
TfLiteDelegatePtr DefaultExecutionProvider::CreateTfLiteDelegate(
const ToolParams& params) const {
return CreateNullDelegate();
}
std::pair<TfLiteDelegatePtr, int>
DefaultExecutionProvider::CreateRankedTfLiteDelegate(
const ToolParams& params) const {
auto ptr = CreateTfLiteDelegate(params);
return std::make_pair(std::move(ptr), 0);
}
} // namespace tools
} // namespace tflite