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/convolution_transposed_3x3_thin_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/convolution_transposed_3x3_thin.h"
25
26 namespace tflite {
27 namespace gpu {
28
ConvolutionTransposed3x3ThinSimpleWeightsTest(TestExecutionEnvironment * env)29 absl::Status ConvolutionTransposed3x3ThinSimpleWeightsTest(
30 TestExecutionEnvironment* env) {
31 TensorFloat32 src_tensor;
32 src_tensor.shape = BHWC(1, 2, 2, 1);
33 src_tensor.data = {0.0f, 1.0f, 2.0f, 3.0f};
34
35 ConvolutionTransposedAttributes attr;
36 attr.padding.prepended = HW(1, 1);
37 attr.padding.appended = HW(1, 1);
38 attr.stride = HW(2, 2);
39 attr.weights.shape = OHWI(1, 3, 3, 1);
40 attr.weights.data = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
41 attr.bias.shape = Linear(2);
42 attr.bias.data = {0.0f, 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 ConvolutionTransposed3x3Thin operation =
54 CreateConvolutionTransposed3x3Thin(env->GetGpuInfo(), op_def, attr);
55 RETURN_IF_ERROR(env->ExecuteGPUOperation(
56 src_tensor,
57 std::make_unique<ConvolutionTransposed3x3Thin>(std::move(operation)),
58 BHWC(1, 4, 4, 1), &dst_tensor));
59 RETURN_IF_ERROR(
60 PointWiseNear({0.0f, 1.0f, 1.0f, 1.0f, 2.0f, 6.0f, 4.0f, 4.0f, 2.0f,
61 5.0f, 3.0f, 3.0f, 2.0f, 5.0f, 3.0f, 3.0f},
62 dst_tensor.data, eps));
63 }
64 }
65 return absl::OkStatus();
66 }
67
ConvolutionTransposed3x3ThinTest(TestExecutionEnvironment * env)68 absl::Status ConvolutionTransposed3x3ThinTest(TestExecutionEnvironment* env) {
69 TensorFloat32 src_tensor;
70 src_tensor.shape = BHWC(1, 2, 2, 1);
71 src_tensor.data = {0.0f, 1.0f, 2.0f, 3.0f};
72
73 ConvolutionTransposedAttributes attr;
74 attr.padding.prepended = HW(1, 1);
75 attr.padding.appended = HW(1, 1);
76 attr.stride = HW(2, 2);
77 attr.weights.shape = OHWI(1, 3, 3, 1);
78 attr.weights.data = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f};
79 attr.bias.shape = Linear(1);
80 attr.bias.data = {0.5f};
81
82 for (auto precision : env->GetSupportedPrecisions()) {
83 auto data_type = DeduceDataTypeFromPrecision(precision);
84 for (auto storage : env->GetSupportedStorages(data_type)) {
85 const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
86 OperationDef op_def;
87 op_def.precision = precision;
88 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
89 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
90 TensorFloat32 dst_tensor;
91 ConvolutionTransposed3x3Thin operation =
92 CreateConvolutionTransposed3x3Thin(env->GetGpuInfo(), op_def, attr);
93 RETURN_IF_ERROR(env->ExecuteGPUOperation(
94 src_tensor,
95 std::make_unique<ConvolutionTransposed3x3Thin>(std::move(operation)),
96 BHWC(1, 4, 4, 1), &dst_tensor));
97 RETURN_IF_ERROR(PointWiseNear(
98 {0.5f, 4.5f, 5.5f, 6.5f, 4.5f, 16.5f, 14.5f, 18.5f, 10.5f, 24.5f,
99 15.5f, 18.5f, 16.5f, 39.5f, 24.5f, 27.5f},
100 dst_tensor.data, eps));
101 }
102 }
103 return absl::OkStatus();
104 }
105
106 } // namespace gpu
107 } // namespace tflite
108