xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/tasks/resize_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/resize_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/resize.h"
25 
26 namespace tflite {
27 namespace gpu {
28 
ResizeBilinearAlignedTest(TestExecutionEnvironment * env)29 absl::Status ResizeBilinearAlignedTest(TestExecutionEnvironment* env) {
30   TensorFloat32 src_tensor;
31   src_tensor.shape = BHWC(1, 2, 3, 1);
32   src_tensor.data = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
33 
34   Resize2DAttributes attr;
35   attr.type = SamplingType::BILINEAR;
36   attr.new_shape = HW(4, 4);
37   attr.align_corners = true;
38 
39   for (auto precision : env->GetSupportedPrecisions()) {
40     auto data_type = DeduceDataTypeFromPrecision(precision);
41     for (auto storage : env->GetSupportedStorages(data_type)) {
42       const float eps = precision == CalculationsPrecision::F32 ? 1e-5f : 1e-2f;
43       OperationDef op_def;
44       op_def.precision = precision;
45       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
46       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
47       TensorFloat32 dst_tensor;
48       Resize operation = CreateResize(op_def, attr);
49       RETURN_IF_ERROR(env->ExecuteGPUOperation(
50           src_tensor, std::make_unique<Resize>(std::move(operation)),
51           BHWC(1, 4, 4, 1), &dst_tensor));
52       RETURN_IF_ERROR(PointWiseNear(
53           {0.0f, 0.666667f, 1.33333f, 2.0f, 1.0f, 1.66667f, 2.33333f, 3.0f,
54            2.0f, 2.66667f, 3.33333f, 4.0f, 3.0f, 3.66667f, 4.33333f, 5.0f},
55           dst_tensor.data, eps));
56     }
57   }
58   return absl::OkStatus();
59 }
60 
ResizeBilinearNonAlignedTest(TestExecutionEnvironment * env)61 absl::Status ResizeBilinearNonAlignedTest(TestExecutionEnvironment* env) {
62   TensorFloat32 src_tensor;
63   src_tensor.shape = BHWC(1, 2, 3, 1);
64   src_tensor.data = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
65 
66   Resize2DAttributes attr;
67   attr.type = SamplingType::BILINEAR;
68   attr.new_shape = HW(4, 4);
69   attr.align_corners = false;
70 
71   for (auto precision : env->GetSupportedPrecisions()) {
72     auto data_type = DeduceDataTypeFromPrecision(precision);
73     for (auto storage : env->GetSupportedStorages(data_type)) {
74       const float eps = precision == CalculationsPrecision::F32 ? 1e-5f : 1e-2f;
75       OperationDef op_def;
76       op_def.precision = precision;
77       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
78       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
79       TensorFloat32 dst_tensor;
80       Resize operation = CreateResize(op_def, attr);
81       RETURN_IF_ERROR(env->ExecuteGPUOperation(
82           src_tensor, std::make_unique<Resize>(std::move(operation)),
83           BHWC(1, 4, 4, 1), &dst_tensor));
84       RETURN_IF_ERROR(
85           PointWiseNear({0.0f, 0.75f, 1.5f, 2.0f, 1.5f, 2.25f, 3.0f, 3.5f, 3.0f,
86                          3.75f, 4.5f, 5.0f, 3.0f, 3.75f, 4.5f, 5.0f},
87                         dst_tensor.data, eps));
88     }
89   }
90   return absl::OkStatus();
91 }
92 
ResizeBilinearWithoutHalfPixelTest(TestExecutionEnvironment * env)93 absl::Status ResizeBilinearWithoutHalfPixelTest(TestExecutionEnvironment* env) {
94   TensorFloat32 src_tensor;
95   src_tensor.shape = BHWC(1, 2, 2, 1);
96   src_tensor.data = {1.0f, 2.0f, 3.0f, 4.0f};
97 
98   Resize2DAttributes attr;
99   attr.type = SamplingType::BILINEAR;
100   attr.new_shape = HW(3, 3);
101   attr.align_corners = false;
102   attr.half_pixel_centers = false;
103 
104   for (auto precision : env->GetSupportedPrecisions()) {
105     auto data_type = DeduceDataTypeFromPrecision(precision);
106     for (auto storage : env->GetSupportedStorages(data_type)) {
107       const float eps = precision == CalculationsPrecision::F32 ? 1e-5f : 1e-2f;
108       OperationDef op_def;
109       op_def.precision = precision;
110       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
111       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
112       TensorFloat32 dst_tensor;
113       Resize operation = CreateResize(op_def, attr);
114       RETURN_IF_ERROR(env->ExecuteGPUOperation(
115           src_tensor, std::make_unique<Resize>(std::move(operation)),
116           BHWC(1, 3, 3, 1), &dst_tensor));
117       RETURN_IF_ERROR(PointWiseNear({1.0f, 1.666666f, 2.0f, 2.333333f, 3.0f,
118                                      3.333333f, 3.0f, 3.666666f, 4.0f},
119                                     dst_tensor.data, eps));
120     }
121   }
122   return absl::OkStatus();
123 }
124 
ResizeBilinearWithHalfPixelTest(TestExecutionEnvironment * env)125 absl::Status ResizeBilinearWithHalfPixelTest(TestExecutionEnvironment* env) {
126   TensorFloat32 src_tensor;
127   src_tensor.shape = BHWC(1, 2, 2, 1);
128   src_tensor.data = {1.0f, 2.0f, 3.0f, 4.0f};
129 
130   Resize2DAttributes attr;
131   attr.type = SamplingType::BILINEAR;
132   attr.new_shape = HW(3, 3);
133   attr.align_corners = false;
134   attr.half_pixel_centers = true;
135 
136   for (auto precision : env->GetSupportedPrecisions()) {
137     auto data_type = DeduceDataTypeFromPrecision(precision);
138     for (auto storage : env->GetSupportedStorages(data_type)) {
139       const float eps = precision == CalculationsPrecision::F32 ? 1e-5f : 1e-2f;
140       OperationDef op_def;
141       op_def.precision = precision;
142       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
143       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
144       TensorFloat32 dst_tensor;
145       Resize operation = CreateResize(op_def, attr);
146       RETURN_IF_ERROR(env->ExecuteGPUOperation(
147           src_tensor, std::make_unique<Resize>(std::move(operation)),
148           BHWC(1, 3, 3, 1), &dst_tensor));
149       RETURN_IF_ERROR(
150           PointWiseNear({1.0f, 1.5f, 2.0f, 2.0f, 2.5f, 3.0f, 3.0f, 3.5f, 4.0f},
151                         dst_tensor.data, eps));
152     }
153   }
154   return absl::OkStatus();
155 }
156 
ResizeNearestTest(TestExecutionEnvironment * env)157 absl::Status ResizeNearestTest(TestExecutionEnvironment* env) {
158   TensorFloat32 src_tensor;
159   src_tensor.shape = BHWC(1, 1, 2, 1);
160   src_tensor.data = {1.0f, 2.0f};
161 
162   Resize2DAttributes attr;
163   attr.align_corners = false;
164   attr.half_pixel_centers = false;
165   attr.new_shape = HW(2, 4);
166   attr.type = SamplingType::NEAREST;
167 
168   for (auto precision : env->GetSupportedPrecisions()) {
169     auto data_type = DeduceDataTypeFromPrecision(precision);
170     for (auto storage : env->GetSupportedStorages(data_type)) {
171       const float eps = precision == CalculationsPrecision::F32 ? 1e-5f : 1e-2f;
172       OperationDef op_def;
173       op_def.precision = precision;
174       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
175       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
176       TensorFloat32 dst_tensor;
177       Resize operation = CreateResize(op_def, attr);
178       RETURN_IF_ERROR(env->ExecuteGPUOperation(
179           src_tensor, std::make_unique<Resize>(std::move(operation)),
180           BHWC(1, 2, 4, 1), &dst_tensor));
181       RETURN_IF_ERROR(
182           PointWiseNear({1.0f, 1.0f, 2.0f, 2.0f, 1.0f, 1.0f, 2.0f, 2.0f},
183                         dst_tensor.data, eps));
184     }
185   }
186   return absl::OkStatus();
187 }
188 
ResizeNearestAlignCornersTest(TestExecutionEnvironment * env)189 absl::Status ResizeNearestAlignCornersTest(TestExecutionEnvironment* env) {
190   TensorFloat32 src_tensor;
191   src_tensor.shape = BHWC(1, 2, 2, 1);
192   src_tensor.data = {3.0f, 6.0f, 9.0f, 12.0f};
193 
194   Resize2DAttributes attr;
195   attr.align_corners = true;
196   attr.half_pixel_centers = false;
197   attr.new_shape = HW(3, 3);
198   attr.type = SamplingType::NEAREST;
199 
200   for (auto precision : env->GetSupportedPrecisions()) {
201     auto data_type = DeduceDataTypeFromPrecision(precision);
202     for (auto storage : env->GetSupportedStorages(data_type)) {
203       const float eps = precision == CalculationsPrecision::F32 ? 1e-5f : 1e-2f;
204       OperationDef op_def;
205       op_def.precision = precision;
206       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
207       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
208       TensorFloat32 dst_tensor;
209       Resize operation = CreateResize(op_def, attr);
210       RETURN_IF_ERROR(env->ExecuteGPUOperation(
211           src_tensor, std::make_unique<Resize>(std::move(operation)),
212           BHWC(1, 3, 3, 1), &dst_tensor));
213       RETURN_IF_ERROR(PointWiseNear(
214           {3.0f, 6.0f, 6.0f, 9.0f, 12.0f, 12.0f, 9.0f, 12.0f, 12.0f},
215           dst_tensor.data, eps));
216     }
217   }
218   return absl::OkStatus();
219 }
220 
ResizeNearestHalfPixelCentersTest(TestExecutionEnvironment * env)221 absl::Status ResizeNearestHalfPixelCentersTest(TestExecutionEnvironment* env) {
222   TensorFloat32 src_tensor;
223   src_tensor.shape = BHWC(1, 2, 2, 1);
224   src_tensor.data = {3.0f, 6.0f, 9.0f, 12.0f};
225 
226   Resize2DAttributes attr;
227   attr.align_corners = false;
228   attr.half_pixel_centers = true;
229   attr.new_shape = HW(3, 3);
230   attr.type = SamplingType::NEAREST;
231 
232   for (auto precision : env->GetSupportedPrecisions()) {
233     auto data_type = DeduceDataTypeFromPrecision(precision);
234     for (auto storage : env->GetSupportedStorages(data_type)) {
235       const float eps = precision == CalculationsPrecision::F32 ? 1e-5f : 1e-2f;
236       OperationDef op_def;
237       op_def.precision = precision;
238       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
239       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
240       TensorFloat32 dst_tensor;
241       Resize operation = CreateResize(op_def, attr);
242       RETURN_IF_ERROR(env->ExecuteGPUOperation(
243           src_tensor, std::make_unique<Resize>(std::move(operation)),
244           BHWC(1, 3, 3, 1), &dst_tensor));
245       RETURN_IF_ERROR(PointWiseNear(
246           {3.0f, 6.0f, 6.0f, 9.0f, 12.0f, 12.0f, 9.0f, 12.0f, 12.0f},
247           dst_tensor.data, eps));
248     }
249   }
250   return absl::OkStatus();
251 }
252 
253 }  // namespace gpu
254 }  // namespace tflite
255