xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/tasks/conv_constants_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/conv_constants_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/conv_constants.h"
25 
26 namespace tflite {
27 namespace gpu {
28 
ConvConstantsSimpleWeightsTest(TestExecutionEnvironment * env)29 absl::Status ConvConstantsSimpleWeightsTest(TestExecutionEnvironment* env) {
30   TensorFloat32 src_tensor;
31   src_tensor.shape = BHWC(1, 2, 2, 2);
32   src_tensor.data = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
33 
34   Convolution2DAttributes attr;
35   attr.padding.prepended = HW(0, 0);
36   attr.padding.appended = HW(1, 1);
37   attr.strides = HW(1, 1);
38   attr.dilations = HW(1, 1);
39   attr.weights.shape = OHWI(1, 2, 2, 2);
40   attr.weights.data = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
41   attr.bias.shape = Linear(1);
42   attr.bias.data = {0.0f};
43 
44   for (auto precision : env->GetSupportedPrecisions()) {
45     auto data_type = DeduceDataTypeFromPrecision(precision);
46     for (auto storage : env->GetSupportedStorages(data_type)) {
47       const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
48       OperationDef op_def;
49       op_def.precision = precision;
50       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
51       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
52       TensorFloat32 dst_tensor;
53       GPUOperation operation =
54           CreateConvConstants(env->GetGpuInfo(), op_def, attr);
55       RETURN_IF_ERROR(env->ExecuteGPUOperation(
56           src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
57           BHWC(1, 2, 2, 1), &dst_tensor));
58       RETURN_IF_ERROR(
59           PointWiseNear({28.0f, 18.0f, 22.0f, 13.0f}, dst_tensor.data, eps));
60     }
61   }
62   return absl::OkStatus();
63 }
64 
ConvConstantsTest(TestExecutionEnvironment * env)65 absl::Status ConvConstantsTest(TestExecutionEnvironment* env) {
66   TensorFloat32 src_tensor;
67   src_tensor.shape = BHWC(1, 2, 2, 2);
68   src_tensor.data = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
69 
70   Convolution2DAttributes attr;
71   attr.padding.prepended = HW(0, 0);
72   attr.padding.appended = HW(1, 1);
73   attr.strides = HW(1, 1);
74   attr.dilations = HW(1, 1);
75   attr.weights.shape = OHWI(2, 2, 2, 2);
76   attr.weights.data = {1.0f, 2.0f,  3.0f,  4.0f,  5.0f,  6.0f,  7.0f,  8.0f,
77                        9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f};
78   attr.bias.shape = Linear(2);
79   attr.bias.data = {0.5f, -0.5f};
80 
81   for (auto precision : env->GetSupportedPrecisions()) {
82     auto data_type = DeduceDataTypeFromPrecision(precision);
83     for (auto storage : env->GetSupportedStorages(data_type)) {
84       const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
85       OperationDef op_def;
86       op_def.precision = precision;
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 =
91           CreateConvConstants(env->GetGpuInfo(), op_def, attr);
92       RETURN_IF_ERROR(env->ExecuteGPUOperation(
93           src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
94           BHWC(1, 2, 2, 2), &dst_tensor));
95       RETURN_IF_ERROR(PointWiseNear(
96           {168.5f, 391.5f, 80.5f, 223.5f, 60.5f, 235.5f, 20.5f, 123.5f},
97           dst_tensor.data, eps));
98     }
99   }
100   return absl::OkStatus();
101 }
102 
103 }  // namespace gpu
104 }  // namespace tflite
105