| 37 | : XlaOpKernel(ctx), conjugate_(conjugate) {} |
| 38 | |
| 39 | void Compile(XlaOpKernelContext* ctx) override { |
| 40 | const TensorShape input_shape = ctx->InputShape("x"); |
| 41 | const TensorShape perm_tensor_shape = ctx->InputShape("perm"); |
| 42 | |
| 43 | // Preliminary validation of sizes. |
| 44 | OP_REQUIRES(ctx, TensorShapeUtils::IsVector(perm_tensor_shape), |
| 45 | errors::InvalidArgument("perm must be a vector, not ", |
| 46 | perm_tensor_shape.DebugString())); |
| 47 | |
| 48 | const int dims = input_shape.dims(); |
| 49 | OP_REQUIRES(ctx, dims == perm_tensor_shape.num_elements(), |
| 50 | errors::InvalidArgument("transpose expects a vector of size ", |
| 51 | input_shape.dims(), |
| 52 | ". But input(1) is a vector of size ", |
| 53 | perm_tensor_shape.num_elements())); |
| 54 | |
| 55 | std::vector<int64> perm; |
| 56 | OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector("perm", &perm)); |
| 57 | |
| 58 | std::vector<int64> transposed_order; |
| 59 | // Check whether permutation is a permutation of integers of [0 .. dims). |
| 60 | absl::InlinedVector<bool, 8> bits(dims); |
| 61 | bool is_identity = true; |
| 62 | for (int i = 0; i < dims; ++i) { |
| 63 | const int64 d = perm[i]; |
| 64 | OP_REQUIRES( |
| 65 | ctx, 0 <= d && d < dims, |
| 66 | errors::InvalidArgument(d, " is out of range [0 .. ", dims, ")")); |
| 67 | bits[d] = true; |
| 68 | transposed_order.push_back(d); |
| 69 | if (d != i) { |
| 70 | is_identity = false; |
| 71 | } |
| 72 | } |
| 73 | for (int i = 0; i < dims; ++i) { |
| 74 | OP_REQUIRES( |
| 75 | ctx, bits[i], |
| 76 | errors::InvalidArgument(i, " is missing from 'perm' argument.")); |
| 77 | } |
| 78 | |
| 79 | xla::XlaOp transposed; |
| 80 | // 0-D, 1-D, and identity transposes do nothing. |
| 81 | if (dims <= 1 || is_identity) { |
| 82 | transposed = ctx->Input("x"); |
| 83 | } else { |
| 84 | transposed = xla::Transpose(ctx->Input("x"), transposed_order); |
| 85 | } |
| 86 | |
| 87 | // Conjugate the transposed result if this is ConjugateTransposeOp. |
| 88 | if (conjugate_) { |
| 89 | ctx->SetOutput(0, xla::Conj(transposed)); |
| 90 | } else { |
| 91 | ctx->SetOutput(0, transposed); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | const bool conjugate_; |
nothing calls this directly
no test coverage detected