Elffuss BraidHub

kikocis/ncnn public

master · 6.5 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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 "pass_ncnn.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21class F_upsample : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
273 2
28pnnx.Input input 0 1 input
29F.upsample op_0 1 1 input out align_corners=%align_corners mode=%mode size=%size
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "Interp";
37 }
38
39 const char* name_str() const
40 {
41 return "upsample";
42 }
43
44 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
45 {
46 const std::string& mode = captured_params.at("mode").s;
47 const std::vector<int>& size = captured_params.at("size").ai;
48
49 if (mode == "nearest")
50 op->params["0"] = 1;
51 if (mode == "bilinear" || mode == "linear")
52 op->params["0"] = 2;
53 if (mode == "bicubic")
54 op->params["0"] = 3;
55
56 if (size.size() == 1)
57 {
58 op->params["3"] = 1;
59 op->params["4"] = size[0];
60 }
61 else if (size.size() == 2)
62 {
63 op->params["3"] = size[0];
64 op->params["4"] = size[1];
65 }
66 else
67 {
68 fprintf(stderr, "unsupported upsample size\n");
69 }
70
71 op->params["6"] = captured_params.at("align_corners").b ? 1 : 0;
72 }
73};
74
75REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_upsample, 20)
76
77class F_upsample_1 : public GraphRewriterPass
78{
79public:
80 const char* match_pattern_graph() const
81 {
82 return R"PNNXIR(7767517
833 2
84pnnx.Input input 0 1 input
85F.upsample op_0 1 1 input out align_corners=%align_corners mode=%mode scale_factor=%scale_factor
86pnnx.Output output 1 0 out
87)PNNXIR";
88 }
89
90 const char* type_str() const
91 {
92 return "Interp";
93 }
94
95 const char* name_str() const
96 {
97 return "upsample";
98 }
99
100 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
101 {
102 const std::string& mode = captured_params.at("mode").s;
103 const std::vector<float>& scale_factor = captured_params.at("scale_factor").af;
104
105 if (mode == "nearest")
106 op->params["0"] = 1;
107 if (mode == "bilinear" || mode == "linear")
108 op->params["0"] = 2;
109 if (mode == "bicubic")
110 op->params["0"] = 3;
111
112 if (scale_factor.size() == 1)
113 {
114 op->params["1"] = 1.f;
115 op->params["2"] = scale_factor[0];
116 }
117 else if (scale_factor.size() == 2)
118 {
119 op->params["1"] = scale_factor[0];
120 op->params["2"] = scale_factor[1];
121 }
122 else
123 {
124 fprintf(stderr, "unsupported upsample scale_factor\n");
125 }
126
127 op->params["6"] = captured_params.at("align_corners").b ? 1 : 0;
128 }
129};
130
131REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_upsample_1, 20)
132
133class F_upsample_2 : public GraphRewriterPass
134{
135public:
136 const char* match_pattern_graph() const
137 {
138 return R"PNNXIR(7767517
1393 2
140pnnx.Input input 0 1 input
141F.upsample op_0 1 1 input out mode=%mode size=%size
142pnnx.Output output 1 0 out
143)PNNXIR";
144 }
145
146 const char* type_str() const
147 {
148 return "Interp";
149 }
150
151 const char* name_str() const
152 {
153 return "upsample";
154 }
155
156 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
157 {
158 const std::string& mode = captured_params.at("mode").s;
159 const std::vector<int>& size = captured_params.at("size").ai;
160
161 if (mode == "nearest")
162 op->params["0"] = 1;
163 if (mode == "bilinear" || mode == "linear")
164 op->params["0"] = 2;
165 if (mode == "bicubic")
166 op->params["0"] = 3;
167
168 if (size.size() == 1)
169 {
170 op->params["3"] = 1;
171 op->params["4"] = size[0];
172 }
173 else if (size.size() == 2)
174 {
175 op->params["3"] = size[0];
176 op->params["4"] = size[1];
177 }
178 else
179 {
180 fprintf(stderr, "unsupported upsample size\n");
181 }
182
183 op->params["6"] = 0; // align_corners
184 }
185};
186
187REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_upsample_2, 20)
188
189class F_upsample_3 : public GraphRewriterPass
190{
191public:
192 const char* match_pattern_graph() const
193 {
194 return R"PNNXIR(7767517
1953 2
196pnnx.Input input 0 1 input
197F.upsample op_0 1 1 input out mode=%mode scale_factor=%scale_factor
198pnnx.Output output 1 0 out
199)PNNXIR";
200 }
201
202 const char* type_str() const
203 {
204 return "Interp";
205 }
206
207 const char* name_str() const
208 {
209 return "upsample";
210 }
211
212 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
213 {
214 const std::string& mode = captured_params.at("mode").s;
215 const std::vector<float>& scale_factor = captured_params.at("scale_factor").af;
216
217 if (mode == "nearest")
218 op->params["0"] = 1;
219 if (mode == "bilinear" || mode == "linear")
220 op->params["0"] = 2;
221 if (mode == "bicubic")
222 op->params["0"] = 3;
223
224 if (scale_factor.size() == 1)
225 {
226 op->params["1"] = 1.f;
227 op->params["2"] = scale_factor[0];
228 }
229 else if (scale_factor.size() == 2)
230 {
231 op->params["1"] = scale_factor[0];
232 op->params["2"] = scale_factor[1];
233 }
234 else
235 {
236 fprintf(stderr, "unsupported upsample scale_factor\n");
237 }
238
239 op->params["6"] = 0; // align_corners
240 }
241};
242
243REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_upsample_3, 20)
244
245} // namespace ncnn
246
247} // namespace pnnx