1 /* Copyright 2019 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
16 #include "tensorflow/lite/delegates/gpu/cl/buffer.h"
17
18 #include <vector>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "tensorflow/lite/delegates/gpu/cl/cl_test.h"
23 #include "tensorflow/lite/delegates/gpu/common/status.h"
24
25 using ::testing::FloatNear;
26 using ::testing::Pointwise;
27
28 namespace tflite {
29 namespace gpu {
30 namespace cl {
31 namespace {
32
TEST_F(OpenCLTest,BufferTestFloat)33 TEST_F(OpenCLTest, BufferTestFloat) {
34 const std::vector<float> data = {1.0, 2.0, 3.0, -4.0, 5.1};
35 Buffer buffer;
36 ASSERT_OK(CreateReadWriteBuffer(sizeof(float) * 5, &env_.context(), &buffer));
37 ASSERT_OK(buffer.WriteData(env_.queue(),
38 absl::MakeConstSpan(data.data(), data.size())));
39 std::vector<float> gpu_data;
40 ASSERT_OK(buffer.ReadData<float>(env_.queue(), &gpu_data));
41
42 EXPECT_THAT(gpu_data, Pointwise(FloatNear(0.0f), data));
43 }
44
TEST_F(OpenCLTest,BufferTestHalf)45 TEST_F(OpenCLTest, BufferTestHalf) {
46 const std::vector<half> data = {half(1.4), half(2.1), half(2.2)};
47 Buffer buffer;
48 ASSERT_OK(CreateReadWriteBuffer(sizeof(half) * 3, &env_.context(), &buffer));
49 ASSERT_OK(buffer.WriteData(env_.queue(),
50 absl::MakeConstSpan(data.data(), data.size())));
51 std::vector<half> gpu_data;
52 ASSERT_OK(buffer.ReadData<half>(env_.queue(), &gpu_data));
53
54 EXPECT_THAT(gpu_data, Pointwise(FloatNear(0.0f), data));
55 }
56
57 } // namespace
58 } // namespace cl
59 } // namespace gpu
60 } // namespace tflite
61