xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/tools/evaluation/evaluation_delegate_provider.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
16 #ifndef TENSORFLOW_LITE_TOOLS_EVALUATION_EVALUATION_DELEGATE_PROVIDER_H_
17 #define TENSORFLOW_LITE_TOOLS_EVALUATION_EVALUATION_DELEGATE_PROVIDER_H_
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "tensorflow/lite/tools/command_line_flags.h"
24 #include "tensorflow/lite/tools/delegates/delegate_provider.h"
25 #include "tensorflow/lite/tools/evaluation/proto/evaluation_stages.pb.h"
26 #include "tensorflow/lite/tools/evaluation/utils.h"
27 #include "tensorflow/lite/tools/tool_params.h"
28 
29 namespace tflite {
30 namespace evaluation {
31 
32 using ProvidedDelegateList = tflite::tools::ProvidedDelegateList;
33 class DelegateProviders {
34  public:
35   DelegateProviders();
36 
37   // Returns a list of commandline flags that delegate providers define.
38   std::vector<Flag> GetFlags();
39 
40   // Initialize delegate-related parameters from commandline arguments and
41   // returns true if successful.
42   bool InitFromCmdlineArgs(int* argc, const char** argv);
43 
44   // Get all parameters from all registered delegate providers.
GetAllParams()45   const tools::ToolParams& GetAllParams() const { return params_; }
46 
47   // Get a new set of parameters based on the given TfliteInferenceParams
48   // 'params' but considering what have been initialized (i.e. 'params_').
49   // Note the same-meaning parameter (e.g. number of TfLite interpreter threads)
50   // in TfliteInferenceParams will take precedence over the parameter of the
51   // same meaning in 'params_'.
52   tools::ToolParams GetAllParams(const TfliteInferenceParams& params) const;
53 
54   // Create the a TfLite delegate instance based on the provided delegate
55   // 'name'. If the specified one isn't found, an empty TfLiteDelegatePtr is
56   // returned.
57   TfLiteDelegatePtr CreateDelegate(const std::string& name) const;
58 
59   // Create a list of TfLite delegates based on what have been initialized (i.e.
60   // 'params_').
CreateAllDelegates()61   std::vector<ProvidedDelegateList::ProvidedDelegate> CreateAllDelegates()
62       const {
63     return delegate_list_util_.CreateAllRankedDelegates();
64   }
65 
66   // Create a list of TfLite delegates based on the given TfliteInferenceParams
67   // 'params' but considering what have been initialized (i.e. 'params_').
CreateAllDelegates(const TfliteInferenceParams & params)68   std::vector<ProvidedDelegateList::ProvidedDelegate> CreateAllDelegates(
69       const TfliteInferenceParams& params) const {
70     auto converted = GetAllParams(params);
71     ProvidedDelegateList util(&converted);
72     return util.CreateAllRankedDelegates();
73   }
74 
75  private:
76   // Contain delegate-related parameters that are initialized from command-line
77   // flags.
78   tools::ToolParams params_;
79 
80   // A helper to create TfLite delegates.
81   ProvidedDelegateList delegate_list_util_;
82 
83   // Key is the delegate name, and the value is the index to a TfLite delegate
84   // provider in the "delegate_list_util_.providers()" list.
85   const std::unordered_map<std::string, int> delegates_map_;
86 };
87 
88 // Parse a string 'val' to the corresponding delegate type defined by
89 // TfliteInferenceParams::Delegate.
90 TfliteInferenceParams::Delegate ParseStringToDelegateType(
91     const std::string& val);
92 
93 // Create a TfLite delegate based on the given TfliteInferenceParams 'params'.
94 // If there's an error during the creation, an error message will be recorded to
95 // 'error_msg' if provided.
96 TfLiteDelegatePtr CreateTfLiteDelegate(const TfliteInferenceParams& params,
97                                        std::string* error_msg = nullptr);
98 }  // namespace evaluation
99 }  // namespace tflite
100 
101 #endif  // TENSORFLOW_LITE_TOOLS_EVALUATION_EVALUATION_DELEGATE_PROVIDER_H_
102