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