Elffuss BraidHub

kikocis/ncnn public

master · 7.1 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 nn_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
29nn.Upsample op_0 1 1 input out mode=%mode scale_factor=%scale_factor size=%size align_corners=%align_corners
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<float>& scale_factor = captured_params.at("scale_factor").af;
48 const std::vector<int>& size = captured_params.at("size").ai;
49
50 if (mode == "nearest")
51 op->params["0"] = 1;
52 if (mode == "bilinear" || mode == "linear")
53 op->params["0"] = 2;
54 if (mode == "bicubic")
55 op->params["0"] = 3;
56
57 if (scale_factor.size() == 1)
58 {
59 op->params["1"] = 1.f;
60 op->params["2"] = scale_factor[0];
61 }
62 else if (scale_factor.size() == 2)
63 {
64 op->params["1"] = scale_factor[0];
65 op->params["2"] = scale_factor[1];
66 }
67 else if (size.size() == 1)
68 {
69 op->params["3"] = 1;
70 op->params["4"] = size[0];
71 }
72 else if (size.size() == 2)
73 {
74 op->params["3"] = size[0];
75 op->params["4"] = size[1];
76 }
77 else
78 {
79 fprintf(stderr, "unsupported upsample scale_factor or size\n");
80 }
81
82 op->params["6"] = captured_params.at("align_corners").b ? 1 : 0;
83 }
84};
85
86REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Upsample, 20)
87
88class nn_Upsample_1 : public GraphRewriterPass
89{
90public:
91 const char* match_pattern_graph() const
92 {
93 return R"PNNXIR(7767517
943 2
95pnnx.Input input 0 1 input
96nn.Upsample op_0 1 1 input out mode=%mode size=%size align_corners=%align_corners
97pnnx.Output output 1 0 out
98)PNNXIR";
99 }
100
101 const char* type_str() const
102 {
103 return "Interp";
104 }
105
106 const char* name_str() const
107 {
108 return "upsample";
109 }
110
111 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
112 {
113 const std::string& mode = captured_params.at("mode").s;
114 const std::vector<int>& size = captured_params.at("size").ai;
115
116 if (mode == "nearest")
117 op->params["0"] = 1;
118 if (mode == "bilinear" || mode == "linear")
119 op->params["0"] = 2;
120 if (mode == "bicubic")
121 op->params["0"] = 3;
122
123 if (size.size() == 1)
124 {
125 op->params["3"] = 1;
126 op->params["4"] = size[0];
127 }
128 else if (size.size() == 2)
129 {
130 op->params["3"] = size[0];
131 op->params["4"] = size[1];
132 }
133 else
134 {
135 fprintf(stderr, "unsupported upsample size\n");
136 }
137
138 op->params["6"] = captured_params.at("align_corners").b ? 1 : 0;
139 }
140};
141
142REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Upsample_1, 20)
143
144class nn_Upsample_2 : public GraphRewriterPass
145{
146public:
147 const char* match_pattern_graph() const
148 {
149 return R"PNNXIR(7767517
1503 2
151pnnx.Input input 0 1 input
152nn.Upsample op_0 1 1 input out mode=%mode scale_factor=%scale_factor size=%size
153pnnx.Output output 1 0 out
154)PNNXIR";
155 }
156
157 const char* type_str() const
158 {
159 return "Interp";
160 }
161
162 const char* name_str() const
163 {
164 return "upsample";
165 }
166
167 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
168 {
169 const std::string& mode = captured_params.at("mode").s;
170 const std::vector<float>& scale_factor = captured_params.at("scale_factor").af;
171 const std::vector<int>& size = captured_params.at("size").ai;
172
173 if (mode == "nearest")
174 op->params["0"] = 1;
175 if (mode == "bilinear" || mode == "linear")
176 op->params["0"] = 2;
177 if (mode == "bicubic")
178 op->params["0"] = 3;
179
180 if (scale_factor.size() == 1)
181 {
182 op->params["1"] = 1.f;
183 op->params["2"] = scale_factor[0];
184 }
185 else if (scale_factor.size() == 2)
186 {
187 op->params["1"] = scale_factor[0];
188 op->params["2"] = scale_factor[1];
189 }
190 else if (size.size() == 1)
191 {
192 op->params["3"] = 1;
193 op->params["4"] = size[0];
194 }
195 else if (size.size() == 2)
196 {
197 op->params["3"] = size[0];
198 op->params["4"] = size[1];
199 }
200 else
201 {
202 fprintf(stderr, "unsupported upsample scale_factor or size\n");
203 }
204
205 op->params["6"] = 0; // align_corner
206 }
207};
208
209REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Upsample_2, 20)
210
211class nn_Upsample_3 : public GraphRewriterPass
212{
213public:
214 const char* match_pattern_graph() const
215 {
216 return R"PNNXIR(7767517
2173 2
218pnnx.Input input 0 1 input
219nn.Upsample op_0 1 1 input out mode=%mode size=%size
220pnnx.Output output 1 0 out
221)PNNXIR";
222 }
223
224 const char* type_str() const
225 {
226 return "Interp";
227 }
228
229 const char* name_str() const
230 {
231 return "upsample";
232 }
233
234 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
235 {
236 const std::string& mode = captured_params.at("mode").s;
237 const std::vector<int>& size = captured_params.at("size").ai;
238
239 if (mode == "nearest")
240 op->params["0"] = 1;
241 if (mode == "bilinear" || mode == "linear")
242 op->params["0"] = 2;
243 if (mode == "bicubic")
244 op->params["0"] = 3;
245
246 if (size.size() == 1)
247 {
248 op->params["3"] = 1;
249 op->params["4"] = size[0];
250 }
251 else if (size.size() == 2)
252 {
253 op->params["3"] = size[0];
254 op->params["4"] = size[1];
255 }
256 else
257 {
258 fprintf(stderr, "unsupported upsample size\n");
259 }
260
261 op->params["6"] = 0; // align_corner
262 }
263};
264
265REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Upsample_3, 20)
266
267} // namespace ncnn
268
269} // namespace pnnx