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 <string>
18
19 #include "tensorflow/lite/tools/command_line_flags.h"
20 #include "tensorflow/lite/tools/logging.h"
21 #include "tensorflow/lite/tools/tool_params.h"
22
23 namespace tflite {
24 constexpr char KernelTestDelegateProviders::kUseSimpleAllocator[];
25
Get()26 /*static*/ KernelTestDelegateProviders* KernelTestDelegateProviders::Get() {
27 static KernelTestDelegateProviders* const providers =
28 new KernelTestDelegateProviders();
29 return providers;
30 }
31
KernelTestDelegateProviders()32 KernelTestDelegateProviders::KernelTestDelegateProviders()
33 : delegate_list_util_(¶ms_) {
34 delegate_list_util_.AddAllDelegateParams();
35 params_.AddParam(kUseSimpleAllocator, tools::ToolParam::Create<bool>(false));
36 }
37
InitFromCmdlineArgs(int * argc,const char ** argv)38 bool KernelTestDelegateProviders::InitFromCmdlineArgs(int* argc,
39 const char** argv) {
40 std::vector<tflite::Flag> flags = {Flag(
41 kUseSimpleAllocator,
42 [this](const bool& val, int argv_position) { // NOLINT
43 this->params_.Set<bool>(kUseSimpleAllocator, val, argv_position);
44 },
45 false, "Use Simple Memory Allocator for SingleOpModel", Flag::kOptional)};
46 delegate_list_util_.AppendCmdlineFlags(flags);
47
48 bool parse_result = tflite::Flags::Parse(argc, argv, flags);
49 if (!parse_result || params_.Get<bool>("help")) {
50 std::string usage = Flags::Usage(argv[0], flags);
51 TFLITE_LOG(ERROR) << usage;
52 // Returning false intentionally when "--help=true" is specified so that
53 // the caller could check the return value to decide stopping the execution.
54 parse_result = false;
55 }
56 return parse_result;
57 }
58 } // namespace tflite
59