| 661 | } |
| 662 | |
| 663 | XlaOp TransposeInMinorDims(XlaOp x) { |
| 664 | XlaBuilder* builder = x.builder(); |
| 665 | return builder->ReportErrorOrReturn([&]() -> StatusOr<XlaOp> { |
| 666 | TF_ASSIGN_OR_RETURN(Shape shape, builder->GetShape(x)); |
| 667 | const int64 n_dims = shape.rank(); |
| 668 | TF_RET_CHECK(n_dims >= 2); |
| 669 | std::vector<int64> permutation(n_dims); |
| 670 | std::iota(permutation.begin(), permutation.end(), 0); |
| 671 | std::swap(permutation[n_dims - 1], permutation[n_dims - 2]); |
| 672 | return Transpose(x, permutation); |
| 673 | }); |
| 674 | } |
| 675 | |
| 676 | XlaOp MaybeTransposeInMinorDims(XlaOp x, bool transpose) { |
| 677 | return transpose ? TransposeInMinorDims(x) : x; |