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 #include "tensorflow/lite/tools/evaluation/utils.h"
19
20 #if (defined(ANDROID) || defined(__ANDROID__)) && \
21 (defined(__arm__) || defined(__aarch64__))
22 #define TFLITE_ENABLE_HEXAGON
23 #endif
24
25 #if defined(TFLITE_ENABLE_HEXAGON)
26 #include "tensorflow/lite/delegates/hexagon/hexagon_delegate.h"
27 #endif
28
29 namespace tflite {
30 namespace tools {
31
32 class HexagonDelegateProvider : public DelegateProvider {
33 public:
HexagonDelegateProvider()34 HexagonDelegateProvider() {
35 #if defined(TFLITE_ENABLE_HEXAGON)
36 default_params_.AddParam("use_hexagon", ToolParam::Create<bool>(false));
37 default_params_.AddParam("hexagon_lib_path",
38 ToolParam::Create<std::string>("/data/local/tmp"));
39 default_params_.AddParam("hexagon_profiling",
40 ToolParam::Create<bool>(false));
41 #endif
42 }
43
44 std::vector<Flag> CreateFlags(ToolParams* params) const final;
45
46 void LogParams(const ToolParams& params, bool verbose) const final;
47
48 TfLiteDelegatePtr CreateTfLiteDelegate(const ToolParams& params) const final;
49
GetName() const50 std::string GetName() const final { return "Hexagon"; }
51 };
52 REGISTER_DELEGATE_PROVIDER(HexagonDelegateProvider);
53
CreateFlags(ToolParams * params) const54 std::vector<Flag> HexagonDelegateProvider::CreateFlags(
55 ToolParams* params) const {
56 #if defined(TFLITE_ENABLE_HEXAGON)
57 std::vector<Flag> flags = {
58 CreateFlag<bool>("use_hexagon", params, "Use Hexagon delegate"),
59 CreateFlag<std::string>(
60 "hexagon_lib_path", params,
61 "The library path for the underlying Hexagon libraries."),
62 CreateFlag<bool>("hexagon_profiling", params,
63 "Enables Hexagon profiling")};
64 return flags;
65 #else
66 return {};
67 #endif
68 }
69
LogParams(const ToolParams & params,bool verbose) const70 void HexagonDelegateProvider::LogParams(const ToolParams& params,
71 bool verbose) const {
72 #if defined(TFLITE_ENABLE_HEXAGON)
73 LOG_TOOL_PARAM(params, bool, "use_hexagon", "Use Hexagon", verbose);
74 LOG_TOOL_PARAM(params, std::string, "hexagon_lib_path", "Hexagon lib path",
75 verbose);
76 LOG_TOOL_PARAM(params, bool, "hexagon_profiling", "Hexagon profiling",
77 verbose);
78 #endif
79 }
80
CreateTfLiteDelegate(const ToolParams & params) const81 TfLiteDelegatePtr HexagonDelegateProvider::CreateTfLiteDelegate(
82 const ToolParams& params) const {
83 TfLiteDelegatePtr delegate(nullptr, [](TfLiteDelegate*) {});
84 #if defined(TFLITE_ENABLE_HEXAGON)
85 if (params.Get<bool>("use_hexagon")) {
86 TfLiteHexagonDelegateOptions options = {0};
87 options.print_graph_profile = params.Get<bool>("hexagon_profiling");
88 options.max_delegated_partitions =
89 params.Get<int>("max_delegated_partitions");
90 options.min_nodes_per_partition =
91 params.Get<int>("min_nodes_per_partition");
92 delegate = evaluation::CreateHexagonDelegate(
93 &options, params.Get<std::string>("hexagon_lib_path"));
94
95 if (!delegate.get()) {
96 TFLITE_LOG(WARN)
97 << "Could not create Hexagon delegate: platform may not support "
98 "delegate or required libraries are missing";
99 }
100 }
101 #endif
102 return delegate;
103 }
104
105 } // namespace tools
106 } // namespace tflite
107