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 "tensorflow/lite/kernels/test_delegate_providers.h"
16
17 #include "tensorflow/lite/tools/command_line_flags.h"
18 #include "tensorflow/lite/tools/logging.h"
19
20 namespace tflite {
Get()21 /*static*/ KernelTestDelegateProviders* KernelTestDelegateProviders::Get() {
22 static KernelTestDelegateProviders* const providers =
23 new KernelTestDelegateProviders();
24 return providers;
25 }
26
KernelTestDelegateProviders()27 KernelTestDelegateProviders::KernelTestDelegateProviders() {
28 for (const auto& one : tools::GetRegisteredDelegateProviders()) {
29 params_.Merge(one->DefaultParams());
30 }
31 }
32
InitFromCmdlineArgs(int * argc,const char ** argv)33 bool KernelTestDelegateProviders::InitFromCmdlineArgs(int* argc,
34 const char** argv) {
35 std::vector<tflite::Flag> flags;
36 for (const auto& one : tools::GetRegisteredDelegateProviders()) {
37 auto one_flags = one->CreateFlags(¶ms_);
38 flags.insert(flags.end(), one_flags.begin(), one_flags.end());
39 }
40 return tflite::Flags::Parse(argc, argv, flags);
41 }
42
43 std::vector<tools::TfLiteDelegatePtr>
CreateAllDelegates(const tools::ToolParams & params) const44 KernelTestDelegateProviders::CreateAllDelegates(
45 const tools::ToolParams& params) const {
46 std::vector<tools::TfLiteDelegatePtr> delegates;
47 for (const auto& one : tools::GetRegisteredDelegateProviders()) {
48 auto ptr = one->CreateTfLiteDelegate(params);
49 // It's possible that a delegate of certain type won't be created as
50 // user-specified benchmark params tells not to.
51 if (ptr == nullptr) continue;
52 delegates.emplace_back(std::move(ptr));
53 TFLITE_LOG(INFO) << one->GetName() << " delegate is created.";
54 }
55 return delegates;
56 }
57 } // namespace tflite
58