• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/reshape_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/reshape.h"
25 #include "tensorflow/lite/delegates/gpu/common/tasks/reshapex4.h"
26 
27 namespace tflite {
28 namespace gpu {
29 
ReshapeTest(TestExecutionEnvironment * env)30 absl::Status ReshapeTest(TestExecutionEnvironment* env) {
31   TensorFloat32 src_tensor;
32   src_tensor.shape = BHWC(1, 2, 1, 3);
33   src_tensor.data = {half(0.5f), half(-1.1f), half(-2.2f),
34                      half(3.1f), half(1.2f),  half(2.9f)};
35 
36   for (auto precision : env->GetSupportedPrecisions()) {
37     auto data_type = DeduceDataTypeFromPrecision(precision);
38     for (auto storage : env->GetSupportedStorages(data_type)) {
39       OperationDef op_def;
40       op_def.precision = precision;
41       op_def.src_tensors.push_back({data_type, storage, Layout::HWC});
42       op_def.dst_tensors.push_back({data_type, storage, Layout::HWC});
43       TensorFloat32 dst_tensor;
44       GPUOperation operation = CreateReshape(op_def);
45       RETURN_IF_ERROR(env->ExecuteGPUOperation(
46           src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
47           BHWC(1, 3, 1, 2), &dst_tensor));
48       RETURN_IF_ERROR(PointWiseNear({half(0.5f), half(-1.1f), half(-2.2f),
49                                      half(3.1f), half(1.2f), half(2.9f)},
50                                     dst_tensor.data, 0.0));
51     }
52   }
53   return absl::OkStatus();
54 }
55 
Reshapex4Test(TestExecutionEnvironment * env)56 absl::Status Reshapex4Test(TestExecutionEnvironment* env) {
57   TensorFloat32 src_tensor;
58   src_tensor.shape = BHWC(1, 1, 1, 8);
59   src_tensor.data = {half(0.5f), half(-1.1f), half(-2.2f), half(3.1f),
60                      half(1.2f), half(2.9f),  half(4.2f),  half(-1.9f)};
61 
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 = CreateReshapex4(op_def);
71       RETURN_IF_ERROR(env->ExecuteGPUOperation(
72           src_tensor, std::make_unique<GPUOperation>(std::move(operation)),
73           BHWC(1, 1, 2, 4), &dst_tensor));
74       RETURN_IF_ERROR(
75           PointWiseNear({half(0.5f), half(-1.1f), half(-2.2f), half(3.1f),
76                          half(1.2f), half(2.9f), half(4.2f), half(-1.9f)},
77                         dst_tensor.data, 0.0f));
78     }
79   }
80   return absl::OkStatus();
81 }
82 
83 }  // namespace gpu
84 }  // namespace tflite
85