| 709 | } |
| 710 | |
| 711 | bool ShapesAgreeUpToExtending(const Shape& shape0, const Shape& shape1) { |
| 712 | CheckNonEmptyShapeDimensions(shape0); |
| 713 | CheckNonEmptyShapeDimensions(shape1); |
| 714 | |
| 715 | const Shape* longer = &shape0; |
| 716 | const Shape* shorter = &shape1; |
| 717 | if (shape1.dimensions_count() > shape0.dimensions_count()) { |
| 718 | longer = &shape1; |
| 719 | shorter = &shape0; |
| 720 | } |
| 721 | |
| 722 | // Walk dimensions back to front until we run out of dimensions in the shorter |
| 723 | // shape. |
| 724 | int longer_index = longer->dimensions_count() - 1; |
| 725 | int shorter_index = shorter->dimensions_count() - 1; |
| 726 | while (shorter_index >= 0) { |
| 727 | const int d_long = longer->dims(longer_index); |
| 728 | const int d_short = shorter->dims(shorter_index); |
| 729 | // Extending fails if the dimensions are different. |
| 730 | if (d_long != d_short) { |
| 731 | return false; |
| 732 | } |
| 733 | longer_index--; |
| 734 | shorter_index--; |
| 735 | } |
| 736 | |
| 737 | // The remaining dimensions in the longer shape must be 1. |
| 738 | while (longer_index >= 0) { |
| 739 | const int d_long = longer->dims(longer_index); |
| 740 | if (d_long != 1) { |
| 741 | return false; |
| 742 | } |
| 743 | longer_index--; |
| 744 | } |
| 745 | |
| 746 | return true; |
| 747 | } |
| 748 | |
| 749 | int RequiredBufferSizeForShape(const Shape& shape) { |
| 750 | CheckValidShape(shape); |