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/gpu_compatibility.h"
16
17 #include <algorithm>
18 #include <cstddef>
19 #include <memory>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include "tensorflow/lite/experimental/acceleration/compatibility/devicedb-sample.h"
24
25 namespace {
26
27 class GPUCompatibilityTest : public ::testing::Test {
28 protected:
GPUCompatibilityTest()29 GPUCompatibilityTest() {
30 list_ = tflite::acceleration::GPUCompatibilityList::Create(
31 g_tflite_acceleration_devicedb_sample_binary,
32 g_tflite_acceleration_devicedb_sample_binary_len);
33 }
34
35 std::unique_ptr<tflite::acceleration::GPUCompatibilityList> list_;
36 };
37
TEST_F(GPUCompatibilityTest,ReturnsSupportedForFullMatch)38 TEST_F(GPUCompatibilityTest, ReturnsSupportedForFullMatch) {
39 ASSERT_TRUE(list_ != nullptr);
40
41 tflite::acceleration::AndroidInfo android_info = {.android_sdk_version = "24",
42 .model = "m712c"};
43
44 tflite::gpu::GpuInfo tflite_gpu_info;
45 tflite_gpu_info.opengl_info.major_version = 3;
46 tflite_gpu_info.opengl_info.minor_version = 1;
47
48 EXPECT_TRUE(list_->Includes(android_info, tflite_gpu_info));
49 }
50
TEST_F(GPUCompatibilityTest,ReturnsUnsupportedForFullMatch)51 TEST_F(GPUCompatibilityTest, ReturnsUnsupportedForFullMatch) {
52 ASSERT_TRUE(list_ != nullptr);
53
54 tflite::acceleration::AndroidInfo android_info = {.android_sdk_version = "28",
55 .model = "SM-G960F",
56 .device = "starlte",
57 .manufacturer = "Samsung"};
58 tflite::gpu::GpuInfo tflite_gpu_info;
59 tflite_gpu_info.opengl_info.renderer_name = "Mali-G72";
60 tflite_gpu_info.opengl_info.major_version = 3;
61 tflite_gpu_info.opengl_info.minor_version = 2;
62 EXPECT_FALSE(list_->Includes(android_info, tflite_gpu_info));
63 }
64
TEST_F(GPUCompatibilityTest,ReturnsDefaultOptions)65 TEST_F(GPUCompatibilityTest, ReturnsDefaultOptions) {
66 ASSERT_TRUE(list_ != nullptr);
67 tflite::acceleration::AndroidInfo android_info;
68 tflite::gpu::GpuInfo tflite_gpu_info;
69 auto default_options = TfLiteGpuDelegateOptionsV2Default();
70 auto best_options = list_->GetBestOptionsFor(android_info, tflite_gpu_info);
71 EXPECT_EQ(best_options.is_precision_loss_allowed,
72 default_options.is_precision_loss_allowed);
73 EXPECT_EQ(best_options.inference_preference,
74 default_options.inference_preference);
75 EXPECT_EQ(best_options.inference_priority1,
76 default_options.inference_priority1);
77 EXPECT_EQ(best_options.inference_priority2,
78 default_options.inference_priority2);
79 EXPECT_EQ(best_options.inference_priority3,
80 default_options.inference_priority3);
81 EXPECT_EQ(best_options.experimental_flags,
82 default_options.experimental_flags);
83 EXPECT_EQ(best_options.max_delegated_partitions,
84 default_options.max_delegated_partitions);
85 }
86
TEST(GPUCompatibility,RecogniseValidCompatibilityListFlatbuffer)87 TEST(GPUCompatibility, RecogniseValidCompatibilityListFlatbuffer) {
88 EXPECT_TRUE(tflite::acceleration::GPUCompatibilityList::IsValidFlatbuffer(
89 g_tflite_acceleration_devicedb_sample_binary,
90 g_tflite_acceleration_devicedb_sample_binary_len));
91 }
92
TEST(GPUCompatibility,RecogniseInvalidCompatibilityListFlatbuffer)93 TEST(GPUCompatibility, RecogniseInvalidCompatibilityListFlatbuffer) {
94 unsigned char invalid_buffer[100];
95 std::fill(invalid_buffer, invalid_buffer + 100, ' ');
96 EXPECT_FALSE(tflite::acceleration::GPUCompatibilityList::IsValidFlatbuffer(
97 invalid_buffer, 100));
98 }
99
TEST(GPUCompatibility,CreationWithInvalidCompatibilityListFlatbuffer)100 TEST(GPUCompatibility, CreationWithInvalidCompatibilityListFlatbuffer) {
101 unsigned char invalid_buffer[10];
102 std::fill(invalid_buffer, invalid_buffer + 10, ' ');
103 std::unique_ptr<tflite::acceleration::GPUCompatibilityList> list =
104 tflite::acceleration::GPUCompatibilityList::Create(invalid_buffer, 10);
105 EXPECT_EQ(list, nullptr);
106 }
107
TEST(GPUCompatibility,CreationWithNullCompatibilityListFlatbuffer)108 TEST(GPUCompatibility, CreationWithNullCompatibilityListFlatbuffer) {
109 std::unique_ptr<tflite::acceleration::GPUCompatibilityList> list =
110 tflite::acceleration::GPUCompatibilityList::Create(nullptr, 0);
111 EXPECT_EQ(list, nullptr);
112 }
113
114 } // namespace
115