| 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_pad : 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.pad op_0 1 1 input out pad=%pad mode=constant value=%value |
| 30 | pnnx.Output output 1 0 out |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* type_str() const |
| 35 | { |
| 36 | return "Padding"; |
| 37 | } |
| 38 | |
| 39 | const char* name_str() const |
| 40 | { |
| 41 | return "pad"; |
| 42 | } |
| 43 | |
| 44 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 45 | { |
| 46 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 47 | for (int x : pad) |
| 48 | { |
| 49 | if (x < 0) |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 57 | { |
| 58 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 59 | |
| 60 | float pad_value = 0.f; |
| 61 | if (captured_params.at("value").type == 2) |
| 62 | pad_value = captured_params.at("value").i; |
| 63 | if (captured_params.at("value").type == 3) |
| 64 | pad_value = captured_params.at("value").f; |
| 65 | |
| 66 | if (pad.size() == 2) |
| 67 | { |
| 68 | op->params["0"] = 0; |
| 69 | op->params["1"] = 0; |
| 70 | op->params["2"] = pad[0]; |
| 71 | op->params["3"] = pad[1]; |
| 72 | } |
| 73 | else if (pad.size() >= 4) |
| 74 | { |
| 75 | op->params["0"] = pad[2]; |
| 76 | op->params["1"] = pad[3]; |
| 77 | op->params["2"] = pad[0]; |
| 78 | op->params["3"] = pad[1]; |
| 79 | } |
| 80 | if (pad.size() >= 6) |
| 81 | { |
| 82 | op->params["7"] = pad[4]; |
| 83 | op->params["8"] = pad[5]; |
| 84 | } |
| 85 | |
| 86 | op->params["4"] = 0; // constant |
| 87 | op->params["5"] = pad_value; |
| 88 | op->params["6"] = 0; // per_channel_pad_data_size |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_pad, 20) |
| 93 | |
| 94 | class F_pad_1 : public GraphRewriterPass |
| 95 | { |
| 96 | public: |
| 97 | const char* match_pattern_graph() const |
| 98 | { |
| 99 | return R"PNNXIR(7767517 |
| 100 | 3 2 |
| 101 | pnnx.Input input 0 1 input |
| 102 | F.pad op_0 1 1 input out pad=%pad mode=%mode |
| 103 | pnnx.Output output 1 0 out |
| 104 | )PNNXIR"; |
| 105 | } |
| 106 | |
| 107 | const char* type_str() const |
| 108 | { |
| 109 | return "Padding"; |
| 110 | } |
| 111 | |
| 112 | const char* name_str() const |
| 113 | { |
| 114 | return "pad"; |
| 115 | } |
| 116 | |
| 117 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 118 | { |
| 119 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 120 | for (int x : pad) |
| 121 | { |
| 122 | if (x < 0) |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 130 | { |
| 131 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 132 | const std::string& mode = captured_params.at("mode").s; |
| 133 | |
| 134 | if (pad.size() == 2) |
| 135 | { |
| 136 | op->params["0"] = 0; |
| 137 | op->params["1"] = 0; |
| 138 | op->params["2"] = pad[0]; |
| 139 | op->params["3"] = pad[1]; |
| 140 | } |
| 141 | else if (pad.size() >= 4) |
| 142 | { |
| 143 | op->params["0"] = pad[2]; |
| 144 | op->params["1"] = pad[3]; |
| 145 | op->params["2"] = pad[0]; |
| 146 | op->params["3"] = pad[1]; |
| 147 | } |
| 148 | if (pad.size() >= 6) |
| 149 | { |
| 150 | op->params["7"] = pad[4]; |
| 151 | op->params["8"] = pad[5]; |
| 152 | } |
| 153 | |
| 154 | if (mode == "constant") |
| 155 | op->params["4"] = 0; |
| 156 | if (mode == "reflect") |
| 157 | op->params["4"] = 2; |
| 158 | if (mode == "replicate") |
| 159 | op->params["4"] = 1; |
| 160 | |
| 161 | op->params["5"] = 0; // value |
| 162 | op->params["6"] = 0; // per_channel_pad_data_size |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_pad_1, 20) |
| 167 | |
| 168 | class F_pad_2 : public GraphRewriterPass |
| 169 | { |
| 170 | public: |
| 171 | const char* match_pattern_graph() const |
| 172 | { |
| 173 | return R"PNNXIR(7767517 |
| 174 | 3 2 |
| 175 | pnnx.Input input 0 1 input |
| 176 | F.pad op_0 1 1 input out pad=%pad mode=constant value=%value |
| 177 | pnnx.Output output 1 0 out |
| 178 | )PNNXIR"; |
| 179 | } |
| 180 | |
| 181 | const char* type_str() const |
| 182 | { |
| 183 | return "Crop"; |
| 184 | } |
| 185 | |
| 186 | const char* name_str() const |
| 187 | { |
| 188 | return "pad"; |
| 189 | } |
| 190 | |
| 191 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 192 | { |
| 193 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 194 | for (int x : pad) |
| 195 | { |
| 196 | if (x > 0) |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 204 | { |
| 205 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 206 | |
| 207 | std::vector<int> starts; |
| 208 | std::vector<int> ends; |
| 209 | std::vector<int> axes; |
| 210 | |
| 211 | if (pad.size() == 2) |
| 212 | { |
| 213 | starts = {-pad[0]}; |
| 214 | ends = {pad[1]}; |
| 215 | axes = {-1}; |
| 216 | } |
| 217 | else if (pad.size() == 4) |
| 218 | { |
| 219 | starts = {-pad[2], -pad[0]}; |
| 220 | ends = {pad[3], pad[1]}; |
| 221 | axes = {-2, -1}; |
| 222 | } |
| 223 | else if (pad.size() == 6) |
| 224 | { |
| 225 | starts = {-pad[4], -pad[2], -pad[0]}; |
| 226 | ends = {pad[5], pad[3], pad[1]}; |
| 227 | axes = {-3, -2, -1}; |
| 228 | } |
| 229 | |
| 230 | op->params["9"] = starts; |
| 231 | op->params["10"] = ends; |
| 232 | op->params["11"] = axes; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_pad_2, 20) |
| 237 | |
| 238 | class F_pad_3 : public GraphRewriterPass |
| 239 | { |
| 240 | public: |
| 241 | const char* match_pattern_graph() const |
| 242 | { |
| 243 | return R"PNNXIR(7767517 |
| 244 | 3 2 |
| 245 | pnnx.Input input 0 1 input |
| 246 | F.pad op_0 1 1 input out pad=%pad mode=%mode |
| 247 | pnnx.Output output 1 0 out |
| 248 | )PNNXIR"; |
| 249 | } |
| 250 | |
| 251 | const char* type_str() const |
| 252 | { |
| 253 | return "Padding"; |
| 254 | } |
| 255 | |
| 256 | const char* name_str() const |
| 257 | { |
| 258 | return "pad"; |
| 259 | } |
| 260 | |
| 261 | bool match(const std::map<std::string, Parameter>& captured_params) const |
| 262 | { |
| 263 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 264 | for (int x : pad) |
| 265 | { |
| 266 | if (x < 0) |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 274 | { |
| 275 | const std::vector<int>& pad = captured_params.at("pad").ai; |
| 276 | |
| 277 | std::vector<int> starts; |
| 278 | std::vector<int> ends; |
| 279 | std::vector<int> axes; |
| 280 | |
| 281 | if (pad.size() == 2) |
| 282 | { |
| 283 | starts = {-pad[0]}; |
| 284 | ends = {pad[1]}; |
| 285 | axes = {-1}; |
| 286 | } |
| 287 | else if (pad.size() == 4) |
| 288 | { |
| 289 | starts = {-pad[2], -pad[0]}; |
| 290 | ends = {pad[3], pad[1]}; |
| 291 | axes = {-2, -1}; |
| 292 | } |
| 293 | else if (pad.size() == 6) |
| 294 | { |
| 295 | starts = {-pad[4], -pad[2], -pad[0]}; |
| 296 | ends = {pad[5], pad[3], pad[1]}; |
| 297 | axes = {-3, -2, -1}; |
| 298 | } |
| 299 | |
| 300 | op->params["9"] = starts; |
| 301 | op->params["10"] = ends; |
| 302 | op->params["11"] = axes; |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_pad_3, 20) |
| 307 | |
| 308 | } // namespace ncnn |
| 309 | |
| 310 | } // namespace pnnx |