1 /* Copyright 2019 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/make_padding.h"
17
18 #include <memory>
19 #include <string>
20 #include <vector>
21
22 #include "absl/memory/memory.h"
23 #include "absl/strings/string_view.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/status.h"
30 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
31
32 namespace tflite {
33 namespace gpu {
34 namespace {
35
IsConstZeros(const Node & node)36 bool IsConstZeros(const Node& node) {
37 if (node.operation.type != ToString(OperationType::CONSTANT)) {
38 return false;
39 }
40 auto& attr =
41 absl::any_cast<const ConstTensorAttributes&>(node.operation.attributes);
42 for (auto f : attr.tensor.data) {
43 if (f != 0) {
44 return false;
45 }
46 }
47 return true;
48 }
49
50 class MakePaddingFromZerosConcat : public NodeTransformation {
51 public:
ApplyToNode(Node * node,GraphFloat32 * graph)52 TransformResult ApplyToNode(Node* node, GraphFloat32* graph) final {
53 if (node->operation.type != ToString(OperationType::CONCAT)) {
54 return {TransformStatus::SKIPPED, ""};
55 }
56 auto inputs = graph->FindInputs(node->id);
57 if (inputs.size() != 2) {
58 return {TransformStatus::SKIPPED, ""};
59 }
60
61 bool first = true;
62 for (auto input : inputs) {
63 auto dep = graph->FindProducer(input->id);
64 if (dep != nullptr && IsConstZeros(*dep)) {
65 auto& concat_attr =
66 absl::any_cast<const ConcatAttributes&>(node->operation.attributes);
67 PadAttributes pad_attr;
68 pad_attr.type = PaddingContentType::ZEROS;
69 pad_attr.appended = BHWC(0, 0, 0, 0);
70 pad_attr.prepended = BHWC(0, 0, 0, 0);
71 BHWC* p = first ? &pad_attr.prepended : &pad_attr.appended;
72 switch (concat_attr.axis) {
73 case Axis::HEIGHT:
74 p->h = input->tensor.shape.h;
75 break;
76 case Axis::WIDTH:
77 p->w = input->tensor.shape.w;
78 break;
79 case Axis::CHANNELS:
80 p->c = input->tensor.shape.c;
81 break;
82 default:
83 return {TransformStatus::DECLINED,
84 "Padding for concat axis is unsupported: " +
85 ToString(concat_attr.axis)};
86 }
87 absl::Status status = RemovePrecedingNode(graph, dep, node);
88 if (!status.ok()) {
89 return {TransformStatus::INVALID, "Unable to remove const node: " +
90 std::string(status.message())};
91 }
92 node->operation.attributes = pad_attr;
93 node->operation.type = ToString(OperationType::PAD);
94 return {TransformStatus::APPLIED, "Replaced concat with padding"};
95 }
96 first = false;
97 }
98 return {TransformStatus::SKIPPED, ""};
99 }
100 };
101
102 } // namespace
103
NewMakePaddingFromConcat()104 std::unique_ptr<NodeTransformation> NewMakePaddingFromConcat() {
105 return absl::make_unique<MakePaddingFromZerosConcat>();
106 }
107
108 } // namespace gpu
109 } // namespace tflite
110