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/experimental/acceleration/compatibility/devicedb.h"
16
17 #include <memory>
18 #include <string>
19
20 #include <gtest/gtest.h>
21 #include "flatbuffers/flatbuffers.h" // from @flatbuffers
22 #include "tensorflow/lite/experimental/acceleration/compatibility/devicedb-sample.h"
23 #include "tensorflow/lite/experimental/acceleration/compatibility/variables.h"
24 #include "tensorflow/lite/testing/util.h"
25
26 namespace tflite {
27 namespace acceleration {
28 namespace {
29
30 class DeviceDbTest : public ::testing::Test {
31 protected:
LoadSample()32 void LoadSample() {
33 device_db_ = flatbuffers::GetRoot<DeviceDatabase>(
34 g_tflite_acceleration_devicedb_sample_binary);
35 }
36
37 const DeviceDatabase* device_db_ = nullptr;
38 };
39
TEST_F(DeviceDbTest,Load)40 TEST_F(DeviceDbTest, Load) {
41 LoadSample();
42 ASSERT_TRUE(device_db_);
43 ASSERT_TRUE(device_db_->root());
44 EXPECT_EQ(device_db_->root()->size(), 3);
45 }
46
TEST_F(DeviceDbTest,SocLookup)47 TEST_F(DeviceDbTest, SocLookup) {
48 LoadSample();
49 ASSERT_TRUE(device_db_);
50 std::map<std::string, std::string> variables;
51
52 // Find first device mapping.
53 variables[kDeviceModel] = "m712c";
54 UpdateVariablesFromDatabase(&variables, *device_db_);
55 EXPECT_EQ(variables[kSoCModel], "exynos_7872");
56
57 // Find second device mapping.
58 variables.clear();
59 variables[kDeviceModel] = "sc_02l";
60 UpdateVariablesFromDatabase(&variables, *device_db_);
61 EXPECT_EQ(variables[kSoCModel], "exynos_7885");
62
63 // Make sure no results are returned without a match.
64 variables.clear();
65 variables[kDeviceModel] = "nosuch";
66 UpdateVariablesFromDatabase(&variables, *device_db_);
67 EXPECT_EQ(variables.find(kSoCModel), variables.end());
68 }
69
TEST_F(DeviceDbTest,StatusLookupWithSoC)70 TEST_F(DeviceDbTest, StatusLookupWithSoC) {
71 LoadSample();
72 ASSERT_TRUE(device_db_);
73 std::map<std::string, std::string> variables;
74
75 // Find exact match.
76 variables[kOpenGLESVersion] = "3.1";
77 variables[kSoCModel] = "exynos_7872";
78 variables[kAndroidSdkVersion] = "24";
79 UpdateVariablesFromDatabase(&variables, *device_db_);
80 EXPECT_EQ(variables[gpu::kStatus], gpu::kStatusSupported);
81
82 // Ensure no results without a match.
83 variables[kOpenGLESVersion] = "3.0";
84 variables.erase(variables.find(gpu::kStatus));
85 UpdateVariablesFromDatabase(&variables, *device_db_);
86 EXPECT_EQ(variables.find(gpu::kStatus), variables.end());
87
88 // Find no results with too low an android version.
89 variables.clear();
90 variables[kOpenGLESVersion] = "3.1";
91 variables[kSoCModel] = "exynos_7883";
92 variables[kAndroidSdkVersion] = "24";
93 UpdateVariablesFromDatabase(&variables, *device_db_);
94 EXPECT_EQ(variables.find(gpu::kStatus), variables.end());
95 // Find a match with android version above minimum.
96 variables[kAndroidSdkVersion] = "29";
97 UpdateVariablesFromDatabase(&variables, *device_db_);
98 EXPECT_EQ(variables[gpu::kStatus], gpu::kStatusSupported);
99 }
100
TEST_F(DeviceDbTest,StatusLookupWithDevice)101 TEST_F(DeviceDbTest, StatusLookupWithDevice) {
102 LoadSample();
103 ASSERT_TRUE(device_db_);
104 std::map<std::string, std::string> variables;
105 // Find unsupported device (same model, different device).
106 variables[kAndroidSdkVersion] = "24";
107 variables[kDeviceModel] = "sm_j810f";
108 variables[kDeviceName] = "j8y18lte";
109 UpdateVariablesFromDatabase(&variables, *device_db_);
110 EXPECT_EQ(variables[gpu::kStatus], gpu::kStatusUnsupported);
111
112 // Find supported device (same model, different device).
113 variables.clear();
114 variables[kAndroidSdkVersion] = "24";
115 variables[kDeviceModel] = "sm_j810m";
116 variables[kDeviceName] = "j8y18lte";
117 UpdateVariablesFromDatabase(&variables, *device_db_);
118 EXPECT_EQ(variables[gpu::kStatus], gpu::kStatusSupported);
119 }
120
TEST_F(DeviceDbTest,StatusLookupBasedOnDerivedProperties)121 TEST_F(DeviceDbTest, StatusLookupBasedOnDerivedProperties) {
122 LoadSample();
123 ASSERT_TRUE(device_db_);
124 std::map<std::string, std::string> variables;
125 // Find status based on SoC derived from model.
126 variables[kOpenGLESVersion] = "3.1";
127 variables[kAndroidSdkVersion] = "24";
128 variables[kDeviceModel] = "m712c";
129 UpdateVariablesFromDatabase(&variables, *device_db_);
130 EXPECT_EQ(variables[gpu::kStatus], gpu::kStatusSupported);
131 }
132
133 } // namespace
134 } // namespace acceleration
135 } // namespace tflite
136