| 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 "fuse_slice_indices.h" |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <algorithm> |
| 19 | #include <stack> |
| 20 | #include <vector> |
| 21 | #include "pass_level2.h" |
| 22 | |
| 23 | namespace pnnx { |
| 24 | |
| 25 | void fuse_slice_indices(Graph& graph) |
| 26 | { |
| 27 | while (1) |
| 28 | { |
| 29 | bool matched = false; |
| 30 | |
| 31 | for (int i = (int)graph.ops.size() - 1; i >= 0; i--) |
| 32 | { |
| 33 | Operator* op = graph.ops[i]; |
| 34 | |
| 35 | if (op->type != "Tensor.slice" && op->type != "Tensor.select") |
| 36 | continue; |
| 37 | |
| 38 | if (op->has_param("dims")) |
| 39 | { |
| 40 | // skip fused ones |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | if (!op->has_param("dim")) |
| 45 | { |
| 46 | fprintf(stderr, "dynamic dim in slice/select chain is not supported\n"); |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | bool static_starts = true; |
| 51 | bool static_ends = true; |
| 52 | bool static_steps = true; |
| 53 | bool static_selects = true; |
| 54 | |
| 55 | if (op->type == "Tensor.slice") |
| 56 | { |
| 57 | if (!op->has_param("start")) static_starts = false; |
| 58 | if (!op->has_param("end")) static_ends = false; |
| 59 | if (!op->has_param("step")) static_steps = false; |
| 60 | } |
| 61 | else // if (op->type == "Tensor.select") |
| 62 | { |
| 63 | if (!op->has_param("index")) static_selects = false; |
| 64 | } |
| 65 | |
| 66 | int descent_dim_current = op->params.at("dim").i; |
| 67 | |
| 68 | // collect slice op chain |
| 69 | std::stack<Operator*> slice_select_ops; |
| 70 | Operator* top_sop = op; |
| 71 | Operand* in0 = op->inputs[0]; |
| 72 | while (in0->producer->type == "Tensor.slice" || in0->producer->type == "Tensor.select") |
| 73 | { |
| 74 | Operator* sop = in0->producer; |
| 75 | if (in0->consumers.size() != 1) |
| 76 | { |
| 77 | // not single chain |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | if (sop->has_param("dims")) |
| 82 | { |
| 83 | // skip fused ones |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | if (!sop->has_param("dim")) |
| 88 | { |
| 89 | fprintf(stderr, "dynamic dim in slice/select chain is not supported\n"); |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | if (sop->type == "Tensor.slice") |
| 94 | { |
| 95 | if (!sop->has_param("start")) static_starts = false; |
| 96 | if (!sop->has_param("end")) static_ends = false; |
| 97 | if (!sop->has_param("step")) static_steps = false; |
| 98 | } |
| 99 | else // if (sop->type == "Tensor.select") |
| 100 | { |
| 101 | if (!sop->has_param("index")) static_selects = false; |
| 102 | } |
| 103 | |
| 104 | int dim = sop->params.at("dim").i; |
| 105 | |
| 106 | if (dim < 0 && descent_dim_current <= dim) |
| 107 | { |
| 108 | // not adjacent slice/select in chain |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | if (dim < 0 && descent_dim_current >= 0) |
| 113 | { |
| 114 | // not adjacent slice/select in chain |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | // only allow select on same dim |
| 119 | if (sop->type == "Tensor.select") |
| 120 | { |
| 121 | if (descent_dim_current >= 0 && dim >= 0 && descent_dim_current < dim) |
| 122 | { |
| 123 | // not adjacent slice/select in chain |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | if (descent_dim_current >= 0 && dim >= 0 && descent_dim_current <= dim) |
| 130 | { |
| 131 | // not adjacent slice/select in chain |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | descent_dim_current = dim; |
| 137 | |
| 138 | slice_select_ops.push(sop); |
| 139 | top_sop = sop; |
| 140 | in0 = sop->inputs[0]; |
| 141 | } |
| 142 | |
| 143 | if (slice_select_ops.empty()) |
| 144 | { |
| 145 | // single orphaned slice/select |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | matched = true; |
| 150 | |
| 151 | // construct one-step slice |
| 152 | std::vector<int> new_dims; |
| 153 | std::vector<int> new_starts; |
| 154 | std::vector<int> new_ends; |
| 155 | std::vector<int> new_steps; |
| 156 | std::vector<int> new_selects; |
| 157 | Operator* op_starts = 0; |
| 158 | Operator* op_ends = 0; |
| 159 | Operator* op_steps = 0; |
| 160 | Operator* op_selects = 0; |
| 161 | if (!static_starts) op_starts = graph.new_operator_before("pnnx.SliceIndexes", op->name + "_ncnnstarts", op); |
| 162 | if (!static_ends) op_ends = graph.new_operator_before("pnnx.SliceIndexes", op->name + "_ncnnends", op); |
| 163 | if (!static_steps) op_steps = graph.new_operator_before("pnnx.SliceIndexes", op->name + "_ncnnsteps", op); |
| 164 | if (!static_selects) op_selects = graph.new_operator_before("pnnx.SliceIndexes", op->name + "_ncnnselects", op); |
| 165 | |
| 166 | std::vector<std::string> starts_indexes; |
| 167 | std::vector<std::string> ends_indexes; |
| 168 | std::vector<std::string> steps_indexes; |
| 169 | std::vector<std::string> selects_indexes; |
| 170 | |
| 171 | int select_dim_offset = 0; |
| 172 | |
| 173 | while (!slice_select_ops.empty()) |
| 174 | { |
| 175 | Operator* sop = slice_select_ops.top(); |
| 176 | slice_select_ops.pop(); |
| 177 | |
| 178 | new_dims.push_back(select_dim_offset + sop->params.at("dim").i); |
| 179 | |
| 180 | if (sop->type == "Tensor.slice") |
| 181 | { |
| 182 | if (static_starts) |
| 183 | { |
| 184 | new_starts.push_back(sop->params.at("start").type == 0 ? 0 : sop->params.at("start").i); |
| 185 | } |
| 186 | else if (sop->has_param("start")) |
| 187 | { |
| 188 | char tmp[32]; |
| 189 | if (sop->params.at("start").type == 0) |
| 190 | { |
| 191 | sprintf(tmp, "0"); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | sprintf(tmp, "%d", sop->params.at("start").i); |
| 196 | } |
| 197 | starts_indexes.push_back(tmp); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | char tmp[32]; |
| 202 | sprintf(tmp, "@%d", (int)op_starts->inputs.size()); |
| 203 | starts_indexes.push_back(tmp); |
| 204 | Operand* start = sop->named_input("start"); |
| 205 | op_starts->inputs.push_back(start); |
| 206 | start->remove_consumer(sop); |
| 207 | start->consumers.push_back(op_starts); |
| 208 | } |
| 209 | |
| 210 | if (static_ends) |
| 211 | { |
| 212 | new_ends.push_back(sop->params.at("end").type == 0 ? INT_MAX : sop->params.at("end").i); |
| 213 | } |
| 214 | else if (sop->has_param("end")) |
| 215 | { |
| 216 | char tmp[32]; |
| 217 | if (sop->params.at("end").type == 0) |
| 218 | { |
| 219 | sprintf(tmp, "%d", INT_MAX); |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | sprintf(tmp, "%d", sop->params.at("end").i); |
| 224 | } |
| 225 | ends_indexes.push_back(tmp); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | char tmp[32]; |
| 230 | sprintf(tmp, "@%d", (int)op_ends->inputs.size()); |
| 231 | ends_indexes.push_back(tmp); |
| 232 | Operand* end = sop->named_input("end"); |
| 233 | op_ends->inputs.push_back(end); |
| 234 | end->remove_consumer(sop); |
| 235 | end->consumers.push_back(op_ends); |
| 236 | } |
| 237 | |
| 238 | if (static_steps) |
| 239 | { |
| 240 | new_steps.push_back(sop->params.at("step").type == 0 ? 1 : sop->params.at("step").i); |
| 241 | } |
| 242 | else if (sop->has_param("step")) |
| 243 | { |
| 244 | char tmp[32]; |
| 245 | if (sop->params.at("step").type == 0) |
| 246 | { |
| 247 | sprintf(tmp, "1"); |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | sprintf(tmp, "%d", sop->params.at("step").i); |
| 252 | } |
| 253 | steps_indexes.push_back(tmp); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | char tmp[32]; |
| 258 | sprintf(tmp, "@%d", (int)op_steps->inputs.size()); |
| 259 | steps_indexes.push_back(tmp); |
| 260 | Operand* step = sop->named_input("step"); |
| 261 | op_steps->inputs.push_back(step); |
| 262 | step->remove_consumer(sop); |
| 263 | step->consumers.push_back(op_steps); |
| 264 | } |
| 265 | |
| 266 | if (static_selects) |
| 267 | { |
| 268 | new_selects.push_back(INT_MAX); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | char tmp[32]; |
| 273 | sprintf(tmp, "%d", INT_MAX); |
| 274 | selects_indexes.push_back(tmp); |
| 275 | } |
| 276 | } |
| 277 | else // if (sop->type == "Tensor.select") |
| 278 | { |
| 279 | if (static_starts) |
| 280 | { |
| 281 | new_starts.push_back(0); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | starts_indexes.push_back("0"); |
| 286 | } |
| 287 | |
| 288 | if (static_ends) |
| 289 | { |
| 290 | new_ends.push_back(0); |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | ends_indexes.push_back("0"); |
| 295 | } |
| 296 | |
| 297 | if (static_steps) |
| 298 | { |
| 299 | new_steps.push_back(0); |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | steps_indexes.push_back("0"); |
| 304 | } |
| 305 | |
| 306 | if (static_selects) |
| 307 | { |
| 308 | new_selects.push_back(sop->params.at("index").type == 0 ? 0 : sop->params.at("index").i); |
| 309 | } |
| 310 | else if (sop->has_param("index")) |
| 311 | { |
| 312 | char tmp[32]; |
| 313 | if (sop->params.at("index").type == 0) |
| 314 | { |
| 315 | sprintf(tmp, "0"); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | sprintf(tmp, "%d", sop->params.at("index").i); |
| 320 | } |
| 321 | selects_indexes.push_back(tmp); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | char tmp[32]; |
| 326 | sprintf(tmp, "@%d", (int)op_selects->inputs.size()); |
| 327 | selects_indexes.push_back(tmp); |
| 328 | Operand* index = sop->named_input("index"); |
| 329 | op_selects->inputs.push_back(index); |
| 330 | index->remove_consumer(sop); |
| 331 | index->consumers.push_back(op_selects); |
| 332 | } |
| 333 | |
| 334 | select_dim_offset += 1; |
| 335 | } |
| 336 | |
| 337 | { |
| 338 | // drop sop and sop output |
| 339 | Operand* sop_out = sop->outputs[0]; |
| 340 | |
| 341 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), sop_out)); |
| 342 | |
| 343 | delete sop_out; |
| 344 | |
| 345 | graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), sop)); |
| 346 | |
| 347 | delete sop; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | new_dims.push_back(select_dim_offset + op->params.at("dim").i); |
| 352 | |
| 353 | if (op->type == "Tensor.slice") |
| 354 | { |
| 355 | if (static_starts) |
| 356 | { |
| 357 | new_starts.push_back(op->params.at("start").type == 0 ? 0 : op->params.at("start").i); |
| 358 | } |
| 359 | else if (op->has_param("start")) |
| 360 | { |
| 361 | char tmp[32]; |
| 362 | if (op->params.at("start").type == 0) |
| 363 | { |
| 364 | sprintf(tmp, "0"); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | sprintf(tmp, "%d", op->params.at("start").i); |
| 369 | } |
| 370 | starts_indexes.push_back(tmp); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | char tmp[32]; |
| 375 | sprintf(tmp, "@%d", (int)op_starts->inputs.size()); |
| 376 | starts_indexes.push_back(tmp); |
| 377 | Operand* start = op->named_input("start"); |
| 378 | op_starts->inputs.push_back(start); |
| 379 | start->remove_consumer(op); |
| 380 | start->consumers.push_back(op_starts); |
| 381 | } |
| 382 | |
| 383 | if (static_ends) |
| 384 | { |
| 385 | new_ends.push_back(op->params.at("end").type == 0 ? INT_MAX : op->params.at("end").i); |
| 386 | } |
| 387 | else if (op->has_param("end")) |
| 388 | { |
| 389 | char tmp[32]; |
| 390 | if (op->params.at("end").type == 0) |
| 391 | { |
| 392 | sprintf(tmp, "%d", INT_MAX); |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | sprintf(tmp, "%d", op->params.at("end").i); |
| 397 | } |
| 398 | ends_indexes.push_back(tmp); |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | char tmp[32]; |
| 403 | sprintf(tmp, "@%d", (int)op_ends->inputs.size()); |
| 404 | ends_indexes.push_back(tmp); |
| 405 | Operand* end = op->named_input("end"); |
| 406 | op_ends->inputs.push_back(end); |
| 407 | end->remove_consumer(op); |
| 408 | end->consumers.push_back(op_ends); |
| 409 | } |
| 410 | |
| 411 | if (static_steps) |
| 412 | { |
| 413 | new_steps.push_back(op->params.at("step").type == 0 ? 1 : op->params.at("step").i); |
| 414 | } |
| 415 | else if (op->has_param("step")) |
| 416 | { |
| 417 | char tmp[32]; |
| 418 | if (op->params.at("step").type == 0) |
| 419 | { |
| 420 | sprintf(tmp, "1"); |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | sprintf(tmp, "%d", op->params.at("step").i); |
| 425 | } |
| 426 | steps_indexes.push_back(tmp); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | char tmp[32]; |
| 431 | sprintf(tmp, "@%d", (int)op_steps->inputs.size()); |
| 432 | steps_indexes.push_back(tmp); |
| 433 | Operand* step = op->named_input("step"); |
| 434 | op_steps->inputs.push_back(step); |
| 435 | step->remove_consumer(op); |
| 436 | step->consumers.push_back(op_steps); |
| 437 | } |
| 438 | |
| 439 | if (static_selects) |
| 440 | { |
| 441 | new_selects.push_back(INT_MAX); |
| 442 | } |
| 443 | else if (op->has_param("index")) |
| 444 | { |
| 445 | char tmp[32]; |
| 446 | sprintf(tmp, "%d", INT_MAX); |
| 447 | selects_indexes.push_back(tmp); |
| 448 | } |
| 449 | } |
| 450 | else // if (op->type == "Tensor.select") |
| 451 | { |
| 452 | if (static_starts) |
| 453 | { |
| 454 | new_starts.push_back(0); |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | starts_indexes.push_back("0"); |
| 459 | } |
| 460 | |
| 461 | if (static_ends) |
| 462 | { |
| 463 | new_ends.push_back(0); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | ends_indexes.push_back("0"); |
| 468 | } |
| 469 | |
| 470 | if (static_steps) |
| 471 | { |
| 472 | new_steps.push_back(0); |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | steps_indexes.push_back("0"); |
| 477 | } |
| 478 | |
| 479 | if (static_selects) |
| 480 | { |
| 481 | new_selects.push_back(op->params.at("index").type == 0 ? 0 : op->params.at("index").i); |
| 482 | } |
| 483 | else if (op->has_param("index")) |
| 484 | { |
| 485 | char tmp[32]; |
| 486 | if (op->params.at("index").type == 0) |
| 487 | { |
| 488 | sprintf(tmp, "0"); |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | sprintf(tmp, "%d", op->params.at("index").i); |
| 493 | } |
| 494 | selects_indexes.push_back(tmp); |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | char tmp[32]; |
| 499 | sprintf(tmp, "@%d", (int)op_selects->inputs.size()); |
| 500 | selects_indexes.push_back(tmp); |
| 501 | Operand* index = op->named_input("index"); |
| 502 | op_selects->inputs.push_back(index); |
| 503 | index->remove_consumer(op); |
| 504 | index->consumers.push_back(op_selects); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | op->type = "Tensor.slice"; |
| 509 | |
| 510 | op->params.clear(); |
| 511 | op->params["dims"] = new_dims; |
| 512 | |
| 513 | op->inputs.clear(); |
| 514 | op->inputnames.clear(); |
| 515 | |
| 516 | op->inputs.push_back(in0); |
| 517 | op->inputnames.push_back("input"); |
| 518 | |
| 519 | in0->remove_consumer(top_sop); |
| 520 | in0->consumers.push_back(op); |
| 521 | |
| 522 | if (static_starts) |
| 523 | { |
| 524 | op->params["starts"] = new_starts; |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | op_starts->params["indexes"] = starts_indexes; |
| 529 | |
| 530 | Operand* starts_out = graph.new_operand(op->name + "_ncnnstarts_out"); |
| 531 | starts_out->producer = op_starts; |
| 532 | op_starts->outputs.push_back(starts_out); |
| 533 | starts_out->consumers.push_back(op); |
| 534 | op->inputs.push_back(starts_out); |
| 535 | op->inputnames.push_back("starts"); |
| 536 | } |
| 537 | |
| 538 | if (static_ends) |
| 539 | { |
| 540 | op->params["ends"] = new_ends; |
| 541 | } |
| 542 | else |
| 543 | { |
| 544 | op_ends->params["indexes"] = ends_indexes; |
| 545 | |
| 546 | Operand* ends_out = graph.new_operand(op->name + "_ncnnends_out"); |
| 547 | ends_out->producer = op_ends; |
| 548 | op_ends->outputs.push_back(ends_out); |
| 549 | ends_out->consumers.push_back(op); |
| 550 | op->inputs.push_back(ends_out); |
| 551 | op->inputnames.push_back("ends"); |
| 552 | } |
| 553 | |
| 554 | if (static_steps) |
| 555 | { |
| 556 | op->params["steps"] = new_steps; |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | op_steps->params["indexes"] = steps_indexes; |
| 561 | |
| 562 | Operand* steps_out = graph.new_operand(op->name + "_ncnnsteps_out"); |
| 563 | steps_out->producer = op_steps; |
| 564 | op_steps->outputs.push_back(steps_out); |
| 565 | steps_out->consumers.push_back(op); |
| 566 | op->inputs.push_back(steps_out); |
| 567 | op->inputnames.push_back("steps"); |
| 568 | } |
| 569 | |
| 570 | if (static_selects) |
| 571 | { |
| 572 | op->params["selects"] = new_selects; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | op_selects->params["indexes"] = selects_indexes; |
| 577 | |
| 578 | Operand* selects_out = graph.new_operand(op->name + "_ncnnselects_out"); |
| 579 | selects_out->producer = op_selects; |
| 580 | op_selects->outputs.push_back(selects_out); |
| 581 | selects_out->consumers.push_back(op); |
| 582 | op->inputs.push_back(selects_out); |
| 583 | op->inputnames.push_back("selects"); |
| 584 | } |
| 585 | |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | if (!matched) |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | } // namespace pnnx |