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