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 #ifndef TENSORFLOW_LITE_KERNELS_TEST_DELEGATE_PROVIDERS_H_ 16 #define TENSORFLOW_LITE_KERNELS_TEST_DELEGATE_PROVIDERS_H_ 17 18 #include <vector> 19 20 #include "tensorflow/lite/tools/delegates/delegate_provider.h" 21 #include "tensorflow/lite/tools/tool_params.h" 22 23 namespace tflite { 24 // A utility class to provide TfLite delegate creations for kernel tests. The 25 // options of a particular delegate could be specified from commandline flags by 26 // using the delegate provider registrar as implemented in lite/tools/delegates 27 // directory. 28 class KernelTestDelegateProviders { 29 public: 30 // Returns a global KernelTestDelegateProviders instance. 31 static KernelTestDelegateProviders* Get(); 32 33 KernelTestDelegateProviders(); 34 35 // Initialize delegate-related parameters from commandline arguments and 36 // returns true if successful. 37 bool InitFromCmdlineArgs(int* argc, const char** argv); 38 39 // This provides a way to overwrite parameter values programmatically before 40 // creating TfLite delegates. Note, changes to the returned ToolParams will 41 // have a global impact on creating TfLite delegates. 42 // If a local-only change is preferred, recommend using the following workflow 43 // create TfLite delegates via delegate providers: 44 // tools::ToolParams local_params; 45 // local_params.Merge(KernelTestDelegateProviders::Get()->ConstParams()); 46 // Overwrite params in local_params by calling local_params.Set<...>(...); 47 // Get TfLite delegates via 48 // KernelTestDelegateProviders::Get()->CreateAllDelegates(local_params); MutableParams()49 tools::ToolParams* MutableParams() { return ¶ms_; } ConstParams()50 const tools::ToolParams& ConstParams() const { return params_; } 51 52 // Create a list of TfLite delegates based on the provided parameters 53 // `params`. CreateAllDelegates(const tools::ToolParams & params)54 std::vector<tools::ProvidedDelegateList::ProvidedDelegate> CreateAllDelegates( 55 const tools::ToolParams& params) const { 56 tools::ProvidedDelegateList util; 57 return util.CreateAllRankedDelegates(params); 58 } 59 60 // Similar to the above, but creating a list of TfLite delegates based on what 61 // have been initialized (i.e. 'params_'). 62 std::vector<tools::ProvidedDelegateList::ProvidedDelegate> CreateAllDelegates()63 CreateAllDelegates() const { 64 return delegate_list_util_.CreateAllRankedDelegates(); 65 } 66 67 // An option name to use Simple Memory Allocator. 68 static constexpr char kUseSimpleAllocator[] = "use_simple_allocator"; 69 70 private: 71 // Contain delegate-related parameters that are initialized from command-line 72 // flags. 73 tools::ToolParams params_; 74 75 // A helper to create TfLite delegates. 76 tools::ProvidedDelegateList delegate_list_util_; 77 }; 78 79 } // namespace tflite 80 81 #endif // TENSORFLOW_LITE_KERNELS_TEST_DELEGATE_PROVIDERS_H_ 82