Commit 69a25ba
Optimize away zero-length concat operands (microsoft#2150)
We optimize `Concat (x1, x2, x3)` if one or more the concat operands has
zero length along the concatenated axis-dimension.
This pattern shows up, for example, in Phi models. See [this
line](https://github.com/huggingface/transformers/blob/786d9c5ed920a099573ea7b6dbf265f1aeb32fc0/src/transformers/models/phi3/modeling_phi3.py#L152)
in the implementation of partial-rotary-embedding:
```py
q_embed = torch.cat([(q_rot * cos) + (rotate_half(q_rot) * sin), q_pass], dim=-1)
```
In the special case of total-rotary-embedding, the second operand
`q_pass` of the concat is empty. This also interferes with the
pattern-matching for GQA in the generated graph. Optimizing the
redundant Concat away will help with GQA fusion as well.
Handle the edge case when all operands have zero size.1 parent 88c10a3 commit 69a25ba
2 files changed
Lines changed: 80 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
558 | 558 | | |
559 | 559 | | |
560 | 560 | | |
| 561 | + | |
| 562 | + | |
561 | 563 | | |
562 | 564 | | |
563 | 565 | | |
564 | | - | |
565 | | - | |
566 | | - | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
567 | 569 | | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
568 | 603 | | |
569 | | - | |
| 604 | + | |
570 | 605 | | |
571 | 606 | | |
572 | 607 | | |
573 | 608 | | |
574 | 609 | | |
575 | 610 | | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
576 | 614 | | |
577 | 615 | | |
578 | 616 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
482 | 520 | | |
483 | 521 | | |
484 | 522 | | |
| |||
0 commit comments