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
17 #include "tensorflow/lite/tools/delegates/delegate_provider.h"
18
19 namespace tflite {
20 namespace tools {
21
22 // This class actually doesn't provide any TFLite delegate instances, it simply
23 // provides common params and flags that are common to all actual delegate
24 // providers.
25 class DefaultExecutionProvider : public DelegateProvider {
26 public:
DefaultExecutionProvider()27 DefaultExecutionProvider() {
28 default_params_.AddParam("num_threads", ToolParam::Create<int32_t>(1));
29 default_params_.AddParam("max_delegated_partitions",
30 ToolParam::Create<int32_t>(0));
31 default_params_.AddParam("min_nodes_per_partition",
32 ToolParam::Create<int32_t>(0));
33 }
34
35 std::vector<Flag> CreateFlags(ToolParams* params) const final;
36 void LogParams(const ToolParams& params, bool verbose) const final;
37 TfLiteDelegatePtr CreateTfLiteDelegate(const ToolParams& params) const final;
GetName() const38 std::string GetName() const final { return "Default-NoDelegate"; }
39 };
40 REGISTER_DELEGATE_PROVIDER(DefaultExecutionProvider);
41
CreateFlags(ToolParams * params) const42 std::vector<Flag> DefaultExecutionProvider::CreateFlags(
43 ToolParams* params) const {
44 std::vector<Flag> flags = {
45 CreateFlag<int32_t>("num_threads", params,
46 "number of threads used for inference on CPU."),
47 CreateFlag<int32_t>("max_delegated_partitions", params,
48 "Max number of partitions to be delegated."),
49 CreateFlag<int32_t>(
50 "min_nodes_per_partition", params,
51 "The minimal number of TFLite graph nodes of a partition that has to "
52 "be reached for it to be delegated.A negative value or 0 means to "
53 "use the default choice of each delegate.")};
54 return flags;
55 }
56
LogParams(const ToolParams & params,bool verbose) const57 void DefaultExecutionProvider::LogParams(const ToolParams& params,
58 bool verbose) const {
59 LOG_TOOL_PARAM(params, int32_t, "num_threads",
60 "#threads used for CPU inference", verbose);
61 LOG_TOOL_PARAM(params, int32_t, "max_delegated_partitions",
62 "Max number of delegated partitions", verbose);
63 LOG_TOOL_PARAM(params, int32_t, "min_nodes_per_partition",
64 "Min nodes per partition", verbose);
65 }
66
CreateTfLiteDelegate(const ToolParams & params) const67 TfLiteDelegatePtr DefaultExecutionProvider::CreateTfLiteDelegate(
68 const ToolParams& params) const {
69 return TfLiteDelegatePtr(nullptr, [](TfLiteDelegate*) {});
70 }
71
72 } // namespace tools
73 } // namespace tflite
74