1 /* Copyright 2021 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/delegates/delegate_provider.h"
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include "tensorflow/lite/tools/tool_params.h"
20
21 namespace tflite {
22 namespace tools {
23 namespace {
TEST(ProvidedDelegateListTest,AddAllDelegateParams)24 TEST(ProvidedDelegateListTest, AddAllDelegateParams) {
25 ToolParams params;
26 ProvidedDelegateList providers(¶ms);
27 providers.AddAllDelegateParams();
28 // As we link the test with XNNPACK and nnapi delegate providers, we should
29 // expect to havethese two knob parameters.
30 EXPECT_TRUE(params.HasParam("use_xnnpack"));
31 EXPECT_TRUE(params.HasParam("use_nnapi"));
32 }
33
TEST(ProvidedDelegateListTest,AppendCmdlineFlags)34 TEST(ProvidedDelegateListTest, AppendCmdlineFlags) {
35 std::vector<Flag> flags;
36
37 ToolParams params;
38 ProvidedDelegateList providers(¶ms);
39 providers.AddAllDelegateParams();
40
41 providers.AppendCmdlineFlags(flags);
42 EXPECT_FALSE(flags.empty());
43 }
44
TEST(KernelTestDelegateProvidersTest,CreateAllRankedDelegates)45 TEST(KernelTestDelegateProvidersTest, CreateAllRankedDelegates) {
46 #if !defined(__Fuchsia__) && !defined(__s390x__) && \
47 !defined(TFLITE_WITHOUT_XNNPACK)
48 ToolParams params;
49 ProvidedDelegateList providers(¶ms);
50 providers.AddAllDelegateParams();
51
52 // We set the position of "use_xnnpack" to be smaller than that of
53 // "use_dummy_delegate" so that the Dummy delegate will be ahead of the
54 // XNNPACK delegate in the returned list.
55 params.Set<bool>("use_xnnpack", true, 2);
56 params.Set<bool>("use_dummy_delegate", true, 1);
57
58 auto delegates = providers.CreateAllRankedDelegates();
59 EXPECT_EQ(2, delegates.size());
60
61 EXPECT_EQ("DummyDelegate", delegates.front().provider->GetName());
62 EXPECT_EQ(1, delegates.front().rank);
63 EXPECT_NE(nullptr, delegates.front().delegate.get());
64
65 EXPECT_EQ("XNNPACK", delegates.back().provider->GetName());
66 EXPECT_NE(nullptr, delegates.back().delegate.get());
67 EXPECT_EQ(2, delegates.back().rank);
68 #endif
69 }
70 } // namespace
71 } // namespace tools
72 } // namespace tflite
73