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 "absl/memory/memory.h"
23 #include "absl/types/any.h"
24 #include "tensorflow/lite/delegates/gpu/common/model.h"
25 #include "tensorflow/lite/delegates/gpu/common/model_transformer.h"
26 #include "tensorflow/lite/delegates/gpu/common/operations.h"
27 #include "tensorflow/lite/delegates/gpu/common/shape.h"
28 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
29
30 namespace tflite {
31 namespace gpu {
32 namespace {
33
IsGlobalPooling(const Pooling2DAttributes & attr,const BHWC & src_shape,const BHWC & dst_shape)34 bool IsGlobalPooling(const Pooling2DAttributes& attr, const BHWC& src_shape,
35 const BHWC& dst_shape) {
36 return dst_shape.w == 1 && dst_shape.h == 1 && attr.kernel.w == src_shape.w &&
37 attr.kernel.h == src_shape.h && attr.padding.appended.w == 0 &&
38 attr.padding.appended.h == 0 && attr.padding.prepended.w == 0 &&
39 attr.padding.prepended.h == 0;
40 }
41
IsGlobalAveragePooling(const Pooling2DAttributes & attr,const BHWC & src_shape,const BHWC & dst_shape)42 bool IsGlobalAveragePooling(const Pooling2DAttributes& attr,
43 const BHWC& src_shape, const BHWC& dst_shape) {
44 return attr.type == tflite::gpu::PoolingType::AVERAGE &&
45 attr.output_indices == false &&
46 IsGlobalPooling(attr, src_shape, dst_shape);
47 }
48
49 class GlobalPoolingToReduceOp : public NodeTransformation {
50 public:
ApplyToNode(Node * node,GraphFloat32 * graph)51 TransformResult ApplyToNode(Node* node, GraphFloat32* graph) final {
52 if (node->operation.type != ToString(OperationType::POOLING_2D)) {
53 return {TransformStatus::SKIPPED, ""};
54 }
55
56 auto inputs = graph->FindInputs(node->id);
57 auto outputs = graph->FindOutputs(node->id);
58 const auto& pool_attr =
59 absl::any_cast<const Pooling2DAttributes&>(node->operation.attributes);
60 if (!IsGlobalAveragePooling(pool_attr, inputs[0]->tensor.shape,
61 outputs[0]->tensor.shape)) {
62 return {TransformStatus::SKIPPED, ""};
63 }
64
65 MeanAttributes mean_attr;
66 mean_attr.dims = {Axis::WIDTH, Axis::HEIGHT};
67
68 node->operation.attributes = mean_attr;
69 node->operation.type = ToString(OperationType::MEAN);
70 return {TransformStatus::APPLIED,
71 "Replaced global average pooling with mean."};
72 }
73 };
74
75 } // namespace
76
NewGlobalPoolingToReduceOp()77 std::unique_ptr<NodeTransformation> NewGlobalPoolingToReduceOp() {
78 return absl::make_unique<GlobalPoolingToReduceOp>();
79 }
80
81 } // namespace gpu
82 } // namespace tflite
83