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/space_to_depth_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/space_to_depth.h"
25
26 namespace tflite {
27 namespace gpu {
28
SpaceToDepthTensorShape1x2x2x1BlockSize2Test(TestExecutionEnvironment * env)29 absl::Status SpaceToDepthTensorShape1x2x2x1BlockSize2Test(
30 TestExecutionEnvironment* env) {
31 TensorFloat32 src_tensor;
32 src_tensor.shape = BHWC(1, 2, 2, 1);
33 src_tensor.data = {half(1.0f), half(2.0f), half(3.0f), half(4.0f)};
34 const SpaceToDepthAttributes attr = {.block_size = 2};
35 for (auto precision : env->GetSupportedPrecisions()) {
36 auto data_type = DeduceDataTypeFromPrecision(precision);
37 for (auto storage : env->GetSupportedStorages(data_type)) {
38 OperationDef op_def;
39 op_def.precision = precision;
40 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
41 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
42 TensorFloat32 dst_tensor;
43 GPUOperation operation = CreateSpaceToDepth(op_def, attr);
44 RETURN_IF_ERROR(env->ExecuteGPUOperation(
45 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
46 BHWC(1, 1, 1, 4), &dst_tensor));
47 RETURN_IF_ERROR(
48 PointWiseNear({half(1.0f), half(2.0f), half(3.0f), half(4.0f)},
49 dst_tensor.data, 0.0f));
50 }
51 }
52 return absl::OkStatus();
53 }
54
SpaceToDepthTensorShape1x2x2x2BlockSize2Test(TestExecutionEnvironment * env)55 absl::Status SpaceToDepthTensorShape1x2x2x2BlockSize2Test(
56 TestExecutionEnvironment* env) {
57 TensorFloat32 src_tensor;
58 src_tensor.shape = BHWC(1, 2, 2, 2);
59 src_tensor.data = {half(1.4f), half(2.3f), half(3.2f), half(4.1f),
60 half(5.4f), half(6.3f), half(7.2f), half(8.1f)};
61 const SpaceToDepthAttributes attr = {.block_size = 2};
62 for (auto precision : env->GetSupportedPrecisions()) {
63 auto data_type = DeduceDataTypeFromPrecision(precision);
64 for (auto storage : env->GetSupportedStorages(data_type)) {
65 OperationDef op_def;
66 op_def.precision = precision;
67 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
68 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
69 TensorFloat32 dst_tensor;
70 GPUOperation operation = CreateSpaceToDepth(op_def, attr);
71 RETURN_IF_ERROR(env->ExecuteGPUOperation(
72 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
73 BHWC(1, 1, 1, 8), &dst_tensor));
74 RETURN_IF_ERROR(
75 PointWiseNear({half(1.4f), half(2.3f), half(3.2f), half(4.1f),
76 half(5.4f), half(6.3f), half(7.2f), half(8.1f)},
77 dst_tensor.data, 0.0f));
78 }
79 }
80 return absl::OkStatus();
81 }
82
SpaceToDepthTensorShape1x2x2x3BlockSize2Test(TestExecutionEnvironment * env)83 absl::Status SpaceToDepthTensorShape1x2x2x3BlockSize2Test(
84 TestExecutionEnvironment* env) {
85 TensorFloat32 src_tensor;
86 src_tensor.shape = BHWC(1, 2, 2, 3);
87 src_tensor.data = {half(1.0f), half(2.0f), half(3.0f), half(4.0f),
88 half(5.0f), half(6.0f), half(7.0f), half(8.0f),
89 half(9.0f), half(10.0f), half(11.0f), half(12.0f)};
90 const SpaceToDepthAttributes attr = {.block_size = 2};
91 for (auto precision : env->GetSupportedPrecisions()) {
92 auto data_type = DeduceDataTypeFromPrecision(precision);
93 for (auto storage : env->GetSupportedStorages(data_type)) {
94 OperationDef op_def;
95 op_def.precision = precision;
96 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
97 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
98 TensorFloat32 dst_tensor;
99 GPUOperation operation = CreateSpaceToDepth(op_def, attr);
100 RETURN_IF_ERROR(env->ExecuteGPUOperation(
101 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
102 BHWC(1, 1, 1, 12), &dst_tensor));
103 RETURN_IF_ERROR(
104 PointWiseNear({half(1.0f), half(2.0f), half(3.0f), half(4.0f),
105 half(5.0f), half(6.0f), half(7.0f), half(8.0f),
106 half(9.0f), half(10.0f), half(11.0f), half(12.0f)},
107 dst_tensor.data, 0.0f));
108 }
109 }
110 return absl::OkStatus();
111 }
112
SpaceToDepthTensorShape1x4x4x1BlockSize2Test(TestExecutionEnvironment * env)113 absl::Status SpaceToDepthTensorShape1x4x4x1BlockSize2Test(
114 TestExecutionEnvironment* env) {
115 TensorFloat32 src_tensor;
116 src_tensor.shape = BHWC(1, 4, 4, 1);
117 src_tensor.data = {half(1.0f), half(2.0f), half(5.0f), half(6.0f),
118 half(3.0f), half(4.0f), half(7.0f), half(8.0f),
119 half(9.0f), half(10.0f), half(13.0f), half(14.0f),
120 half(11.0f), half(12.0f), half(15.0f), half(16.0f)};
121 const SpaceToDepthAttributes attr = {.block_size = 2};
122 for (auto precision : env->GetSupportedPrecisions()) {
123 auto data_type = DeduceDataTypeFromPrecision(precision);
124 for (auto storage : env->GetSupportedStorages(data_type)) {
125 OperationDef op_def;
126 op_def.precision = precision;
127 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
128 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
129 TensorFloat32 dst_tensor;
130 GPUOperation operation = CreateSpaceToDepth(op_def, attr);
131 RETURN_IF_ERROR(env->ExecuteGPUOperation(
132 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
133 BHWC(1, 2, 2, 4), &dst_tensor));
134 RETURN_IF_ERROR(
135 PointWiseNear({half(1.0f), half(2.0f), half(3.0f), half(4.0f),
136 half(5.0f), half(6.0f), half(7.0f), half(8.0f),
137 half(9.0f), half(10.0f), half(11.0f), half(12.0f),
138 half(13.0f), half(14.0f), half(15.0f), half(16.0f)},
139 dst_tensor.data, 0.0f));
140 }
141 }
142 return absl::OkStatus();
143 }
144
145 } // namespace gpu
146 } // namespace tflite
147