1 /* Copyright 2019 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
16 #ifndef TENSORFLOW_LITE_TOOLS_EVALUATION_UTILS_H_
17 #define TENSORFLOW_LITE_TOOLS_EVALUATION_UTILS_H_
18
19 #include <memory>
20 #include <string>
21 #include <unordered_set>
22 #include <vector>
23
24 #if defined(__ANDROID__) || defined(CL_DELEGATE_NO_GL)
25 #define TFLITE_SUPPORTS_GPU_DELEGATE 1
26 #endif
27
28 #include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
29
30 #if TFLITE_SUPPORTS_GPU_DELEGATE
31 #include "tensorflow/lite/delegates/gpu/delegate.h"
32 #endif
33
34 #if defined(__ANDROID__) && (defined(__arm__) || defined(__aarch64__))
35 #include "tensorflow/lite/delegates/hexagon/hexagon_delegate.h"
36 #endif
37
38 // TODO(b/149248802): include XNNPACK delegate when the issue is resolved.
39 #if !defined(__Fuchsia__) || defined(TFLITE_WITHOUT_XNNPACK)
40 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
41 #endif
42
43 #include "tensorflow/lite/c/common.h"
44
45 namespace tflite {
46 namespace evaluation {
47
48 // Same as Interpreter::TfLiteDelegatePtr, defined here to avoid pulling
49 // in tensorflow/lite/interpreter.h dependency.
50 using TfLiteDelegatePtr =
51 std::unique_ptr<TfLiteDelegate, void (*)(TfLiteDelegate*)>;
52
53 std::string StripTrailingSlashes(const std::string& path);
54
55 bool ReadFileLines(const std::string& file_path,
56 std::vector<std::string>* lines_output);
57
58 // If extension set is empty, all files will be listed. The strings in
59 // extension set are expected to be in lowercase and include the dot.
60 TfLiteStatus GetSortedFileNames(
61 const std::string& directory, std::vector<std::string>* result,
62 const std::unordered_set<std::string>& extensions);
63
GetSortedFileNames(const std::string & directory,std::vector<std::string> * result)64 inline TfLiteStatus GetSortedFileNames(const std::string& directory,
65 std::vector<std::string>* result) {
66 return GetSortedFileNames(directory, result,
67 std::unordered_set<std::string>());
68 }
69
70 // Returns nullptr on error, e.g. if NNAPI isn't supported on this platform.
71 TfLiteDelegatePtr CreateNNAPIDelegate();
72 TfLiteDelegatePtr CreateNNAPIDelegate(StatefulNnApiDelegate::Options options);
73
74 TfLiteDelegatePtr CreateGPUDelegate();
75 #if TFLITE_SUPPORTS_GPU_DELEGATE
76 TfLiteDelegatePtr CreateGPUDelegate(TfLiteGpuDelegateOptionsV2* options);
77 #endif
78
79 TfLiteDelegatePtr CreateHexagonDelegate(
80 const std::string& library_directory_path, bool profiling);
81 #if defined(__ANDROID__) && (defined(__arm__) || defined(__aarch64__))
82 TfLiteDelegatePtr CreateHexagonDelegate(
83 const TfLiteHexagonDelegateOptions* options,
84 const std::string& library_directory_path);
85 #endif
86
87 // TODO(b/149248802): include XNNPACK delegate when the issue is resolved.
88 #if !defined(__Fuchsia__) || defined(TFLITE_WITHOUT_XNNPACK)
89 TfLiteDelegatePtr CreateXNNPACKDelegate();
90 TfLiteDelegatePtr CreateXNNPACKDelegate(
91 const TfLiteXNNPackDelegateOptions* options);
92 #endif
93 TfLiteDelegatePtr CreateXNNPACKDelegate(int num_threads);
94 } // namespace evaluation
95 } // namespace tflite
96
97 #endif // TENSORFLOW_LITE_TOOLS_EVALUATION_UTILS_H_
98