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/transformations/global_pooling_to_reduce_op.h"
17
18 #include <memory>
19 #include <string>
20 #include <vector>
21
22 #include <gtest/gtest.h>
23 #include "absl/status/status.h"
24 #include "absl/types/any.h"
25 #include "tensorflow/lite/delegates/gpu/common/model.h"
26 #include "tensorflow/lite/delegates/gpu/common/model_transformer.h"
27 #include "tensorflow/lite/delegates/gpu/common/operations.h"
28 #include "tensorflow/lite/delegates/gpu/common/shape.h"
29 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
30
31 namespace tflite {
32 namespace gpu {
33 namespace {
34
TEST(MakeMeanFromGlobalAveragePooling,Smoke)35 TEST(MakeMeanFromGlobalAveragePooling, Smoke) {
36 GraphFloat32 graph;
37 auto input = graph.NewValue();
38 input->tensor.shape = BHWC(1, 4, 4, 8);
39
40 Pooling2DAttributes attr;
41 attr.padding.prepended = tflite::gpu::HW(0, 0);
42 attr.padding.appended = tflite::gpu::HW(0, 0);
43 attr.strides = tflite::gpu::HW(4, 4);
44 attr.kernel = tflite::gpu::HW(4, 4);
45 attr.type = tflite::gpu::PoolingType::AVERAGE;
46 attr.output_indices = false;
47
48 auto pool_node = graph.NewNode();
49 pool_node->operation.type = ToString(OperationType::POOLING_2D);
50 pool_node->operation.attributes = attr;
51
52 ASSERT_TRUE(graph.AddConsumer(pool_node->id, input->id).ok());
53
54 Value* output = nullptr;
55 ASSERT_TRUE(AddOutput(&graph, pool_node, &output).ok());
56 output->tensor.shape = BHWC(1, 1, 1, 8);
57
58 ASSERT_EQ(1, graph.nodes().size());
59 ASSERT_EQ(2, graph.values().size());
60
61 auto transformation = NewGlobalPoolingToReduceOp();
62 ModelTransformer transformer(&graph);
63 transformer.Apply("global_average_pooling_to_mean", transformation.get());
64
65 ASSERT_EQ(1, graph.nodes().size());
66 ASSERT_EQ(2, graph.values().size());
67 ASSERT_EQ(ToString(OperationType::MEAN), graph.nodes()[0]->operation.type);
68 }
69
70 } // namespace
71 } // namespace gpu
72 } // namespace tflite
73