| 640 | } |
| 641 | |
| 642 | StatusOr<Literal> LiteralBase::Reshape( |
| 643 | absl::Span<const int64> dimensions) const { |
| 644 | if (!shape().IsArray()) { |
| 645 | return InvalidArgument("Reshape does not support tuples."); |
| 646 | } |
| 647 | Literal output; |
| 648 | if (!LayoutUtil::IsMonotonicWithDim0Major(shape().layout())) { |
| 649 | output = Relayout(LayoutUtil::GetDefaultLayoutForRank(shape().rank())); |
| 650 | } else { |
| 651 | output = Clone(); |
| 652 | } |
| 653 | // Because the layout is monotonic, we can simply reuse the same sequence of |
| 654 | // values without changing their order. |
| 655 | *output.mutable_shape_do_not_use() = |
| 656 | ShapeUtil::MakeShape(shape().element_type(), dimensions); |
| 657 | |
| 658 | int64 elements_before = ShapeUtil::ElementsIn(shape()); |
| 659 | int64 elements_after = ShapeUtil::ElementsIn(output.shape()); |
| 660 | if (elements_before != elements_after) { |
| 661 | return InvalidArgument( |
| 662 | "Shapes before and after Literal::Reshape have different numbers " |
| 663 | "of elements: %s vs %s.", |
| 664 | ShapeUtil::HumanString(shape()), |
| 665 | ShapeUtil::HumanString(output.shape())); |
| 666 | } |
| 667 | return std::move(output); |
| 668 | } |
| 669 | |
| 670 | Literal LiteralBase::Transpose(absl::Span<const int64> permutation) const { |
| 671 | CHECK(shape().IsArray()) << "Tuple is not supported for transpose"; |