1 /* Copyright 2020 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/relu_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/relu.h"
25
26 namespace tflite {
27 namespace gpu {
28
ReLUNoClipNoAlphaTest(TestExecutionEnvironment * env)29 absl::Status ReLUNoClipNoAlphaTest(TestExecutionEnvironment* env) {
30 TensorFloat32 src_tensor;
31 src_tensor.shape = BHWC(1, 2, 1, 2);
32 src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
33
34 ReLUAttributes attr;
35 attr.alpha = 0.0f;
36 attr.clip = 0.0f;
37
38 for (auto precision : env->GetSupportedPrecisions()) {
39 auto data_type = DeduceDataTypeFromPrecision(precision);
40 for (auto storage : env->GetSupportedStorages(data_type)) {
41 const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
42 OperationDef op_def;
43 op_def.precision = precision;
44 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
45 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
46 TensorFloat32 dst_tensor;
47 GPUOperation operation = CreateReLU(op_def, attr);
48 RETURN_IF_ERROR(env->ExecuteGPUOperation(
49 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
50 BHWC(1, 2, 1, 2), &dst_tensor));
51 RETURN_IF_ERROR(
52 PointWiseNear({0.0f, 0.8f, 0.0f, 3.2f}, dst_tensor.data, eps));
53 }
54 }
55 return absl::OkStatus();
56 }
57
ReLUClipTest(TestExecutionEnvironment * env)58 absl::Status ReLUClipTest(TestExecutionEnvironment* env) {
59 TensorFloat32 src_tensor;
60 src_tensor.shape = BHWC(1, 2, 1, 2);
61 src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
62
63 ReLUAttributes attr;
64 attr.alpha = 0.0f;
65 attr.clip = 0.9f;
66
67 for (auto precision : env->GetSupportedPrecisions()) {
68 auto data_type = DeduceDataTypeFromPrecision(precision);
69 for (auto storage : env->GetSupportedStorages(data_type)) {
70 const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
71 OperationDef op_def;
72 op_def.precision = precision;
73 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
74 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
75 TensorFloat32 dst_tensor;
76 GPUOperation operation = CreateReLU(op_def, attr);
77 RETURN_IF_ERROR(env->ExecuteGPUOperation(
78 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
79 BHWC(1, 2, 1, 2), &dst_tensor));
80 RETURN_IF_ERROR(
81 PointWiseNear({0.0f, 0.8f, 0.0f, 0.9f}, dst_tensor.data, eps));
82 }
83 }
84 return absl::OkStatus();
85 }
86
ReLUAlphaTest(TestExecutionEnvironment * env)87 absl::Status ReLUAlphaTest(TestExecutionEnvironment* env) {
88 TensorFloat32 src_tensor;
89 src_tensor.shape = BHWC(1, 2, 1, 2);
90 src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
91
92 ReLUAttributes attr;
93 attr.alpha = 0.5f;
94 attr.clip = 0.0f;
95
96 for (auto precision : env->GetSupportedPrecisions()) {
97 auto data_type = DeduceDataTypeFromPrecision(precision);
98 for (auto storage : env->GetSupportedStorages(data_type)) {
99 const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
100 OperationDef op_def;
101 op_def.precision = precision;
102 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
103 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
104 TensorFloat32 dst_tensor;
105 GPUOperation operation = CreateReLU(op_def, attr);
106 RETURN_IF_ERROR(env->ExecuteGPUOperation(
107 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
108 BHWC(1, 2, 1, 2), &dst_tensor));
109 RETURN_IF_ERROR(
110 PointWiseNear({-0.25f, 0.8f, -0.3f, 3.2f}, dst_tensor.data, eps));
111 }
112 }
113 return absl::OkStatus();
114 }
115
ReLUAlphaClipTest(TestExecutionEnvironment * env)116 absl::Status ReLUAlphaClipTest(TestExecutionEnvironment* env) {
117 TensorFloat32 src_tensor;
118 src_tensor.shape = BHWC(1, 2, 1, 2);
119 src_tensor.data = {-0.5f, 0.8f, -0.6f, 3.2f};
120
121 ReLUAttributes attr;
122 attr.alpha = 0.5f;
123 attr.clip = 0.5f;
124
125 for (auto precision : env->GetSupportedPrecisions()) {
126 auto data_type = DeduceDataTypeFromPrecision(precision);
127 for (auto storage : env->GetSupportedStorages(data_type)) {
128 const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
129 OperationDef op_def;
130 op_def.precision = precision;
131 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
132 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
133 TensorFloat32 dst_tensor;
134 GPUOperation operation = CreateReLU(op_def, attr);
135 RETURN_IF_ERROR(env->ExecuteGPUOperation(
136 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
137 BHWC(1, 2, 1, 2), &dst_tensor));
138 RETURN_IF_ERROR(
139 PointWiseNear({-0.25f, 0.5f, -0.3f, 0.5f}, dst_tensor.data, eps));
140 }
141 }
142 return absl::OkStatus();
143 }
144
145 } // namespace gpu
146 } // namespace tflite
147