| 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_level2.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <map> |
| 19 | #include <set> |
| 20 | #include <unordered_map> |
| 21 | #include <unordered_set> |
| 22 | |
| 23 | namespace pnnx { |
| 24 | |
| 25 | GraphRewriterPass::~GraphRewriterPass() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | const char* GraphRewriterPass::replace_pattern_graph() const |
| 30 | { |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | const char* GraphRewriterPass::type_str() const |
| 35 | { |
| 36 | fprintf(stderr, "GraphRewriterPass type_str() should be implemented\n"); |
| 37 | return "unk"; |
| 38 | } |
| 39 | |
| 40 | const char* GraphRewriterPass::name_str() const |
| 41 | { |
| 42 | return type_str(); |
| 43 | } |
| 44 | |
| 45 | bool GraphRewriterPass::match(const std::map<std::string, Parameter>& /*captured_params*/) const |
| 46 | { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | bool GraphRewriterPass::match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 51 | { |
| 52 | return match(captured_params); |
| 53 | } |
| 54 | |
| 55 | bool GraphRewriterPass::match(const std::map<std::string, const Operator*>& /*matched_operators*/, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 56 | { |
| 57 | return match(captured_params, captured_attrs); |
| 58 | } |
| 59 | |
| 60 | void GraphRewriterPass::write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 61 | { |
| 62 | if (replace_pattern_graph() == 0) |
| 63 | { |
| 64 | for (auto x : captured_params) |
| 65 | { |
| 66 | op->params[x.first] = x.second; |
| 67 | } |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | for (auto x : op->params) |
| 73 | { |
| 74 | if (x.second.type != 4) |
| 75 | continue; |
| 76 | |
| 77 | std::string str = x.second.s; |
| 78 | if (str.find('%') == std::string::npos) |
| 79 | continue; |
| 80 | |
| 81 | // search % token and replace with captured |
| 82 | size_t pos = str.find('%'); |
| 83 | while (pos != std::string::npos) |
| 84 | { |
| 85 | // %xyz |
| 86 | char buf[256]; |
| 87 | sscanf(str.c_str() + pos + 1, "%255[^][,() ]", buf); |
| 88 | std::string key(buf); |
| 89 | |
| 90 | if (captured_params.find(key) == captured_params.end()) |
| 91 | { |
| 92 | fprintf(stderr, "replace pattern param %%%s missing captured\n", key.c_str()); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // replace %xyz with encoded_str |
| 97 | std::string encoded_str = Parameter::encode_to_string(captured_params.at(key)); |
| 98 | str.replace(pos, key.size() + 1, encoded_str); |
| 99 | |
| 100 | pos = str.find('%', pos + 1); |
| 101 | } |
| 102 | |
| 103 | op->params[x.first] = Parameter::parse_from_string(str); |
| 104 | } |
| 105 | |
| 106 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 107 | { |
| 108 | Operand* operand = op->inputs[i]; |
| 109 | std::vector<int>& shape = operand->shape; |
| 110 | for (size_t j = 0; j < shape.size(); j++) |
| 111 | { |
| 112 | int ai = shape[j]; |
| 113 | if (ai == -233) |
| 114 | { |
| 115 | std::string key = operand->params.at(std::string("__shape__") + std::to_string(j)).s; |
| 116 | |
| 117 | if (captured_params.find(key) == captured_params.end()) |
| 118 | { |
| 119 | fprintf(stderr, "replace pattern param %%%s missing captured\n", key.c_str()); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | shape[j] = captured_params.at(key).i; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | for (size_t i = 0; i < op->outputs.size(); i++) |
| 129 | { |
| 130 | Operand* operand = op->outputs[i]; |
| 131 | std::vector<int>& shape = operand->shape; |
| 132 | for (size_t j = 0; j < shape.size(); j++) |
| 133 | { |
| 134 | int ai = shape[j]; |
| 135 | if (ai == -233) |
| 136 | { |
| 137 | std::string key = operand->params.at(std::string("__shape__") + std::to_string(j)).s; |
| 138 | |
| 139 | if (captured_params.find(key) == captured_params.end()) |
| 140 | { |
| 141 | fprintf(stderr, "replace pattern param %%%s missing captured\n", key.c_str()); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | shape[j] = captured_params.at(key).i; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void GraphRewriterPass::write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 152 | { |
| 153 | write(op, captured_params); |
| 154 | |
| 155 | for (auto x : op->attrs) |
| 156 | { |
| 157 | if (x.second.type != 0) |
| 158 | continue; |
| 159 | |
| 160 | std::string key((const char*)x.second.data.data()); |
| 161 | if (key.empty()) |
| 162 | continue; |
| 163 | |
| 164 | op->attrs[x.first] = captured_attrs.at(key); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void GraphRewriterPass::write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params) const |
| 169 | { |
| 170 | for (auto x : ops) |
| 171 | { |
| 172 | Operator* op = x.second; |
| 173 | write(op, captured_params); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void GraphRewriterPass::write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 178 | { |
| 179 | write(ops, captured_params); |
| 180 | |
| 181 | for (auto x : ops) |
| 182 | { |
| 183 | Operator* op = x.second; |
| 184 | for (auto x : op->attrs) |
| 185 | { |
| 186 | if (x.second.type != 0) |
| 187 | continue; |
| 188 | |
| 189 | std::string key(x.second.data.begin(), x.second.data.end()); |
| 190 | if (key.empty() || key[0] != '%') |
| 191 | continue; |
| 192 | |
| 193 | op->attrs[x.first] = captured_attrs.at(key.substr(1)); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static std::map<int, std::vector<const GraphRewriterPass*> > g_global_pnnx_graph_rewriter_passes; |
| 199 | |
| 200 | GraphRewriterPassRegister::GraphRewriterPassRegister(const GraphRewriterPass* _pass, int priority) |
| 201 | : pass(_pass) |
| 202 | { |
| 203 | if (g_global_pnnx_graph_rewriter_passes.find(priority) == g_global_pnnx_graph_rewriter_passes.end()) |
| 204 | { |
| 205 | g_global_pnnx_graph_rewriter_passes[priority] = std::vector<const GraphRewriterPass*>(); |
| 206 | } |
| 207 | |
| 208 | g_global_pnnx_graph_rewriter_passes[priority].push_back(pass); |
| 209 | } |
| 210 | |
| 211 | GraphRewriterPassRegister::~GraphRewriterPassRegister() |
| 212 | { |
| 213 | delete pass; |
| 214 | } |
| 215 | |
| 216 | static bool token_is_argument(const std::string& t) |
| 217 | { |
| 218 | if (t[0] != '@' || t.size() < 2) |
| 219 | return false; |
| 220 | |
| 221 | for (size_t i = 1; i < t.size(); i++) |
| 222 | { |
| 223 | if (t[i] < '0' || t[i] > '9') |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | static bool match_expression(const Operator* a, const Operator* b, std::map<std::string, Parameter>& captured_params) |
| 231 | { |
| 232 | if (a->params.size() != 1 || a->params.find("expr") == a->params.end()) |
| 233 | return false; |
| 234 | |
| 235 | if (b->params.size() != 1 || b->params.find("expr") == b->params.end()) |
| 236 | return false; |
| 237 | |
| 238 | const std::string& a_expr = a->params.at("expr").s; |
| 239 | const std::string& b_expr = b->params.at("expr").s; |
| 240 | |
| 241 | if (a_expr == b_expr) |
| 242 | return true; |
| 243 | |
| 244 | // split into tokens |
| 245 | std::vector<std::string> a_tokens; |
| 246 | std::vector<std::string> b_tokens; |
| 247 | { |
| 248 | std::string t; |
| 249 | for (size_t i = 0; i < a_expr.size(); i++) |
| 250 | { |
| 251 | char ch = a_expr[i]; |
| 252 | |
| 253 | if (ch == '[') // list |
| 254 | { |
| 255 | t += ch; |
| 256 | a_tokens.push_back(t); |
| 257 | t.clear(); |
| 258 | } |
| 259 | else if (ch == '(' || ch == ')' || ch == ',' || ch == ']') |
| 260 | { |
| 261 | if (!t.empty()) |
| 262 | { |
| 263 | a_tokens.push_back(t); |
| 264 | t.clear(); |
| 265 | } |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | t += ch; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (!t.empty()) |
| 274 | { |
| 275 | a_tokens.push_back(t); |
| 276 | } |
| 277 | } |
| 278 | { |
| 279 | std::string t; |
| 280 | for (size_t i = 0; i < b_expr.size(); i++) |
| 281 | { |
| 282 | char ch = b_expr[i]; |
| 283 | |
| 284 | if (ch == '[') // list |
| 285 | { |
| 286 | t += ch; |
| 287 | b_tokens.push_back(t); |
| 288 | t.clear(); |
| 289 | } |
| 290 | else if (ch == '(' || ch == ')' || ch == ',' || ch == ']') |
| 291 | { |
| 292 | if (!t.empty()) |
| 293 | { |
| 294 | b_tokens.push_back(t); |
| 295 | t.clear(); |
| 296 | } |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | t += ch; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (!t.empty()) |
| 305 | { |
| 306 | b_tokens.push_back(t); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (a_tokens.size() != b_tokens.size()) |
| 311 | return false; |
| 312 | |
| 313 | // capture values |
| 314 | for (size_t i = 0; i < a_tokens.size(); i++) |
| 315 | { |
| 316 | const std::string& at = a_tokens[i]; |
| 317 | const std::string& bt = b_tokens[i]; |
| 318 | |
| 319 | if (at == bt) |
| 320 | continue; |
| 321 | |
| 322 | if (bt[0] != '%') |
| 323 | return false; |
| 324 | |
| 325 | if (token_is_argument(at)) |
| 326 | return false; |
| 327 | |
| 328 | std::string key = bt.substr(1); |
| 329 | |
| 330 | captured_params[key] = Parameter::parse_from_string(at); |
| 331 | } |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | static bool match_parameter(const Parameter& a, const Parameter& b, std::map<std::string, Parameter>& captured_params) |
| 337 | { |
| 338 | if (b.type == 4 && b.s[0] == '%') |
| 339 | { |
| 340 | std::string key = b.s.substr(1); |
| 341 | if (captured_params.find(key) != captured_params.end()) |
| 342 | { |
| 343 | // match previous captured parameter |
| 344 | return captured_params.at(key) == a; |
| 345 | } |
| 346 | |
| 347 | // captured parameter |
| 348 | captured_params[key] = a; |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | if (b.type == 4 && b.s == "*") |
| 353 | { |
| 354 | // ignored parameter |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | if (b.type == 4 && (b.s[0] == '(' || b.s[0] == '[') && b.s.find('%') != std::string::npos) |
| 359 | { |
| 360 | // list with pattern |
| 361 | if (a.type != 5 && a.type != 6 && a.type != 7) |
| 362 | return false; |
| 363 | |
| 364 | std::string lc = b.s.substr(1, b.s.size() - 2); |
| 365 | std::istringstream lcss(lc); |
| 366 | |
| 367 | size_t i = 0; |
| 368 | while (!lcss.eof()) |
| 369 | { |
| 370 | std::string elem; |
| 371 | std::getline(lcss, elem, ','); |
| 372 | |
| 373 | if (elem[0] == '%') |
| 374 | { |
| 375 | std::string key = elem.substr(1); |
| 376 | if (captured_params.find(key) != captured_params.end()) |
| 377 | { |
| 378 | // match previous captured parameter |
| 379 | if (a.type == 5 && captured_params.at(key).i != a.ai[i]) |
| 380 | return false; |
| 381 | if (a.type == 6 && captured_params.at(key).f != a.af[i]) |
| 382 | return false; |
| 383 | if (a.type == 7 && captured_params.at(key).s != a.as[i]) |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | // captured parameter |
| 388 | if (a.type == 5) |
| 389 | captured_params[key] = a.ai[i]; |
| 390 | if (a.type == 6) |
| 391 | captured_params[key] = a.af[i]; |
| 392 | if (a.type == 7) |
| 393 | captured_params[key] = a.as[i]; |
| 394 | } |
| 395 | else if ((elem[0] != '-' && (elem[0] < '0' || elem[0] > '9')) || (elem[0] == '-' && (elem[1] < '0' || elem[1] > '9'))) |
| 396 | { |
| 397 | // string |
| 398 | if (a.type != 7) |
| 399 | return false; |
| 400 | |
| 401 | if (a.as[i] != elem) |
| 402 | return false; |
| 403 | } |
| 404 | else if (elem.find('.') != std::string::npos || elem.find('e') != std::string::npos) |
| 405 | { |
| 406 | // float |
| 407 | if (a.type != 6) |
| 408 | return false; |
| 409 | |
| 410 | if (a.af[i] != std::stof(elem)) |
| 411 | return false; |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | // integer |
| 416 | if (a.type != 5) |
| 417 | return false; |
| 418 | |
| 419 | if (a.ai[i] != std::stoi(elem)) |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | i++; |
| 424 | } |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | if (a.type != b.type) |
| 430 | { |
| 431 | if (a.type == 2 && b.type == 3) |
| 432 | return a.i == b.f; |
| 433 | |
| 434 | if (a.type == 3 && b.type == 2) |
| 435 | return a.f == b.i; |
| 436 | |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | const int type = a.type; |
| 441 | |
| 442 | if (type == 0) |
| 443 | { |
| 444 | return true; |
| 445 | } |
| 446 | if (type == 1) |
| 447 | { |
| 448 | return a.b == b.b; |
| 449 | } |
| 450 | if (type == 2) |
| 451 | { |
| 452 | return a.i == b.i; |
| 453 | } |
| 454 | if (type == 3) |
| 455 | { |
| 456 | return a.f == b.f; |
| 457 | } |
| 458 | if (type == 4) |
| 459 | { |
| 460 | return a.s == b.s; |
| 461 | } |
| 462 | if (type == 5) |
| 463 | { |
| 464 | if (a.ai.size() != b.ai.size()) |
| 465 | return false; |
| 466 | |
| 467 | for (size_t i = 0; i < a.ai.size(); i++) |
| 468 | { |
| 469 | if (a.ai[i] != b.ai[i]) |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | return true; |
| 474 | } |
| 475 | if (type == 6) |
| 476 | { |
| 477 | if (a.af.size() != b.af.size()) |
| 478 | return false; |
| 479 | |
| 480 | for (size_t i = 0; i < a.af.size(); i++) |
| 481 | { |
| 482 | if (a.af[i] != b.af[i]) |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | if (type == 7) |
| 489 | { |
| 490 | if (a.as.size() != b.as.size()) |
| 491 | return false; |
| 492 | |
| 493 | for (size_t i = 0; i < a.as.size(); i++) |
| 494 | { |
| 495 | if (a.as[i] != b.as[i]) |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | // unknown |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | static bool match_attribute(const Attribute& a, const Attribute& b, std::map<std::string, Parameter>& captured_params, const std::string& attrname, std::map<std::string, Attribute>& captured_attrs) |
| 507 | { |
| 508 | // @data |
| 509 | // @data=(1,2,3,4)f32 |
| 510 | // @data=%op1.data |
| 511 | |
| 512 | if (b.type == 0) |
| 513 | { |
| 514 | std::string bs(b.data.begin(), b.data.end()); |
| 515 | if (bs.empty()) |
| 516 | { |
| 517 | // capture any shape |
| 518 | captured_attrs[attrname] = a; |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | if (bs[0] == '%') |
| 523 | { |
| 524 | // the captured replace |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | fprintf(stderr, "malformed attribute pattern %s\n", bs.c_str()); |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | const std::vector<int>& a_shape = a.shape; |
| 533 | const std::vector<int>& b_shape = b.shape; |
| 534 | if (b_shape.empty()) |
| 535 | return false; |
| 536 | |
| 537 | if (a_shape.empty()) |
| 538 | return false; |
| 539 | |
| 540 | if (a_shape.size() != b_shape.size()) |
| 541 | return false; |
| 542 | |
| 543 | for (size_t j = 0; j < a_shape.size(); j++) |
| 544 | { |
| 545 | int ai = a_shape[j]; |
| 546 | int bi = b_shape[j]; |
| 547 | if (ai == bi) |
| 548 | continue; |
| 549 | |
| 550 | if (bi == -1) |
| 551 | continue; |
| 552 | |
| 553 | if (bi > 0) |
| 554 | return false; |
| 555 | |
| 556 | if (bi != -233) |
| 557 | return false; |
| 558 | |
| 559 | std::string key = b.params.at(std::string("__shape__") + std::to_string(j)).s; |
| 560 | |
| 561 | if (captured_params.find(key) != captured_params.end()) |
| 562 | { |
| 563 | // match previous captured parameter |
| 564 | if (captured_params.at(key).i != ai) |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | // captured parameter |
| 569 | captured_params[key] = ai; |
| 570 | } |
| 571 | |
| 572 | captured_attrs[attrname] = a; |
| 573 | return true; |
| 574 | } |
| 575 | |
| 576 | static bool match_operator(const Operator* a, const Operator* b, std::map<std::string, Parameter>& captured_params, std::map<std::string, Attribute>& captured_attrs) |
| 577 | { |
| 578 | if (a->type != b->type) |
| 579 | return false; |
| 580 | |
| 581 | if (a->inputs.size() != b->inputs.size()) |
| 582 | return false; |
| 583 | |
| 584 | if (a->outputs.size() != b->outputs.size()) |
| 585 | return false; |
| 586 | |
| 587 | // match params |
| 588 | if (b->params.size() == 1 && b->params.find("%*") != b->params.end() && b->params.at("%*").type == 4 && b->params.at("%*").s == "%*") |
| 589 | { |
| 590 | for (const auto& p : a->params) |
| 591 | { |
| 592 | const std::string& pkey = p.first; |
| 593 | const Parameter& pp = p.second; |
| 594 | |
| 595 | // capture all parameters |
| 596 | captured_params[b->name + '.' + pkey] = pp; |
| 597 | } |
| 598 | } |
| 599 | else if (a->type == "pnnx.Expression") |
| 600 | { |
| 601 | if (!match_expression(a, b, captured_params)) |
| 602 | return false; |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | if (a->params.size() != b->params.size()) |
| 607 | return false; |
| 608 | |
| 609 | for (const auto& p : a->params) |
| 610 | { |
| 611 | const std::string& akey = p.first; |
| 612 | const Parameter& ap = p.second; |
| 613 | |
| 614 | if (b->params.find(akey) == b->params.end()) |
| 615 | return false; |
| 616 | |
| 617 | if (!match_parameter(ap, b->params.at(akey), captured_params)) |
| 618 | return false; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // match shapes |
| 623 | for (size_t i = 0; i < a->inputs.size(); i++) |
| 624 | { |
| 625 | int a_type = a->inputs[i]->type; |
| 626 | int b_type = b->inputs[i]->type; |
| 627 | if (b_type != 0 && a_type != b_type) |
| 628 | return false; |
| 629 | |
| 630 | const std::vector<int>& a_shape = a->inputs[i]->shape; |
| 631 | const std::vector<int>& b_shape = b->inputs[i]->shape; |
| 632 | if (b_shape.empty()) |
| 633 | continue; |
| 634 | |
| 635 | if (a_shape.empty()) |
| 636 | return false; |
| 637 | |
| 638 | if (a_shape.size() != b_shape.size()) |
| 639 | return false; |
| 640 | |
| 641 | for (size_t j = 0; j < a_shape.size(); j++) |
| 642 | { |
| 643 | int ai = a_shape[j]; |
| 644 | int bi = b_shape[j]; |
| 645 | if (ai == bi) |
| 646 | continue; |
| 647 | |
| 648 | if (bi == -1) |
| 649 | continue; |
| 650 | |
| 651 | if (bi > 0) |
| 652 | return false; |
| 653 | |
| 654 | if (bi != -233) |
| 655 | return false; |
| 656 | |
| 657 | std::string key = b->inputs[i]->params.at(std::string("__shape__") + std::to_string(j)).s; |
| 658 | |
| 659 | if (captured_params.find(key) != captured_params.end()) |
| 660 | { |
| 661 | // match previous captured parameter |
| 662 | if (captured_params.at(key).i != ai) |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | // captured parameter |
| 667 | captured_params[key] = ai; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | for (size_t i = 0; i < a->outputs.size(); i++) |
| 672 | { |
| 673 | int a_type = a->outputs[i]->type; |
| 674 | int b_type = b->outputs[i]->type; |
| 675 | if (b_type != 0 && a_type != b_type) |
| 676 | return false; |
| 677 | |
| 678 | const std::vector<int>& a_shape = a->outputs[i]->shape; |
| 679 | const std::vector<int>& b_shape = b->outputs[i]->shape; |
| 680 | if (b_shape.empty()) |
| 681 | continue; |
| 682 | |
| 683 | if (a_shape.empty()) |
| 684 | return false; |
| 685 | |
| 686 | if (a_shape.size() != b_shape.size()) |
| 687 | return false; |
| 688 | |
| 689 | for (size_t j = 0; j < a_shape.size(); j++) |
| 690 | { |
| 691 | int ai = a_shape[j]; |
| 692 | int bi = b_shape[j]; |
| 693 | if (ai == bi) |
| 694 | continue; |
| 695 | |
| 696 | if (bi == -1) |
| 697 | continue; |
| 698 | |
| 699 | if (bi > 0) |
| 700 | return false; |
| 701 | |
| 702 | if (bi != -233) |
| 703 | return false; |
| 704 | |
| 705 | std::string key = b->outputs[i]->params.at(std::string("__shape__") + std::to_string(j)).s; |
| 706 | |
| 707 | if (captured_params.find(key) != captured_params.end()) |
| 708 | { |
| 709 | // match previous captured parameter |
| 710 | if (captured_params.at(key).i != ai) |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | // captured parameter |
| 715 | captured_params[key] = ai; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | for (const auto& p : a->attrs) |
| 720 | { |
| 721 | const std::string& akey = p.first; |
| 722 | const Attribute& aa = p.second; |
| 723 | |
| 724 | std::string attrname = b->name + '.' + akey; |
| 725 | |
| 726 | if (b->attrs.find(akey) == b->attrs.end()) |
| 727 | { |
| 728 | // capture all attributes |
| 729 | captured_attrs[attrname] = aa; |
| 730 | } |
| 731 | else |
| 732 | { |
| 733 | if (!match_attribute(aa, b->attrs.at(akey), captured_params, attrname, captured_attrs)) |
| 734 | return false; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | return true; |
| 739 | } |
| 740 | |
| 741 | static bool match(const Operator* anchor, const Operator* pattern, std::map<std::string, const Operator*>& matched_operators, std::map<std::string, const Operand*>& matched_inputs, std::map<std::string, const Operand*>& matched_outputs, std::map<std::string, Parameter>& captured_params, std::map<std::string, Attribute>& captured_attrs) |
| 742 | { |
| 743 | if (!match_operator(anchor, pattern, captured_params, captured_attrs)) |
| 744 | return false; |
| 745 | |
| 746 | for (size_t i = 0; i < pattern->outputs.size(); i++) |
| 747 | { |
| 748 | if (pattern->outputs[i]->consumers.size() == 1 && pattern->outputs[i]->consumers[0]->type == "pnnx.Output") |
| 749 | { |
| 750 | if (matched_outputs.find(pattern->outputs[i]->name) == matched_outputs.end()) |
| 751 | { |
| 752 | matched_outputs[pattern->outputs[i]->name] = anchor->outputs[i]; |
| 753 | } |
| 754 | else if (matched_outputs[pattern->outputs[i]->name] != anchor->outputs[i]) |
| 755 | { |
| 756 | return false; |
| 757 | } |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | if (anchor->outputs[i]->consumers.size() != pattern->outputs[i]->consumers.size()) |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | matched_operators[pattern->name] = anchor; |
| 766 | |
| 767 | // lets match |
| 768 | for (size_t i = 0; i < pattern->inputs.size(); i++) |
| 769 | { |
| 770 | const Operator* anchor2 = anchor->inputs[i]->producer; |
| 771 | const Operator* pattern2 = pattern->inputs[i]->producer; |
| 772 | |
| 773 | if (pattern2->type == "pnnx.Input") |
| 774 | { |
| 775 | if (matched_inputs.find(pattern->inputs[i]->name) == matched_inputs.end()) |
| 776 | { |
| 777 | matched_inputs[pattern->inputs[i]->name] = anchor->inputs[i]; |
| 778 | } |
| 779 | else if (matched_inputs[pattern->inputs[i]->name] != anchor->inputs[i]) |
| 780 | { |
| 781 | return false; |
| 782 | } |
| 783 | continue; |
| 784 | } |
| 785 | |
| 786 | if (!match(anchor2, pattern2, matched_operators, matched_inputs, matched_outputs, captured_params, captured_attrs)) |
| 787 | return false; |
| 788 | } |
| 789 | |
| 790 | return true; |
| 791 | } |
| 792 | |
| 793 | void pnnx_graph_rewrite(Graph& graph, const GraphRewriterPass* pass, int& opindex) |
| 794 | { |
| 795 | Graph pattern_graph; |
| 796 | pattern_graph.parse(pass->match_pattern_graph()); |
| 797 | |
| 798 | // collect pattern inputs and outputs order |
| 799 | std::vector<std::string> pattern_graph_inputs; |
| 800 | std::vector<std::string> pattern_graph_outputs; |
| 801 | std::vector<const Operator*> pattern_graph_output_operators; |
| 802 | for (const auto& x : pattern_graph.ops) |
| 803 | { |
| 804 | if (x->type == "pnnx.Input") |
| 805 | { |
| 806 | for (const auto& y : x->outputs) |
| 807 | pattern_graph_inputs.push_back(y->name); |
| 808 | } |
| 809 | if (x->type == "pnnx.Output") |
| 810 | { |
| 811 | pattern_graph_output_operators.push_back(x); |
| 812 | for (const auto& y : x->inputs) |
| 813 | pattern_graph_outputs.push_back(y->name); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | std::vector<Operator*> new_ops; |
| 818 | |
| 819 | while (1) |
| 820 | { |
| 821 | const int graph_op_count = (int)graph.ops.size(); |
| 822 | |
| 823 | bool matched = true; |
| 824 | |
| 825 | // lets match from output |
| 826 | std::map<std::string, const Operator*> matched_operators; |
| 827 | std::map<std::string, const Operand*> matched_inputs; |
| 828 | std::map<std::string, const Operand*> matched_outputs; |
| 829 | std::map<std::string, Parameter> captured_params; |
| 830 | std::map<std::string, Attribute> captured_attrs; |
| 831 | |
| 832 | // pattern match from end to beginning |
| 833 | int q = graph_op_count - 1; |
| 834 | for (; q >= 1; q--) |
| 835 | { |
| 836 | matched = true; |
| 837 | |
| 838 | for (const Operator* pattern : pattern_graph_output_operators) |
| 839 | { |
| 840 | for (size_t i = 0; i < pattern->inputs.size(); i++) |
| 841 | { |
| 842 | const Operator* pattern2 = pattern->inputs[i]->producer; |
| 843 | |
| 844 | int j = q; |
| 845 | for (; j >= 0; j--) |
| 846 | { |
| 847 | const Operator* anchor = graph.ops[j]; |
| 848 | |
| 849 | std::map<std::string, const Operator*> matched_operators2; |
| 850 | std::map<std::string, const Operand*> matched_inputs2; |
| 851 | std::map<std::string, const Operand*> matched_outputs2; |
| 852 | std::map<std::string, Parameter> captured_params2; |
| 853 | std::map<std::string, Attribute> captured_attrs2; |
| 854 | if (!match(anchor, pattern2, matched_operators2, matched_inputs2, matched_outputs2, captured_params2, captured_attrs2)) |
| 855 | continue; |
| 856 | |
| 857 | bool submatch_matched = true; |
| 858 | for (auto x : matched_operators2) |
| 859 | { |
| 860 | // check these matched operators are same with previous matched ones |
| 861 | if (matched_operators.find(x.first) != matched_operators.end()) |
| 862 | { |
| 863 | if (matched_operators[x.first] != x.second) |
| 864 | { |
| 865 | // unmatched two sub-matches |
| 866 | submatch_matched = false; |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | matched_operators[x.first] = x.second; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (!submatch_matched) |
| 877 | continue; |
| 878 | |
| 879 | for (auto x : matched_inputs2) |
| 880 | { |
| 881 | if (matched_inputs.find(x.first) == matched_inputs.end()) |
| 882 | { |
| 883 | matched_inputs[x.first] = x.second; |
| 884 | } |
| 885 | } |
| 886 | for (auto x : matched_outputs2) |
| 887 | { |
| 888 | if (matched_outputs.find(x.first) == matched_outputs.end()) |
| 889 | { |
| 890 | matched_outputs[x.first] = x.second; |
| 891 | } |
| 892 | } |
| 893 | for (auto x : captured_params2) |
| 894 | { |
| 895 | captured_params[x.first] = x.second; |
| 896 | } |
| 897 | for (auto x : captured_attrs2) |
| 898 | { |
| 899 | captured_attrs[x.first] = x.second; |
| 900 | } |
| 901 | |
| 902 | // match ! |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | if (j == -1) |
| 907 | { |
| 908 | matched = false; |
| 909 | break; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (!matched) |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | if (matched && !pass->match(matched_operators, captured_params, captured_attrs)) |
| 918 | { |
| 919 | matched_operators.clear(); |
| 920 | matched_inputs.clear(); |
| 921 | matched_outputs.clear(); |
| 922 | captured_params.clear(); |
| 923 | captured_attrs.clear(); |
| 924 | matched = false; |
| 925 | continue; |
| 926 | } |
| 927 | |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | if (!matched) |
| 932 | break; |
| 933 | |
| 934 | // fprintf(stderr, "matched !\n"); |
| 935 | |
| 936 | // lets replace |
| 937 | |
| 938 | // remove all operands inside matched graph |
| 939 | std::map<std::string, Operand*> operands_to_remove; |
| 940 | for (auto& _x : matched_operators) |
| 941 | { |
| 942 | Operator* x = (Operator*)_x.second; |
| 943 | for (auto& r : x->inputs) |
| 944 | { |
| 945 | r->remove_consumer(x); |
| 946 | |
| 947 | bool is_input = false; |
| 948 | for (auto& r2 : matched_inputs) |
| 949 | { |
| 950 | if (r2.second == r) |
| 951 | { |
| 952 | is_input = true; |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | if (!is_input) |
| 958 | operands_to_remove[r->name] = r; |
| 959 | } |
| 960 | |
| 961 | x->inputs.clear(); |
| 962 | |
| 963 | for (auto& r : x->outputs) |
| 964 | { |
| 965 | r->producer = 0; |
| 966 | |
| 967 | bool is_output = false; |
| 968 | for (auto& r2 : matched_outputs) |
| 969 | { |
| 970 | if (r2.second == r) |
| 971 | { |
| 972 | is_output = true; |
| 973 | break; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | if (!is_output) |
| 978 | operands_to_remove[r->name] = r; |
| 979 | } |
| 980 | |
| 981 | x->outputs.clear(); |
| 982 | } |
| 983 | for (auto& _x : operands_to_remove) |
| 984 | { |
| 985 | Operand* r = _x.second; |
| 986 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), r)); |
| 987 | delete r; |
| 988 | } |
| 989 | |
| 990 | // insert new operator at the last matched one |
| 991 | const Operator* cur = 0; |
| 992 | { |
| 993 | int cur_index = 1; |
| 994 | for (auto& o : matched_operators) |
| 995 | { |
| 996 | int c_index = std::find(graph.ops.begin(), graph.ops.end(), o.second) - graph.ops.begin(); |
| 997 | cur_index = std::max(cur_index, c_index + 1); |
| 998 | } |
| 999 | |
| 1000 | cur_index = std::min(cur_index, (int)graph.ops.size() - 1); |
| 1001 | cur = graph.ops[cur_index]; |
| 1002 | } |
| 1003 | |
| 1004 | // remove all matched_operators |
| 1005 | for (auto& _x : matched_operators) |
| 1006 | { |
| 1007 | // fprintf(stderr, "remove %s\n", _x.second->name.c_str()); |
| 1008 | |
| 1009 | Operator* x = (Operator*)_x.second; |
| 1010 | |
| 1011 | graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), x)); |
| 1012 | |
| 1013 | delete _x.second; |
| 1014 | } |
| 1015 | |
| 1016 | if (pass->replace_pattern_graph() == 0) |
| 1017 | { |
| 1018 | // insert single |
| 1019 | Operator* op = graph.new_operator_before(pass->type_str(), std::string(pass->name_str()), cur); |
| 1020 | |
| 1021 | for (const auto& k : pattern_graph_inputs) |
| 1022 | { |
| 1023 | Operand* r = (Operand*)matched_inputs.at(k); |
| 1024 | r->consumers.push_back(op); |
| 1025 | op->inputs.push_back(r); |
| 1026 | |
| 1027 | op->inputnames.push_back(k); |
| 1028 | } |
| 1029 | |
| 1030 | for (const auto& k : pattern_graph_outputs) |
| 1031 | { |
| 1032 | Operand* r = (Operand*)matched_outputs.at(k); |
| 1033 | r->producer = op; |
| 1034 | op->outputs.push_back(r); |
| 1035 | } |
| 1036 | |
| 1037 | pass->write(op, captured_params, captured_attrs); |
| 1038 | |
| 1039 | new_ops.push_back(op); |
| 1040 | } |
| 1041 | else |
| 1042 | { |
| 1043 | // insert multiple |
| 1044 | Graph replace_graph; |
| 1045 | replace_graph.parse(pass->replace_pattern_graph()); |
| 1046 | |
| 1047 | // move operators and operands from replace_graph to graph except input and output |
| 1048 | std::map<std::string, Operator*> ops; |
| 1049 | for (size_t i = 0; i < replace_graph.ops.size(); i++) |
| 1050 | { |
| 1051 | Operator* op = replace_graph.ops[i]; |
| 1052 | if (op->type == "pnnx.Input" || op->type == "pnnx.Output") |
| 1053 | continue; |
| 1054 | |
| 1055 | graph.ops.insert(std::find(graph.ops.begin(), graph.ops.end(), cur), op); |
| 1056 | replace_graph.ops[i] = 0; |
| 1057 | ops[op->name] = op; |
| 1058 | } |
| 1059 | |
| 1060 | for (size_t i = 0; i < replace_graph.operands.size(); i++) |
| 1061 | { |
| 1062 | Operand* r = replace_graph.operands[i]; |
| 1063 | if (r->producer->type == "pnnx.Input" || (r->consumers.size() == 1 && r->consumers[0]->type == "pnnx.Output")) |
| 1064 | continue; |
| 1065 | |
| 1066 | graph.operands.push_back(r); |
| 1067 | replace_graph.operands[i] = 0; |
| 1068 | } |
| 1069 | |
| 1070 | replace_graph.ops.erase(std::remove(replace_graph.ops.begin(), replace_graph.ops.end(), (Operator*)0), replace_graph.ops.end()); |
| 1071 | replace_graph.operands.erase(std::remove(replace_graph.operands.begin(), replace_graph.operands.end(), (Operand*)0), replace_graph.operands.end()); |
| 1072 | |
| 1073 | for (size_t i = 0; i < pattern_graph_inputs.size(); i++) |
| 1074 | { |
| 1075 | const std::string& k = pattern_graph_inputs[i]; |
| 1076 | Operand* r = (Operand*)matched_inputs.at(k); |
| 1077 | const Operand* rr = replace_graph.get_operand(k); |
| 1078 | |
| 1079 | for (auto x : rr->consumers) |
| 1080 | { |
| 1081 | r->consumers.push_back(x); |
| 1082 | |
| 1083 | x->inputnames.resize(x->inputs.size()); |
| 1084 | for (size_t j = 0; j < x->inputs.size(); j++) |
| 1085 | { |
| 1086 | if (x->inputs[j]->name == k) |
| 1087 | { |
| 1088 | x->inputs[j] = r; |
| 1089 | x->inputnames[j] = k; |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | for (size_t i = 0; i < pattern_graph_outputs.size(); i++) |
| 1097 | { |
| 1098 | const std::string& k = pattern_graph_outputs[i]; |
| 1099 | Operand* r = (Operand*)matched_outputs.at(k); |
| 1100 | const Operand* rr = replace_graph.get_operand(k); |
| 1101 | |
| 1102 | r->producer = rr->producer; |
| 1103 | |
| 1104 | for (size_t j = 0; j < r->producer->outputs.size(); j++) |
| 1105 | { |
| 1106 | if (r->producer->outputs[j]->name == k) |
| 1107 | { |
| 1108 | r->producer->outputs[j] = r; |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | pass->write(ops, captured_params, captured_attrs); |
| 1115 | |
| 1116 | for (auto x : ops) |
| 1117 | { |
| 1118 | new_ops.push_back(x.second); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | // assign new op name number |
| 1124 | for (int i = (int)new_ops.size() - 1; i >= 0; i--) |
| 1125 | { |
| 1126 | new_ops[i]->name = new_ops[i]->name + "_" + std::to_string(opindex++); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | static bool is_alias_op(const Operator* op) |
| 1131 | { |
| 1132 | if (op->type == "aten::slice" || op->type == "aten::select") |
| 1133 | return true; |
| 1134 | |
| 1135 | if (op->type == "aten::view") |
| 1136 | return true; |
| 1137 | |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
| 1141 | static void functionize(Graph& graph) |
| 1142 | { |
| 1143 | // graph.save("0.param", "0.bin"); |
| 1144 | |
| 1145 | // 1. create shadow view/slice/select/... for each consumer |
| 1146 | // 2. replace inplace op, append copy |
| 1147 | // 3. tag operand alias for view/slice/select/... output |
| 1148 | // 4. scan inplace op, collect affacted alias |
| 1149 | // 5. look for any op after the inplace op with alias input |
| 1150 | // 6. collect ops on the chain back to alias |
| 1151 | // 7. move chain after copy op |
| 1152 | // 8. update all alias uses after copy op, retag alias |
| 1153 | // 9. clear all alias tag |
| 1154 | |
| 1155 | // 1. create shadow view/slice/select/... for each consumer |
| 1156 | { |
| 1157 | for (int i = (int)graph.ops.size() - 1; i >= 0; i--) |
| 1158 | { |
| 1159 | Operator* op = graph.ops[i]; |
| 1160 | |
| 1161 | if (!is_alias_op(op)) |
| 1162 | continue; |
| 1163 | |
| 1164 | Operand* out0 = op->outputs[0]; |
| 1165 | |
| 1166 | if (out0->consumers.size() == 1) |
| 1167 | continue; |
| 1168 | |
| 1169 | bool all_consumers_are_same = true; |
| 1170 | for (size_t j = 1; j < out0->consumers.size(); j++) |
| 1171 | { |
| 1172 | if (out0->consumers[j] != out0->consumers[0]) |
| 1173 | { |
| 1174 | all_consumers_are_same = false; |
| 1175 | break; |
| 1176 | } |
| 1177 | } |
| 1178 | if (all_consumers_are_same) |
| 1179 | continue; |
| 1180 | |
| 1181 | for (int j = (int)out0->consumers.size() - 1; j > 0; j--) |
| 1182 | { |
| 1183 | Operator* op1 = out0->consumers[j]; |
| 1184 | |
| 1185 | Operator* op_shadow = graph.new_operator_after(op->type, op->name + "_pnnxshadow_" + std::to_string(j), op); |
| 1186 | |
| 1187 | Operand* shadow_out = graph.new_operand(op_shadow->name + "_out"); |
| 1188 | |
| 1189 | op_shadow->inputs = op->inputs; |
| 1190 | op_shadow->params = op->params; |
| 1191 | op_shadow->outputs.push_back(shadow_out); |
| 1192 | |
| 1193 | for (Operand* x : op->inputs) |
| 1194 | { |
| 1195 | x->consumers.push_back(op_shadow); |
| 1196 | } |
| 1197 | |
| 1198 | shadow_out->producer = op_shadow; |
| 1199 | shadow_out->type = out0->type; |
| 1200 | shadow_out->shape = out0->shape; |
| 1201 | shadow_out->params = out0->params; |
| 1202 | |
| 1203 | shadow_out->consumers.push_back(op1); |
| 1204 | |
| 1205 | for (size_t k = 0; k < op1->inputs.size(); k++) |
| 1206 | { |
| 1207 | if (op1->inputs[k] == out0) |
| 1208 | op1->inputs[k] = shadow_out; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | out0->consumers.resize(1); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // graph.save("1.param", "1.bin"); |
| 1217 | |
| 1218 | // 2. replace inplace op, append copy |
| 1219 | // 3. tag operand alias for view/slice/select/... output |
| 1220 | { |
| 1221 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 1222 | { |
| 1223 | Operator* op = graph.ops[i]; |
| 1224 | |
| 1225 | bool is_inplace_op = op->type.size() > 2 && op->type[op->type.size() - 2] != '_' && op->type[op->type.size() - 1] == '_'; |
| 1226 | |
| 1227 | if (op->type != "aten::copy_" && !is_alias_op(op) && !is_inplace_op) |
| 1228 | continue; |
| 1229 | |
| 1230 | Operand* in = op->inputs[0]; |
| 1231 | |
| 1232 | int alias_index; |
| 1233 | if (in->params.find("__alias__") != in->params.end()) |
| 1234 | { |
| 1235 | alias_index = in->params.at("__alias__").i; |
| 1236 | } |
| 1237 | else |
| 1238 | { |
| 1239 | alias_index = std::find(graph.operands.begin(), graph.operands.end(), in) - graph.operands.begin(); |
| 1240 | } |
| 1241 | |
| 1242 | if (op->type == "aten::copy_") |
| 1243 | { |
| 1244 | op->outputs[0]->params["__alias__"] = alias_index; |
| 1245 | // fprintf(stderr, "operand %s is alias of %s\n", op->outputs[0]->name.c_str(), graph.operands[alias_index]->name.c_str()); |
| 1246 | |
| 1247 | // set copy output shape as the alias one |
| 1248 | op->outputs[0]->type = graph.operands[alias_index]->type; |
| 1249 | op->outputs[0]->shape = graph.operands[alias_index]->shape; |
| 1250 | |
| 1251 | continue; |
| 1252 | } |
| 1253 | |
| 1254 | if (is_alias_op(op)) |
| 1255 | { |
| 1256 | op->outputs[0]->params["__alias__"] = alias_index; |
| 1257 | // fprintf(stderr, "operand %s is alias of %s\n", op->outputs[0]->name.c_str(), graph.operands[alias_index]->name.c_str()); |
| 1258 | continue; |
| 1259 | } |
| 1260 | |
| 1261 | if (is_inplace_op) |
| 1262 | { |
| 1263 | // replace with non-inplace version, create copy op |
| 1264 | op->type = op->type.substr(0, op->type.size() - 1); |
| 1265 | |
| 1266 | // append aten::copy_ |
| 1267 | if (graph.operands[alias_index]->consumers.size() > 1) |
| 1268 | { |
| 1269 | Operand* in0 = op->inputs[0]; |
| 1270 | Operand* out0 = op->outputs[0]; |
| 1271 | |
| 1272 | Operator* op_copy = graph.new_operator_after("aten::copy_", op->name + "_copy", op); |
| 1273 | Operand* copy_out = graph.new_operand(op->name + "_copy_out"); |
| 1274 | |
| 1275 | op_copy->inputs.push_back(in0); |
| 1276 | op_copy->inputs.push_back(out0); |
| 1277 | in0->consumers.push_back(op_copy); |
| 1278 | out0->consumers.push_back(op_copy); |
| 1279 | |
| 1280 | op_copy->outputs.push_back(copy_out); |
| 1281 | copy_out->producer = op_copy; |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | // graph.save("3.param", "3.bin"); |
| 1288 | |
| 1289 | // 4. scan inplace copy op, collect affacted alias |
| 1290 | { |
| 1291 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 1292 | { |
| 1293 | Operator* op = graph.ops[i]; |
| 1294 | |
| 1295 | if (op->type != "aten::copy_") |
| 1296 | continue; |
| 1297 | |
| 1298 | op->type = "aten::copy"; |
| 1299 | |
| 1300 | Operand* out0 = op->outputs[0]; |
| 1301 | |
| 1302 | // inplace op output always alias with the input |
| 1303 | const int alias_index = out0->params.at("__alias__").i; |
| 1304 | Operand* alias_in0 = graph.operands[alias_index]; |
| 1305 | |
| 1306 | // fprintf(stderr, "\n---> %s for %s\n", op->name.c_str(), alias_in0->name.c_str()); |
| 1307 | |
| 1308 | size_t i_advanced = 0; |
| 1309 | |
| 1310 | // 5. look for any op after the inplace op with alias input |
| 1311 | for (size_t j = i + 1; j < graph.ops.size(); j++) |
| 1312 | { |
| 1313 | Operator* op1 = graph.ops[j]; |
| 1314 | |
| 1315 | bool affacted = false; |
| 1316 | for (Operand* x : op1->inputs) |
| 1317 | { |
| 1318 | if (x == alias_in0) |
| 1319 | { |
| 1320 | affacted = true; |
| 1321 | break; |
| 1322 | } |
| 1323 | |
| 1324 | if (x->params.find("__alias__") == x->params.end()) |
| 1325 | continue; |
| 1326 | |
| 1327 | int alias_index_1 = x->params.at("__alias__").i; |
| 1328 | if (alias_index_1 == alias_index) |
| 1329 | { |
| 1330 | affacted = true; |
| 1331 | break; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if (!affacted) |
| 1336 | continue; |
| 1337 | |
| 1338 | // 6. collect ops on the chain back to alias |
| 1339 | std::set<size_t> chainsx_op_indexes; |
| 1340 | { |
| 1341 | size_t op1_index = std::find(graph.ops.begin(), graph.ops.end(), op1) - graph.ops.begin(); |
| 1342 | |
| 1343 | if (op1_index < i - i_advanced) |
| 1344 | { |
| 1345 | chainsx_op_indexes.insert(op1_index); |
| 1346 | // fprintf(stderr, "affacted op %s for %s\n", op1->name.c_str(), graph.operands[alias_index]->name.c_str()); |
| 1347 | } |
| 1348 | |
| 1349 | while (1) |
| 1350 | { |
| 1351 | Operand* x = op1->inputs[0]; |
| 1352 | if (x->params.find("__alias__") == x->params.end()) |
| 1353 | break; |
| 1354 | |
| 1355 | int alias_index_1 = x->params.at("__alias__").i; |
| 1356 | if (alias_index_1 != alias_index) |
| 1357 | break; |
| 1358 | |
| 1359 | op1 = x->producer; |
| 1360 | size_t op1_index = std::find(graph.ops.begin(), graph.ops.end(), op1) - graph.ops.begin(); |
| 1361 | |
| 1362 | if (op1_index < i - i_advanced) |
| 1363 | { |
| 1364 | chainsx_op_indexes.insert(op1_index); |
| 1365 | // fprintf(stderr, "affacted op %s for %s chained\n", op1->name.c_str(), graph.operands[alias_index]->name.c_str()); |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | // 7. move chain after copy op |
| 1371 | { |
| 1372 | int k = 0; |
| 1373 | for (size_t doi : chainsx_op_indexes) |
| 1374 | { |
| 1375 | doi -= k; |
| 1376 | // fprintf(stderr, "---> move %s after %s\n", graph.ops[doi]->name.c_str(), graph.ops[i - i_advanced]->name.c_str()); |
| 1377 | |
| 1378 | for (size_t l = doi; l < i - i_advanced; l++) |
| 1379 | { |
| 1380 | std::swap(graph.ops[l], graph.ops[l + 1]); |
| 1381 | } |
| 1382 | |
| 1383 | k += 1; |
| 1384 | } |
| 1385 | |
| 1386 | i_advanced += chainsx_op_indexes.size(); |
| 1387 | } |
| 1388 | |
| 1389 | // 8. update all alias uses after copy op, retag alias |
| 1390 | out0->params.erase("__alias__"); |
| 1391 | const int new_alias_index = std::find(graph.operands.begin(), graph.operands.end(), out0) - graph.operands.begin(); |
| 1392 | for (size_t k = i - i_advanced + 1; k < graph.ops.size(); k++) |
| 1393 | { |
| 1394 | Operator* op2 = graph.ops[k]; |
| 1395 | |
| 1396 | // bool use_in0 = false; |
| 1397 | for (size_t l = 0; l < op2->inputs.size(); l++) |
| 1398 | { |
| 1399 | if (op2->inputs[l] == alias_in0) |
| 1400 | { |
| 1401 | // fprintf(stderr, "---> replace %s input %s to %s\n", op2->name.c_str(), op2->inputs[l]->name.c_str(), out0->name.c_str()); |
| 1402 | |
| 1403 | op2->inputs[l] = out0; |
| 1404 | alias_in0->remove_consumer(op2); |
| 1405 | out0->consumers.push_back(op2); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | for (Operand* x : op2->outputs) |
| 1410 | { |
| 1411 | if (x->params.find("__alias__") != x->params.end() && x->params.at("__alias__").i == alias_index) |
| 1412 | { |
| 1413 | x->params["__alias__"] = new_alias_index; |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | // rewind to the updated copy operator |
| 1419 | j -= chainsx_op_indexes.size(); |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | // graph.save("4.param", "4.bin"); |
| 1425 | |
| 1426 | // 9. clear all alias tag |
| 1427 | { |
| 1428 | for (Operand* x : graph.operands) |
| 1429 | { |
| 1430 | x->params.erase("__alias__"); |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | void pass_level2(Graph& g) |
| 1436 | { |
| 1437 | functionize(g); |
| 1438 | |
| 1439 | int opindex = 0; |
| 1440 | for (auto x : g_global_pnnx_graph_rewriter_passes) |
| 1441 | { |
| 1442 | for (auto rewriter : x.second) |
| 1443 | { |
| 1444 | pnnx_graph_rewrite(g, rewriter, opindex); |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | } // namespace pnnx |