xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/tasks/gather_test_util.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
16 #include "tensorflow/lite/delegates/gpu/common/tasks/gather_test_util.h"
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "tensorflow/lite/delegates/gpu/common/operations.h"
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 #include "tensorflow/lite/delegates/gpu/common/task/testing_util.h"
24 #include "tensorflow/lite/delegates/gpu/common/tasks/gather.h"
25 
26 namespace tflite {
27 namespace gpu {
28 namespace {
GatherWidthIntTest(TestExecutionEnvironment * env)29 absl::Status GatherWidthIntTest(TestExecutionEnvironment* env) {
30   TensorFloat32 src_tensor;
31   src_tensor.shape = BHWC(1, 1, 5, 1);
32   src_tensor.data = {half(1.5f), half(2.4f), half(3.3f), half(4.2f),
33                      half(5.1f)};
34   tflite::gpu::Tensor<BHWC, DataType::INT32> src_indices;
35   src_indices.shape = BHWC(1, 1, 1, 9);
36   src_indices.data = {1, 2, 3, 0, 1, 4, 2, 3, 1};
37   GatherAttributes attr;
38   attr.axis = Axis::WIDTH;
39   for (auto precision : env->GetSupportedPrecisions()) {
40     auto data_type = DeduceDataTypeFromPrecision(precision);
41     for (auto storage : env->GetSupportedStorages(data_type)) {
42       OperationDef op_def;
43       op_def.precision = precision;
44       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
45       op_def.src_tensors.push_back({DataType::INT32, storage, Layout::HWC});
46       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
47       TensorDescriptor src_0, src_1, dst;
48       src_0 = op_def.src_tensors[0];
49       src_1 = op_def.src_tensors[1];
50       src_0.UploadData(src_tensor);
51       src_1.UploadData(src_indices);
52       dst.SetBHWDCShape(BHWDC(1, 1, 9, 1, 1));
53       GPUOperation operation = CreateGather(op_def, attr);
54       RETURN_IF_ERROR(env->ExecuteGPUOperation(
55           {&src_0, &src_1}, {&dst},
56           std::make_unique<GPUOperation>(std::move(operation))));
57       TensorFloat32 dst_tensor;
58       dst.DownloadData(&dst_tensor);
59       RETURN_IF_ERROR(PointWiseNear(
60           {half(2.4f), half(3.3f), half(4.2f), half(1.5f), half(2.4f),
61            half(5.1f), half(3.3f), half(4.2f), half(2.4f)},
62           dst_tensor.data, 0.0f));
63     }
64   }
65   return absl::OkStatus();
66 }
67 }  // namespace
68 
GatherWidthTest(TestExecutionEnvironment * env)69 absl::Status GatherWidthTest(TestExecutionEnvironment* env) {
70   TensorFloat32 src_tensor;
71   src_tensor.shape = BHWC(1, 1, 5, 1);
72   src_tensor.data = {half(1.5f), half(2.4f), half(3.3f), half(4.2f),
73                      half(5.1f)};
74   TensorFloat32 src_indices;
75   src_indices.shape = BHWC(1, 1, 1, 9);
76   src_indices.data = {half(1.1f), half(2.1f), half(3.1f),
77                       half(0.1f), half(1.1f), half(4.1f),
78                       half(2.1f), half(3.1f), half(1.1f)};
79   GatherAttributes attr;
80   attr.axis = Axis::WIDTH;
81   for (auto precision : env->GetSupportedPrecisions()) {
82     auto data_type = DeduceDataTypeFromPrecision(precision);
83     for (auto storage : env->GetSupportedStorages(data_type)) {
84       OperationDef op_def;
85       op_def.precision = precision;
86       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
87       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
88       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
89       TensorFloat32 dst_tensor;
90       GPUOperation operation = CreateGather(op_def, attr);
91       RETURN_IF_ERROR(env->ExecuteGPUOperation(
92           {src_tensor, src_indices},
93           std::make_unique<GPUOperation>(std::move(operation)),
94           BHWC(1, 1, 9, 1), &dst_tensor));
95       RETURN_IF_ERROR(PointWiseNear(
96           {half(2.4f), half(3.3f), half(4.2f), half(1.5f), half(2.4f),
97            half(5.1f), half(3.3f), half(4.2f), half(2.4f)},
98           dst_tensor.data, 0.0f));
99     }
100   }
101 
102   RETURN_IF_ERROR(GatherWidthIntTest(env));
103   return absl::OkStatus();
104 }
105 
106 }  // namespace gpu
107 }  // namespace tflite
108