• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/tools/evaluation/evaluation_delegate_provider.h"
16 
17 #include <gtest/gtest.h>
18 #include "tensorflow/lite/tools/evaluation/proto/evaluation_stages.pb.h"
19 #include "tensorflow/lite/tools/tool_params.h"
20 
21 namespace tflite {
22 namespace evaluation {
23 namespace {
TEST(EvaluationDelegateProviderTest,ParseStringToDelegateType)24 TEST(EvaluationDelegateProviderTest, ParseStringToDelegateType) {
25   EXPECT_EQ(TfliteInferenceParams::NNAPI, ParseStringToDelegateType("nnapi"));
26   EXPECT_EQ(TfliteInferenceParams::GPU, ParseStringToDelegateType("gpu"));
27   EXPECT_EQ(TfliteInferenceParams::HEXAGON,
28             ParseStringToDelegateType("hexagon"));
29   EXPECT_EQ(TfliteInferenceParams::XNNPACK,
30             ParseStringToDelegateType("xnnpack"));
31 
32   EXPECT_EQ(TfliteInferenceParams::NONE, ParseStringToDelegateType("Gpu"));
33   EXPECT_EQ(TfliteInferenceParams::NONE, ParseStringToDelegateType("Testing"));
34 }
35 
TEST(EvaluationDelegateProviderTest,CreateTfLiteDelegate)36 TEST(EvaluationDelegateProviderTest, CreateTfLiteDelegate) {
37   TfliteInferenceParams params;
38   params.set_delegate(TfliteInferenceParams::NONE);
39   // A NONE delegate type will return a nullptr TfLite delegate ptr.
40   EXPECT_TRUE(!CreateTfLiteDelegate(params));
41 }
42 
TEST(EvaluationDelegateProviderTest,DelegateProvidersParams)43 TEST(EvaluationDelegateProviderTest, DelegateProvidersParams) {
44   DelegateProviders providers;
45   const auto& params = providers.GetAllParams();
46   EXPECT_TRUE(params.HasParam("use_nnapi"));
47   EXPECT_TRUE(params.HasParam("use_gpu"));
48 
49   int argc = 3;
50   const char* argv[] = {"program_name", "--use_gpu=true",
51                         "--other_undefined_flag=1"};
52   EXPECT_TRUE(providers.InitFromCmdlineArgs(&argc, argv));
53   EXPECT_TRUE(params.Get<bool>("use_gpu"));
54   EXPECT_EQ(2, argc);
55   EXPECT_EQ("--other_undefined_flag=1", argv[1]);
56 }
57 
TEST(EvaluationDelegateProviderTest,GetAllParamsWithTfliteInferenceParams)58 TEST(EvaluationDelegateProviderTest, GetAllParamsWithTfliteInferenceParams) {
59   DelegateProviders providers;
60   int argc = 2;
61   const char* argv[] = {"program_name", "--num_threads=1"};
62   EXPECT_TRUE(providers.InitFromCmdlineArgs(&argc, argv));
63   const auto& default_params = providers.GetAllParams();
64   EXPECT_EQ(1, default_params.Get<int>("num_threads"));
65 
66   TfliteInferenceParams params;
67   params.set_delegate(TfliteInferenceParams::NONE);
68   params.set_num_threads(4);
69   // The same-meaning parameter in TfliteInferenceParams takes precedence.
70   tools::ToolParams tool_params = providers.GetAllParams(params);
71   EXPECT_EQ(4, tool_params.Get<int>("num_threads"));
72   EXPECT_EQ(1, argc);
73 }
74 
75 }  // namespace
76 }  // namespace evaluation
77 }  // namespace tflite
78