| 22 | namespace { |
| 23 | |
| 24 | xla::StatusOr<xla::XlaOp> Contract(xla::XlaOp input, int64 dim) { |
| 25 | xla::XlaBuilder* builder = input.builder(); |
| 26 | TF_ASSIGN_OR_RETURN(xla::Shape input_shape, builder->GetShape(input)); |
| 27 | |
| 28 | if (input_shape.dimensions().back() != 4) { |
| 29 | return errors::InvalidArgument("Expected last dimension to be 4; got ", |
| 30 | input_shape.dimensions().back()); |
| 31 | } |
| 32 | |
| 33 | // Transpose the input so C is directly followed by VECT_C. |
| 34 | std::vector<int64> permutation; |
| 35 | for (int64 i = 0; i != input_shape.rank() - 1; ++i) { |
| 36 | permutation.push_back(i); |
| 37 | if (i == dim) { |
| 38 | permutation.push_back(input_shape.rank() - 1); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Now merge the adjacent dimensions with a reshape. |
| 43 | std::vector<int64> contracted_shape(input_shape.dimensions().begin(), |
| 44 | input_shape.dimensions().end() - 1); |
| 45 | contracted_shape[dim] *= 4; |
| 46 | |
| 47 | return xla::Reshape(xla::Transpose(input, permutation), contracted_shape); |
| 48 | } |
| 49 | |
| 50 | xla::StatusOr<xla::XlaOp> Expand(xla::XlaOp input, int64 dim) { |
| 51 | xla::XlaBuilder* builder = input.builder(); |
no test coverage detected