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/delegates/gpu/cl/cl_arguments.h"
16
17 #include <cstdint>
18 #include <string>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "absl/strings/match.h"
23 #include "tensorflow/lite/delegates/gpu/cl/buffer.h"
24 #include "tensorflow/lite/delegates/gpu/cl/gpu_object.h"
25 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h"
26
27 namespace tflite {
28 namespace gpu {
29 namespace cl {
TEST(CLArgumentsTest,TestSelectorResolve)30 TEST(CLArgumentsTest, TestSelectorResolve) {
31 BufferDescriptor desc;
32 desc.element_type = DataType::FLOAT32;
33 desc.element_size = 4;
34 desc.memory_type = MemoryType::GLOBAL;
35
36 Arguments args;
37 args.AddObjectRef("weights", AccessType::READ,
38 std::make_unique<BufferDescriptor>(std::move(desc)));
39 std::string sample_code = R"(
40 __kernel void main_function($0) {
41 if (a < 3) {
42 value = args.weights.Read(id);
43 }
44 })";
45
46 CLArguments cl_args;
47 GpuInfo gpu_info;
48 ASSERT_OK(cl_args.Init(gpu_info, {}, nullptr, &args, &sample_code));
49 EXPECT_TRUE(absl::StrContains(sample_code, "value = weights_buffer[id];"));
50 EXPECT_TRUE(
51 absl::StrContains(sample_code, "__global float4* weights_buffer"));
52 }
53
TEST(CLArgumentsTest,TestNoSelector)54 TEST(CLArgumentsTest, TestNoSelector) {
55 BufferDescriptor desc;
56 desc.element_type = DataType::FLOAT32;
57 desc.element_size = 4;
58 desc.memory_type = MemoryType::GLOBAL;
59
60 Arguments args;
61 args.AddObjectRef("weights", AccessType::READ,
62 std::make_unique<BufferDescriptor>(std::move(desc)));
63 std::string sample_code = R"(
64 if (a < 3) {
65 value = args.weights.UnknownSelector(id);
66 }
67 )";
68 CLArguments cl_args;
69 GpuInfo gpu_info;
70 EXPECT_FALSE(cl_args.Init(gpu_info, {}, nullptr, &args, &sample_code).ok());
71 }
72
73 } // namespace cl
74 } // namespace gpu
75 } // namespace tflite
76