Elffuss BraidHub

kikocis/ncnn public

master · 3.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "eliminate_noop_upsample.h"
16
17#include <algorithm>
18#include "pass_level2.h"
19
20namespace pnnx {
21
22void eliminate_noop_upsample(Graph& graph)
23{
24 while (1)
25 {
26 bool matched = false;
27
28 for (size_t i = 0; i < graph.ops.size(); i++)
29 {
30 Operator* op = graph.ops[i];
31
32 if (op->type != "F.upsample" && op->type != "F.upsample_bilinear" && op->type != "F.upsample_nearest" && op->type != "F.interpolate"
33 && op->type != "nn.Upsample" && op->type != "nn.UpsamplingBilinear2d" && op->type != "nn.UpsamplingNearest2d")
34 continue;
35
36 if (op->inputs.size() != 1)
37 continue;
38
39 if (op->params.find("scale_factor") != op->params.end())
40 {
41 matched = true;
42
43 std::vector<float> scale_factor;
44 if (op->params.at("scale_factor").type == 3)
45 {
46 scale_factor.push_back(op->params.at("scale_factor").f);
47 }
48 else
49 {
50 scale_factor = op->params.at("scale_factor").af;
51 }
52
53 if (scale_factor.empty())
54 matched = false;
55
56 for (auto s : scale_factor)
57 {
58 if (s != 1.f)
59 {
60 matched = false;
61 break;
62 }
63 }
64 }
65
66 if (!op->inputs[0]->shape.empty() && op->inputs[0]->shape == op->outputs[0]->shape)
67 {
68 matched = true;
69
70 // dynamic shape comparison always fail
71 for (auto s : op->inputs[0]->shape)
72 {
73 if (s == -1)
74 {
75 matched = false;
76 break;
77 }
78 }
79 }
80
81 // delete noop-like upsample
82 if (matched)
83 {
84 for (auto& x : op->inputs)
85 {
86 x->remove_consumer(op);
87 }
88
89 Operand* upsample_out = op->outputs[0];
90
91 for (auto& x : upsample_out->consumers)
92 {
93 for (size_t j = 0; j < x->inputs.size(); j++)
94 {
95 if (x->inputs[j] == upsample_out)
96 x->inputs[j] = op->inputs[0];
97 }
98
99 op->inputs[0]->consumers.push_back(x);
100 }
101
102 op->inputs[0]->name = upsample_out->name;
103
104 upsample_out->producer = 0;
105 upsample_out->consumers.clear();
106
107 graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), upsample_out));
108 delete upsample_out;
109
110 op->inputs.clear();
111 op->outputs.clear();
112
113 graph.ops.erase(graph.ops.begin() + i);
114 delete op;
115
116 break;
117 }
118 }
119
120 if (!matched)
121 break;
122 }
123}
124
125} // namespace pnnx