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/prelu_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/prelu.h"
25
26 namespace tflite {
27 namespace gpu {
28
PReLUAlphaTest(TestExecutionEnvironment * env)29 absl::Status PReLUAlphaTest(TestExecutionEnvironment* env) {
30 TensorFloat32 src_tensor;
31 src_tensor.shape = BHWC(1, 2, 1, 2);
32 src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
33
34 PReLUAttributes attr;
35 ::tflite::gpu::Tensor<Linear, DataType::FLOAT32> parameters;
36 parameters.shape = Linear(2);
37 parameters.data = {0.5f, -2.0f};
38 attr.alpha = parameters;
39
40 for (auto precision : env->GetSupportedPrecisions()) {
41 auto data_type = DeduceDataTypeFromPrecision(precision);
42 for (auto storage : env->GetSupportedStorages(data_type)) {
43 const float eps = precision == CalculationsPrecision::F32 ? 1e-6f : 1e-3f;
44 OperationDef op_def;
45 op_def.precision = precision;
46 op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
47 op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
48 TensorFloat32 dst_tensor;
49 GPUOperation operation = CreatePReLU(env->GetGpuInfo(), op_def, attr);
50 RETURN_IF_ERROR(env->ExecuteGPUOperation(
51 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
52 BHWC(1, 2, 1, 2), &dst_tensor));
53 RETURN_IF_ERROR(
54 PointWiseNear({0.0f, 2.0f, -1.0f, 3.0f}, dst_tensor.data, eps));
55 }
56 }
57 return absl::OkStatus();
58 }
59
PReLUHWCAlphaTest(TestExecutionEnvironment * env)60 absl::Status PReLUHWCAlphaTest(TestExecutionEnvironment* env) {
61 TensorFloat32 src_tensor;
62 src_tensor.shape = BHWC(1, 2, 1, 2);
63 src_tensor.data = {0.0f, -1.0f, -2.0f, 3.0f};
64
65 PReLUAttributes attr;
66 ::tflite::gpu::Tensor<HWC, DataType::FLOAT32> hwc_tensor;
67 hwc_tensor.shape = HWC(2, 1, 2);
68 hwc_tensor.data = {0.5f, -2.0f, 0.7f, 4.7f};
69 attr.alpha = hwc_tensor;
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-6f : 1e-3f;
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 GPUOperation operation = CreatePReLU(env->GetGpuInfo(), op_def, attr);
81 RETURN_IF_ERROR(env->ExecuteGPUOperation(
82 src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
83 BHWC(1, 2, 1, 2), &dst_tensor));
84 RETURN_IF_ERROR(
85 PointWiseNear({0.0f, 2.0f, -1.4f, 3.0f}, dst_tensor.data, eps));
86 }
87 }
88 return absl::OkStatus();
89 }
90
91 } // namespace gpu
92 } // namespace tflite
93